chunkedseq
container library for large in-memory data sets
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
annotation.hpp
Go to the documentation of this file.
1 
14 #include <assert.h>
15 #include <algorithm>
16 
17 #include "tagged.hpp"
18 
19 #ifndef _PASL_DATA_ANNOTATION_H_
20 #define _PASL_DATA_ANNOTATION_H_
21 
22 namespace pasl {
23 namespace data {
24 namespace chunkedseq {
25 namespace annotation {
26 
27 /***********************************************************************/
28 
29 /*---------------------------------------------------------------------*/
30 /* Optional support for parent pointers */
31 
32 using parent_pointer_tag = enum {
33  PARENT_POINTER_BOOTSTRAP_INTERIOR_NODE=0,
34  PARENT_POINTER_BOOTSTRAP_LAYER_NODE=1,
35  PARENT_POINTER_CHUNK=2,
36  PARENT_POINTER_UNINITIALIZED=3
37 };
38 
39 template <class Measured>
41 public:
42 
44 
45 private:
46 
47  mutable void* parent_ptr;
48  mutable Measured prefix;
49  mutable int depth;
50 
51 public:
52 
53  static constexpr bool enabled = true;
54 
56  set_pointer(nullptr, PARENT_POINTER_UNINITIALIZED);
57  depth = -1;
58  }
59 
60  template <class Pointer>
61  Pointer get_pointer() const {
62  return tagged::extract_value<Pointer, void*>(parent_ptr);
63  }
64 
66  long t = tagged::extract_tag<void*, void*>(parent_ptr);
67  if (t == PARENT_POINTER_BOOTSTRAP_INTERIOR_NODE)
68  return PARENT_POINTER_BOOTSTRAP_INTERIOR_NODE;
69  else if (t == PARENT_POINTER_BOOTSTRAP_LAYER_NODE)
70  return PARENT_POINTER_BOOTSTRAP_LAYER_NODE;
71  else if (t == PARENT_POINTER_UNINITIALIZED)
72  return PARENT_POINTER_UNINITIALIZED;
73  else {
74  assert(false);
75  return PARENT_POINTER_UNINITIALIZED;
76  }
77  }
78 
79  int get_depth() const {
80  return depth;
81  }
82 
83  template <class Measured1>
84  Measured1 get_prefix() const {
85  return prefix;
86  }
87 
88  template <class Pointer>
89  void set_pointer(Pointer p, parent_pointer_tag t) const {
90  parent_ptr = tagged::create<Pointer, void*>(p, t);
91  assert(get_tag() == t);
92  }
93 
94  void set_depth(int d) const {
95  depth = d;
96  }
97 
98  template <class Measured1>
99  void set_prefix(Measured1 m) const {
100  prefix = m;
101  }
102 
103  void swap(self_type& other) {
104  std::swap(parent_ptr, other.parent_ptr);
105  std::swap(depth, other.depth);
106  std::swap(prefix, other.prefix);
107  }
108 
109 };
110 
112 public:
113 
115 
116  static constexpr bool enabled = false;
117 
118  template <class Pointer>
119  Pointer get_pointer() const {
120  return nullptr;
121  }
122 
124  return PARENT_POINTER_UNINITIALIZED;
125  }
126 
127  int get_depth() const {
128  return -1;
129  }
130 
131  template <class Measured>
132  Measured get_prefix() const {
133  return Measured();
134  }
135 
136  template <class Pointer>
137  void set_pointer(Pointer, parent_pointer_tag) const {
138  }
139 
140  void set_depth(int) const {
141 
142  }
143 
144  template <class Measured>
145  void set_prefix(Measured) const {
146 
147  }
148 
149  void swap(self_type&) {
150 
151  }
152 
153 };
154 
155 /*---------------------------------------------------------------------*/
156 /* Optional suppport for chains of inter-chunk pointers */
157 
158 class with_chain {
159 public:
160 
162 
163 private:
164 
165  void* next;
166  void* prev;
167 
168 public:
169 
170  static constexpr bool enabled = true;
171 
173  : next(nullptr), prev(nullptr) { }
174 
175  template <class Pointer>
176  Pointer get_next() const {
177  return (Pointer)next;
178  }
179 
180  template <class Pointer>
181  Pointer get_prev() const {
182  return (Pointer)prev;
183  }
184 
185  template <class Pointer>
186  static void link(self_type& l1, self_type& l2, Pointer p1, Pointer p2) {
187  l1.next = (void*)p2;
188  l2.prev = (void*)p1;
189  }
190 
191  template <class Pointer>
192  static void unlink(self_type& l1, self_type& l2, Pointer p1, Pointer p2) {
193  assert(l1.get_next<void*>() == (void*)p2);
194  assert((void*)p1 == l2.get_prev<void*>());
195  l1.next = nullptr;
196  l2.prev = nullptr;
197  }
198 
199  void swap(self_type& other) {
200  std::swap(next, other.next);
201  std::swap(prev, other.prev);
202  }
203 
204 };
205 
207 public:
208 
210 
211  static constexpr bool enabled = false;
212 
213  template <class Pointer>
214  Pointer get_next() const {
215  return nullptr;
216  }
217 
218  template <class Pointer>
219  Pointer get_prev() const {
220  return nullptr;
221  }
222 
223  template <class Pointer>
224  static void link(self_type& l1, self_type& l2, Pointer p1, Pointer p2) {
225  }
226 
227  template <class Pointer>
228  static void unlink(self_type& l1, self_type& l2, Pointer p1, Pointer p2) {
229  }
230 
231  void swap(self_type& other) {
232  }
233 
234 };
235 
236 /*---------------------------------------------------------------------*/
237 /* Optional support for cached measurements */
238 
240 public:
241 
242  using measured_type = struct { };
244 
245  static constexpr bool enabled = false;
246 
247  template <class Measured>
248  Measured get_cached() const {
249  return Measured();
250  }
251 
252  template <class Measured>
253  void set_cached(Measured m) const {
254 
255  }
256 
257  void swap(self_type& other) {
258 
259  }
260 
261 };
262 
263 class std_swap {
264 public:
265  template <class T>
266  static void swap(T& x, T& y) {
267  std::swap(x, y);
268  }
269 };
270 
271 template <class Measured, class Swap_measured=std_swap>
273 public:
274 
275  using measured_type = Measured;
277 
278  static constexpr bool enabled = true;
279 
281 
282  template <class Measured1>
283  Measured1 get_cached() const {
284  return cached;
285  }
286 
287  template <class Measured1>
288  void set_cached(Measured1 m) const {
289  cached = m;
290  }
291 
292  void swap(self_type& other) {
293  Swap_measured::swap(cached, other.cached);
294  }
295 
296 };
297 
298 /*---------------------------------------------------------------------*/
299 /* Annotation builder */
300 
301 template <
302  class Measured=without_measured,
303  class Parent_pointer=without_parent_pointer,
304  class Sibling_pointer=without_chain
305 >
307 public:
308 
310  using cached_prefix_type = Measured;
311  using parent_pointer_type = Parent_pointer;
312 
313  static constexpr bool finger_search_enabled = Measured::enabled && Parent_pointer::enabled;
314 
315  Measured prefix;
316  Parent_pointer parent;
317  Sibling_pointer sibling;
318 
319  void swap(self_type& other) {
320  prefix.swap(other.prefix);
321  parent.swap(other.parent);
322  sibling.swap(other.sibling);
323  }
324 
325 };
326 
327 /***********************************************************************/
328 
329 } // end namespace
330 } // end namespace
331 } // end namespace
332 } // end namespace
333 
334 #endif
enum{PARENT_POINTER_BOOTSTRAP_INTERIOR_NODE=0, PARENT_POINTER_BOOTSTRAP_LAYER_NODE=1, PARENT_POINTER_CHUNK=2, PARENT_POINTER_UNINITIALIZED=3} parent_pointer_tag
Definition: annotation.hpp:37
static void unlink(self_type &l1, self_type &l2, Pointer p1, Pointer p2)
Definition: annotation.hpp:228
static void link(self_type &l1, self_type &l2, Pointer p1, Pointer p2)
Definition: annotation.hpp:224
Definition: algebra.hpp:18
static void link(self_type &l1, self_type &l2, Pointer p1, Pointer p2)
Definition: annotation.hpp:186
void set_pointer(Pointer, parent_pointer_tag) const
Definition: annotation.hpp:137
void set_pointer(Pointer p, parent_pointer_tag t) const
Definition: annotation.hpp:89
static void unlink(self_type &l1, self_type &l2, Pointer p1, Pointer p2)
Definition: annotation.hpp:192