13 #ifndef STXXL_CONTAINERS_BTREE__BTREE_H
14 #define STXXL_CONTAINERS_BTREE__BTREE_H
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>
27 __STXXL_BEGIN_NAMESPACE
31 template <
class KeyType,
38 class btree :
private noncopyable
41 typedef KeyType key_type;
42 typedef DataType data_type;
43 typedef CompareType key_compare;
45 typedef btree<KeyType, DataType, CompareType, RawNodeSize, RawLeafSize, PDAllocStrategy> SelfType;
47 typedef PDAllocStrategy alloc_strategy_type;
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;
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>;
66 typedef btree_iterator<SelfType> iterator;
67 typedef btree_const_iterator<SelfType> const_iterator;
68 friend class btree_iterator_base<SelfType>;
70 typedef iterator_map<SelfType> iterator_map_type;
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>;
79 typedef typename leaf_type::value_compare value_compare;
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
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_;
94 unsigned_type height_;
95 bool prefetching_enabled_;
97 alloc_strategy_type alloc_strategy_;
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;
105 root_node_type root_node_;
106 iterator end_iterator;
109 template <
class BIDType>
110 void insert_into_root(
const std::pair<key_type, BIDType> & splitter)
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)
117 STXXL_VERBOSE1(
"btree::insert_into_root, overflow happened, splitting");
119 node_bid_type LeftBid;
120 node_type * LeftNode = node_cache_.get_new_node(LeftBid);
122 node_bid_type RightBid;
123 node_type * RightNode = node_cache_.get_new_node(RightBid);
126 const unsigned_type old_size = root_node_.size();
127 const unsigned_type half = root_node_.size() / 2;
129 root_node_iterator_type it = root_node_.begin();
130 typename node_block_type::iterator block_it = LeftNode->block().begin();
138 LeftNode->block().info.cur_size = half;
139 key_type LeftKey = (LeftNode->block()[half - 1]).first;
141 block_it = RightNode->block().begin();
149 unsigned_type right_size = RightNode->block().info.cur_size = old_size - half;
150 key_type RightKey = (RightNode->block()[right_size - 1]).first;
152 assert(old_size == RightNode->size() + LeftNode->size());
156 root_node_.insert(root_node_pair_type(LeftKey, LeftBid));
157 root_node_.insert(root_node_pair_type(RightKey, RightBid));
161 STXXL_VERBOSE1(
"btree Increasing height to " << height_);
162 if (node_cache_.size() < (height_ - 1))
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.");
171 template <
class CacheType>
172 void fuse_or_balance(root_node_iterator_type UIt, CacheType & cache_)
174 typedef typename CacheType::node_type local_node_type;
175 typedef typename local_node_type::bid_type local_bid_type;
177 root_node_iterator_type leftIt, rightIt;
178 if (UIt->first == key_compare::max_value())
180 assert(UIt != root_node_.begin());
188 assert(rightIt != root_node_.end());
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);
197 const unsigned_type TotalSize = LeftNode->size() + RightNode->size();
198 if (TotalSize <= RightNode->max_nelements())
201 RightNode->fuse(*LeftNode);
203 cache_.unfix_node(RightBid);
204 cache_.delete_node(LeftBid);
206 root_node_.erase(leftIt);
212 key_type NewSplitter = RightNode->balance(*LeftNode);
214 root_node_.erase(leftIt);
216 root_node_.insert(root_node_pair_type(NewSplitter, (node_bid_type)LeftBid));
218 cache_.unfix_node(LeftBid);
219 cache_.unfix_node(RightBid);
223 void create_empty_leaf()
225 leaf_bid_type NewBid;
226 leaf_type * NewLeaf = leaf_cache_.get_new_node(NewBid);
228 end_iterator = NewLeaf->end();
229 root_node_.insert(root_node_pair_type(key_compare::max_value(), (node_bid_type)NewBid));
232 void deallocate_children()
237 root_node_const_iterator_type it = root_node_.begin();
238 for ( ; it != root_node_.end(); ++it)
241 leaf_cache_.delete_node((leaf_bid_type)it->second);
246 root_node_const_iterator_type it = root_node_.begin();
247 for ( ; it != root_node_.end(); ++it)
249 node_type * Node = node_cache_.get_node((node_bid_type)it->second);
251 Node->deallocate_children(height_ - 1);
253 node_cache_.delete_node((node_bid_type)it->second);
258 template <
class InputIterator>
259 void bulk_construction(InputIterator b, InputIterator e,
double node_fill_factor,
double leaf_fill_factor)
261 assert(node_fill_factor >= 0.5);
262 assert(leaf_fill_factor >= 0.5);
263 key_type lastKey = key_compare::max_value();
265 typedef std::pair<key_type, node_bid_type> key_bid_pair;
266 typedef typename stxxl::VECTOR_GENERATOR<key_bid_pair, 1, 1,
269 key_bid_vector_type Bids;
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);
280 if (key_compare_(b->first, lastKey) || key_compare_(lastKey, b->first))
283 if (Leaf->size() == max_leaf_elements)
286 Bids.push_back(key_bid_pair(Leaf->back().first, (node_bid_type)NewBid));
288 leaf_type * NewLeaf = leaf_cache_.get_new_node(NewBid);
291 Leaf->succ() = NewLeaf->my_bid();
292 NewLeaf->pred() = Leaf->my_bid();
303 if (Leaf->underflows() && !Bids.empty())
305 leaf_type * LeftLeaf = leaf_cache_.get_node((leaf_bid_type)(Bids.back().second));
307 if (LeftLeaf->size() + Leaf->size() <= Leaf->max_nelements())
309 Leaf->fuse(*LeftLeaf);
310 leaf_cache_.delete_node((leaf_bid_type)(Bids.back().second));
312 assert(!Leaf->overflows() && !Leaf->underflows());
317 const key_type NewSplitter = Leaf->balance(*LeftLeaf);
318 Bids.back().first = NewSplitter;
319 assert(!LeftLeaf->overflows() && !LeftLeaf->underflows());
323 assert(!Leaf->overflows() && (!Leaf->underflows() || size_ <= max_leaf_size));
325 end_iterator = Leaf->end();
327 Bids.push_back(key_bid_pair(key_compare::max_value(), (node_bid_type)NewBid));
329 const unsigned_type max_node_elements = unsigned_type(
double(max_node_size) * node_fill_factor);
331 while (Bids.size() > max_node_elements)
333 key_bid_vector_type ParentBids;
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 STXXL_UNUSED(nparents);
340 typename key_bid_vector_type::const_iterator it = Bids.begin();
344 node_bid_type NewBid;
345 node_type * Node = node_cache_.get_new_node(NewBid);
347 unsigned_type cnt = 0;
348 for ( ; cnt < max_node_elements && it != Bids.end(); ++cnt, ++it)
350 Node->push_back(*it);
352 STXXL_VERBOSE1(
"btree bulk construct Node size : " << Node->size() <<
" limits: " <<
353 Node->min_nelements() <<
" " << Node->max_nelements() <<
" max_node_elements: " << max_node_elements);
355 if (Node->underflows())
357 assert(it == Bids.end());
358 assert(!ParentBids.empty());
360 node_type * LeftNode = node_cache_.get_node(ParentBids.back().second);
362 if (LeftNode->size() + Node->size() <= Node->max_nelements())
364 Node->fuse(*LeftNode);
365 node_cache_.delete_node(ParentBids.back().second);
366 ParentBids.pop_back();
370 const key_type NewSplitter = Node->balance(*LeftNode);
371 ParentBids.back().first = NewSplitter;
372 assert(!LeftNode->overflows() && !LeftNode->underflows());
375 assert(!Node->overflows() && !Node->underflows());
377 ParentBids.push_back(key_bid_pair(Node->back().first, NewBid));
378 }
while (it != Bids.end());
380 std::swap(ParentBids, Bids);
382 assert(nparents == Bids.size() || (nparents - 1) == Bids.size());
385 STXXL_VERBOSE1(
"Increasing height to " << height_);
386 if (node_cache_.size() < (height_ - 1))
388 STXXL_THROW(std::runtime_error,
"btree::bulk_construction",
"The height of the tree (" << height_ <<
") has exceeded the required capacity ("
389 << (node_cache_.size() + 1) <<
") of the node cache. " <<
390 "Increase the node cache size.");
394 root_node_.insert(Bids.begin(), Bids.end());
398 btree(unsigned_type node_cache_size_in_bytes,
399 unsigned_type leaf_cache_size_in_bytes
401 node_cache_(node_cache_size_in_bytes, this, key_compare_),
402 leaf_cache_(leaf_cache_size_in_bytes, this, key_compare_),
406 prefetching_enabled_(true),
409 STXXL_VERBOSE1(
"Creating a btree, addr=" <<
this);
414 STXXL_VERBOSE1(
" size of a node element: " <<
sizeof(
typename node_block_type::value_type));
415 STXXL_VERBOSE1(
" size of a leaf element: " <<
sizeof(
typename leaf_block_type::value_type));
421 btree(
const key_compare & c_,
422 unsigned_type node_cache_size_in_bytes,
423 unsigned_type leaf_cache_size_in_bytes
426 node_cache_(node_cache_size_in_bytes, this, key_compare_),
427 leaf_cache_(leaf_cache_size_in_bytes, this, key_compare_),
431 prefetching_enabled_(true),
434 STXXL_VERBOSE1(
"Creating a btree, addr=" <<
this);
445 deallocate_children();
452 size_type size()
const
457 size_type max_size()
const
459 return (std::numeric_limits<size_type>::max)();
467 std::pair<iterator, bool> insert(
const value_type & x)
469 root_node_iterator_type it = root_node_.lower_bound(x.first);
470 assert(!root_node_.empty());
471 assert(it != root_node_.end());
474 STXXL_VERBOSE1(
"Inserting new value into a leaf");
475 leaf_type * Leaf = leaf_cache_.get_node((leaf_bid_type)it->second,
true);
477 std::pair<key_type, leaf_bid_type> Splitter;
478 std::pair<iterator, bool> result = Leaf->insert(x, Splitter);
482 leaf_cache_.unfix_node((leaf_bid_type)it->second);
484 if (!(key_compare_(key_compare::max_value(), Splitter.first) ||
485 key_compare_(Splitter.first, key_compare::max_value())))
489 STXXL_VERBOSE1(
"Inserting new value into root node");
491 insert_into_root(Splitter);
493 assert(leaf_cache_.nfixed() == 0);
494 assert(node_cache_.nfixed() == 0);
499 STXXL_VERBOSE1(
"Inserting new value into a node");
500 node_type * Node = node_cache_.get_node((node_bid_type)it->second,
true);
502 std::pair<key_type, node_bid_type> Splitter;
503 std::pair<iterator, bool> result = Node->insert(x, height_ - 1, Splitter);
507 node_cache_.unfix_node((node_bid_type)it->second);
509 if (!(key_compare_(key_compare::max_value(), Splitter.first) ||
510 key_compare_(Splitter.first, key_compare::max_value())))
514 STXXL_VERBOSE1(
"Inserting new value into root node");
516 insert_into_root(Splitter);
518 assert(leaf_cache_.nfixed() == 0);
519 assert(node_cache_.nfixed() == 0);
526 root_node_iterator_type it = root_node_.begin();
527 assert(it != root_node_.end());
531 STXXL_VERBOSE1(
"btree: retrieving begin() from the first leaf");
532 leaf_type * Leaf = leaf_cache_.get_node((leaf_bid_type)it->second);
535 assert(leaf_cache_.nfixed() == 0);
536 assert(node_cache_.nfixed() == 0);
537 return Leaf->begin();
541 STXXL_VERBOSE1(
"btree: retrieving begin() from the first node");
542 node_type * Node = node_cache_.get_node((node_bid_type)it->second,
true);
544 iterator result = Node->begin(height_ - 1);
545 node_cache_.unfix_node((node_bid_type)it->second);
547 assert(leaf_cache_.nfixed() == 0);
548 assert(node_cache_.nfixed() == 0);
553 const_iterator begin()
const
555 root_node_const_iterator_type it = root_node_.begin();
556 assert(it != root_node_.end());
560 STXXL_VERBOSE1(
"btree: retrieving begin() from the first leaf");
561 leaf_type
const * Leaf = leaf_cache_.get_const_node((leaf_bid_type)it->second);
563 assert(leaf_cache_.nfixed() == 0);
564 assert(node_cache_.nfixed() == 0);
565 return Leaf->begin();
569 STXXL_VERBOSE1(
"btree: retrieving begin() from the first node");
570 node_type
const * Node = node_cache_.get_const_node((node_bid_type)it->second,
true);
572 const_iterator result = Node->begin(height_ - 1);
573 node_cache_.unfix_node((node_bid_type)it->second);
574 assert(leaf_cache_.nfixed() == 0);
575 assert(node_cache_.nfixed() == 0);
584 const_iterator end()
const
589 data_type & operator [] (
const key_type & k)
591 return (*((insert(value_type(k, data_type()))).first)).second;
594 iterator
find(
const key_type & k)
596 root_node_iterator_type it = root_node_.lower_bound(k);
597 assert(it != root_node_.end());
601 STXXL_VERBOSE1(
"Searching in a leaf");
602 leaf_type * Leaf = leaf_cache_.get_node((leaf_bid_type)it->second,
true);
604 iterator result = Leaf->find(k);
605 leaf_cache_.unfix_node((leaf_bid_type)it->second);
606 assert(result == end() || result->first == k);
607 assert(leaf_cache_.nfixed() == 0);
608 assert(node_cache_.nfixed() == 0);
613 STXXL_VERBOSE1(
"Searching in a node");
614 node_type * Node = node_cache_.get_node((node_bid_type)it->second,
true);
616 iterator result = Node->find(k, height_ - 1);
617 node_cache_.unfix_node((node_bid_type)it->second);
619 assert(result == end() || result->first == k);
620 assert(leaf_cache_.nfixed() == 0);
621 assert(node_cache_.nfixed() == 0);
625 const_iterator
find(
const key_type & k)
const
627 root_node_const_iterator_type it = root_node_.lower_bound(k);
628 assert(it != root_node_.end());
632 STXXL_VERBOSE1(
"Searching in a leaf");
633 leaf_type
const * Leaf = leaf_cache_.get_const_node((leaf_bid_type)it->second,
true);
635 const_iterator result = Leaf->find(k);
636 leaf_cache_.unfix_node((leaf_bid_type)it->second);
637 assert(result == end() || result->first == k);
638 assert(leaf_cache_.nfixed() == 0);
639 assert(node_cache_.nfixed() == 0);
644 STXXL_VERBOSE1(
"Searching in a node");
645 node_type
const * Node = node_cache_.get_const_node((node_bid_type)it->second,
true);
647 const_iterator result = Node->find(k, height_ - 1);
648 node_cache_.unfix_node((node_bid_type)it->second);
650 assert(result == end() || result->first == k);
651 assert(leaf_cache_.nfixed() == 0);
652 assert(node_cache_.nfixed() == 0);
656 iterator lower_bound(
const key_type & k)
658 root_node_iterator_type it = root_node_.lower_bound(k);
659 assert(it != root_node_.end());
663 STXXL_VERBOSE1(
"Searching lower bound in a leaf");
664 leaf_type * Leaf = leaf_cache_.get_node((leaf_bid_type)it->second,
true);
666 iterator result = Leaf->lower_bound(k);
667 leaf_cache_.unfix_node((leaf_bid_type)it->second);
668 assert(leaf_cache_.nfixed() == 0);
669 assert(node_cache_.nfixed() == 0);
674 STXXL_VERBOSE1(
"Searching lower bound in a node");
675 node_type * Node = node_cache_.get_node((node_bid_type)it->second,
true);
677 iterator result = Node->lower_bound(k, height_ - 1);
678 node_cache_.unfix_node((node_bid_type)it->second);
680 assert(leaf_cache_.nfixed() == 0);
681 assert(node_cache_.nfixed() == 0);
685 const_iterator lower_bound(
const key_type & k)
const
687 root_node_const_iterator_type it = root_node_.lower_bound(k);
688 assert(it != root_node_.end());
692 STXXL_VERBOSE1(
"Searching lower bound in a leaf");
693 leaf_type
const * Leaf = leaf_cache_.get_const_node((leaf_bid_type)it->second,
true);
695 const_iterator result = Leaf->lower_bound(k);
696 leaf_cache_.unfix_node((leaf_bid_type)it->second);
698 assert(leaf_cache_.nfixed() == 0);
699 assert(node_cache_.nfixed() == 0);
704 STXXL_VERBOSE1(
"Searching lower bound in a node");
705 node_type
const * Node = node_cache_.get_const_node((node_bid_type)it->second,
true);
707 const_iterator result = Node->lower_bound(k, height_ - 1);
708 node_cache_.unfix_node((node_bid_type)it->second);
710 assert(leaf_cache_.nfixed() == 0);
711 assert(node_cache_.nfixed() == 0);
715 iterator upper_bound(
const key_type & k)
717 root_node_iterator_type it = root_node_.upper_bound(k);
718 assert(it != root_node_.end());
722 STXXL_VERBOSE1(
"Searching upper bound in a leaf");
723 leaf_type * Leaf = leaf_cache_.get_node((leaf_bid_type)it->second,
true);
725 iterator result = Leaf->upper_bound(k);
726 leaf_cache_.unfix_node((leaf_bid_type)it->second);
728 assert(leaf_cache_.nfixed() == 0);
729 assert(node_cache_.nfixed() == 0);
734 STXXL_VERBOSE1(
"Searching upper bound in a node");
735 node_type * Node = node_cache_.get_node((node_bid_type)it->second,
true);
737 iterator result = Node->upper_bound(k, height_ - 1);
738 node_cache_.unfix_node((node_bid_type)it->second);
740 assert(leaf_cache_.nfixed() == 0);
741 assert(node_cache_.nfixed() == 0);
745 const_iterator upper_bound(
const key_type & k)
const
747 root_node_const_iterator_type it = root_node_.upper_bound(k);
748 assert(it != root_node_.end());
752 STXXL_VERBOSE1(
"Searching upper bound in a leaf");
753 leaf_type
const * Leaf = leaf_cache_.get_const_node((leaf_bid_type)it->second,
true);
755 const_iterator result = Leaf->upper_bound(k);
756 leaf_cache_.unfix_node((leaf_bid_type)it->second);
758 assert(leaf_cache_.nfixed() == 0);
759 assert(node_cache_.nfixed() == 0);
764 STXXL_VERBOSE1(
"Searching upper bound in a node");
765 node_type
const * Node = node_cache_.get_const_node((node_bid_type)it->second,
true);
767 const_iterator result = Node->upper_bound(k, height_ - 1);
768 node_cache_.unfix_node((node_bid_type)it->second);
770 assert(leaf_cache_.nfixed() == 0);
771 assert(node_cache_.nfixed() == 0);
775 std::pair<iterator, iterator> equal_range(
const key_type & k)
777 iterator l = lower_bound(k);
779 if (l == end() || key_compare_(k, l->first))
780 return std::pair<iterator, iterator>(l, l);
786 assert(leaf_cache_.nfixed() == 0);
787 assert(node_cache_.nfixed() == 0);
789 return std::pair<iterator, iterator>(l, u);
792 std::pair<const_iterator, const_iterator> equal_range(
const key_type & k)
const
794 const_iterator l = lower_bound(k);
796 if (l == end() || key_compare_(k, l->first))
797 return std::pair<const_iterator, const_iterator>(l, l);
800 const_iterator u = l;
803 assert(leaf_cache_.nfixed() == 0);
804 assert(node_cache_.nfixed() == 0);
805 return std::pair<const_iterator, const_iterator>(l, u);
808 size_type erase(
const key_type & k)
810 root_node_iterator_type it = root_node_.lower_bound(k);
811 assert(it != root_node_.end());
814 STXXL_VERBOSE1(
"Deleting key from a leaf");
815 leaf_type * Leaf = leaf_cache_.get_node((leaf_bid_type)it->second,
true);
817 size_type result = Leaf->erase(k);
819 leaf_cache_.unfix_node((leaf_bid_type)it->second);
820 assert(leaf_cache_.nfixed() == 0);
821 assert(node_cache_.nfixed() == 0);
823 if ((!Leaf->underflows()) || root_node_.size() == 1)
827 STXXL_VERBOSE1(
"btree: Fusing or rebalancing a leaf");
828 fuse_or_balance(it, leaf_cache_);
830 assert(leaf_cache_.nfixed() == 0);
831 assert(node_cache_.nfixed() == 0);
837 STXXL_VERBOSE1(
"Deleting key from a node");
838 assert(root_node_.size() >= 2);
839 node_type * Node = node_cache_.get_node((node_bid_type)it->second,
true);
841 size_type result = Node->erase(k, height_ - 1);
843 node_cache_.unfix_node((node_bid_type)it->second);
844 assert(leaf_cache_.nfixed() == 0);
845 assert(node_cache_.nfixed() == 0);
846 if (!Node->underflows())
850 STXXL_VERBOSE1(
"Fusing or rebalancing a node");
851 fuse_or_balance(it, node_cache_);
853 if (root_node_.size() == 1)
855 STXXL_VERBOSE1(
"btree Root has size 1 and height > 2");
856 STXXL_VERBOSE1(
"btree Deallocate root and decrease height");
857 it = root_node_.begin();
858 node_bid_type RootBid = it->second;
859 assert(it->first == key_compare::max_value());
860 node_type * RootNode = node_cache_.get_node(RootBid);
862 assert(RootNode->back().first == key_compare::max_value());
864 root_node_.insert(RootNode->block().begin(),
865 RootNode->block().begin() + RootNode->size());
867 node_cache_.delete_node(RootBid);
869 STXXL_VERBOSE1(
"btree Decreasing height to " << height_);
872 assert(leaf_cache_.nfixed() == 0);
873 assert(node_cache_.nfixed() == 0);
878 size_type count(
const key_type & k)
880 if (
find(k) == end())
886 void erase(iterator pos)
888 assert(pos != end());
890 size_type old_size = size();
895 assert(size() == old_size - 1);
898 iterator insert(iterator ,
const value_type & x)
900 return insert(x).first;
905 deallocate_children();
913 assert(leaf_cache_.nfixed() == 0);
914 assert(node_cache_.nfixed() == 0);
917 template <
class InputIterator>
918 void insert(InputIterator b, InputIterator e)
926 template <
class InputIterator>
927 btree(InputIterator b,
929 const key_compare & c_,
930 unsigned_type node_cache_size_in_bytes,
931 unsigned_type leaf_cache_size_in_bytes,
932 bool range_sorted =
false,
933 double node_fill_factor = 0.75,
934 double leaf_fill_factor = 0.6
937 node_cache_(node_cache_size_in_bytes, this, key_compare_),
938 leaf_cache_(leaf_cache_size_in_bytes, this, key_compare_),
942 prefetching_enabled_(true),
945 STXXL_VERBOSE1(
"Creating a btree, addr=" <<
this);
949 if (range_sorted ==
false)
953 assert(leaf_cache_.nfixed() == 0);
954 assert(node_cache_.nfixed() == 0);
958 bulk_construction(b, e, node_fill_factor, leaf_fill_factor);
959 assert(leaf_cache_.nfixed() == 0);
960 assert(node_cache_.nfixed() == 0);
964 template <
class InputIterator>
965 btree(InputIterator b,
967 unsigned_type node_cache_size_in_bytes,
968 unsigned_type leaf_cache_size_in_bytes,
969 bool range_sorted =
false,
970 double node_fill_factor = 0.75,
971 double leaf_fill_factor = 0.6
973 node_cache_(node_cache_size_in_bytes, this, key_compare_),
974 leaf_cache_(leaf_cache_size_in_bytes, this, key_compare_),
978 prefetching_enabled_(true),
981 STXXL_VERBOSE1(
"Creating a btree, addr=" <<
this);
985 if (range_sorted ==
false)
989 assert(leaf_cache_.nfixed() == 0);
990 assert(node_cache_.nfixed() == 0);
994 bulk_construction(b, e, node_fill_factor, leaf_fill_factor);
995 assert(leaf_cache_.nfixed() == 0);
996 assert(node_cache_.nfixed() == 0);
999 void erase(iterator first, iterator last)
1001 if (first == begin() && last == end())
1005 while (first != last)
1009 key_compare key_comp()
const
1011 return key_compare_;
1013 value_compare value_comp()
const
1015 return value_compare(key_compare_);
1018 void swap(btree & obj)
1020 std::swap(key_compare_, obj.key_compare_);
1022 std::swap(node_cache_, obj.node_cache_);
1023 std::swap(leaf_cache_, obj.leaf_cache_);
1026 std::swap(iterator_map_, obj.iterator_map_);
1028 std::swap(end_iterator, obj.end_iterator);
1029 std::swap(size_, obj.size_);
1030 std::swap(height_, obj.height_);
1031 std::swap(alloc_strategy_, obj.alloc_strategy_);
1032 std::swap(root_node_, obj.root_node_);
1035 void enable_prefetching()
1037 prefetching_enabled_ =
true;
1039 void disable_prefetching()
1041 prefetching_enabled_ =
false;
1043 bool prefetching_enabled()
1045 return prefetching_enabled_;
1048 void print_statistics(std::ostream & o)
const
1050 o <<
"Node cache statistics:" << std::endl;
1051 node_cache_.print_statistics(o);
1052 o <<
"Leaf cache statistics:" << std::endl;
1053 leaf_cache_.print_statistics(o);
1055 void reset_statistics()
1057 node_cache_.reset_statistics();
1058 leaf_cache_.reset_statistics();
1062 template <
class KeyType,
1065 unsigned LogNodeSize,
1066 unsigned LogLeafSize,
1067 class PDAllocStrategy
1069 inline bool operator == (
const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & a,
1070 const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & b)
1072 return a.size() == b.size() && std::equal(a.begin(), a.end(), b.begin());
1075 template <
class KeyType,
1078 unsigned LogNodeSize,
1079 unsigned LogLeafSize,
1080 class PDAllocStrategy
1082 inline bool operator != (
const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & a,
1083 const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & b)
1089 template <
class KeyType,
1092 unsigned LogNodeSize,
1093 unsigned LogLeafSize,
1094 class PDAllocStrategy
1096 inline bool operator < (const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & a,
1097 const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & b)
1099 return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
1103 template <
class KeyType,
1106 unsigned LogNodeSize,
1107 unsigned LogLeafSize,
1108 class PDAllocStrategy
1110 inline bool operator > (
const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & a,
1111 const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & b)
1117 template <
class KeyType,
1120 unsigned LogNodeSize,
1121 unsigned LogLeafSize,
1122 class PDAllocStrategy
1124 inline bool operator <= (const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & a,
1125 const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & b)
1130 template <
class KeyType,
1133 unsigned LogNodeSize,
1134 unsigned LogLeafSize,
1135 class PDAllocStrategy
1137 inline bool operator >= (
const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & a,
1138 const btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & b)
1144 __STXXL_END_NAMESPACE
1149 template <
class KeyType,
1152 unsigned LogNodeSize,
1153 unsigned LogLeafSize,
1154 class PDAllocStrategy
1156 void swap(stxxl::btree::btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & a,
1157 stxxl::btree::btree<KeyType, DataType, CompareType, LogNodeSize, LogLeafSize, PDAllocStrategy> & b)
size of block in bytes
Definition: typed_block.h:239
Block size.
Definition: bid.h:44
number of elements in block
Definition: typed_block.h:240
_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