Multilevel Deduplication Engine (MDE)
Loading...
Searching...
No Matches
mde_common.hpp
Go to the documentation of this file.
1
6#ifndef MDE_CONFIG_HPP
7#define MDE_CONFIG_HPP
8
9#include <cstddef>
10#include <iostream>
11#include <memory>
12#include <ostream>
13#include <sstream>
14#include <stdexcept>
15#include <vector>
16#include <map>
17#include <unordered_map>
18#include <set>
19#include <unordered_set>
20#include <functional>
21#include <string>
22
23#ifdef MDE_ENABLE_PARALLEL
24#include <atomic>
25#include <mutex>
26#include <shared_mutex>
27#endif
28
29#ifdef MDE_ENABLE_TBB
30#include <tbb/tbb.h>
31#include <tbb/concurrent_map.h>
32#include <tbb/concurrent_vector.h>
33#endif
34
35#ifdef MDE_ENABLE_SERIALIZATION
36#include "mde_serialization.hpp"
37#endif
38
39#define MDE_VERSION_MAJOR "0"
40#define MDE_VERSION_MINOR "6"
41#define MDE_VERSION_PATCH "0"
42#define MDE_VERSION_STRING (MDE_VERSION_MAJOR "." MDE_VERSION_MINOR "." MDE_VERSION_PATCH)
43
44#define MDE_SORTED_VECTOR_BINARY_SEARCH_THRESHOLD 12
45#define MDE_DEFAULT_BLOCK_SHIFT 5
46#define MDE_DEFAULT_BLOCK_SIZE (1 << MDE_DEFAULT_BLOCK_SHIFT)
47#define MDE_DEFAULT_BLOCK_MASK (MDE_DEFAULT_BLOCK_SIZE - 1)
48#define MDE_DISABLE_INTERNAL_INTEGRITY_CHECK true
49
50namespace mde {
51
52#define ____MDE__STR(x) #x
53#define __MDE_STR(x) ____MDE__STR(x)
54
55#ifdef MDE_ENABLE_DEBUG
56#define MDE_DEBUG(x) { x };
57#else
58#define MDE_DEBUG(x)
59#endif
60
61using Size = std::size_t;
62
63using String = std::string;
64
65#ifdef MDE_ENABLE_PARALLEL
66
67using RWMutex = std::shared_mutex;
68
69using ReadLock = std::shared_lock<RWMutex>;
70
71using WriteLock = std::lock_guard<RWMutex>;
72
73#endif
74
76
77template<typename T>
78using UniquePointer = std::unique_ptr<T>;
79
80template<typename T>
81using Vector = std::vector<T>;
82
83template<typename K, typename V>
84using HashMap = std::unordered_map<K, V>;
85
86template<typename K, typename V>
87using OrderedMap = std::map<K, V>;
88
89template<typename T>
90using HashSet = std::unordered_set<T>;
91
92template<typename T>
93using OrderedSet = std::set<T>;
94
95template<typename T>
96using DefaultLess = std::less<T>;
97
98template<typename T>
99using DefaultHash = std::hash<T>;
100
101template<typename T>
102using DefaultEqual = std::equal_to<T>;
103
104template<typename T>
107 std::stringstream s;
108 s << x;
109 return s.str();
110 }
111};
112
116struct AbsentValueAccessError : public std::invalid_argument {
117 AbsentValueAccessError(const std::string &message):
118 std::invalid_argument(message.c_str()) {}
119};
120
127template<typename T>
129 const T *value = nullptr;
130 OptionalRef(): value(nullptr) {}
131
132public:
133 OptionalRef(const T &value): value(&value) {}
134
137 return OptionalRef();
138 }
139
141 bool is_present() const {
142 return value != nullptr;
143 }
144
146 const T &get() {
147 if (value) {
148 return *value;
149 } else {
150 throw AbsentValueAccessError("Tried to access an absent value. "
151 "A check is likely missing.");
152 }
153 }
154};
155
162template<typename T>
163class Optional {
164 const T value{};
165 const bool present;
166
167 Optional(): present(false) {}
168
169public:
170 Optional(const T &value): value(value), present(true) {}
171
173 static Optional absent() {
174 return Optional();
175 }
176
178 bool is_present() {
179 return present;
180 }
181
183 const T &get() {
184 if (present) {
185 return value;
186 } else {
187 throw AbsentValueAccessError("Tried to access an absent value. "
188 "A check is likely missing.");
189 }
190 }
191};
192
203
204
205// The index of the empty set. The first set that will ever be inserted
206// in the property set value storage is the empty set.
207static const constexpr Size EMPTY_SET_VALUE = 0;
208
221template<typename T, typename Hash = DefaultHash<T>>
222inline Size compose_hash(const Size prev, T next) {
223 return prev ^ (Hash()(next) + 0x9e3779b9 + (prev << 6) + (prev >> 2));
224}
225
226};
227#endif
Describes an optional reference of some type T. The value may either be present or absent.
const T & get()
Gets the underlying value. Throws an exception if it is absent.
bool is_present() const
Informs if the value is present.
static OptionalRef absent()
Explicitly creates an absent value.
OptionalRef(const T &value)
Describes an optional of some type T. The value may either be present or absent.
static Optional absent()
Explicitly creates an absent value.
const T & get()
Gets the underlying value. Throws an exception if it is absent.
bool is_present()
Informs if the value is present.
Optional(const T &value)
Describes serialization building blocks for MDE.
Definition mde.hpp:16
SubsetRelation
Used to store a subset relation between two set indices. Because the index pair must be in sorted ord...
@ UNKNOWN
@ SUPERSET
std::map< K, V > OrderedMap
std::less< T > DefaultLess
std::size_t Size
std::unordered_set< T > HashSet
std::set< T > OrderedSet
std::string String
Size IndexValue
std::vector< T > Vector
std::unique_ptr< T > UniquePointer
std::hash< T > DefaultHash
std::unordered_map< K, V > HashMap
Size compose_hash(const Size prev, T next)
Composes a preexisting hash with another variable. Useful for Hashing containers. Adapted from boost:...
std::equal_to< T > DefaultEqual
Thrown if an Optional is accessed when the value is absent.
AbsentValueAccessError(const std::string &message)
String operator()(T x)