erlang各种有用的函数包括一些有用nif封装,还有一些性能测试case。
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

2394 行
80 KiB

  1. // Copyright 2013 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // A btree implementation of the STL set and map interfaces. A btree is both
  16. // smaller and faster than STL set/map. The red-black tree implementation of
  17. // STL set/map has an overhead of 3 pointers (left, right and parent) plus the
  18. // node color information for each stored value. So a set<int32> consumes 20
  19. // bytes for each value stored. This btree implementation stores multiple
  20. // values on fixed size nodes (usually 256 bytes) and doesn't store child
  21. // pointers for leaf nodes. The result is that a btree_set<int32> may use much
  22. // less memory per stored value. For the random insertion benchmark in
  23. // btree_test.cc, a btree_set<int32> with node-size of 256 uses 4.9 bytes per
  24. // stored value.
  25. //
  26. // The packing of multiple values on to each node of a btree has another effect
  27. // besides better space utilization: better cache locality due to fewer cache
  28. // lines being accessed. Better cache locality translates into faster
  29. // operations.
  30. //
  31. // CAVEATS
  32. //
  33. // Insertions and deletions on a btree can cause splitting, merging or
  34. // rebalancing of btree nodes. And even without these operations, insertions
  35. // and deletions on a btree will move values around within a node. In both
  36. // cases, the result is that insertions and deletions can invalidate iterators
  37. // pointing to values other than the one being inserted/deleted. This is
  38. // notably different from STL set/map which takes care to not invalidate
  39. // iterators on insert/erase except, of course, for iterators pointing to the
  40. // value being erased. A partial workaround when erasing is available:
  41. // erase() returns an iterator pointing to the item just after the one that was
  42. // erased (or end() if none exists). See also safe_btree.
  43. // PERFORMANCE
  44. //
  45. // btree_bench --benchmarks=. 2>&1 | ./benchmarks.awk
  46. //
  47. // Run on pmattis-warp.nyc (4 X 2200 MHz CPUs); 2010/03/04-15:23:06
  48. // Benchmark STL(ns) B-Tree(ns) @ <size>
  49. // --------------------------------------------------------
  50. // BM_set_int32_insert 1516 608 +59.89% <256> [40.0, 5.2]
  51. // BM_set_int32_lookup 1160 414 +64.31% <256> [40.0, 5.2]
  52. // BM_set_int32_fulllookup 960 410 +57.29% <256> [40.0, 4.4]
  53. // BM_set_int32_delete 1741 528 +69.67% <256> [40.0, 5.2]
  54. // BM_set_int32_queueaddrem 3078 1046 +66.02% <256> [40.0, 5.5]
  55. // BM_set_int32_mixedaddrem 3600 1384 +61.56% <256> [40.0, 5.3]
  56. // BM_set_int32_fifo 227 113 +50.22% <256> [40.0, 4.4]
  57. // BM_set_int32_fwditer 158 26 +83.54% <256> [40.0, 5.2]
  58. // BM_map_int32_insert 1551 636 +58.99% <256> [48.0, 10.5]
  59. // BM_map_int32_lookup 1200 508 +57.67% <256> [48.0, 10.5]
  60. // BM_map_int32_fulllookup 989 487 +50.76% <256> [48.0, 8.8]
  61. // BM_map_int32_delete 1794 628 +64.99% <256> [48.0, 10.5]
  62. // BM_map_int32_queueaddrem 3189 1266 +60.30% <256> [48.0, 11.6]
  63. // BM_map_int32_mixedaddrem 3822 1623 +57.54% <256> [48.0, 10.9]
  64. // BM_map_int32_fifo 151 134 +11.26% <256> [48.0, 8.8]
  65. // BM_map_int32_fwditer 161 32 +80.12% <256> [48.0, 10.5]
  66. // BM_set_int64_insert 1546 636 +58.86% <256> [40.0, 10.5]
  67. // BM_set_int64_lookup 1200 512 +57.33% <256> [40.0, 10.5]
  68. // BM_set_int64_fulllookup 971 487 +49.85% <256> [40.0, 8.8]
  69. // BM_set_int64_delete 1745 616 +64.70% <256> [40.0, 10.5]
  70. // BM_set_int64_queueaddrem 3163 1195 +62.22% <256> [40.0, 11.6]
  71. // BM_set_int64_mixedaddrem 3760 1564 +58.40% <256> [40.0, 10.9]
  72. // BM_set_int64_fifo 146 103 +29.45% <256> [40.0, 8.8]
  73. // BM_set_int64_fwditer 162 31 +80.86% <256> [40.0, 10.5]
  74. // BM_map_int64_insert 1551 720 +53.58% <256> [48.0, 20.7]
  75. // BM_map_int64_lookup 1214 612 +49.59% <256> [48.0, 20.7]
  76. // BM_map_int64_fulllookup 994 592 +40.44% <256> [48.0, 17.2]
  77. // BM_map_int64_delete 1778 764 +57.03% <256> [48.0, 20.7]
  78. // BM_map_int64_queueaddrem 3189 1547 +51.49% <256> [48.0, 20.9]
  79. // BM_map_int64_mixedaddrem 3779 1887 +50.07% <256> [48.0, 21.6]
  80. // BM_map_int64_fifo 147 145 +1.36% <256> [48.0, 17.2]
  81. // BM_map_int64_fwditer 162 41 +74.69% <256> [48.0, 20.7]
  82. // BM_set_string_insert 1989 1966 +1.16% <256> [64.0, 44.5]
  83. // BM_set_string_lookup 1709 1600 +6.38% <256> [64.0, 44.5]
  84. // BM_set_string_fulllookup 1573 1529 +2.80% <256> [64.0, 35.4]
  85. // BM_set_string_delete 2520 1920 +23.81% <256> [64.0, 44.5]
  86. // BM_set_string_queueaddrem 4706 4309 +8.44% <256> [64.0, 48.3]
  87. // BM_set_string_mixedaddrem 5080 4654 +8.39% <256> [64.0, 46.7]
  88. // BM_set_string_fifo 318 512 -61.01% <256> [64.0, 35.4]
  89. // BM_set_string_fwditer 182 93 +48.90% <256> [64.0, 44.5]
  90. // BM_map_string_insert 2600 2227 +14.35% <256> [72.0, 55.8]
  91. // BM_map_string_lookup 2068 1730 +16.34% <256> [72.0, 55.8]
  92. // BM_map_string_fulllookup 1859 1618 +12.96% <256> [72.0, 44.0]
  93. // BM_map_string_delete 3168 2080 +34.34% <256> [72.0, 55.8]
  94. // BM_map_string_queueaddrem 5840 4701 +19.50% <256> [72.0, 59.4]
  95. // BM_map_string_mixedaddrem 6400 5200 +18.75% <256> [72.0, 57.8]
  96. // BM_map_string_fifo 398 596 -49.75% <256> [72.0, 44.0]
  97. // BM_map_string_fwditer 243 113 +53.50% <256> [72.0, 55.8]
  98. #ifndef UTIL_BTREE_BTREE_H__
  99. #define UTIL_BTREE_BTREE_H__
  100. #include <assert.h>
  101. #include <stddef.h>
  102. #include <string.h>
  103. #include <sys/types.h>
  104. #include <algorithm>
  105. #include <functional>
  106. #include <iostream>
  107. #include <iterator>
  108. #include <limits>
  109. #include <type_traits>
  110. #include <new>
  111. #include <ostream>
  112. #include <string>
  113. #include <utility>
  114. #ifndef NDEBUG
  115. #define NDEBUG 1
  116. #endif
  117. namespace btree {
  118. // Inside a btree method, if we just call swap(), it will choose the
  119. // btree::swap method, which we don't want. And we can't say ::swap
  120. // because then MSVC won't pickup any std::swap() implementations. We
  121. // can't just use std::swap() directly because then we don't get the
  122. // specialization for types outside the std namespace. So the solution
  123. // is to have a special swap helper function whose name doesn't
  124. // collide with other swap functions defined by the btree classes.
  125. template <typename T>
  126. inline void btree_swap_helper(T &a, T &b) {
  127. using std::swap;
  128. swap(a, b);
  129. }
  130. // A template helper used to select A or B based on a condition.
  131. template<bool cond, typename A, typename B>
  132. struct if_{
  133. typedef A type;
  134. };
  135. template<typename A, typename B>
  136. struct if_<false, A, B> {
  137. typedef B type;
  138. };
  139. // Types small_ and big_ are promise that sizeof(small_) < sizeof(big_)
  140. typedef char small_;
  141. struct big_ {
  142. char dummy[2];
  143. };
  144. // A compile-time assertion.
  145. template <bool>
  146. struct CompileAssert {
  147. };
  148. #define COMPILE_ASSERT(expr, msg) \
  149. typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : 0]
  150. // A helper type used to indicate that a key-compare-to functor has been
  151. // provided. A user can specify a key-compare-to functor by doing:
  152. //
  153. // struct MyStringComparer
  154. // : public util::btree::btree_key_compare_to_tag {
  155. // int operator()(const string &a, const string &b) const {
  156. // return a.compare(b);
  157. // }
  158. // };
  159. //
  160. // Note that the return type is an int and not a bool. There is a
  161. // COMPILE_ASSERT which enforces this return type.
  162. struct btree_key_compare_to_tag {
  163. };
  164. // A helper class that indicates if the Compare parameter is derived from
  165. // btree_key_compare_to_tag.
  166. template <typename Compare>
  167. struct btree_is_key_compare_to
  168. : public std::is_convertible<Compare, btree_key_compare_to_tag> {
  169. };
  170. // A helper class to convert a boolean comparison into a three-way
  171. // "compare-to" comparison that returns a negative value to indicate
  172. // less-than, zero to indicate equality and a positive value to
  173. // indicate greater-than. This helper class is specialized for
  174. // less<string> and greater<string>. The btree_key_compare_to_adapter
  175. // class is provided so that btree users automatically get the more
  176. // efficient compare-to code when using common google string types
  177. // with common comparison functors.
  178. template <typename Compare>
  179. struct btree_key_compare_to_adapter : Compare {
  180. btree_key_compare_to_adapter() { }
  181. btree_key_compare_to_adapter(const Compare &c) : Compare(c) { }
  182. btree_key_compare_to_adapter(const btree_key_compare_to_adapter<Compare> &c)
  183. : Compare(c) {
  184. }
  185. };
  186. template <>
  187. struct btree_key_compare_to_adapter<std::less<std::string> >
  188. : public btree_key_compare_to_tag {
  189. btree_key_compare_to_adapter() {}
  190. btree_key_compare_to_adapter(const std::less<std::string>&) {}
  191. btree_key_compare_to_adapter(
  192. const btree_key_compare_to_adapter<std::less<std::string> >&) {}
  193. int operator()(const std::string &a, const std::string &b) const {
  194. return a.compare(b);
  195. }
  196. };
  197. template <>
  198. struct btree_key_compare_to_adapter<std::greater<std::string> >
  199. : public btree_key_compare_to_tag {
  200. btree_key_compare_to_adapter() {}
  201. btree_key_compare_to_adapter(const std::greater<std::string>&) {}
  202. btree_key_compare_to_adapter(
  203. const btree_key_compare_to_adapter<std::greater<std::string> >&) {}
  204. int operator()(const std::string &a, const std::string &b) const {
  205. return b.compare(a);
  206. }
  207. };
  208. // A helper class that allows a compare-to functor to behave like a plain
  209. // compare functor. This specialization is used when we do not have a
  210. // compare-to functor.
  211. template <typename Key, typename Compare, bool HaveCompareTo>
  212. struct btree_key_comparer {
  213. btree_key_comparer() {}
  214. btree_key_comparer(Compare c) : comp(c) {}
  215. static bool bool_compare(const Compare &comp, const Key &x, const Key &y) {
  216. return comp(x, y);
  217. }
  218. bool operator()(const Key &x, const Key &y) const {
  219. return bool_compare(comp, x, y);
  220. }
  221. Compare comp;
  222. };
  223. // A specialization of btree_key_comparer when a compare-to functor is
  224. // present. We need a plain (boolean) comparison in some parts of the btree
  225. // code, such as insert-with-hint.
  226. template <typename Key, typename Compare>
  227. struct btree_key_comparer<Key, Compare, true> {
  228. btree_key_comparer() {}
  229. btree_key_comparer(Compare c) : comp(c) {}
  230. static bool bool_compare(const Compare &comp, const Key &x, const Key &y) {
  231. return comp(x, y) < 0;
  232. }
  233. bool operator()(const Key &x, const Key &y) const {
  234. return bool_compare(comp, x, y);
  235. }
  236. Compare comp;
  237. };
  238. // A helper function to compare to keys using the specified compare
  239. // functor. This dispatches to the appropriate btree_key_comparer comparison,
  240. // depending on whether we have a compare-to functor or not (which depends on
  241. // whether Compare is derived from btree_key_compare_to_tag).
  242. template <typename Key, typename Compare>
  243. static bool btree_compare_keys(
  244. const Compare &comp, const Key &x, const Key &y) {
  245. typedef btree_key_comparer<Key, Compare,
  246. btree_is_key_compare_to<Compare>::value> key_comparer;
  247. return key_comparer::bool_compare(comp, x, y);
  248. }
  249. template <typename Key, typename Compare,
  250. typename Alloc, int TargetNodeSize, int ValueSize>
  251. struct btree_common_params {
  252. // If Compare is derived from btree_key_compare_to_tag then use it as the
  253. // key_compare type. Otherwise, use btree_key_compare_to_adapter<> which will
  254. // fall-back to Compare if we don't have an appropriate specialization.
  255. typedef typename if_<
  256. btree_is_key_compare_to<Compare>::value,
  257. Compare, btree_key_compare_to_adapter<Compare> >::type key_compare;
  258. // A type which indicates if we have a key-compare-to functor or a plain old
  259. // key-compare functor.
  260. typedef btree_is_key_compare_to<key_compare> is_key_compare_to;
  261. typedef Alloc allocator_type;
  262. typedef Key key_type;
  263. typedef size_t size_type;
  264. typedef ptrdiff_t difference_type;
  265. enum {
  266. kTargetNodeSize = TargetNodeSize,
  267. // Available space for values. This is largest for leaf nodes,
  268. // which has overhead no fewer than two pointers.
  269. kNodeValueSpace = TargetNodeSize - 2 * sizeof(void*),
  270. };
  271. // This is an integral type large enough to hold as many
  272. // ValueSize-values as will fit a node of TargetNodeSize bytes.
  273. typedef typename if_<
  274. (kNodeValueSpace / ValueSize) >= 256,
  275. uint16_t,
  276. uint8_t>::type node_count_type;
  277. };
  278. // A parameters structure for holding the type parameters for a btree_map.
  279. template <typename Key, typename Data, typename Compare,
  280. typename Alloc, int TargetNodeSize>
  281. struct btree_map_params
  282. : public btree_common_params<Key, Compare, Alloc, TargetNodeSize,
  283. sizeof(Key) + sizeof(Data)> {
  284. typedef Data data_type;
  285. typedef Data mapped_type;
  286. typedef std::pair<const Key, data_type> value_type;
  287. typedef std::pair<Key, data_type> mutable_value_type;
  288. typedef value_type* pointer;
  289. typedef const value_type* const_pointer;
  290. typedef value_type& reference;
  291. typedef const value_type& const_reference;
  292. enum {
  293. kValueSize = sizeof(Key) + sizeof(data_type),
  294. };
  295. static const Key& key(const value_type &x) { return x.first; }
  296. static const Key& key(const mutable_value_type &x) { return x.first; }
  297. static void swap(mutable_value_type *a, mutable_value_type *b) {
  298. btree_swap_helper(a->first, b->first);
  299. btree_swap_helper(a->second, b->second);
  300. }
  301. };
  302. // A parameters structure for holding the type parameters for a btree_set.
  303. template <typename Key, typename Compare, typename Alloc, int TargetNodeSize>
  304. struct btree_set_params
  305. : public btree_common_params<Key, Compare, Alloc, TargetNodeSize,
  306. sizeof(Key)> {
  307. typedef std::false_type data_type;
  308. typedef std::false_type mapped_type;
  309. typedef Key value_type;
  310. typedef value_type mutable_value_type;
  311. typedef value_type* pointer;
  312. typedef const value_type* const_pointer;
  313. typedef value_type& reference;
  314. typedef const value_type& const_reference;
  315. enum {
  316. kValueSize = sizeof(Key),
  317. };
  318. static const Key& key(const value_type &x) { return x; }
  319. static void swap(mutable_value_type *a, mutable_value_type *b) {
  320. btree_swap_helper<mutable_value_type>(*a, *b);
  321. }
  322. };
  323. // An adapter class that converts a lower-bound compare into an upper-bound
  324. // compare.
  325. template <typename Key, typename Compare>
  326. struct btree_upper_bound_adapter : public Compare {
  327. btree_upper_bound_adapter(Compare c) : Compare(c) {}
  328. bool operator()(const Key &a, const Key &b) const {
  329. return !static_cast<const Compare&>(*this)(b, a);
  330. }
  331. };
  332. template <typename Key, typename CompareTo>
  333. struct btree_upper_bound_compare_to_adapter : public CompareTo {
  334. btree_upper_bound_compare_to_adapter(CompareTo c) : CompareTo(c) {}
  335. int operator()(const Key &a, const Key &b) const {
  336. return static_cast<const CompareTo&>(*this)(b, a);
  337. }
  338. };
  339. // Dispatch helper class for using linear search with plain compare.
  340. template <typename K, typename N, typename Compare>
  341. struct btree_linear_search_plain_compare {
  342. static int lower_bound(const K &k, const N &n, Compare comp) {
  343. return n.linear_search_plain_compare(k, 0, n.count(), comp);
  344. }
  345. static int upper_bound(const K &k, const N &n, Compare comp) {
  346. typedef btree_upper_bound_adapter<K, Compare> upper_compare;
  347. return n.linear_search_plain_compare(k, 0, n.count(), upper_compare(comp));
  348. }
  349. };
  350. // Dispatch helper class for using linear search with compare-to
  351. template <typename K, typename N, typename CompareTo>
  352. struct btree_linear_search_compare_to {
  353. static int lower_bound(const K &k, const N &n, CompareTo comp) {
  354. return n.linear_search_compare_to(k, 0, n.count(), comp);
  355. }
  356. static int upper_bound(const K &k, const N &n, CompareTo comp) {
  357. typedef btree_upper_bound_adapter<K,
  358. btree_key_comparer<K, CompareTo, true> > upper_compare;
  359. return n.linear_search_plain_compare(k, 0, n.count(), upper_compare(comp));
  360. }
  361. };
  362. // Dispatch helper class for using binary search with plain compare.
  363. template <typename K, typename N, typename Compare>
  364. struct btree_binary_search_plain_compare {
  365. static int lower_bound(const K &k, const N &n, Compare comp) {
  366. return n.binary_search_plain_compare(k, 0, n.count(), comp);
  367. }
  368. static int upper_bound(const K &k, const N &n, Compare comp) {
  369. typedef btree_upper_bound_adapter<K, Compare> upper_compare;
  370. return n.binary_search_plain_compare(k, 0, n.count(), upper_compare(comp));
  371. }
  372. };
  373. // Dispatch helper class for using binary search with compare-to.
  374. template <typename K, typename N, typename CompareTo>
  375. struct btree_binary_search_compare_to {
  376. static int lower_bound(const K &k, const N &n, CompareTo comp) {
  377. return n.binary_search_compare_to(k, 0, n.count(), CompareTo());
  378. }
  379. static int upper_bound(const K &k, const N &n, CompareTo comp) {
  380. typedef btree_upper_bound_adapter<K,
  381. btree_key_comparer<K, CompareTo, true> > upper_compare;
  382. return n.linear_search_plain_compare(k, 0, n.count(), upper_compare(comp));
  383. }
  384. };
  385. // A node in the btree holding. The same node type is used for both internal
  386. // and leaf nodes in the btree, though the nodes are allocated in such a way
  387. // that the children array is only valid in internal nodes.
  388. template <typename Params>
  389. class btree_node {
  390. public:
  391. typedef Params params_type;
  392. typedef btree_node<Params> self_type;
  393. typedef typename Params::key_type key_type;
  394. typedef typename Params::data_type data_type;
  395. typedef typename Params::value_type value_type;
  396. typedef typename Params::mutable_value_type mutable_value_type;
  397. typedef typename Params::pointer pointer;
  398. typedef typename Params::const_pointer const_pointer;
  399. typedef typename Params::reference reference;
  400. typedef typename Params::const_reference const_reference;
  401. typedef typename Params::key_compare key_compare;
  402. typedef typename Params::size_type size_type;
  403. typedef typename Params::difference_type difference_type;
  404. // Typedefs for the various types of node searches.
  405. typedef btree_linear_search_plain_compare<
  406. key_type, self_type, key_compare> linear_search_plain_compare_type;
  407. typedef btree_linear_search_compare_to<
  408. key_type, self_type, key_compare> linear_search_compare_to_type;
  409. typedef btree_binary_search_plain_compare<
  410. key_type, self_type, key_compare> binary_search_plain_compare_type;
  411. typedef btree_binary_search_compare_to<
  412. key_type, self_type, key_compare> binary_search_compare_to_type;
  413. // If we have a valid key-compare-to type, use linear_search_compare_to,
  414. // otherwise use linear_search_plain_compare.
  415. typedef typename if_<
  416. Params::is_key_compare_to::value,
  417. linear_search_compare_to_type,
  418. linear_search_plain_compare_type>::type linear_search_type;
  419. // If we have a valid key-compare-to type, use binary_search_compare_to,
  420. // otherwise use binary_search_plain_compare.
  421. typedef typename if_<
  422. Params::is_key_compare_to::value,
  423. binary_search_compare_to_type,
  424. binary_search_plain_compare_type>::type binary_search_type;
  425. // If the key is an integral or floating point type, use linear search which
  426. // is faster than binary search for such types. Might be wise to also
  427. // configure linear search based on node-size.
  428. typedef typename if_<
  429. std::is_integral<key_type>::value ||
  430. std::is_floating_point<key_type>::value,
  431. linear_search_type, binary_search_type>::type search_type;
  432. struct base_fields {
  433. typedef typename Params::node_count_type field_type;
  434. // A boolean indicating whether the node is a leaf or not.
  435. bool leaf;
  436. // The position of the node in the node's parent.
  437. field_type position;
  438. // The maximum number of values the node can hold.
  439. field_type max_count;
  440. // The count of the number of values in the node.
  441. field_type count;
  442. // A pointer to the node's parent.
  443. btree_node *parent;
  444. };
  445. enum {
  446. kValueSize = params_type::kValueSize,
  447. kTargetNodeSize = params_type::kTargetNodeSize,
  448. // Compute how many values we can fit onto a leaf node.
  449. kNodeTargetValues = (kTargetNodeSize - sizeof(base_fields)) / kValueSize,
  450. // We need a minimum of 3 values per internal node in order to perform
  451. // splitting (1 value for the two nodes involved in the split and 1 value
  452. // propagated to the parent as the delimiter for the split).
  453. kNodeValues = kNodeTargetValues >= 3 ? kNodeTargetValues : 3,
  454. kExactMatch = 1 << 30,
  455. kMatchMask = kExactMatch - 1,
  456. };
  457. struct leaf_fields : public base_fields {
  458. // The array of values. Only the first count of these values have been
  459. // constructed and are valid.
  460. mutable_value_type values[kNodeValues];
  461. };
  462. struct internal_fields : public leaf_fields {
  463. // The array of child pointers. The keys in children_[i] are all less than
  464. // key(i). The keys in children_[i + 1] are all greater than key(i). There
  465. // are always count + 1 children.
  466. btree_node *children[kNodeValues + 1];
  467. };
  468. struct root_fields : public internal_fields {
  469. btree_node *rightmost;
  470. size_type size;
  471. };
  472. public:
  473. // Getter/setter for whether this is a leaf node or not. This value doesn't
  474. // change after the node is created.
  475. bool leaf() const { return fields_.leaf; }
  476. // Getter for the position of this node in its parent.
  477. int position() const { return fields_.position; }
  478. void set_position(int v) { fields_.position = v; }
  479. // Getter/setter for the number of values stored in this node.
  480. int count() const { return fields_.count; }
  481. void set_count(int v) { fields_.count = v; }
  482. int max_count() const { return fields_.max_count; }
  483. // Getter for the parent of this node.
  484. btree_node* parent() const { return fields_.parent; }
  485. // Getter for whether the node is the root of the tree. The parent of the
  486. // root of the tree is the leftmost node in the tree which is guaranteed to
  487. // be a leaf.
  488. bool is_root() const { return parent()->leaf(); }
  489. void make_root() {
  490. assert(parent()->is_root());
  491. fields_.parent = fields_.parent->parent();
  492. }
  493. // Getter for the rightmost root node field. Only valid on the root node.
  494. btree_node* rightmost() const { return fields_.rightmost; }
  495. btree_node** mutable_rightmost() { return &fields_.rightmost; }
  496. // Getter for the size root node field. Only valid on the root node.
  497. size_type size() const { return fields_.size; }
  498. size_type* mutable_size() { return &fields_.size; }
  499. // Getters for the key/value at position i in the node.
  500. const key_type& key(int i) const {
  501. return params_type::key(fields_.values[i]);
  502. }
  503. reference value(int i) {
  504. return reinterpret_cast<reference>(fields_.values[i]);
  505. }
  506. const_reference value(int i) const {
  507. return reinterpret_cast<const_reference>(fields_.values[i]);
  508. }
  509. mutable_value_type* mutable_value(int i) {
  510. return &fields_.values[i];
  511. }
  512. // Swap value i in this node with value j in node x.
  513. void value_swap(int i, btree_node *x, int j) {
  514. params_type::swap(mutable_value(i), x->mutable_value(j));
  515. }
  516. // Getters/setter for the child at position i in the node.
  517. btree_node* child(int i) const { return fields_.children[i]; }
  518. btree_node** mutable_child(int i) { return &fields_.children[i]; }
  519. void set_child(int i, btree_node *c) {
  520. *mutable_child(i) = c;
  521. c->fields_.parent = this;
  522. c->fields_.position = i;
  523. }
  524. // Returns the position of the first value whose key is not less than k.
  525. template <typename Compare>
  526. int lower_bound(const key_type &k, const Compare &comp) const {
  527. return search_type::lower_bound(k, *this, comp);
  528. }
  529. // Returns the position of the first value whose key is greater than k.
  530. template <typename Compare>
  531. int upper_bound(const key_type &k, const Compare &comp) const {
  532. return search_type::upper_bound(k, *this, comp);
  533. }
  534. // Returns the position of the first value whose key is not less than k using
  535. // linear search performed using plain compare.
  536. template <typename Compare>
  537. int linear_search_plain_compare(
  538. const key_type &k, int s, int e, const Compare &comp) const {
  539. while (s < e) {
  540. if (!btree_compare_keys(comp, key(s), k)) {
  541. break;
  542. }
  543. ++s;
  544. }
  545. return s;
  546. }
  547. // Returns the position of the first value whose key is not less than k using
  548. // linear search performed using compare-to.
  549. template <typename Compare>
  550. int linear_search_compare_to(
  551. const key_type &k, int s, int e, const Compare &comp) const {
  552. while (s < e) {
  553. int c = comp(key(s), k);
  554. if (c == 0) {
  555. return s | kExactMatch;
  556. } else if (c > 0) {
  557. break;
  558. }
  559. ++s;
  560. }
  561. return s;
  562. }
  563. // Returns the position of the first value whose key is not less than k using
  564. // binary search performed using plain compare.
  565. template <typename Compare>
  566. int binary_search_plain_compare(
  567. const key_type &k, int s, int e, const Compare &comp) const {
  568. while (s != e) {
  569. int mid = (s + e) / 2;
  570. if (btree_compare_keys(comp, key(mid), k)) {
  571. s = mid + 1;
  572. } else {
  573. e = mid;
  574. }
  575. }
  576. return s;
  577. }
  578. // Returns the position of the first value whose key is not less than k using
  579. // binary search performed using compare-to.
  580. template <typename CompareTo>
  581. int binary_search_compare_to(
  582. const key_type &k, int s, int e, const CompareTo &comp) const {
  583. while (s != e) {
  584. int mid = (s + e) / 2;
  585. int c = comp(key(mid), k);
  586. if (c < 0) {
  587. s = mid + 1;
  588. } else if (c > 0) {
  589. e = mid;
  590. } else {
  591. // Need to return the first value whose key is not less than k, which
  592. // requires continuing the binary search. Note that we are guaranteed
  593. // that the result is an exact match because if "key(mid-1) < k" the
  594. // call to binary_search_compare_to() will return "mid".
  595. s = binary_search_compare_to(k, s, mid, comp);
  596. return s | kExactMatch;
  597. }
  598. }
  599. return s;
  600. }
  601. // Inserts the value x at position i, shifting all existing values and
  602. // children at positions >= i to the right by 1.
  603. void insert_value(int i, const value_type &x);
  604. // Removes the value at position i, shifting all existing values and children
  605. // at positions > i to the left by 1.
  606. void remove_value(int i);
  607. // Rebalances a node with its right sibling.
  608. void rebalance_right_to_left(btree_node *sibling, int to_move);
  609. void rebalance_left_to_right(btree_node *sibling, int to_move);
  610. // Splits a node, moving a portion of the node's values to its right sibling.
  611. void split(btree_node *sibling, int insert_position);
  612. // Merges a node with its right sibling, moving all of the values and the
  613. // delimiting key in the parent node onto itself.
  614. void merge(btree_node *sibling);
  615. // Swap the contents of "this" and "src".
  616. void swap(btree_node *src);
  617. // Node allocation/deletion routines.
  618. static btree_node* init_leaf(
  619. leaf_fields *f, btree_node *parent, int max_count) {
  620. btree_node *n = reinterpret_cast<btree_node*>(f);
  621. f->leaf = 1;
  622. f->position = 0;
  623. f->max_count = max_count;
  624. f->count = 0;
  625. f->parent = parent;
  626. if (!NDEBUG) {
  627. memset(&f->values, 0, max_count * sizeof(value_type));
  628. }
  629. return n;
  630. }
  631. static btree_node* init_internal(internal_fields *f, btree_node *parent) {
  632. btree_node *n = init_leaf(f, parent, kNodeValues);
  633. f->leaf = 0;
  634. if (!NDEBUG) {
  635. memset(f->children, 0, sizeof(f->children));
  636. }
  637. return n;
  638. }
  639. static btree_node* init_root(root_fields *f, btree_node *parent) {
  640. btree_node *n = init_internal(f, parent);
  641. f->rightmost = parent;
  642. f->size = parent->count();
  643. return n;
  644. }
  645. void destroy() {
  646. for (int i = 0; i < count(); ++i) {
  647. value_destroy(i);
  648. }
  649. }
  650. private:
  651. void value_init(int i) {
  652. new (&fields_.values[i]) mutable_value_type;
  653. }
  654. void value_init(int i, const value_type &x) {
  655. new (&fields_.values[i]) mutable_value_type(x);
  656. }
  657. void value_destroy(int i) {
  658. fields_.values[i].~mutable_value_type();
  659. }
  660. private:
  661. root_fields fields_;
  662. private:
  663. btree_node(const btree_node&);
  664. void operator=(const btree_node&);
  665. };
  666. template <typename Node, typename Reference, typename Pointer>
  667. struct btree_iterator {
  668. typedef typename Node::key_type key_type;
  669. typedef typename Node::size_type size_type;
  670. typedef typename Node::difference_type difference_type;
  671. typedef typename Node::params_type params_type;
  672. typedef Node node_type;
  673. typedef typename std::remove_const<Node>::type normal_node;
  674. typedef const Node const_node;
  675. typedef typename params_type::value_type value_type;
  676. typedef typename params_type::pointer normal_pointer;
  677. typedef typename params_type::reference normal_reference;
  678. typedef typename params_type::const_pointer const_pointer;
  679. typedef typename params_type::const_reference const_reference;
  680. typedef Pointer pointer;
  681. typedef Reference reference;
  682. typedef std::bidirectional_iterator_tag iterator_category;
  683. typedef btree_iterator<
  684. normal_node, normal_reference, normal_pointer> iterator;
  685. typedef btree_iterator<
  686. const_node, const_reference, const_pointer> const_iterator;
  687. typedef btree_iterator<Node, Reference, Pointer> self_type;
  688. btree_iterator()
  689. : node(NULL),
  690. position(-1) {
  691. }
  692. btree_iterator(Node *n, int p)
  693. : node(n),
  694. position(p) {
  695. }
  696. btree_iterator(const iterator &x)
  697. : node(x.node),
  698. position(x.position) {
  699. }
  700. // Increment/decrement the iterator.
  701. void increment() {
  702. if (node->leaf() && ++position < node->count()) {
  703. return;
  704. }
  705. increment_slow();
  706. }
  707. void increment_by(int count);
  708. void increment_slow();
  709. void decrement() {
  710. if (node->leaf() && --position >= 0) {
  711. return;
  712. }
  713. decrement_slow();
  714. }
  715. void decrement_slow();
  716. bool operator==(const const_iterator &x) const {
  717. return node == x.node && position == x.position;
  718. }
  719. bool operator!=(const const_iterator &x) const {
  720. return node != x.node || position != x.position;
  721. }
  722. // Accessors for the key/value the iterator is pointing at.
  723. const key_type& key() const {
  724. return node->key(position);
  725. }
  726. reference operator*() const {
  727. return node->value(position);
  728. }
  729. pointer operator->() const {
  730. return &node->value(position);
  731. }
  732. self_type& operator++() {
  733. increment();
  734. return *this;
  735. }
  736. self_type& operator--() {
  737. decrement();
  738. return *this;
  739. }
  740. self_type operator++(int) {
  741. self_type tmp = *this;
  742. ++*this;
  743. return tmp;
  744. }
  745. self_type operator--(int) {
  746. self_type tmp = *this;
  747. --*this;
  748. return tmp;
  749. }
  750. // The node in the tree the iterator is pointing at.
  751. Node *node;
  752. // The position within the node of the tree the iterator is pointing at.
  753. int position;
  754. };
  755. // Dispatch helper class for using btree::internal_locate with plain compare.
  756. struct btree_internal_locate_plain_compare {
  757. template <typename K, typename T, typename Iter>
  758. static std::pair<Iter, int> dispatch(const K &k, const T &t, Iter iter) {
  759. return t.internal_locate_plain_compare(k, iter);
  760. }
  761. };
  762. // Dispatch helper class for using btree::internal_locate with compare-to.
  763. struct btree_internal_locate_compare_to {
  764. template <typename K, typename T, typename Iter>
  765. static std::pair<Iter, int> dispatch(const K &k, const T &t, Iter iter) {
  766. return t.internal_locate_compare_to(k, iter);
  767. }
  768. };
  769. template <typename Params>
  770. class btree : public Params::key_compare {
  771. typedef btree<Params> self_type;
  772. typedef btree_node<Params> node_type;
  773. typedef typename node_type::base_fields base_fields;
  774. typedef typename node_type::leaf_fields leaf_fields;
  775. typedef typename node_type::internal_fields internal_fields;
  776. typedef typename node_type::root_fields root_fields;
  777. typedef typename Params::is_key_compare_to is_key_compare_to;
  778. friend struct btree_internal_locate_plain_compare;
  779. friend struct btree_internal_locate_compare_to;
  780. typedef typename if_<
  781. is_key_compare_to::value,
  782. btree_internal_locate_compare_to,
  783. btree_internal_locate_plain_compare>::type internal_locate_type;
  784. enum {
  785. kNodeValues = node_type::kNodeValues,
  786. kMinNodeValues = kNodeValues / 2,
  787. kValueSize = node_type::kValueSize,
  788. kExactMatch = node_type::kExactMatch,
  789. kMatchMask = node_type::kMatchMask,
  790. };
  791. // A helper class to get the empty base class optimization for 0-size
  792. // allocators. Base is internal_allocator_type.
  793. // (e.g. empty_base_handle<internal_allocator_type, node_type*>). If Base is
  794. // 0-size, the compiler doesn't have to reserve any space for it and
  795. // sizeof(empty_base_handle) will simply be sizeof(Data). Google [empty base
  796. // class optimization] for more details.
  797. template <typename Base, typename Data>
  798. struct empty_base_handle : public Base {
  799. empty_base_handle(const Base &b, const Data &d)
  800. : Base(b),
  801. data(d) {
  802. }
  803. Data data;
  804. };
  805. struct node_stats {
  806. node_stats(size_t l, size_t i)
  807. : leaf_nodes(l),
  808. internal_nodes(i) {
  809. }
  810. node_stats& operator+=(const node_stats &x) {
  811. leaf_nodes += x.leaf_nodes;
  812. internal_nodes += x.internal_nodes;
  813. return *this;
  814. }
  815. size_t leaf_nodes;
  816. size_t internal_nodes;
  817. };
  818. public:
  819. typedef Params params_type;
  820. typedef typename Params::key_type key_type;
  821. typedef typename Params::data_type data_type;
  822. typedef typename Params::mapped_type mapped_type;
  823. typedef typename Params::value_type value_type;
  824. typedef typename Params::key_compare key_compare;
  825. typedef typename Params::pointer pointer;
  826. typedef typename Params::const_pointer const_pointer;
  827. typedef typename Params::reference reference;
  828. typedef typename Params::const_reference const_reference;
  829. typedef typename Params::size_type size_type;
  830. typedef typename Params::difference_type difference_type;
  831. typedef btree_iterator<node_type, reference, pointer> iterator;
  832. typedef typename iterator::const_iterator const_iterator;
  833. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  834. typedef std::reverse_iterator<iterator> reverse_iterator;
  835. typedef typename Params::allocator_type allocator_type;
  836. typedef typename allocator_type::template rebind<char>::other
  837. internal_allocator_type;
  838. public:
  839. // Default constructor.
  840. btree(const key_compare &comp, const allocator_type &alloc);
  841. // Copy constructor.
  842. btree(const self_type &x);
  843. // Destructor.
  844. ~btree() {
  845. clear();
  846. }
  847. // Iterator routines.
  848. iterator begin() {
  849. return iterator(leftmost(), 0);
  850. }
  851. const_iterator begin() const {
  852. return const_iterator(leftmost(), 0);
  853. }
  854. iterator end() {
  855. return iterator(rightmost(), rightmost() ? rightmost()->count() : 0);
  856. }
  857. const_iterator end() const {
  858. return const_iterator(rightmost(), rightmost() ? rightmost()->count() : 0);
  859. }
  860. reverse_iterator rbegin() {
  861. return reverse_iterator(end());
  862. }
  863. const_reverse_iterator rbegin() const {
  864. return const_reverse_iterator(end());
  865. }
  866. reverse_iterator rend() {
  867. return reverse_iterator(begin());
  868. }
  869. const_reverse_iterator rend() const {
  870. return const_reverse_iterator(begin());
  871. }
  872. // Finds the first element whose key is not less than key.
  873. iterator lower_bound(const key_type &key) {
  874. return internal_end(
  875. internal_lower_bound(key, iterator(root(), 0)));
  876. }
  877. const_iterator lower_bound(const key_type &key) const {
  878. return internal_end(
  879. internal_lower_bound(key, const_iterator(root(), 0)));
  880. }
  881. // Finds the first element whose key is greater than key.
  882. iterator upper_bound(const key_type &key) {
  883. return internal_end(
  884. internal_upper_bound(key, iterator(root(), 0)));
  885. }
  886. const_iterator upper_bound(const key_type &key) const {
  887. return internal_end(
  888. internal_upper_bound(key, const_iterator(root(), 0)));
  889. }
  890. // Finds the range of values which compare equal to key. The first member of
  891. // the returned pair is equal to lower_bound(key). The second member pair of
  892. // the pair is equal to upper_bound(key).
  893. std::pair<iterator,iterator> equal_range(const key_type &key) {
  894. return std::make_pair(lower_bound(key), upper_bound(key));
  895. }
  896. std::pair<const_iterator,const_iterator> equal_range(const key_type &key) const {
  897. return std::make_pair(lower_bound(key), upper_bound(key));
  898. }
  899. // Inserts a value into the btree only if it does not already exist. The
  900. // boolean return value indicates whether insertion succeeded or failed. The
  901. // ValuePointer type is used to avoid instatiating the value unless the key
  902. // is being inserted. Value is not dereferenced if the key already exists in
  903. // the btree. See btree_map::operator[].
  904. template <typename ValuePointer>
  905. std::pair<iterator,bool> insert_unique(const key_type &key, ValuePointer value);
  906. // Inserts a value into the btree only if it does not already exist. The
  907. // boolean return value indicates whether insertion succeeded or failed.
  908. std::pair<iterator,bool> insert_unique(const value_type &v) {
  909. return insert_unique(params_type::key(v), &v);
  910. }
  911. // Insert with hint. Check to see if the value should be placed immediately
  912. // before position in the tree. If it does, then the insertion will take
  913. // amortized constant time. If not, the insertion will take amortized
  914. // logarithmic time as if a call to insert_unique(v) were made.
  915. iterator insert_unique(iterator position, const value_type &v);
  916. // Insert a range of values into the btree.
  917. template <typename InputIterator>
  918. void insert_unique(InputIterator b, InputIterator e);
  919. // Inserts a value into the btree. The ValuePointer type is used to avoid
  920. // instatiating the value unless the key is being inserted. Value is not
  921. // dereferenced if the key already exists in the btree. See
  922. // btree_map::operator[].
  923. template <typename ValuePointer>
  924. iterator insert_multi(const key_type &key, ValuePointer value);
  925. // Inserts a value into the btree.
  926. iterator insert_multi(const value_type &v) {
  927. return insert_multi(params_type::key(v), &v);
  928. }
  929. // Insert with hint. Check to see if the value should be placed immediately
  930. // before position in the tree. If it does, then the insertion will take
  931. // amortized constant time. If not, the insertion will take amortized
  932. // logarithmic time as if a call to insert_multi(v) were made.
  933. iterator insert_multi(iterator position, const value_type &v);
  934. // Insert a range of values into the btree.
  935. template <typename InputIterator>
  936. void insert_multi(InputIterator b, InputIterator e);
  937. void assign(const self_type &x);
  938. // Erase the specified iterator from the btree. The iterator must be valid
  939. // (i.e. not equal to end()). Return an iterator pointing to the node after
  940. // the one that was erased (or end() if none exists).
  941. iterator erase(iterator iter);
  942. // Erases range. Returns the number of keys erased.
  943. int erase(iterator begin, iterator end);
  944. // Erases the specified key from the btree. Returns 1 if an element was
  945. // erased and 0 otherwise.
  946. int erase_unique(const key_type &key);
  947. // Erases all of the entries matching the specified key from the
  948. // btree. Returns the number of elements erased.
  949. int erase_multi(const key_type &key);
  950. // Finds the iterator corresponding to a key or returns end() if the key is
  951. // not present.
  952. iterator find_unique(const key_type &key) {
  953. return internal_end(
  954. internal_find_unique(key, iterator(root(), 0)));
  955. }
  956. const_iterator find_unique(const key_type &key) const {
  957. return internal_end(
  958. internal_find_unique(key, const_iterator(root(), 0)));
  959. }
  960. iterator find_multi(const key_type &key) {
  961. return internal_end(
  962. internal_find_multi(key, iterator(root(), 0)));
  963. }
  964. const_iterator find_multi(const key_type &key) const {
  965. return internal_end(
  966. internal_find_multi(key, const_iterator(root(), 0)));
  967. }
  968. // Returns a count of the number of times the key appears in the btree.
  969. size_type count_unique(const key_type &key) const {
  970. const_iterator begin = internal_find_unique(
  971. key, const_iterator(root(), 0));
  972. if (!begin.node) {
  973. // The key doesn't exist in the tree.
  974. return 0;
  975. }
  976. return 1;
  977. }
  978. // Returns a count of the number of times the key appears in the btree.
  979. size_type count_multi(const key_type &key) const {
  980. return distance(lower_bound(key), upper_bound(key));
  981. }
  982. // Clear the btree, deleting all of the values it contains.
  983. void clear();
  984. // Swap the contents of *this and x.
  985. void swap(self_type &x);
  986. // Assign the contents of x to *this.
  987. self_type& operator=(const self_type &x) {
  988. if (&x == this) {
  989. // Don't copy onto ourselves.
  990. return *this;
  991. }
  992. assign(x);
  993. return *this;
  994. }
  995. key_compare* mutable_key_comp() {
  996. return this;
  997. }
  998. const key_compare& key_comp() const {
  999. return *this;
  1000. }
  1001. bool compare_keys(const key_type &x, const key_type &y) const {
  1002. return btree_compare_keys(key_comp(), x, y);
  1003. }
  1004. // Dump the btree to the specified ostream. Requires that operator<< is
  1005. // defined for Key and Value.
  1006. void dump(std::ostream &os) const {
  1007. if (root() != NULL) {
  1008. internal_dump(os, root(), 0);
  1009. }
  1010. }
  1011. // Verifies the structure of the btree.
  1012. void verify() const;
  1013. // Size routines. Note that empty() is slightly faster than doing size()==0.
  1014. size_type size() const {
  1015. if (empty()) return 0;
  1016. if (root()->leaf()) return root()->count();
  1017. return root()->size();
  1018. }
  1019. size_type max_size() const { return std::numeric_limits<size_type>::max(); }
  1020. bool empty() const { return root() == NULL; }
  1021. // The height of the btree. An empty tree will have height 0.
  1022. size_type height() const {
  1023. size_type h = 0;
  1024. if (root()) {
  1025. // Count the length of the chain from the leftmost node up to the
  1026. // root. We actually count from the root back around to the level below
  1027. // the root, but the calculation is the same because of the circularity
  1028. // of that traversal.
  1029. const node_type *n = root();
  1030. do {
  1031. ++h;
  1032. n = n->parent();
  1033. } while (n != root());
  1034. }
  1035. return h;
  1036. }
  1037. // The number of internal, leaf and total nodes used by the btree.
  1038. size_type leaf_nodes() const {
  1039. return internal_stats(root()).leaf_nodes;
  1040. }
  1041. size_type internal_nodes() const {
  1042. return internal_stats(root()).internal_nodes;
  1043. }
  1044. size_type nodes() const {
  1045. node_stats stats = internal_stats(root());
  1046. return stats.leaf_nodes + stats.internal_nodes;
  1047. }
  1048. // The total number of bytes used by the btree.
  1049. size_type bytes_used() const {
  1050. node_stats stats = internal_stats(root());
  1051. if (stats.leaf_nodes == 1 && stats.internal_nodes == 0) {
  1052. return sizeof(*this) +
  1053. sizeof(base_fields) + root()->max_count() * sizeof(value_type);
  1054. } else {
  1055. return sizeof(*this) +
  1056. sizeof(root_fields) - sizeof(internal_fields) +
  1057. stats.leaf_nodes * sizeof(leaf_fields) +
  1058. stats.internal_nodes * sizeof(internal_fields);
  1059. }
  1060. }
  1061. // The average number of bytes used per value stored in the btree.
  1062. static double average_bytes_per_value() {
  1063. // Returns the number of bytes per value on a leaf node that is 75%
  1064. // full. Experimentally, this matches up nicely with the computed number of
  1065. // bytes per value in trees that had their values inserted in random order.
  1066. return sizeof(leaf_fields) / (kNodeValues * 0.75);
  1067. }
  1068. // The fullness of the btree. Computed as the number of elements in the btree
  1069. // divided by the maximum number of elements a tree with the current number
  1070. // of nodes could hold. A value of 1 indicates perfect space
  1071. // utilization. Smaller values indicate space wastage.
  1072. double fullness() const {
  1073. return double(size()) / (nodes() * kNodeValues);
  1074. }
  1075. // The overhead of the btree structure in bytes per node. Computed as the
  1076. // total number of bytes used by the btree minus the number of bytes used for
  1077. // storing elements divided by the number of elements.
  1078. double overhead() const {
  1079. if (empty()) {
  1080. return 0.0;
  1081. }
  1082. return (bytes_used() - size() * kValueSize) / double(size());
  1083. }
  1084. private:
  1085. // Internal accessor routines.
  1086. node_type* root() { return root_.data; }
  1087. const node_type* root() const { return root_.data; }
  1088. node_type** mutable_root() { return &root_.data; }
  1089. // The rightmost node is stored in the root node.
  1090. node_type* rightmost() {
  1091. return (!root() || root()->leaf()) ? root() : root()->rightmost();
  1092. }
  1093. const node_type* rightmost() const {
  1094. return (!root() || root()->leaf()) ? root() : root()->rightmost();
  1095. }
  1096. node_type** mutable_rightmost() { return root()->mutable_rightmost(); }
  1097. // The leftmost node is stored as the parent of the root node.
  1098. node_type* leftmost() { return root() ? root()->parent() : NULL; }
  1099. const node_type* leftmost() const { return root() ? root()->parent() : NULL; }
  1100. // The size of the tree is stored in the root node.
  1101. size_type* mutable_size() { return root()->mutable_size(); }
  1102. // Allocator routines.
  1103. internal_allocator_type* mutable_internal_allocator() {
  1104. return static_cast<internal_allocator_type*>(&root_);
  1105. }
  1106. const internal_allocator_type& internal_allocator() const {
  1107. return *static_cast<const internal_allocator_type*>(&root_);
  1108. }
  1109. // Node creation/deletion routines.
  1110. node_type* new_internal_node(node_type *parent) {
  1111. internal_fields *p = reinterpret_cast<internal_fields*>(
  1112. mutable_internal_allocator()->allocate(sizeof(internal_fields)));
  1113. return node_type::init_internal(p, parent);
  1114. }
  1115. node_type* new_internal_root_node() {
  1116. root_fields *p = reinterpret_cast<root_fields*>(
  1117. mutable_internal_allocator()->allocate(sizeof(root_fields)));
  1118. return node_type::init_root(p, root()->parent());
  1119. }
  1120. node_type* new_leaf_node(node_type *parent) {
  1121. leaf_fields *p = reinterpret_cast<leaf_fields*>(
  1122. mutable_internal_allocator()->allocate(sizeof(leaf_fields)));
  1123. return node_type::init_leaf(p, parent, kNodeValues);
  1124. }
  1125. node_type* new_leaf_root_node(int max_count) {
  1126. leaf_fields *p = reinterpret_cast<leaf_fields*>(
  1127. mutable_internal_allocator()->allocate(
  1128. sizeof(base_fields) + max_count * sizeof(value_type)));
  1129. return node_type::init_leaf(p, reinterpret_cast<node_type*>(p), max_count);
  1130. }
  1131. void delete_internal_node(node_type *node) {
  1132. node->destroy();
  1133. assert(node != root());
  1134. mutable_internal_allocator()->deallocate(
  1135. reinterpret_cast<char*>(node), sizeof(internal_fields));
  1136. }
  1137. void delete_internal_root_node() {
  1138. root()->destroy();
  1139. mutable_internal_allocator()->deallocate(
  1140. reinterpret_cast<char*>(root()), sizeof(root_fields));
  1141. }
  1142. void delete_leaf_node(node_type *node) {
  1143. node->destroy();
  1144. mutable_internal_allocator()->deallocate(
  1145. reinterpret_cast<char*>(node),
  1146. sizeof(base_fields) + node->max_count() * sizeof(value_type));
  1147. }
  1148. // Rebalances or splits the node iter points to.
  1149. void rebalance_or_split(iterator *iter);
  1150. // Merges the values of left, right and the delimiting key on their parent
  1151. // onto left, removing the delimiting key and deleting right.
  1152. void merge_nodes(node_type *left, node_type *right);
  1153. // Tries to merge node with its left or right sibling, and failing that,
  1154. // rebalance with its left or right sibling. Returns true if a merge
  1155. // occurred, at which point it is no longer valid to access node. Returns
  1156. // false if no merging took place.
  1157. bool try_merge_or_rebalance(iterator *iter);
  1158. // Tries to shrink the height of the tree by 1.
  1159. void try_shrink();
  1160. iterator internal_end(iterator iter) {
  1161. return iter.node ? iter : end();
  1162. }
  1163. const_iterator internal_end(const_iterator iter) const {
  1164. return iter.node ? iter : end();
  1165. }
  1166. // Inserts a value into the btree immediately before iter. Requires that
  1167. // key(v) <= iter.key() and (--iter).key() <= key(v).
  1168. iterator internal_insert(iterator iter, const value_type &v);
  1169. // Returns an iterator pointing to the first value >= the value "iter" is
  1170. // pointing at. Note that "iter" might be pointing to an invalid location as
  1171. // iter.position == iter.node->count(). This routine simply moves iter up in
  1172. // the tree to a valid location.
  1173. template <typename IterType>
  1174. static IterType internal_last(IterType iter);
  1175. // Returns an iterator pointing to the leaf position at which key would
  1176. // reside in the tree. We provide 2 versions of internal_locate. The first
  1177. // version (internal_locate_plain_compare) always returns 0 for the second
  1178. // field of the pair. The second version (internal_locate_compare_to) is for
  1179. // the key-compare-to specialization and returns either kExactMatch (if the
  1180. // key was found in the tree) or -kExactMatch (if it wasn't) in the second
  1181. // field of the pair. The compare_to specialization allows the caller to
  1182. // avoid a subsequent comparison to determine if an exact match was made,
  1183. // speeding up string keys.
  1184. template <typename IterType>
  1185. std::pair<IterType, int> internal_locate(
  1186. const key_type &key, IterType iter) const;
  1187. template <typename IterType>
  1188. std::pair<IterType, int> internal_locate_plain_compare(
  1189. const key_type &key, IterType iter) const;
  1190. template <typename IterType>
  1191. std::pair<IterType, int> internal_locate_compare_to(
  1192. const key_type &key, IterType iter) const;
  1193. // Internal routine which implements lower_bound().
  1194. template <typename IterType>
  1195. IterType internal_lower_bound(
  1196. const key_type &key, IterType iter) const;
  1197. // Internal routine which implements upper_bound().
  1198. template <typename IterType>
  1199. IterType internal_upper_bound(
  1200. const key_type &key, IterType iter) const;
  1201. // Internal routine which implements find_unique().
  1202. template <typename IterType>
  1203. IterType internal_find_unique(
  1204. const key_type &key, IterType iter) const;
  1205. // Internal routine which implements find_multi().
  1206. template <typename IterType>
  1207. IterType internal_find_multi(
  1208. const key_type &key, IterType iter) const;
  1209. // Deletes a node and all of its children.
  1210. void internal_clear(node_type *node);
  1211. // Dumps a node and all of its children to the specified ostream.
  1212. void internal_dump(std::ostream &os, const node_type *node, int level) const;
  1213. // Verifies the tree structure of node.
  1214. int internal_verify(const node_type *node,
  1215. const key_type *lo, const key_type *hi) const;
  1216. node_stats internal_stats(const node_type *node) const {
  1217. if (!node) {
  1218. return node_stats(0, 0);
  1219. }
  1220. if (node->leaf()) {
  1221. return node_stats(1, 0);
  1222. }
  1223. node_stats res(0, 1);
  1224. for (int i = 0; i <= node->count(); ++i) {
  1225. res += internal_stats(node->child(i));
  1226. }
  1227. return res;
  1228. }
  1229. private:
  1230. empty_base_handle<internal_allocator_type, node_type*> root_;
  1231. private:
  1232. // A never instantiated helper function that returns big_ if we have a
  1233. // key-compare-to functor or if R is bool and small_ otherwise.
  1234. template <typename R>
  1235. static typename if_<
  1236. if_<is_key_compare_to::value,
  1237. std::is_same<R, int>,
  1238. std::is_same<R, bool> >::type::value,
  1239. big_, small_>::type key_compare_checker(R);
  1240. // A never instantiated helper function that returns the key comparison
  1241. // functor.
  1242. static key_compare key_compare_helper();
  1243. // Verify that key_compare returns a bool. This is similar to the way
  1244. // is_convertible in base/type_traits.h works. Note that key_compare_checker
  1245. // is never actually invoked. The compiler will select which
  1246. // key_compare_checker() to instantiate and then figure out the size of the
  1247. // return type of key_compare_checker() at compile time which we then check
  1248. // against the sizeof of big_.
  1249. COMPILE_ASSERT(
  1250. sizeof(key_compare_checker(key_compare_helper()(key_type(), key_type()))) ==
  1251. sizeof(big_),
  1252. key_comparison_function_must_return_bool);
  1253. // Note: We insist on kTargetValues, which is computed from
  1254. // Params::kTargetNodeSize, must fit the base_fields::field_type.
  1255. COMPILE_ASSERT(kNodeValues <
  1256. (1 << (8 * sizeof(typename base_fields::field_type))),
  1257. target_node_size_too_large);
  1258. // Test the assumption made in setting kNodeValueSpace.
  1259. COMPILE_ASSERT(sizeof(base_fields) >= 2 * sizeof(void*),
  1260. node_space_assumption_incorrect);
  1261. };
  1262. ////
  1263. // btree_node methods
  1264. template <typename P>
  1265. inline void btree_node<P>::insert_value(int i, const value_type &x) {
  1266. assert(i <= count());
  1267. value_init(count(), x);
  1268. for (int j = count(); j > i; --j) {
  1269. value_swap(j, this, j - 1);
  1270. }
  1271. set_count(count() + 1);
  1272. if (!leaf()) {
  1273. ++i;
  1274. for (int j = count(); j > i; --j) {
  1275. *mutable_child(j) = child(j - 1);
  1276. child(j)->set_position(j);
  1277. }
  1278. *mutable_child(i) = NULL;
  1279. }
  1280. }
  1281. template <typename P>
  1282. inline void btree_node<P>::remove_value(int i) {
  1283. if (!leaf()) {
  1284. assert(child(i + 1)->count() == 0);
  1285. for (int j = i + 1; j < count(); ++j) {
  1286. *mutable_child(j) = child(j + 1);
  1287. child(j)->set_position(j);
  1288. }
  1289. *mutable_child(count()) = NULL;
  1290. }
  1291. set_count(count() - 1);
  1292. for (; i < count(); ++i) {
  1293. value_swap(i, this, i + 1);
  1294. }
  1295. value_destroy(i);
  1296. }
  1297. template <typename P>
  1298. void btree_node<P>::rebalance_right_to_left(btree_node *src, int to_move) {
  1299. assert(parent() == src->parent());
  1300. assert(position() + 1 == src->position());
  1301. assert(src->count() >= count());
  1302. assert(to_move >= 1);
  1303. assert(to_move <= src->count());
  1304. // Make room in the left node for the new values.
  1305. for (int i = 0; i < to_move; ++i) {
  1306. value_init(i + count());
  1307. }
  1308. // Move the delimiting value to the left node and the new delimiting value
  1309. // from the right node.
  1310. value_swap(count(), parent(), position());
  1311. parent()->value_swap(position(), src, to_move - 1);
  1312. // Move the values from the right to the left node.
  1313. for (int i = 1; i < to_move; ++i) {
  1314. value_swap(count() + i, src, i - 1);
  1315. }
  1316. // Shift the values in the right node to their correct position.
  1317. for (int i = to_move; i < src->count(); ++i) {
  1318. src->value_swap(i - to_move, src, i);
  1319. }
  1320. for (int i = 1; i <= to_move; ++i) {
  1321. src->value_destroy(src->count() - i);
  1322. }
  1323. if (!leaf()) {
  1324. // Move the child pointers from the right to the left node.
  1325. for (int i = 0; i < to_move; ++i) {
  1326. set_child(1 + count() + i, src->child(i));
  1327. }
  1328. for (int i = 0; i <= src->count() - to_move; ++i) {
  1329. assert(i + to_move <= src->max_count());
  1330. src->set_child(i, src->child(i + to_move));
  1331. *src->mutable_child(i + to_move) = NULL;
  1332. }
  1333. }
  1334. // Fixup the counts on the src and dest nodes.
  1335. set_count(count() + to_move);
  1336. src->set_count(src->count() - to_move);
  1337. }
  1338. template <typename P>
  1339. void btree_node<P>::rebalance_left_to_right(btree_node *dest, int to_move) {
  1340. assert(parent() == dest->parent());
  1341. assert(position() + 1 == dest->position());
  1342. assert(count() >= dest->count());
  1343. assert(to_move >= 1);
  1344. assert(to_move <= count());
  1345. // Make room in the right node for the new values.
  1346. for (int i = 0; i < to_move; ++i) {
  1347. dest->value_init(i + dest->count());
  1348. }
  1349. for (int i = dest->count() - 1; i >= 0; --i) {
  1350. dest->value_swap(i, dest, i + to_move);
  1351. }
  1352. // Move the delimiting value to the right node and the new delimiting value
  1353. // from the left node.
  1354. dest->value_swap(to_move - 1, parent(), position());
  1355. parent()->value_swap(position(), this, count() - to_move);
  1356. value_destroy(count() - to_move);
  1357. // Move the values from the left to the right node.
  1358. for (int i = 1; i < to_move; ++i) {
  1359. value_swap(count() - to_move + i, dest, i - 1);
  1360. value_destroy(count() - to_move + i);
  1361. }
  1362. if (!leaf()) {
  1363. // Move the child pointers from the left to the right node.
  1364. for (int i = dest->count(); i >= 0; --i) {
  1365. dest->set_child(i + to_move, dest->child(i));
  1366. *dest->mutable_child(i) = NULL;
  1367. }
  1368. for (int i = 1; i <= to_move; ++i) {
  1369. dest->set_child(i - 1, child(count() - to_move + i));
  1370. *mutable_child(count() - to_move + i) = NULL;
  1371. }
  1372. }
  1373. // Fixup the counts on the src and dest nodes.
  1374. set_count(count() - to_move);
  1375. dest->set_count(dest->count() + to_move);
  1376. }
  1377. template <typename P>
  1378. void btree_node<P>::split(btree_node *dest, int insert_position) {
  1379. assert(dest->count() == 0);
  1380. // We bias the split based on the position being inserted. If we're
  1381. // inserting at the beginning of the left node then bias the split to put
  1382. // more values on the right node. If we're inserting at the end of the
  1383. // right node then bias the split to put more values on the left node.
  1384. if (insert_position == 0) {
  1385. dest->set_count(count() - 1);
  1386. } else if (insert_position == max_count()) {
  1387. dest->set_count(0);
  1388. } else {
  1389. dest->set_count(count() / 2);
  1390. }
  1391. set_count(count() - dest->count());
  1392. assert(count() >= 1);
  1393. // Move values from the left sibling to the right sibling.
  1394. for (int i = 0; i < dest->count(); ++i) {
  1395. dest->value_init(i);
  1396. value_swap(count() + i, dest, i);
  1397. value_destroy(count() + i);
  1398. }
  1399. // The split key is the largest value in the left sibling.
  1400. set_count(count() - 1);
  1401. parent()->insert_value(position(), value_type());
  1402. value_swap(count(), parent(), position());
  1403. value_destroy(count());
  1404. parent()->set_child(position() + 1, dest);
  1405. if (!leaf()) {
  1406. for (int i = 0; i <= dest->count(); ++i) {
  1407. assert(child(count() + i + 1) != NULL);
  1408. dest->set_child(i, child(count() + i + 1));
  1409. *mutable_child(count() + i + 1) = NULL;
  1410. }
  1411. }
  1412. }
  1413. template <typename P>
  1414. void btree_node<P>::merge(btree_node *src) {
  1415. assert(parent() == src->parent());
  1416. assert(position() + 1 == src->position());
  1417. // Move the delimiting value to the left node.
  1418. value_init(count());
  1419. value_swap(count(), parent(), position());
  1420. // Move the values from the right to the left node.
  1421. for (int i = 0; i < src->count(); ++i) {
  1422. value_init(1 + count() + i);
  1423. value_swap(1 + count() + i, src, i);
  1424. src->value_destroy(i);
  1425. }
  1426. if (!leaf()) {
  1427. // Move the child pointers from the right to the left node.
  1428. for (int i = 0; i <= src->count(); ++i) {
  1429. set_child(1 + count() + i, src->child(i));
  1430. *src->mutable_child(i) = NULL;
  1431. }
  1432. }
  1433. // Fixup the counts on the src and dest nodes.
  1434. set_count(1 + count() + src->count());
  1435. src->set_count(0);
  1436. // Remove the value on the parent node.
  1437. parent()->remove_value(position());
  1438. }
  1439. template <typename P>
  1440. void btree_node<P>::swap(btree_node *x) {
  1441. assert(leaf() == x->leaf());
  1442. // Swap the values.
  1443. for (int i = count(); i < x->count(); ++i) {
  1444. value_init(i);
  1445. }
  1446. for (int i = x->count(); i < count(); ++i) {
  1447. x->value_init(i);
  1448. }
  1449. int n = std::max(count(), x->count());
  1450. for (int i = 0; i < n; ++i) {
  1451. value_swap(i, x, i);
  1452. }
  1453. for (int i = count(); i < x->count(); ++i) {
  1454. x->value_destroy(i);
  1455. }
  1456. for (int i = x->count(); i < count(); ++i) {
  1457. value_destroy(i);
  1458. }
  1459. if (!leaf()) {
  1460. // Swap the child pointers.
  1461. for (int i = 0; i <= n; ++i) {
  1462. btree_swap_helper(*mutable_child(i), *x->mutable_child(i));
  1463. }
  1464. for (int i = 0; i <= count(); ++i) {
  1465. x->child(i)->fields_.parent = x;
  1466. }
  1467. for (int i = 0; i <= x->count(); ++i) {
  1468. child(i)->fields_.parent = this;
  1469. }
  1470. }
  1471. // Swap the counts.
  1472. btree_swap_helper(fields_.count, x->fields_.count);
  1473. }
  1474. ////
  1475. // btree_iterator methods
  1476. template <typename N, typename R, typename P>
  1477. void btree_iterator<N, R, P>::increment_slow() {
  1478. if (node->leaf()) {
  1479. assert(position >= node->count());
  1480. self_type save(*this);
  1481. while (position == node->count() && !node->is_root()) {
  1482. assert(node->parent()->child(node->position()) == node);
  1483. position = node->position();
  1484. node = node->parent();
  1485. }
  1486. if (position == node->count()) {
  1487. *this = save;
  1488. }
  1489. } else {
  1490. assert(position < node->count());
  1491. node = node->child(position + 1);
  1492. while (!node->leaf()) {
  1493. node = node->child(0);
  1494. }
  1495. position = 0;
  1496. }
  1497. }
  1498. template <typename N, typename R, typename P>
  1499. void btree_iterator<N, R, P>::increment_by(int count) {
  1500. while (count > 0) {
  1501. if (node->leaf()) {
  1502. int rest = node->count() - position;
  1503. position += std::min(rest, count);
  1504. count = count - rest;
  1505. if (position < node->count()) {
  1506. return;
  1507. }
  1508. } else {
  1509. --count;
  1510. }
  1511. increment_slow();
  1512. }
  1513. }
  1514. template <typename N, typename R, typename P>
  1515. void btree_iterator<N, R, P>::decrement_slow() {
  1516. if (node->leaf()) {
  1517. assert(position <= -1);
  1518. self_type save(*this);
  1519. while (position < 0 && !node->is_root()) {
  1520. assert(node->parent()->child(node->position()) == node);
  1521. position = node->position() - 1;
  1522. node = node->parent();
  1523. }
  1524. if (position < 0) {
  1525. *this = save;
  1526. }
  1527. } else {
  1528. assert(position >= 0);
  1529. node = node->child(position);
  1530. while (!node->leaf()) {
  1531. node = node->child(node->count());
  1532. }
  1533. position = node->count() - 1;
  1534. }
  1535. }
  1536. ////
  1537. // btree methods
  1538. template <typename P>
  1539. btree<P>::btree(const key_compare &comp, const allocator_type &alloc)
  1540. : key_compare(comp),
  1541. root_(alloc, NULL) {
  1542. }
  1543. template <typename P>
  1544. btree<P>::btree(const self_type &x)
  1545. : key_compare(x.key_comp()),
  1546. root_(x.internal_allocator(), NULL) {
  1547. assign(x);
  1548. }
  1549. template <typename P> template <typename ValuePointer>
  1550. std::pair<typename btree<P>::iterator, bool>
  1551. btree<P>::insert_unique(const key_type &key, ValuePointer value) {
  1552. if (empty()) {
  1553. *mutable_root() = new_leaf_root_node(1);
  1554. }
  1555. std::pair<iterator, int> res = internal_locate(key, iterator(root(), 0));
  1556. iterator &iter = res.first;
  1557. if (res.second == kExactMatch) {
  1558. // The key already exists in the tree, do nothing.
  1559. return std::make_pair(internal_last(iter), false);
  1560. } else if (!res.second) {
  1561. iterator last = internal_last(iter);
  1562. if (last.node && !compare_keys(key, last.key())) {
  1563. // The key already exists in the tree, do nothing.
  1564. return std::make_pair(last, false);
  1565. }
  1566. }
  1567. return std::make_pair(internal_insert(iter, *value), true);
  1568. }
  1569. template <typename P>
  1570. inline typename btree<P>::iterator
  1571. btree<P>::insert_unique(iterator position, const value_type &v) {
  1572. if (!empty()) {
  1573. const key_type &key = params_type::key(v);
  1574. if (position == end() || compare_keys(key, position.key())) {
  1575. iterator prev = position;
  1576. if (position == begin() || compare_keys((--prev).key(), key)) {
  1577. // prev.key() < key < position.key()
  1578. return internal_insert(position, v);
  1579. }
  1580. } else if (compare_keys(position.key(), key)) {
  1581. iterator next = position;
  1582. ++next;
  1583. if (next == end() || compare_keys(key, next.key())) {
  1584. // position.key() < key < next.key()
  1585. return internal_insert(next, v);
  1586. }
  1587. } else {
  1588. // position.key() == key
  1589. return position;
  1590. }
  1591. }
  1592. return insert_unique(v).first;
  1593. }
  1594. template <typename P> template <typename InputIterator>
  1595. void btree<P>::insert_unique(InputIterator b, InputIterator e) {
  1596. for (; b != e; ++b) {
  1597. insert_unique(end(), *b);
  1598. }
  1599. }
  1600. template <typename P> template <typename ValuePointer>
  1601. typename btree<P>::iterator
  1602. btree<P>::insert_multi(const key_type &key, ValuePointer value) {
  1603. if (empty()) {
  1604. *mutable_root() = new_leaf_root_node(1);
  1605. }
  1606. iterator iter = internal_upper_bound(key, iterator(root(), 0));
  1607. if (!iter.node) {
  1608. iter = end();
  1609. }
  1610. return internal_insert(iter, *value);
  1611. }
  1612. template <typename P>
  1613. typename btree<P>::iterator
  1614. btree<P>::insert_multi(iterator position, const value_type &v) {
  1615. if (!empty()) {
  1616. const key_type &key = params_type::key(v);
  1617. if (position == end() || !compare_keys(position.key(), key)) {
  1618. iterator prev = position;
  1619. if (position == begin() || !compare_keys(key, (--prev).key())) {
  1620. // prev.key() <= key <= position.key()
  1621. return internal_insert(position, v);
  1622. }
  1623. } else {
  1624. iterator next = position;
  1625. ++next;
  1626. if (next == end() || !compare_keys(next.key(), key)) {
  1627. // position.key() < key <= next.key()
  1628. return internal_insert(next, v);
  1629. }
  1630. }
  1631. }
  1632. return insert_multi(v);
  1633. }
  1634. template <typename P> template <typename InputIterator>
  1635. void btree<P>::insert_multi(InputIterator b, InputIterator e) {
  1636. for (; b != e; ++b) {
  1637. insert_multi(end(), *b);
  1638. }
  1639. }
  1640. template <typename P>
  1641. void btree<P>::assign(const self_type &x) {
  1642. clear();
  1643. *mutable_key_comp() = x.key_comp();
  1644. *mutable_internal_allocator() = x.internal_allocator();
  1645. // Assignment can avoid key comparisons because we know the order of the
  1646. // values is the same order we'll store them in.
  1647. for (const_iterator iter = x.begin(); iter != x.end(); ++iter) {
  1648. if (empty()) {
  1649. insert_multi(*iter);
  1650. } else {
  1651. // If the btree is not empty, we can just insert the new value at the end
  1652. // of the tree!
  1653. internal_insert(end(), *iter);
  1654. }
  1655. }
  1656. }
  1657. template <typename P>
  1658. typename btree<P>::iterator btree<P>::erase(iterator iter) {
  1659. bool internal_delete = false;
  1660. if (!iter.node->leaf()) {
  1661. // Deletion of a value on an internal node. Swap the key with the largest
  1662. // value of our left child. This is easy, we just decrement iter.
  1663. iterator tmp_iter(iter--);
  1664. assert(iter.node->leaf());
  1665. assert(!compare_keys(tmp_iter.key(), iter.key()));
  1666. iter.node->value_swap(iter.position, tmp_iter.node, tmp_iter.position);
  1667. internal_delete = true;
  1668. --*mutable_size();
  1669. } else if (!root()->leaf()) {
  1670. --*mutable_size();
  1671. }
  1672. // Delete the key from the leaf.
  1673. iter.node->remove_value(iter.position);
  1674. // We want to return the next value after the one we just erased. If we
  1675. // erased from an internal node (internal_delete == true), then the next
  1676. // value is ++(++iter). If we erased from a leaf node (internal_delete ==
  1677. // false) then the next value is ++iter. Note that ++iter may point to an
  1678. // internal node and the value in the internal node may move to a leaf node
  1679. // (iter.node) when rebalancing is performed at the leaf level.
  1680. // Merge/rebalance as we walk back up the tree.
  1681. iterator res(iter);
  1682. for (;;) {
  1683. if (iter.node == root()) {
  1684. try_shrink();
  1685. if (empty()) {
  1686. return end();
  1687. }
  1688. break;
  1689. }
  1690. if (iter.node->count() >= kMinNodeValues) {
  1691. break;
  1692. }
  1693. bool merged = try_merge_or_rebalance(&iter);
  1694. if (iter.node->leaf()) {
  1695. res = iter;
  1696. }
  1697. if (!merged) {
  1698. break;
  1699. }
  1700. iter.node = iter.node->parent();
  1701. }
  1702. // Adjust our return value. If we're pointing at the end of a node, advance
  1703. // the iterator.
  1704. if (res.position == res.node->count()) {
  1705. res.position = res.node->count() - 1;
  1706. ++res;
  1707. }
  1708. // If we erased from an internal node, advance the iterator.
  1709. if (internal_delete) {
  1710. ++res;
  1711. }
  1712. return res;
  1713. }
  1714. template <typename P>
  1715. int btree<P>::erase(iterator begin, iterator end) {
  1716. int count = distance(begin, end);
  1717. for (int i = 0; i < count; i++) {
  1718. begin = erase(begin);
  1719. }
  1720. return count;
  1721. }
  1722. template <typename P>
  1723. int btree<P>::erase_unique(const key_type &key) {
  1724. iterator iter = internal_find_unique(key, iterator(root(), 0));
  1725. if (!iter.node) {
  1726. // The key doesn't exist in the tree, return nothing done.
  1727. return 0;
  1728. }
  1729. erase(iter);
  1730. return 1;
  1731. }
  1732. template <typename P>
  1733. int btree<P>::erase_multi(const key_type &key) {
  1734. iterator begin = internal_lower_bound(key, iterator(root(), 0));
  1735. if (!begin.node) {
  1736. // The key doesn't exist in the tree, return nothing done.
  1737. return 0;
  1738. }
  1739. // Delete all of the keys between begin and upper_bound(key).
  1740. iterator end = internal_end(
  1741. internal_upper_bound(key, iterator(root(), 0)));
  1742. return erase(begin, end);
  1743. }
  1744. template <typename P>
  1745. void btree<P>::clear() {
  1746. if (root() != NULL) {
  1747. internal_clear(root());
  1748. }
  1749. *mutable_root() = NULL;
  1750. }
  1751. template <typename P>
  1752. void btree<P>::swap(self_type &x) {
  1753. std::swap(static_cast<key_compare&>(*this), static_cast<key_compare&>(x));
  1754. std::swap(root_, x.root_);
  1755. }
  1756. template <typename P>
  1757. void btree<P>::verify() const {
  1758. if (root() != NULL) {
  1759. assert(size() == internal_verify(root(), NULL, NULL));
  1760. assert(leftmost() == (++const_iterator(root(), -1)).node);
  1761. assert(rightmost() == (--const_iterator(root(), root()->count())).node);
  1762. assert(leftmost()->leaf());
  1763. assert(rightmost()->leaf());
  1764. } else {
  1765. assert(size() == 0);
  1766. assert(leftmost() == NULL);
  1767. assert(rightmost() == NULL);
  1768. }
  1769. }
  1770. template <typename P>
  1771. void btree<P>::rebalance_or_split(iterator *iter) {
  1772. node_type *&node = iter->node;
  1773. int &insert_position = iter->position;
  1774. assert(node->count() == node->max_count());
  1775. // First try to make room on the node by rebalancing.
  1776. node_type *parent = node->parent();
  1777. if (node != root()) {
  1778. if (node->position() > 0) {
  1779. // Try rebalancing with our left sibling.
  1780. node_type *left = parent->child(node->position() - 1);
  1781. if (left->count() < left->max_count()) {
  1782. // We bias rebalancing based on the position being inserted. If we're
  1783. // inserting at the end of the right node then we bias rebalancing to
  1784. // fill up the left node.
  1785. int to_move = (left->max_count() - left->count()) /
  1786. (1 + (insert_position < left->max_count()));
  1787. to_move = std::max(1, to_move);
  1788. if (((insert_position - to_move) >= 0) ||
  1789. ((left->count() + to_move) < left->max_count())) {
  1790. left->rebalance_right_to_left(node, to_move);
  1791. assert(node->max_count() - node->count() == to_move);
  1792. insert_position = insert_position - to_move;
  1793. if (insert_position < 0) {
  1794. insert_position = insert_position + left->count() + 1;
  1795. node = left;
  1796. }
  1797. assert(node->count() < node->max_count());
  1798. return;
  1799. }
  1800. }
  1801. }
  1802. if (node->position() < parent->count()) {
  1803. // Try rebalancing with our right sibling.
  1804. node_type *right = parent->child(node->position() + 1);
  1805. if (right->count() < right->max_count()) {
  1806. // We bias rebalancing based on the position being inserted. If we're
  1807. // inserting at the beginning of the left node then we bias rebalancing
  1808. // to fill up the right node.
  1809. int to_move = (right->max_count() - right->count()) /
  1810. (1 + (insert_position > 0));
  1811. to_move = std::max(1, to_move);
  1812. if ((insert_position <= (node->count() - to_move)) ||
  1813. ((right->count() + to_move) < right->max_count())) {
  1814. node->rebalance_left_to_right(right, to_move);
  1815. if (insert_position > node->count()) {
  1816. insert_position = insert_position - node->count() - 1;
  1817. node = right;
  1818. }
  1819. assert(node->count() < node->max_count());
  1820. return;
  1821. }
  1822. }
  1823. }
  1824. // Rebalancing failed, make sure there is room on the parent node for a new
  1825. // value.
  1826. if (parent->count() == parent->max_count()) {
  1827. iterator parent_iter(node->parent(), node->position());
  1828. rebalance_or_split(&parent_iter);
  1829. }
  1830. } else {
  1831. // Rebalancing not possible because this is the root node.
  1832. if (root()->leaf()) {
  1833. // The root node is currently a leaf node: create a new root node and set
  1834. // the current root node as the child of the new root.
  1835. parent = new_internal_root_node();
  1836. parent->set_child(0, root());
  1837. *mutable_root() = parent;
  1838. assert(*mutable_rightmost() == parent->child(0));
  1839. } else {
  1840. // The root node is an internal node. We do not want to create a new root
  1841. // node because the root node is special and holds the size of the tree
  1842. // and a pointer to the rightmost node. So we create a new internal node
  1843. // and move all of the items on the current root into the new node.
  1844. parent = new_internal_node(parent);
  1845. parent->set_child(0, parent);
  1846. parent->swap(root());
  1847. node = parent;
  1848. }
  1849. }
  1850. // Split the node.
  1851. node_type *split_node;
  1852. if (node->leaf()) {
  1853. split_node = new_leaf_node(parent);
  1854. node->split(split_node, insert_position);
  1855. if (rightmost() == node) {
  1856. *mutable_rightmost() = split_node;
  1857. }
  1858. } else {
  1859. split_node = new_internal_node(parent);
  1860. node->split(split_node, insert_position);
  1861. }
  1862. if (insert_position > node->count()) {
  1863. insert_position = insert_position - node->count() - 1;
  1864. node = split_node;
  1865. }
  1866. }
  1867. template <typename P>
  1868. void btree<P>::merge_nodes(node_type *left, node_type *right) {
  1869. left->merge(right);
  1870. if (right->leaf()) {
  1871. if (rightmost() == right) {
  1872. *mutable_rightmost() = left;
  1873. }
  1874. delete_leaf_node(right);
  1875. } else {
  1876. delete_internal_node(right);
  1877. }
  1878. }
  1879. template <typename P>
  1880. bool btree<P>::try_merge_or_rebalance(iterator *iter) {
  1881. node_type *parent = iter->node->parent();
  1882. if (iter->node->position() > 0) {
  1883. // Try merging with our left sibling.
  1884. node_type *left = parent->child(iter->node->position() - 1);
  1885. if ((1 + left->count() + iter->node->count()) <= left->max_count()) {
  1886. iter->position += 1 + left->count();
  1887. merge_nodes(left, iter->node);
  1888. iter->node = left;
  1889. return true;
  1890. }
  1891. }
  1892. if (iter->node->position() < parent->count()) {
  1893. // Try merging with our right sibling.
  1894. node_type *right = parent->child(iter->node->position() + 1);
  1895. if ((1 + iter->node->count() + right->count()) <= right->max_count()) {
  1896. merge_nodes(iter->node, right);
  1897. return true;
  1898. }
  1899. // Try rebalancing with our right sibling. We don't perform rebalancing if
  1900. // we deleted the first element from iter->node and the node is not
  1901. // empty. This is a small optimization for the common pattern of deleting
  1902. // from the front of the tree.
  1903. if ((right->count() > kMinNodeValues) &&
  1904. ((iter->node->count() == 0) ||
  1905. (iter->position > 0))) {
  1906. int to_move = (right->count() - iter->node->count()) / 2;
  1907. to_move = std::min(to_move, right->count() - 1);
  1908. iter->node->rebalance_right_to_left(right, to_move);
  1909. return false;
  1910. }
  1911. }
  1912. if (iter->node->position() > 0) {
  1913. // Try rebalancing with our left sibling. We don't perform rebalancing if
  1914. // we deleted the last element from iter->node and the node is not
  1915. // empty. This is a small optimization for the common pattern of deleting
  1916. // from the back of the tree.
  1917. node_type *left = parent->child(iter->node->position() - 1);
  1918. if ((left->count() > kMinNodeValues) &&
  1919. ((iter->node->count() == 0) ||
  1920. (iter->position < iter->node->count()))) {
  1921. int to_move = (left->count() - iter->node->count()) / 2;
  1922. to_move = std::min(to_move, left->count() - 1);
  1923. left->rebalance_left_to_right(iter->node, to_move);
  1924. iter->position += to_move;
  1925. return false;
  1926. }
  1927. }
  1928. return false;
  1929. }
  1930. template <typename P>
  1931. void btree<P>::try_shrink() {
  1932. if (root()->count() > 0) {
  1933. return;
  1934. }
  1935. // Deleted the last item on the root node, shrink the height of the tree.
  1936. if (root()->leaf()) {
  1937. assert(size() == 0);
  1938. delete_leaf_node(root());
  1939. *mutable_root() = NULL;
  1940. } else {
  1941. node_type *child = root()->child(0);
  1942. if (child->leaf()) {
  1943. // The child is a leaf node so simply make it the root node in the tree.
  1944. child->make_root();
  1945. delete_internal_root_node();
  1946. *mutable_root() = child;
  1947. } else {
  1948. // The child is an internal node. We want to keep the existing root node
  1949. // so we move all of the values from the child node into the existing
  1950. // (empty) root node.
  1951. child->swap(root());
  1952. delete_internal_node(child);
  1953. }
  1954. }
  1955. }
  1956. template <typename P> template <typename IterType>
  1957. inline IterType btree<P>::internal_last(IterType iter) {
  1958. while (iter.node && iter.position == iter.node->count()) {
  1959. iter.position = iter.node->position();
  1960. iter.node = iter.node->parent();
  1961. if (iter.node->leaf()) {
  1962. iter.node = NULL;
  1963. }
  1964. }
  1965. return iter;
  1966. }
  1967. template <typename P>
  1968. inline typename btree<P>::iterator
  1969. btree<P>::internal_insert(iterator iter, const value_type &v) {
  1970. if (!iter.node->leaf()) {
  1971. // We can't insert on an internal node. Instead, we'll insert after the
  1972. // previous value which is guaranteed to be on a leaf node.
  1973. --iter;
  1974. ++iter.position;
  1975. }
  1976. if (iter.node->count() == iter.node->max_count()) {
  1977. // Make room in the leaf for the new item.
  1978. if (iter.node->max_count() < kNodeValues) {
  1979. // Insertion into the root where the root is smaller that the full node
  1980. // size. Simply grow the size of the root node.
  1981. assert(iter.node == root());
  1982. iter.node = new_leaf_root_node(
  1983. std::min<int>(kNodeValues, 2 * iter.node->max_count()));
  1984. iter.node->swap(root());
  1985. delete_leaf_node(root());
  1986. *mutable_root() = iter.node;
  1987. } else {
  1988. rebalance_or_split(&iter);
  1989. ++*mutable_size();
  1990. }
  1991. } else if (!root()->leaf()) {
  1992. ++*mutable_size();
  1993. }
  1994. iter.node->insert_value(iter.position, v);
  1995. return iter;
  1996. }
  1997. template <typename P> template <typename IterType>
  1998. inline std::pair<IterType, int> btree<P>::internal_locate(
  1999. const key_type &key, IterType iter) const {
  2000. return internal_locate_type::dispatch(key, *this, iter);
  2001. }
  2002. template <typename P> template <typename IterType>
  2003. inline std::pair<IterType, int> btree<P>::internal_locate_plain_compare(
  2004. const key_type &key, IterType iter) const {
  2005. for (;;) {
  2006. iter.position = iter.node->lower_bound(key, key_comp());
  2007. if (iter.node->leaf()) {
  2008. break;
  2009. }
  2010. iter.node = iter.node->child(iter.position);
  2011. }
  2012. return std::make_pair(iter, 0);
  2013. }
  2014. template <typename P> template <typename IterType>
  2015. inline std::pair<IterType, int> btree<P>::internal_locate_compare_to(
  2016. const key_type &key, IterType iter) const {
  2017. for (;;) {
  2018. int res = iter.node->lower_bound(key, key_comp());
  2019. iter.position = res & kMatchMask;
  2020. if (res & kExactMatch) {
  2021. return std::make_pair(iter, static_cast<int>(kExactMatch));
  2022. }
  2023. if (iter.node->leaf()) {
  2024. break;
  2025. }
  2026. iter.node = iter.node->child(iter.position);
  2027. }
  2028. return std::make_pair(iter, -kExactMatch);
  2029. }
  2030. template <typename P> template <typename IterType>
  2031. IterType btree<P>::internal_lower_bound(
  2032. const key_type &key, IterType iter) const {
  2033. if (iter.node) {
  2034. for (;;) {
  2035. iter.position =
  2036. iter.node->lower_bound(key, key_comp()) & kMatchMask;
  2037. if (iter.node->leaf()) {
  2038. break;
  2039. }
  2040. iter.node = iter.node->child(iter.position);
  2041. }
  2042. iter = internal_last(iter);
  2043. }
  2044. return iter;
  2045. }
  2046. template <typename P> template <typename IterType>
  2047. IterType btree<P>::internal_upper_bound(
  2048. const key_type &key, IterType iter) const {
  2049. if (iter.node) {
  2050. for (;;) {
  2051. iter.position = iter.node->upper_bound(key, key_comp());
  2052. if (iter.node->leaf()) {
  2053. break;
  2054. }
  2055. iter.node = iter.node->child(iter.position);
  2056. }
  2057. iter = internal_last(iter);
  2058. }
  2059. return iter;
  2060. }
  2061. template <typename P> template <typename IterType>
  2062. IterType btree<P>::internal_find_unique(
  2063. const key_type &key, IterType iter) const {
  2064. if (iter.node) {
  2065. std::pair<IterType, int> res = internal_locate(key, iter);
  2066. if (res.second == kExactMatch) {
  2067. return res.first;
  2068. }
  2069. if (!res.second) {
  2070. iter = internal_last(res.first);
  2071. if (iter.node && !compare_keys(key, iter.key())) {
  2072. return iter;
  2073. }
  2074. }
  2075. }
  2076. return IterType(NULL, 0);
  2077. }
  2078. template <typename P> template <typename IterType>
  2079. IterType btree<P>::internal_find_multi(
  2080. const key_type &key, IterType iter) const {
  2081. if (iter.node) {
  2082. iter = internal_lower_bound(key, iter);
  2083. if (iter.node) {
  2084. iter = internal_last(iter);
  2085. if (iter.node && !compare_keys(key, iter.key())) {
  2086. return iter;
  2087. }
  2088. }
  2089. }
  2090. return IterType(NULL, 0);
  2091. }
  2092. template <typename P>
  2093. void btree<P>::internal_clear(node_type *node) {
  2094. if (!node->leaf()) {
  2095. for (int i = 0; i <= node->count(); ++i) {
  2096. internal_clear(node->child(i));
  2097. }
  2098. if (node == root()) {
  2099. delete_internal_root_node();
  2100. } else {
  2101. delete_internal_node(node);
  2102. }
  2103. } else {
  2104. delete_leaf_node(node);
  2105. }
  2106. }
  2107. template <typename P>
  2108. void btree<P>::internal_dump(
  2109. std::ostream &os, const node_type *node, int level) const {
  2110. for (int i = 0; i < node->count(); ++i) {
  2111. if (!node->leaf()) {
  2112. internal_dump(os, node->child(i), level + 1);
  2113. }
  2114. for (int j = 0; j < level; ++j) {
  2115. os << " ";
  2116. }
  2117. os << node->key(i) << " [" << level << "]\n";
  2118. }
  2119. if (!node->leaf()) {
  2120. internal_dump(os, node->child(node->count()), level + 1);
  2121. }
  2122. }
  2123. template <typename P>
  2124. int btree<P>::internal_verify(
  2125. const node_type *node, const key_type *lo, const key_type *hi) const {
  2126. assert(node->count() > 0);
  2127. assert(node->count() <= node->max_count());
  2128. if (lo) {
  2129. assert(!compare_keys(node->key(0), *lo));
  2130. }
  2131. if (hi) {
  2132. assert(!compare_keys(*hi, node->key(node->count() - 1)));
  2133. }
  2134. for (int i = 1; i < node->count(); ++i) {
  2135. assert(!compare_keys(node->key(i), node->key(i - 1)));
  2136. }
  2137. int count = node->count();
  2138. if (!node->leaf()) {
  2139. for (int i = 0; i <= node->count(); ++i) {
  2140. assert(node->child(i) != NULL);
  2141. assert(node->child(i)->parent() == node);
  2142. assert(node->child(i)->position() == i);
  2143. count += internal_verify(
  2144. node->child(i),
  2145. (i == 0) ? lo : &node->key(i - 1),
  2146. (i == node->count()) ? hi : &node->key(i));
  2147. }
  2148. }
  2149. return count;
  2150. }
  2151. } // namespace btree
  2152. #endif // UTIL_BTREE_BTREE_H__