TPIE

2362a60
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 namespace bbits {
111 
112 //O = flags, a, b = B-tree parameters, C = comparator, K = key extractor, A = augmenter
113 template <int O_, int a_, int b_, size_t bs_, typename C_, typename K_, typename A_>
114 struct Opt {
115  static const int O=O_;
116  static const int a=a_;
117  static const int b=b_;
118  static const size_t bs=bs_;
119  typedef C_ C;
120  typedef K_ K;
121  typedef A_ A;
122 };
123 
124 template <typename ... Opt>
125 struct OptComp {};
126 
127 template <>
128 struct OptComp<> {
130 };
131 
132 template <int i, typename ... T>
133 struct OptComp<int_opt<i> , T...> {
134  typedef typename OptComp<T...>::type P;
136 };
137 
138 template <typename C, typename ... T>
139 struct OptComp<btree_comp<C> , T...> {
140  typedef typename OptComp<T...>::type P;
142 };
143 
144 template <typename K, typename ... T>
145 struct OptComp<btree_key<K> , T...> {
146  typedef typename OptComp<T...>::type P;
148 };
149 
150 template <typename A, typename ... T>
151 struct OptComp<btree_augment<A> , T...> {
152  typedef typename OptComp<T...>::type P;
154 };
155 
156 template <int a, int b, typename ... T>
157 struct OptComp<btree_fanout<a, b> , T...> {
158  typedef typename OptComp<T...>::type P;
160 };
161 
162 template <size_t bs, typename ... T>
163 struct OptComp<btree_blocksize<bs> , T...> {
164  typedef typename OptComp<T...>::type P;
166 };
167 
168 } //namespace bbits
169 
170 
171 
177 template <typename S>
179 
180 
181 template <typename S>
183 
184 namespace bbits {
185 
193 template <typename T,
194  typename O>
195 class tree;
196 
204 template <typename T, typename O>
205 class builder;
206 
207 template <typename, bool>
209 
210 template <typename T, typename A, std::size_t a, std::size_t b>
212 
213 template <typename T, typename A, std::size_t a, std::size_t b, std::size_t bs>
215 
216 template <typename T, typename A, std::size_t a, std::size_t b, std::size_t bs>
218 
219 struct enab {};
220 
221 template <typename X, bool b>
222 struct Enable {};
223 
224 template <>
225 struct Enable<enab, true> {typedef enab type;};
226 
227 template <typename X, bool b>
228 using enable = typename Enable<X,b>::type;
229 
230 template <typename T, typename O>
231 class tree_state {
232 public:
233  static const bool is_internal = O::O & bbits::f_internal;
234  static const bool is_static = O::O & bbits::f_static;
235  static const bool is_ordered = ! (O::O & bbits::f_unordered);
236  static const bool is_serialized = O::O & bbits::f_serialized;
237  static_assert(!is_serialized || is_static, "Serialized B-tree cannot be dynamic.");
238 
239  typedef typename std::conditional<
240  is_ordered,
241  typename O::K,
242  no_key>::type keyextract_type;
243 
244  typedef typename O::A augmenter_type;
245 
246  typedef T value_type;
247 
248  typedef typename std::decay<decltype(std::declval<augmenter_type>()(std::declval<value_type>()))>::type augment_type;
249 
250  typedef typename std::decay<decltype(std::declval<keyextract_type>()(std::declval<value_type>()))>::type key_type;
251 
252  struct key_augment {
253  key_type key;
254  };
255 
257  : public key_augment
258  , public augment_type {};
259 
260 
262  template <typename N>
263  combined_augment operator()(const N & node) {
264  combined_augment ans;
265  *static_cast<augment_type*>(&ans) = m_augmenter(node);
266 
267  static_cast<key_augment*>(&ans)->key =
268  node.is_leaf()
269  ? m_key_extract(node.value(0))
270  : static_cast<const key_augment*>(&node.get_combined_augmentation(0))->key;
271  return ans;
272  }
273 
275  augmenter_type a,
276  keyextract_type key_extract)
277  : m_key_extract(std::move(key_extract))
278  , m_augmenter(std::move(a)) {}
279 
280  keyextract_type m_key_extract;
281  augmenter_type m_augmenter;
282  };
283 
284  typedef typename std::conditional<
285  is_internal,
287  typename std::conditional<
288  is_serialized,
291  >::type
292  >::type store_type;
293 
294  typedef typename store_type::internal_type internal_type;
295  typedef typename store_type::leaf_type leaf_type;
296 
297  key_type min_key(internal_type node, size_t i) const {
298  return static_cast<const key_augment*>(&m_store.augment(node, i))->key;
299  }
300 
301  key_type min_key(leaf_type node, size_t i) const {
302  return m_augmenter.m_key_extract(m_store.get(node, i));
303  }
304 
305  key_type min_key(T v) const {
306  return m_augmenter.m_key_extract(v);
307  }
308 
309  key_type min_key(internal_type v) const {
310  return min_key(v, 0);
311  }
312 
313  key_type min_key(leaf_type v) const {
314  return min_key(v, 0);
315  }
316 
317  const store_type & store() const {
318  return m_store;
319  }
320 
321  store_type & store() {
322  return m_store;
323  }
324 
325  static const augment_type & user_augment(const combined_augment & a) {
326  return *static_cast<const augment_type *>(&a);
327  }
328 
329  tree_state(store_type store, augmenter_type augmenter, keyextract_type keyextract)
330  : m_augmenter(std::move(augmenter), std::move(keyextract))
331  , m_store(std::move(store)) {}
332 
333  combined_augmenter m_augmenter;
334  store_type m_store;
335 };
336 
337 
338 } //namespace bbits
339 
340 
341 } //namespace tpie
342 #endif /*_TPIE_BTREE_BASE_H_*/
Default < comparator for the btree.
Definition: base.h:53
Augmentation struct used in an un-augmented btree.
Definition: base.h:29
Storage used for an internal btree.
Definition: base.h:211
Augmented btree.
Definition: base.h:195
This file contains a few deprecated definitions for legacy code.
Functor used to extract the key from a value in case keys and values are the same.
Definition: base.h:45
Augmented btree builder.
Definition: base.h:205
Storage used for an external btree.
Definition: base.h:214
Serializing store.
Definition: base.h:217
Type that is useful for navigating a btree.
Definition: base.h:178
Functor used to augment an un-augmented btree.
Definition: base.h:34