TPIE

11a2c2d
base.h
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2014 The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
20 #ifndef _TPIE_BTREE_BASE_H_
21 #define _TPIE_BTREE_BASE_H_
22 #include <tpie/portability.h>
23 #include <functional>
24 namespace tpie {
25 
29 struct empty_augment {};
30 
35  typedef empty_augment value_type;
36 
37  template <typename T>
38  empty_augment operator()(const T &) {return empty_augment();}
39 };
40 
45 struct identity_key {
46  template <typename T>
47  const T & operator()(const T & t) const noexcept {return t;}
48 };
49 
53 struct default_comp {
54  template <typename L, typename R>
55  bool operator()(const L & l, const R & r) const noexcept {
56  return l < r;
57  }
58 };
59 
60 struct empty_key {};
61 
62 struct no_key {
63  template <typename T>
64  empty_key operator()(const T &) const noexcept {return empty_key{};};
65 };
66 
67 namespace bbits {
68 template <int i>
69 struct int_opt {static const int O=i;};
70 
71 static const int f_internal = 1;
72 static const int f_static = 2;
73 static const int f_unordered = 4;
74 static const int f_serialized = 8;
75 
76 } //namespace bbits
77 
78 template <typename T>
79 struct btree_comp {typedef T type;};
80 
81 template <typename T>
82 struct btree_key {typedef T type;};
83 
84 template <typename T>
85 struct btree_augment {typedef T type;};
86 
87 template <int a_, int b_>
88 struct btree_fanout {
89  static const int a = a_;
90  static const int b = b_;
91 };
92 
93 template <size_t bs_>
95  static const size_t bs = bs_;
96 };
97 
100 
103 
106 
109 
110 enum btree_flags : uint64_t {
111  compress_none = 0x0,
112  compress_lz4 = 0x1,
113  compress_snappy = 0x2,
114  compress_zstd = 0x3,
115  compression_mask = 0xFF,
116  compression_level_mask = 0xFF00,
117  compress_level_default = 0x0000,
118  compress_level_1 = 0x0100,
119  compress_level_2 = 0x0200,
120  compress_level_3 = 0x0300,
121  compress_level_4 = 0x0400,
122  compress_level_5 = 0x0500,
123  compress_level_6 = 0x0600,
124  compress_level_7 = 0x0700,
125  compress_level_8 = 0x0800,
126  compress_level_9 = 0x0900,
127  compress_level_10 = 0x0A00,
128  compress_level_11 = 0x0B00,
129  compress_level_12 = 0x0C00,
130  compress_level_13 = 0x0D00,
131  compress_level_14 = 0x0E00,
132  compress_level_15 = 0x0F00,
133  compress_level_16 = 0x1000,
134  compress_level_17 = 0x1100,
135  compress_level_18 = 0x1200,
136  compress_level_19 = 0x1300,
137  compress_level_20 = 0x1400,
138  compress_level_21 = 0x1500,
139  compress_level_22 = 0x1600,
140  compress_default = compress_level_default | compress_lz4,
141  read = 0x010000,
142  write = 0x020000,
143  defaults = read | write,
144  defaults_v0 = read | write
145 };
146 
147 inline constexpr btree_flags operator |(btree_flags f1, btree_flags f2) { return static_cast<btree_flags>(uint64_t(f1) | uint64_t(f2)); }
148 inline btree_flags & operator |=(btree_flags & f1, btree_flags f2) { return f1 = f1 | f2; }
149 inline constexpr btree_flags operator &(btree_flags f1, btree_flags f2) { return static_cast<btree_flags>(uint64_t(f1) & uint64_t(f2)); }
150 inline btree_flags & operator &=(btree_flags & f1, btree_flags f2) { return f1 = f1 & f2; }
151 inline constexpr btree_flags operator ~(btree_flags f1) { return static_cast<btree_flags>(~uint64_t(f1)); }
152 
153 namespace bbits {
154 
155 //O = flags, a, b = B-tree parameters, C = comparator, K = key extractor, A = augmenter
156 template <int O_, int a_, int b_, size_t bs_, typename C_, typename K_, typename A_>
157 struct Opt {
158  static const int O=O_;
159  static const int a=a_;
160  static const int b=b_;
161  static const size_t bs=bs_;
162  typedef C_ C;
163  typedef K_ K;
164  typedef A_ A;
165 };
166 
167 template <typename ... Opt>
168 struct OptComp {};
169 
170 template <>
171 struct OptComp<> {
173 };
174 
175 template <int i, typename ... T>
176 struct OptComp<int_opt<i> , T...> {
177  typedef typename OptComp<T...>::type P;
179 };
180 
181 template <typename C, typename ... T>
182 struct OptComp<btree_comp<C> , T...> {
183  typedef typename OptComp<T...>::type P;
185 };
186 
187 template <typename K, typename ... T>
188 struct OptComp<btree_key<K> , T...> {
189  typedef typename OptComp<T...>::type P;
191 };
192 
193 template <typename A, typename ... T>
194 struct OptComp<btree_augment<A> , T...> {
195  typedef typename OptComp<T...>::type P;
197 };
198 
199 template <int a, int b, typename ... T>
200 struct OptComp<btree_fanout<a, b> , T...> {
201  typedef typename OptComp<T...>::type P;
203 };
204 
205 template <size_t bs, typename ... T>
206 struct OptComp<btree_blocksize<bs> , T...> {
207  typedef typename OptComp<T...>::type P;
209 };
210 
211 } //namespace bbits
212 
213 
214 
220 template <typename S>
222 
223 
224 template <typename S>
226 
227 namespace bbits {
228 
236 template <typename T,
237  typename O>
238 class tree;
239 
247 template <typename T, typename O>
248 class builder;
249 
250 template <typename, bool>
252 
253 template <typename T, typename A, std::size_t a, std::size_t b>
255 
256 template <typename T, typename A, std::size_t a, std::size_t b, std::size_t bs>
258 
259 template <typename T, typename A, std::size_t a, std::size_t b, std::size_t bs>
261 
262 struct enab {};
263 
264 template <typename X, bool b>
265 struct Enable {};
266 
267 template <>
268 struct Enable<enab, true> {typedef enab type;};
269 
270 template <typename X, bool b>
271 using enable = typename Enable<X,b>::type;
272 
273 template <typename T, typename O>
274 class tree_state {
275 public:
276  static const bool is_internal = O::O & bbits::f_internal;
277  static const bool is_static = O::O & bbits::f_static;
278  static const bool is_ordered = ! (O::O & bbits::f_unordered);
279  static const bool is_serialized = O::O & bbits::f_serialized;
280  static_assert(!is_serialized || is_static, "Serialized B-tree cannot be dynamic.");
281 
282  typedef typename std::conditional<
283  is_ordered,
284  typename O::K,
285  no_key>::type keyextract_type;
286 
287  typedef typename O::A augmenter_type;
288 
289  typedef T value_type;
290 
291  typedef typename std::decay<decltype(std::declval<augmenter_type>()(std::declval<value_type>()))>::type augment_type;
292 
293  typedef typename std::decay<decltype(std::declval<keyextract_type>()(std::declval<value_type>()))>::type key_type;
294 
295  struct key_augment {
296  key_type key;
297  };
298 
300  : public key_augment
301  , public augment_type {};
302 
303 
305  template <typename N>
306  combined_augment operator()(const N & node) {
307  combined_augment ans;
308  *static_cast<augment_type*>(&ans) = m_augmenter(node);
309 
310  static_cast<key_augment*>(&ans)->key =
311  node.is_leaf()
312  ? m_key_extract(node.value(0))
313  : static_cast<const key_augment*>(&node.get_combined_augmentation(0))->key;
314  return ans;
315  }
316 
318  augmenter_type a,
319  keyextract_type key_extract)
320  : m_key_extract(std::move(key_extract))
321  , m_augmenter(std::move(a)) {}
322 
323  keyextract_type m_key_extract;
324  augmenter_type m_augmenter;
325  };
326 
327  typedef typename std::conditional<
328  is_internal,
330  typename std::conditional<
331  is_serialized,
334  >::type
335  >::type store_type;
336 
337  typedef typename store_type::internal_type internal_type;
338  typedef typename store_type::leaf_type leaf_type;
339 
340  key_type min_key(internal_type node, size_t i) const {
341  return static_cast<const key_augment*>(&m_store.augment(node, i))->key;
342  }
343 
344  key_type min_key(leaf_type node, size_t i) const {
345  return m_augmenter.m_key_extract(m_store.get(node, i));
346  }
347 
348  key_type min_key(T v) const {
349  return m_augmenter.m_key_extract(v);
350  }
351 
352  key_type min_key(internal_type v) const {
353  return min_key(v, 0);
354  }
355 
356  key_type min_key(leaf_type v) const {
357  return min_key(v, 0);
358  }
359 
360  const store_type & store() const {
361  return m_store;
362  }
363 
364  store_type & store() {
365  return m_store;
366  }
367 
368  static const augment_type & user_augment(const combined_augment & a) {
369  return *static_cast<const augment_type *>(&a);
370  }
371 
372  tree_state(store_type store, augmenter_type augmenter, keyextract_type keyextract)
373  : m_augmenter(std::move(augmenter), std::move(keyextract))
374  , m_store(std::move(store)) {}
375 
376  combined_augmenter m_augmenter;
377  store_type m_store;
378 };
379 
380 
381 } //namespace bbits
382 
383 
384 } //namespace tpie
385 #endif /*_TPIE_BTREE_BASE_H_*/
portability.h
tpie::btree_iterator
Definition: base.h:225
tpie::empty_key
Definition: base.h:60
tpie::bbits::tree
Augmented btree.
Definition: base.h:238
tpie::bbits::tree_state
Definition: base.h:274
tpie::bbits::serialized_store
Serializing store.
Definition: base.h:260
tpie::btree_fanout
Definition: base.h:88
tpie::bbits::tree_state::combined_augmenter
Definition: base.h:304
tpie::bbits::OptComp
Definition: base.h:168
tpie::btree_comp
Definition: base.h:79
tpie::bbits::enab
Definition: base.h:262
tpie::bbits::int_opt
Definition: base.h:69
tpie::bbits::internal_store
Storage used for an internal btree.
Definition: base.h:254
tpie::btree_augment
Definition: base.h:85
tpie::bbits::external_store
Storage used for an external btree.
Definition: base.h:257
tpie::bbits::tree_state::key_augment
Definition: base.h:295
tpie::default_comp
Default < comparator for the btree.
Definition: base.h:53
tpie::bbits::tree_state::combined_augment
Definition: base.h:299
tpie::bbits::Enable
Definition: base.h:265
tpie::empty_augment
Augmentation struct used in an un-augmented btree.
Definition: base.h:29
tpie::bbits::block_size_getter
Definition: base.h:251
tpie::empty_augmenter
Functor used to augment an un-augmented btree.
Definition: base.h:34
tpie::bbits::Opt
Definition: base.h:157
tpie::btree_blocksize
Definition: base.h:94
tpie::identity_key
Functor used to extract the key from a value in case keys and values are the same.
Definition: base.h:45
tpie::btree_key
Definition: base.h:82
tpie::no_key
Definition: base.h:62
tpie::btree_node
Type that is useful for navigating a btree.
Definition: base.h:221
tpie
Definition: access_type.h:26
tpie::bbits::builder
Augmented btree builder.
Definition: base.h:248