Stxxl  1.3.1
btree.h
1 /***************************************************************************
2  * include/stxxl/bits/containers/btree/btree.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2006, 2008 Roman Dementiev <dementiev@ira.uka.de>
7  *
8  * Distributed under the Boost Software License, Version 1.0.
9  * (See accompanying file LICENSE_1_0.txt or copy at
10  * http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
13 #ifndef STXXL_CONTAINERS_BTREE__BTREE_H
14 #define STXXL_CONTAINERS_BTREE__BTREE_H
15 
16 #include <limits>
17 #include <stxxl/bits/namespace.h>
18 #include <stxxl/bits/containers/btree/iterator.h>
19 #include <stxxl/bits/containers/btree/iterator_map.h>
20 #include <stxxl/bits/containers/btree/leaf.h>
21 #include <stxxl/bits/containers/btree/node_cache.h>
22 #include <stxxl/bits/containers/btree/root_node.h>
23 #include <stxxl/bits/containers/btree/node.h>
24 #include <stxxl/vector>
25 
26 
27 __STXXL_BEGIN_NAMESPACE
28 
29 namespace btree
30 {
31  template <class KeyType,
32  class DataType,
33  class CompareType,
34  unsigned RawNodeSize,
35  unsigned RawLeafSize,
36  class PDAllocStrategy
37  >
38  class btree : private noncopyable
39  {
40  public:
41  typedef KeyType key_type;
42  typedef DataType data_type;
43  typedef CompareType key_compare;
44 
45  typedef btree<KeyType, DataType, CompareType, RawNodeSize, RawLeafSize, PDAllocStrategy> SelfType;
46 
47  typedef PDAllocStrategy alloc_strategy_type;
48 
49  typedef stxxl::uint64 size_type;
50  typedef stxxl::int64 difference_type;
51  typedef std::pair<const key_type, data_type> value_type;
52  typedef value_type & reference;
53  typedef const value_type & const_reference;
54  typedef value_type * pointer;
55  typedef value_type const * const_pointer;
56 
57 
58  // leaf type declarations
59  typedef normal_leaf<key_type, data_type, key_compare, RawLeafSize, SelfType> leaf_type;
60  friend class normal_leaf<key_type, data_type, key_compare, RawLeafSize, SelfType>;
61  typedef typename leaf_type::block_type leaf_block_type;
62  typedef typename leaf_type::bid_type leaf_bid_type;
63  typedef node_cache<leaf_type, SelfType> leaf_cache_type;
64  friend class node_cache<leaf_type, SelfType>;
65  // iterator types
66  typedef btree_iterator<SelfType> iterator;
67  typedef btree_const_iterator<SelfType> const_iterator;
68  friend class btree_iterator_base<SelfType>;
69  // iterator map type
70  typedef iterator_map<SelfType> iterator_map_type;
71  // node type declarations
72  typedef normal_node<key_type, key_compare, RawNodeSize, SelfType> node_type;
73  typedef typename node_type::block_type node_block_type;
74  friend class normal_node<key_type, key_compare, RawNodeSize, SelfType>;
75  typedef typename node_type::bid_type node_bid_type;
76  typedef node_cache<node_type, SelfType> node_cache_type;
77  friend class node_cache<node_type, SelfType>;
78 
79  typedef typename leaf_type::value_compare value_compare;
80 
81  enum {
82  min_node_size = node_type::min_size,
83  max_node_size = node_type::max_size,
84  min_leaf_size = leaf_type::min_size,
85  max_leaf_size = leaf_type::max_size
86  };
87 
88  private:
89  key_compare key_compare_;
90  mutable node_cache_type node_cache_;
91  mutable leaf_cache_type leaf_cache_;
92  iterator_map_type iterator_map_;
93  size_type size_;
94  unsigned_type height_;
95  bool prefetching_enabled_;
96  block_manager * bm_;
97  alloc_strategy_type alloc_strategy_;
98 
99  typedef std::map<key_type, node_bid_type, key_compare> root_node_type;
100  typedef typename root_node_type::iterator root_node_iterator_type;
101  typedef typename root_node_type::const_iterator root_node_const_iterator_type;
102  typedef std::pair<key_type, node_bid_type> root_node_pair_type;
103 
104 
105  root_node_type root_node_;
106  iterator end_iterator;
107 
108 
109  template <class BIDType>
110  void insert_into_root(const std::pair<key_type, BIDType> & splitter)
111  {
112  std::pair<root_node_iterator_type, bool> result =
113  root_node_.insert(splitter);
114  assert(result.second == true);
115  if (root_node_.size() > max_node_size) // root overflow
116  {
117  STXXL_VERBOSE1("btree::insert_into_root, overflow happened, splitting");
118 
119  node_bid_type LeftBid;
120  node_type * LeftNode = node_cache_.get_new_node(LeftBid);
121  assert(LeftNode);
122  node_bid_type RightBid;
123  node_type * RightNode = node_cache_.get_new_node(RightBid);
124  assert(RightNode);
125 
126  const unsigned_type old_size = root_node_.size();
127  const unsigned_type half = root_node_.size() / 2;
128  unsigned_type i = 0;
129  root_node_iterator_type it = root_node_.begin();
130  typename node_block_type::iterator block_it = LeftNode->block().begin();
131  while (i < half) // copy smaller part
132  {
133  *block_it = *it;
134  ++i;
135  ++block_it;
136  ++it;
137  }
138  LeftNode->block().info.cur_size = half;
139  key_type LeftKey = (LeftNode->block()[half - 1]).first;
140 
141  block_it = RightNode->block().begin();
142  while (i < old_size) // copy larger part
143  {
144  *block_it = *it;
145  ++i;
146  ++block_it;
147  ++it;
148  }
149  unsigned_type right_size = RightNode->block().info.cur_size = old_size - half;
150  key_type RightKey = (RightNode->block()[right_size - 1]).first;
151 
152  assert(old_size == RightNode->size() + LeftNode->size());
153 
154  // create new root node
155  root_node_.clear();
156  root_node_.insert(root_node_pair_type(LeftKey, LeftBid));
157  root_node_.insert(root_node_pair_type(RightKey, RightBid));
158 
159 
160  ++height_;
161  STXXL_VERBOSE1("btree Increasing height to " << height_);
162  if (node_cache_.size() < (height_ - 1))
163  {
164  STXXL_THROW(std::runtime_error, "btree::bulk_construction", "The height of the tree (" << height_ << ") has exceeded the required capacity ("
165  << (node_cache_.size() + 1) << ") of the node cache. " <<
166  "Increase the node cache size.");
167  }
168  }
169  }
170 
171  template <class CacheType>
172  void fuse_or_balance(root_node_iterator_type UIt, CacheType & cache_)
173  {
174  typedef typename CacheType::node_type local_node_type;
175  typedef typename local_node_type::bid_type local_bid_type;
176 
177  root_node_iterator_type leftIt, rightIt;
178  if (UIt->first == key_compare::max_value()) // UIt is the last entry in the root
179  {
180  assert(UIt != root_node_.begin());
181  rightIt = UIt;
182  leftIt = --UIt;
183  }
184  else
185  {
186  leftIt = UIt;
187  rightIt = ++UIt;
188  assert(rightIt != root_node_.end());
189  }
190 
191  // now fuse or balance nodes pointed by leftIt and rightIt
192  local_bid_type LeftBid = (local_bid_type)leftIt->second;
193  local_bid_type RightBid = (local_bid_type)rightIt->second;
194  local_node_type * LeftNode = cache_.get_node(LeftBid, true);
195  local_node_type * RightNode = cache_.get_node(RightBid, true);
196 
197  const unsigned_type TotalSize = LeftNode->size() + RightNode->size();
198  if (TotalSize <= RightNode->max_nelements())
199  {
200  // fuse
201  RightNode->fuse(*LeftNode); // add the content of LeftNode to RightNode
202 
203  cache_.unfix_node(RightBid);
204  cache_.delete_node(LeftBid); // 'delete_node' unfixes LeftBid also
205 
206  root_node_.erase(leftIt); // delete left BID from the root
207  }
208  else
209  {
210  // balance
211 
212  key_type NewSplitter = RightNode->balance(*LeftNode);
213 
214  root_node_.erase(leftIt); // delete left BID from the root
215  // reinsert with the new key
216  root_node_.insert(root_node_pair_type(NewSplitter, (node_bid_type)LeftBid));
217 
218  cache_.unfix_node(LeftBid);
219  cache_.unfix_node(RightBid);
220  }
221  }
222 
223  void create_empty_leaf()
224  {
225  leaf_bid_type NewBid;
226  leaf_type * NewLeaf = leaf_cache_.get_new_node(NewBid);
227  assert(NewLeaf);
228  end_iterator = NewLeaf->end(); // initialize end() iterator
229  root_node_.insert(root_node_pair_type(key_compare::max_value(), (node_bid_type)NewBid));
230  }
231 
232  void deallocate_children()
233  {
234  if (height_ == 2)
235  {
236  // we have children leaves here
237  root_node_const_iterator_type it = root_node_.begin();
238  for ( ; it != root_node_.end(); ++it)
239  {
240  // delete from leaf cache and deallocate bid
241  leaf_cache_.delete_node((leaf_bid_type)it->second);
242  }
243  }
244  else
245  {
246  root_node_const_iterator_type it = root_node_.begin();
247  for ( ; it != root_node_.end(); ++it)
248  {
249  node_type * Node = node_cache_.get_node((node_bid_type)it->second);
250  assert(Node);
251  Node->deallocate_children(height_ - 1);
252  // delete from node cache and deallocate bid
253  node_cache_.delete_node((node_bid_type)it->second);
254  }
255  }
256  }
257 
258  template <class InputIterator>
259  void bulk_construction(InputIterator b, InputIterator e, double node_fill_factor, double leaf_fill_factor)
260  {
261  assert(node_fill_factor >= 0.5);
262  assert(leaf_fill_factor >= 0.5);
263  key_type lastKey = key_compare::max_value();
264 
265  typedef std::pair<key_type, node_bid_type> key_bid_pair;
266  typedef typename stxxl::VECTOR_GENERATOR<key_bid_pair, 1, 1,
267  node_block_type::raw_size>::result key_bid_vector_type;
268 
269  key_bid_vector_type Bids;
270 
271  leaf_bid_type NewBid;
272  leaf_type * Leaf = leaf_cache_.get_new_node(NewBid);
273  const unsigned_type max_leaf_elements = unsigned_type(double(Leaf->max_nelements()) * leaf_fill_factor);
274 
275  while (b != e)
276  {
277  // write data in leaves
278 
279  // if *b not equal to the last element
280  if (key_compare_(b->first, lastKey) || key_compare_(lastKey, b->first))
281  {
282  ++size_;
283  if (Leaf->size() == max_leaf_elements)
284  {
285  // overflow, need a new block
286  Bids.push_back(key_bid_pair(Leaf->back().first, (node_bid_type)NewBid));
287 
288  leaf_type * NewLeaf = leaf_cache_.get_new_node(NewBid);
289  assert(NewLeaf);
290  // Setting links
291  Leaf->succ() = NewLeaf->my_bid();
292  NewLeaf->pred() = Leaf->my_bid();
293 
294  Leaf = NewLeaf;
295  }
296  Leaf->push_back(*b);
297  lastKey = b->first;
298  }
299  ++b;
300  }
301 
302  // rebalance the last leaf
303  if (Leaf->underflows() && !Bids.empty())
304  {
305  leaf_type * LeftLeaf = leaf_cache_.get_node((leaf_bid_type)(Bids.back().second));
306  assert(LeftLeaf);
307  if (LeftLeaf->size() + Leaf->size() <= Leaf->max_nelements()) // can fuse
308  {
309  Leaf->fuse(*LeftLeaf);
310  leaf_cache_.delete_node((leaf_bid_type)(Bids.back().second));
311  Bids.pop_back();
312  assert(!Leaf->overflows() && !Leaf->underflows());
313  }
314  else
315  {
316  // need to rebalance
317  const key_type NewSplitter = Leaf->balance(*LeftLeaf);
318  Bids.back().first = NewSplitter;
319  assert(!LeftLeaf->overflows() && !LeftLeaf->underflows());
320  }
321  }
322 
323  assert(!Leaf->overflows() && (!Leaf->underflows() || size_ <= max_leaf_size));
324 
325  end_iterator = Leaf->end(); // initialize end() iterator
326 
327  Bids.push_back(key_bid_pair(key_compare::max_value(), (node_bid_type)NewBid));
328 
329  const unsigned_type max_node_elements = unsigned_type(double(max_node_size) * node_fill_factor);
330 
331  while (Bids.size() > max_node_elements)
332  {
333  key_bid_vector_type ParentBids;
334 
335  stxxl::uint64 nparents = div_ceil(Bids.size(), max_node_elements);
336  assert(nparents >= 2);
337  STXXL_VERBOSE1("btree bulk constructBids.size() " << Bids.size() << " nparents: " << nparents << " max_ns: "
338  << max_node_elements);
339  typename key_bid_vector_type::const_iterator it = Bids.begin();
340 
341  do
342  {
343  node_bid_type NewBid;
344  node_type * Node = node_cache_.get_new_node(NewBid);
345  assert(Node);
346  unsigned_type cnt = 0;
347  for ( ; cnt < max_node_elements && it != Bids.end(); ++cnt, ++it)
348  {
349  Node->push_back(*it);
350  }
351  STXXL_VERBOSE1("btree bulk construct Node size : " << Node->size() << " limits: " <<
352  Node->min_nelements() << " " << Node->max_nelements() << " max_node_elements: " << max_node_elements);
353 
354  if (Node->underflows())
355  {
356  assert(it == Bids.end()); // this can happen only at the end
357  assert(!ParentBids.empty());
358 
359  node_type * LeftNode = node_cache_.get_node(ParentBids.back().second);
360  assert(LeftNode);
361  if (LeftNode->size() + Node->size() <= Node->max_nelements()) // can fuse
362  {
363  Node->fuse(*LeftNode);
364  node_cache_.delete_node(ParentBids.back().second);
365  ParentBids.pop_back();
366  }
367  else
368  { // need to rebalance
369  const key_type NewSplitter = Node->balance(*LeftNode);
370  ParentBids.back().first = NewSplitter;
371  assert(!LeftNode->overflows() && !LeftNode->underflows());
372  }
373  }
374  assert(!Node->overflows() && !Node->underflows());
375 
376  ParentBids.push_back(key_bid_pair(Node->back().first, NewBid));
377  } while (it != Bids.end());
378 
379  std::swap(ParentBids, Bids);
380 
381  assert(nparents == Bids.size() || (nparents - 1) == Bids.size());
382 
383  ++height_;
384  STXXL_VERBOSE1("Increasing height to " << height_);
385  if (node_cache_.size() < (height_ - 1))
386  {
387  STXXL_THROW(std::runtime_error, "btree::bulk_construction", "The height of the tree (" << height_ << ") has exceeded the required capacity ("
388  << (node_cache_.size() + 1) << ") of the node cache. " <<
389  "Increase the node cache size.");
390  }
391  }
392 
393  root_node_.insert(Bids.begin(), Bids.end());
394  }
395 
396  public:
397  btree(unsigned_type node_cache_size_in_bytes,
398  unsigned_type leaf_cache_size_in_bytes
399  ) :
400  node_cache_(node_cache_size_in_bytes, this, key_compare_),
401  leaf_cache_(leaf_cache_size_in_bytes, this, key_compare_),
402  iterator_map_(this),
403  size_(0),
404  height_(2),
405  prefetching_enabled_(true),
406  bm_(block_manager::get_instance())
407  {
408  STXXL_VERBOSE1("Creating a btree, addr=" << this);
409  STXXL_VERBOSE1(" bytes in a node: " << node_bid_type::size);
410  STXXL_VERBOSE1(" bytes in a leaf: " << leaf_bid_type::size);
411  STXXL_VERBOSE1(" elements in a node: " << node_block_type::size);
412  STXXL_VERBOSE1(" elements in a leaf: " << leaf_block_type::size);
413  STXXL_VERBOSE1(" size of a node element: " << sizeof(typename node_block_type::value_type));
414  STXXL_VERBOSE1(" size of a leaf element: " << sizeof(typename leaf_block_type::value_type));
415 
416 
417  create_empty_leaf();
418  }
419 
420  btree(const key_compare & c_,
421  unsigned_type node_cache_size_in_bytes,
422  unsigned_type leaf_cache_size_in_bytes
423  ) :
424  key_compare_(c_),
425  node_cache_(node_cache_size_in_bytes, this, key_compare_),
426  leaf_cache_(leaf_cache_size_in_bytes, this, key_compare_),
427  iterator_map_(this),
428  size_(0),
429  height_(2),
430  prefetching_enabled_(true),
431  bm_(block_manager::get_instance())
432  {
433  STXXL_VERBOSE1("Creating a btree, addr=" << this);
434  STXXL_VERBOSE1(" bytes in a node: " << node_bid_type::size);
435  STXXL_VERBOSE1(" bytes in a leaf: " << leaf_bid_type::size);
436 
437  create_empty_leaf();
438  }
439 
440  virtual ~btree()
441  {
442  try
443  {
444  deallocate_children();
445  } catch (...)
446  {
447  // no exceptions in destructor
448  }
449  }
450 
451  size_type size() const
452  {
453  return size_;
454  }
455 
456  size_type max_size() const
457  {
458  return (std::numeric_limits<size_type>::max)();
459  }
460 
461  bool empty() const
462  {
463  return !size_;
464  }
465 
466  std::pair<iterator, bool> insert(const value_type & x)
467  {
468  root_node_iterator_type it = root_node_.lower_bound(x.first);
469  assert(!root_node_.empty());
470  assert(it != root_node_.end());
471  if (height_ == 2) // 'it' points to a leaf
472  {
473  STXXL_VERBOSE1("Inserting new value into a leaf");
474  leaf_type * Leaf = leaf_cache_.get_node((leaf_bid_type)it->second, true);
475  assert(Leaf);
476  std::pair<key_type, leaf_bid_type> Splitter;
477  std::pair<iterator, bool> result = Leaf->insert(x, Splitter);
478  if (result.second)
479  ++size_;
480 
481  leaf_cache_.unfix_node((leaf_bid_type)it->second);
482  //if(key_compare::max_value() == Splitter.first)
483  if (!(key_compare_(key_compare::max_value(), Splitter.first) ||
484  key_compare_(Splitter.first, key_compare::max_value())))
485  return result;
486  // no overflow/splitting happened
487 
488  STXXL_VERBOSE1("Inserting new value into root node");
489 
490  insert_into_root(Splitter);
491 
492  assert(leaf_cache_.nfixed() == 0);
493  assert(node_cache_.nfixed() == 0);
494  return result;
495  }
496 
497  // 'it' points to a node
498  STXXL_VERBOSE1("Inserting new value into a node");
499  node_type * Node = node_cache_.get_node((node_bid_type)it->second, true);
500  assert(Node);
501  std::pair<key_type, node_bid_type> Splitter;
502  std::pair<iterator, bool> result = Node->insert(x, height_ - 1, Splitter);
503  if (result.second)
504  ++size_;
505 
506  node_cache_.unfix_node((node_bid_type)it->second);
507  //if(key_compare::max_value() == Splitter.first)
508  if (!(key_compare_(key_compare::max_value(), Splitter.first) ||
509  key_compare_(Splitter.first, key_compare::max_value())))
510  return result;
511  // no overflow/splitting happened
512 
513  STXXL_VERBOSE1("Inserting new value into root node");
514 
515  insert_into_root(Splitter);
516 
517  assert(leaf_cache_.nfixed() == 0);
518  assert(node_cache_.nfixed() == 0);
519 
520  return result;
521  }
522 
523  iterator begin()
524  {
525  root_node_iterator_type it = root_node_.begin();
526  assert(it != root_node_.end());
527 
528  if (height_ == 2) // 'it' points to a leaf
529  {
530  STXXL_VERBOSE1("btree: retrieving begin() from the first leaf");
531  leaf_type * Leaf = leaf_cache_.get_node((leaf_bid_type)it->second);
532  assert(Leaf);
533 
534  assert(leaf_cache_.nfixed() == 0);
535  assert(node_cache_.nfixed() == 0);
536  return Leaf->begin();
537  }
538 
539  // 'it' points to a node
540  STXXL_VERBOSE1("btree: retrieving begin() from the first node");
541  node_type * Node = node_cache_.get_node((node_bid_type)it->second, true);
542  assert(Node);
543  iterator result = Node->begin(height_ - 1);
544  node_cache_.unfix_node((node_bid_type)it->second);
545 
546  assert(leaf_cache_.nfixed() == 0);
547  assert(node_cache_.nfixed() == 0);
548 
549  return result;
550  }
551 
552  const_iterator begin() const
553  {
554  root_node_const_iterator_type it = root_node_.begin();
555  assert(it != root_node_.end());
556 
557  if (height_ == 2) // 'it' points to a leaf
558  {
559  STXXL_VERBOSE1("btree: retrieving begin() from the first leaf");
560  leaf_type const * Leaf = leaf_cache_.get_const_node((leaf_bid_type)it->second);
561  assert(Leaf);
562  assert(leaf_cache_.nfixed() == 0);
563  assert(node_cache_.nfixed() == 0);
564  return Leaf->begin();
565  }
566 
567  // 'it' points to a node
568  STXXL_VERBOSE1("btree: retrieving begin() from the first node");
569  node_type const * Node = node_cache_.get_const_node((node_bid_type)it->second, true);
570  assert(Node);
571  const_iterator result = Node->begin(height_ - 1);
572  node_cache_.unfix_node((node_bid_type)it->second);
573  assert(leaf_cache_.nfixed() == 0);
574  assert(node_cache_.nfixed() == 0);
575  return result;
576  }
577 
578  iterator end()
579  {
580  return end_iterator;
581  }
582 
583  const_iterator end() const
584  {
585  return end_iterator;
586  }
587 
588  data_type & operator [] (const key_type & k)
589  {
590  return (*((insert(value_type(k, data_type()))).first)).second;
591  }
592 
593  iterator find(const key_type & k)
594  {
595  root_node_iterator_type it = root_node_.lower_bound(k);
596  assert(it != root_node_.end());
597 
598  if (height_ == 2) // 'it' points to a leaf
599  {
600  STXXL_VERBOSE1("Searching in a leaf");
601  leaf_type * Leaf = leaf_cache_.get_node((leaf_bid_type)it->second, true);
602  assert(Leaf);
603  iterator result = Leaf->find(k);
604  leaf_cache_.unfix_node((leaf_bid_type)it->second);
605  assert(result == end() || result->first == k);
606  assert(leaf_cache_.nfixed() == 0);
607  assert(node_cache_.nfixed() == 0);
608  return result;
609  }
610 
611  // 'it' points to a node
612  STXXL_VERBOSE1("Searching in a node");
613  node_type * Node = node_cache_.get_node((node_bid_type)it->second, true);
614  assert(Node);
615  iterator result = Node->find(k, height_ - 1);
616  node_cache_.unfix_node((node_bid_type)it->second);
617 
618  assert(result == end() || result->first == k);
619  assert(leaf_cache_.nfixed() == 0);
620  assert(node_cache_.nfixed() == 0);
621  return result;
622  }
623 
624  const_iterator find(const key_type & k) const
625  {
626  root_node_const_iterator_type it = root_node_.lower_bound(k);
627  assert(it != root_node_.end());
628 
629  if (height_ == 2) // 'it' points to a leaf
630  {
631  STXXL_VERBOSE1("Searching in a leaf");
632  leaf_type const * Leaf = leaf_cache_.get_const_node((leaf_bid_type)it->second, true);
633  assert(Leaf);
634  const_iterator result = Leaf->find(k);
635  leaf_cache_.unfix_node((leaf_bid_type)it->second);
636  assert(result == end() || result->first == k);
637  assert(leaf_cache_.nfixed() == 0);
638  assert(node_cache_.nfixed() == 0);
639  return result;
640  }
641 
642  // 'it' points to a node
643  STXXL_VERBOSE1("Searching in a node");
644  node_type const * Node = node_cache_.get_const_node((node_bid_type)it->second, true);
645  assert(Node);
646  const_iterator result = Node->find(k, height_ - 1);
647  node_cache_.unfix_node((node_bid_type)it->second);
648 
649  assert(result == end() || result->first == k);
650  assert(leaf_cache_.nfixed() == 0);
651  assert(node_cache_.nfixed() == 0);
652  return result;
653  }
654 
655  iterator lower_bound(const key_type & k)
656  {
657  root_node_iterator_type it = root_node_.lower_bound(k);
658  assert(it != root_node_.end());
659 
660  if (height_ == 2) // 'it' points to a leaf
661  {
662  STXXL_VERBOSE1("Searching lower bound in a leaf");
663  leaf_type * Leaf = leaf_cache_.get_node((leaf_bid_type)it->second, true);
664  assert(Leaf);
665  iterator result = Leaf->lower_bound(k);
666  leaf_cache_.unfix_node((leaf_bid_type)it->second);
667  assert(leaf_cache_.nfixed() == 0);
668  assert(node_cache_.nfixed() == 0);
669  return result;
670  }
671 
672  // 'it' points to a node
673  STXXL_VERBOSE1("Searching lower bound in a node");
674  node_type * Node = node_cache_.get_node((node_bid_type)it->second, true);
675  assert(Node);
676  iterator result = Node->lower_bound(k, height_ - 1);
677  node_cache_.unfix_node((node_bid_type)it->second);
678 
679  assert(leaf_cache_.nfixed() == 0);
680  assert(node_cache_.nfixed() == 0);
681  return result;
682  }
683 
684  const_iterator lower_bound(const key_type & k) const
685  {
686  root_node_const_iterator_type it = root_node_.lower_bound(k);
687  assert(it != root_node_.end());
688 
689  if (height_ == 2) // 'it' points to a leaf
690  {
691  STXXL_VERBOSE1("Searching lower bound in a leaf");
692  leaf_type const * Leaf = leaf_cache_.get_const_node((leaf_bid_type)it->second, true);
693  assert(Leaf);
694  const_iterator result = Leaf->lower_bound(k);
695  leaf_cache_.unfix_node((leaf_bid_type)it->second);
696 
697  assert(leaf_cache_.nfixed() == 0);
698  assert(node_cache_.nfixed() == 0);
699  return result;
700  }
701 
702  // 'it' points to a node
703  STXXL_VERBOSE1("Searching lower bound in a node");
704  node_type const * Node = node_cache_.get_const_node((node_bid_type)it->second, true);
705  assert(Node);
706  const_iterator result = Node->lower_bound(k, height_ - 1);
707  node_cache_.unfix_node((node_bid_type)it->second);
708 
709  assert(leaf_cache_.nfixed() == 0);
710  assert(node_cache_.nfixed() == 0);
711  return result;
712  }
713 
714  iterator upper_bound(const key_type & k)
715  {
716  root_node_iterator_type it = root_node_.upper_bound(k);
717  assert(it != root_node_.end());
718 
719  if (height_ == 2) // 'it' points to a leaf
720  {
721  STXXL_VERBOSE1("Searching upper bound in a leaf");
722  leaf_type * Leaf = leaf_cache_.get_node((leaf_bid_type)it->second, true);
723  assert(Leaf);
724  iterator result = Leaf->upper_bound(k);
725  leaf_cache_.unfix_node((leaf_bid_type)it->second);
726 
727  assert(leaf_cache_.nfixed() == 0);
728  assert(node_cache_.nfixed() == 0);
729  return result;
730  }
731 
732  // 'it' points to a node
733  STXXL_VERBOSE1("Searching upper bound in a node");
734  node_type * Node = node_cache_.get_node((node_bid_type)it->second, true);
735  assert(Node);
736  iterator result = Node->upper_bound(k, height_ - 1);
737  node_cache_.unfix_node((node_bid_type)it->second);
738 
739  assert(leaf_cache_.nfixed() == 0);
740  assert(node_cache_.nfixed() == 0);
741  return result;
742  }
743 
744  const_iterator upper_bound(const key_type & k) const
745  {
746  root_node_const_iterator_type it = root_node_.upper_bound(k);
747  assert(it != root_node_.end());
748 
749  if (height_ == 2) // 'it' points to a leaf
750  {
751  STXXL_VERBOSE1("Searching upper bound in a leaf");
752  leaf_type const * Leaf = leaf_cache_.get_const_node((leaf_bid_type)it->second, true);
753  assert(Leaf);
754  const_iterator result = Leaf->upper_bound(k);
755  leaf_cache_.unfix_node((leaf_bid_type)it->second);
756 
757  assert(leaf_cache_.nfixed() == 0);
758  assert(node_cache_.nfixed() == 0);
759  return result;
760  }
761 
762  // 'it' points to a node
763  STXXL_VERBOSE1("Searching upper bound in a node");
764  node_type const * Node = node_cache_.get_const_node((node_bid_type)it->second, true);
765  assert(Node);
766  const_iterator result = Node->upper_bound(k, height_ - 1);
767  node_cache_.unfix_node((node_bid_type)it->second);
768 
769  assert(leaf_cache_.nfixed() == 0);
770  assert(node_cache_.nfixed() == 0);
771  return result;
772  }
773 
774  std::pair<iterator, iterator> equal_range(const key_type & k)
775  {
776  iterator l = lower_bound(k); // l->first >= k
777 
778  if (l == end() || key_compare_(k, l->first)) // if (k < l->first)
779  return std::pair<iterator, iterator>(l, l);
780  // then upper_bound == lower_bound
781 
782  iterator u = l;
783  ++u; // only one element ==k can exist
784 
785  assert(leaf_cache_.nfixed() == 0);
786  assert(node_cache_.nfixed() == 0);
787 
788  return std::pair<iterator, iterator>(l, u); // then upper_bound == (lower_bound+1)
789  }
790 
791  std::pair<const_iterator, const_iterator> equal_range(const key_type & k) const
792  {
793  const_iterator l = lower_bound(k); // l->first >= k
794 
795  if (l == end() || key_compare_(k, l->first)) // if (k < l->first)
796  return std::pair<const_iterator, const_iterator>(l, l);
797  // then upper_bound == lower_bound
798 
799  const_iterator u = l;
800  ++u; // only one element ==k can exist
801 
802  assert(leaf_cache_.nfixed() == 0);
803  assert(node_cache_.nfixed() == 0);
804  return std::pair<const_iterator, const_iterator>(l, u); // then upper_bound == (lower_bound+1)
805  }
806 
807  size_type erase(const key_type & k)
808  {
809  root_node_iterator_type it = root_node_.lower_bound(k);
810  assert(it != root_node_.end());
811  if (height_ == 2) // 'it' points to a leaf
812  {
813  STXXL_VERBOSE1("Deleting key from a leaf");
814  leaf_type * Leaf = leaf_cache_.get_node((leaf_bid_type)it->second, true);
815  assert(Leaf);
816  size_type result = Leaf->erase(k);
817  size_ -= result;
818  leaf_cache_.unfix_node((leaf_bid_type)it->second);
819  assert(leaf_cache_.nfixed() == 0);
820  assert(node_cache_.nfixed() == 0);
821 
822  if ((!Leaf->underflows()) || root_node_.size() == 1)
823  return result;
824  // no underflow or root has a special degree 1 (too few elements)
825 
826  STXXL_VERBOSE1("btree: Fusing or rebalancing a leaf");
827  fuse_or_balance(it, leaf_cache_);
828 
829  assert(leaf_cache_.nfixed() == 0);
830  assert(node_cache_.nfixed() == 0);
831 
832  return result;
833  }
834 
835  // 'it' points to a node
836  STXXL_VERBOSE1("Deleting key from a node");
837  assert(root_node_.size() >= 2);
838  node_type * Node = node_cache_.get_node((node_bid_type)it->second, true);
839  assert(Node);
840  size_type result = Node->erase(k, height_ - 1);
841  size_ -= result;
842  node_cache_.unfix_node((node_bid_type)it->second);
843  assert(leaf_cache_.nfixed() == 0);
844  assert(node_cache_.nfixed() == 0);
845  if (!Node->underflows())
846  return result;
847  // no underflow happened
848 
849  STXXL_VERBOSE1("Fusing or rebalancing a node");
850  fuse_or_balance(it, node_cache_);
851 
852  if (root_node_.size() == 1)
853  {
854  STXXL_VERBOSE1("btree Root has size 1 and height > 2");
855  STXXL_VERBOSE1("btree Deallocate root and decrease height");
856  it = root_node_.begin();
857  node_bid_type RootBid = it->second;
858  assert(it->first == key_compare::max_value());
859  node_type * RootNode = node_cache_.get_node(RootBid);
860  assert(RootNode);
861  assert(RootNode->back().first == key_compare::max_value());
862  root_node_.clear();
863  root_node_.insert(RootNode->block().begin(),
864  RootNode->block().begin() + RootNode->size());
865 
866  node_cache_.delete_node(RootBid);
867  --height_;
868  STXXL_VERBOSE1("btree Decreasing height to " << height_);
869  }
870 
871  assert(leaf_cache_.nfixed() == 0);
872  assert(node_cache_.nfixed() == 0);
873 
874  return result;
875  }
876 
877  size_type count(const key_type & k)
878  {
879  if (find(k) == end())
880  return 0;
881 
882  return 1;
883  }
884 
885  void erase(iterator pos)
886  {
887  assert(pos != end());
888 #ifndef NDEBUG
889  size_type old_size = size();
890 #endif
891 
892  erase(pos->first);
893 
894  assert(size() == old_size - 1);
895  }
896 
897  iterator insert(iterator /*pos*/, const value_type & x)
898  {
899  return insert(x).first; // pos ignored in the current version
900  }
901 
902  void clear()
903  {
904  deallocate_children();
905 
906  root_node_.clear();
907 
908  size_ = 0;
909  height_ = 2,
910 
911  create_empty_leaf();
912  assert(leaf_cache_.nfixed() == 0);
913  assert(node_cache_.nfixed() == 0);
914  }
915 
916  template <class InputIterator>
917  void insert(InputIterator b, InputIterator e)
918  {
919  while (b != e)
920  {
921  insert(*(b++));
922  }
923  }
924 
925  template <class InputIterator>
926  btree(InputIterator b,
927  InputIterator e,
928  const key_compare & c_,
929  unsigned_type node_cache_size_in_bytes,
930  unsigned_type leaf_cache_size_in_bytes,
931  bool range_sorted = false,
932  double node_fill_factor = 0.75,
933  double leaf_fill_factor = 0.6
934  ) :
935  key_compare_(c_),
936  node_cache_(node_cache_size_in_bytes, this, key_compare_),
937  leaf_cache_(leaf_cache_size_in_bytes, this, key_compare_),
938  iterator_map_(this),
939  size_(0),
940  height_(2),
941  prefetching_enabled_(true),
942  bm_(block_manager::get_instance())
943  {
944  STXXL_VERBOSE1("Creating a btree, addr=" << this);
945  STXXL_VERBOSE1(" bytes in a node: " << node_bid_type::size);
946  STXXL_VERBOSE1(" bytes in a leaf: " << leaf_bid_type::size);
947 
948  if (range_sorted == false)
949  {
950  create_empty_leaf();
951  insert(b, e);
952  assert(leaf_cache_.nfixed() == 0);
953  assert(node_cache_.nfixed() == 0);
954  return;
955  }
956 
957  bulk_construction(b, e, node_fill_factor, leaf_fill_factor);
958  assert(leaf_cache_.nfixed() == 0);
959  assert(node_cache_.nfixed() == 0);
960  }
961 
962 
963  template <class InputIterator>
964  btree(InputIterator b,
965  InputIterator e,
966  unsigned_type node_cache_size_in_bytes,
967  unsigned_type leaf_cache_size_in_bytes,
968  bool range_sorted = false,
969  double node_fill_factor = 0.75,
970  double leaf_fill_factor = 0.6
971  ) :
972  node_cache_(node_cache_size_in_bytes, this, key_compare_),
973  leaf_cache_(leaf_cache_size_in_bytes, this, key_compare_),
974  iterator_map_(this),
975  size_(0),
976  height_(2),
977  prefetching_enabled_(true),
978  bm_(block_manager::get_instance())
979  {
980  STXXL_VERBOSE1("Creating a btree, addr=" << this);
981  STXXL_VERBOSE1(" bytes in a node: " << node_bid_type::size);
982  STXXL_VERBOSE1(" bytes in a leaf: " << leaf_bid_type::size);
983 
984  if (range_sorted == false)
985  {
986  create_empty_leaf();
987  insert(b, e);
988  assert(leaf_cache_.nfixed() == 0);
989  assert(node_cache_.nfixed() == 0);
990  return;
991  }
992 
993  bulk_construction(b, e, node_fill_factor, leaf_fill_factor);
994  assert(leaf_cache_.nfixed() == 0);
995  assert(node_cache_.nfixed() == 0);
996  }
997 
998  void erase(iterator first, iterator last)
999  {
1000  if (first == begin() && last == end())
1001  clear();
1002 
1003  else
1004  while (first != last)
1005  erase(first++);
1006  }
1007 
1008  key_compare key_comp() const
1009  {
1010  return key_compare_;
1011  }
1012  value_compare value_comp() const
1013  {
1014  return value_compare(key_compare_);
1015  }
1016 
1017  void swap(btree & obj)
1018  {
1019  std::swap(key_compare_, obj.key_compare_); // OK
1020 
1021  std::swap(node_cache_, obj.node_cache_); // OK
1022  std::swap(leaf_cache_, obj.leaf_cache_); // OK
1023 
1024 
1025  std::swap(iterator_map_, obj.iterator_map_); // must update all iterators
1026 
1027  std::swap(end_iterator, obj.end_iterator);
1028  std::swap(size_, obj.size_);
1029  std::swap(height_, obj.height_);
1030  std::swap(alloc_strategy_, obj.alloc_strategy_);
1031  std::swap(root_node_, obj.root_node_);
1032  }
1033 
1034  void enable_prefetching()
1035  {
1036  prefetching_enabled_ = true;
1037  }
1038  void disable_prefetching()
1039  {
1040  prefetching_enabled_ = false;
1041  }
1042  bool prefetching_enabled()
1043  {
1044  return prefetching_enabled_;
1045  }
1046 
1047  void print_statistics(std::ostream & o) const
1048  {
1049  o << "Node cache statistics:" << std::endl;
1050  node_cache_.print_statistics(o);
1051  o << "Leaf cache statistics:" << std::endl;
1052  leaf_cache_.print_statistics(o);
1053  }
1054  void reset_statistics()
1055  {
1056  node_cache_.reset_statistics();
1057  leaf_cache_.reset_statistics();
1058  }
1059  };
1060 
1061  template <class KeyType,
1062  class DataType,
1063  class CompareType,
1064  unsigned LogNodeSize,
1065  unsigned LogLeafSize,
1066  class PDAllocStrategy
1067  >
1068  inline bool operator == (const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & a,
1069  const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & b)
1070  {
1071  return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin());
1072  }
1073 
1074  template <class KeyType,
1075  class DataType,
1076  class CompareType,
1077  unsigned LogNodeSize,
1078  unsigned LogLeafSize,
1079  class PDAllocStrategy
1080  >
1081  inline bool operator != (const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & a,
1082  const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & b)
1083  {
1084  return !(a == b);
1085  }
1086 
1087 
1088  template <class KeyType,
1089  class DataType,
1090  class CompareType,
1091  unsigned LogNodeSize,
1092  unsigned LogLeafSize,
1093  class PDAllocStrategy
1094  >
1095  inline bool operator < (const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & a,
1096  const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & b)
1097  {
1098  return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
1099  }
1100 
1101 
1102  template <class KeyType,
1103  class DataType,
1104  class CompareType,
1105  unsigned LogNodeSize,
1106  unsigned LogLeafSize,
1107  class PDAllocStrategy
1108  >
1109  inline bool operator > (const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & a,
1110  const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & b)
1111  {
1112  return b < a;
1113  }
1114 
1115 
1116  template <class KeyType,
1117  class DataType,
1118  class CompareType,
1119  unsigned LogNodeSize,
1120  unsigned LogLeafSize,
1121  class PDAllocStrategy
1122  >
1123  inline bool operator <= (const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & a,
1124  const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & b)
1125  {
1126  return !(b < a);
1127  }
1128 
1129  template <class KeyType,
1130  class DataType,
1131  class CompareType,
1132  unsigned LogNodeSize,
1133  unsigned LogLeafSize,
1134  class PDAllocStrategy
1135  >
1136  inline bool operator >= (const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & a,
1137  const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & b)
1138  {
1139  return !(a < b);
1140  }
1141 }
1142 
1143 __STXXL_END_NAMESPACE
1144 
1145 
1146 namespace std
1147 {
1148  template <class KeyType,
1149  class DataType,
1150  class CompareType,
1151  unsigned LogNodeSize,
1152  unsigned LogLeafSize,
1153  class PDAllocStrategy
1154  >
1155  void swap(stxxl::btree::btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & a,
1156  stxxl::btree::btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & b)
1157  {
1158  if (&a != &b)
1159  a.swap(b);
1160  }
1161 }
1162 
1163 #endif /* STXXL_CONTAINERS_BTREE__BTREE_H */
_ExtIterator find(_ExtIterator _begin, _ExtIterator _end, const _EqualityComparable &_value, int_type nbuffers)
External equivalent of std::find.
Definition: scan.h:215
Block manager class.
Definition: mng.h:59