sgtree_algorithms.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2007-2014
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. //
  13. // Scapegoat tree algorithms are taken from the paper titled:
  14. // "Scapegoat Trees" by Igal Galperin Ronald L. Rivest.
  15. //
  16. /////////////////////////////////////////////////////////////////////////////
  17. #ifndef BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP
  18. #define BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP
  19. #include <boost/intrusive/detail/config_begin.hpp>
  20. #include <boost/intrusive/intrusive_fwd.hpp>
  21. #include <cstddef>
  22. #include <boost/intrusive/detail/algo_type.hpp>
  23. #include <boost/intrusive/bstree_algorithms.hpp>
  24. #if defined(BOOST_HAS_PRAGMA_ONCE)
  25. # pragma once
  26. #endif
  27. namespace boost {
  28. namespace intrusive {
  29. //! sgtree_algorithms is configured with a NodeTraits class, which encapsulates the
  30. //! information about the node to be manipulated. NodeTraits must support the
  31. //! following interface:
  32. //!
  33. //! <b>Typedefs</b>:
  34. //!
  35. //! <tt>node</tt>: The type of the node that forms the binary search tree
  36. //!
  37. //! <tt>node_ptr</tt>: A pointer to a node
  38. //!
  39. //! <tt>const_node_ptr</tt>: A pointer to a const node
  40. //!
  41. //! <b>Static functions</b>:
  42. //!
  43. //! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
  44. //!
  45. //! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
  46. //!
  47. //! <tt>static node_ptr get_left(const_node_ptr n);</tt>
  48. //!
  49. //! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
  50. //!
  51. //! <tt>static node_ptr get_right(const_node_ptr n);</tt>
  52. //!
  53. //! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
  54. template<class NodeTraits>
  55. class sgtree_algorithms
  56. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  57. : public bstree_algorithms<NodeTraits>
  58. #endif
  59. {
  60. public:
  61. typedef typename NodeTraits::node node;
  62. typedef NodeTraits node_traits;
  63. typedef typename NodeTraits::node_ptr node_ptr;
  64. typedef typename NodeTraits::const_node_ptr const_node_ptr;
  65. /// @cond
  66. private:
  67. typedef bstree_algorithms<NodeTraits> bstree_algo;
  68. /// @endcond
  69. public:
  70. //! This type is the information that will be
  71. //! filled by insert_unique_check
  72. struct insert_commit_data
  73. : bstree_algo::insert_commit_data
  74. {
  75. std::size_t depth;
  76. };
  77. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  78. //! @copydoc ::boost::intrusive::bstree_algorithms::get_header(const const_node_ptr&)
  79. static node_ptr get_header(const_node_ptr n);
  80. //! @copydoc ::boost::intrusive::bstree_algorithms::begin_node
  81. static node_ptr begin_node(const_node_ptr header);
  82. //! @copydoc ::boost::intrusive::bstree_algorithms::end_node
  83. static node_ptr end_node(const_node_ptr header);
  84. //! @copydoc ::boost::intrusive::bstree_algorithms::swap_tree
  85. static void swap_tree(node_ptr header1, node_ptr header2);
  86. //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(node_ptr,node_ptr)
  87. static void swap_nodes(node_ptr node1, node_ptr node2);
  88. //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(node_ptr,node_ptr,node_ptr,node_ptr)
  89. static void swap_nodes(node_ptr node1, node_ptr header1, node_ptr node2, node_ptr header2);
  90. //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(node_ptr,node_ptr)
  91. static void replace_node(node_ptr node_to_be_replaced, node_ptr new_node);
  92. //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(node_ptr,node_ptr,node_ptr)
  93. static void replace_node(node_ptr node_to_be_replaced, node_ptr header, node_ptr new_node);
  94. //Unlink is not possible since tree metadata is needed to update the tree
  95. //!static void unlink(node_ptr node);
  96. //! @copydoc ::boost::intrusive::bstree_algorithms::unlink_leftmost_without_rebalance
  97. static node_ptr unlink_leftmost_without_rebalance(node_ptr header);
  98. //! @copydoc ::boost::intrusive::bstree_algorithms::unique(const const_node_ptr&)
  99. static bool unique(const_node_ptr node);
  100. //! @copydoc ::boost::intrusive::bstree_algorithms::size(const const_node_ptr&)
  101. static std::size_t size(const_node_ptr header);
  102. //! @copydoc ::boost::intrusive::bstree_algorithms::next_node(const node_ptr&)
  103. static node_ptr next_node(node_ptr node);
  104. //! @copydoc ::boost::intrusive::bstree_algorithms::prev_node(const node_ptr&)
  105. static node_ptr prev_node(node_ptr node);
  106. //! @copydoc ::boost::intrusive::bstree_algorithms::init(node_ptr)
  107. static void init(node_ptr node);
  108. //! @copydoc ::boost::intrusive::bstree_algorithms::init_header(node_ptr)
  109. static void init_header(node_ptr header);
  110. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  111. //! @copydoc ::boost::intrusive::bstree_algorithms::erase(node_ptr,node_ptr)
  112. template<class AlphaByMaxSize>
  113. static node_ptr erase(node_ptr header, node_ptr z, std::size_t tree_size, std::size_t &max_tree_size, AlphaByMaxSize alpha_by_maxsize)
  114. {
  115. bstree_algo::erase(header, z);
  116. --tree_size;
  117. if (tree_size > 0 &&
  118. tree_size < alpha_by_maxsize(max_tree_size)){
  119. bstree_algo::rebalance(header);
  120. max_tree_size = tree_size;
  121. }
  122. return z;
  123. }
  124. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  125. //! @copydoc ::boost::intrusive::bstree_algorithms::clone(const const_node_ptr&,node_ptr,Cloner,Disposer)
  126. template <class Cloner, class Disposer>
  127. static void clone
  128. (const_node_ptr source_header, node_ptr target_header, Cloner cloner, Disposer disposer);
  129. //! @copydoc ::boost::intrusive::bstree_algorithms::clear_and_dispose(const node_ptr&,Disposer)
  130. template<class Disposer>
  131. static void clear_and_dispose(node_ptr header, Disposer disposer);
  132. //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
  133. template<class KeyType, class KeyNodePtrCompare>
  134. static node_ptr lower_bound
  135. (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp);
  136. //! @copydoc ::boost::intrusive::bstree_algorithms::upper_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
  137. template<class KeyType, class KeyNodePtrCompare>
  138. static node_ptr upper_bound
  139. (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp);
  140. //! @copydoc ::boost::intrusive::bstree_algorithms::find(const const_node_ptr&, const KeyType&,KeyNodePtrCompare)
  141. template<class KeyType, class KeyNodePtrCompare>
  142. static node_ptr find
  143. (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp);
  144. //! @copydoc ::boost::intrusive::bstree_algorithms::equal_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
  145. template<class KeyType, class KeyNodePtrCompare>
  146. static std::pair<node_ptr, node_ptr> equal_range
  147. (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp);
  148. //! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const const_node_ptr&,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool)
  149. template<class KeyType, class KeyNodePtrCompare>
  150. static std::pair<node_ptr, node_ptr> bounded_range
  151. (const_node_ptr header, const KeyType &lower_key, const KeyType &upper_key, KeyNodePtrCompare comp
  152. , bool left_closed, bool right_closed);
  153. //! @copydoc ::boost::intrusive::bstree_algorithms::count(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
  154. template<class KeyType, class KeyNodePtrCompare>
  155. static std::size_t count(const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp);
  156. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  157. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_upper_bound(node_ptr,node_ptr,NodePtrCompare)
  158. template<class NodePtrCompare, class H_Alpha>
  159. static node_ptr insert_equal_upper_bound
  160. (node_ptr h, node_ptr new_node, NodePtrCompare comp
  161. ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
  162. {
  163. std::size_t depth;
  164. bstree_algo::insert_equal_upper_bound(h, new_node, comp, &depth);
  165. rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
  166. return new_node;
  167. }
  168. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_lower_bound(node_ptr,node_ptr,NodePtrCompare)
  169. template<class NodePtrCompare, class H_Alpha>
  170. static node_ptr insert_equal_lower_bound
  171. (node_ptr h, node_ptr new_node, NodePtrCompare comp
  172. ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
  173. {
  174. std::size_t depth;
  175. bstree_algo::insert_equal_lower_bound(h, new_node, comp, &depth);
  176. rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
  177. return new_node;
  178. }
  179. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal(node_ptr,node_ptr,node_ptr,NodePtrCompare)
  180. template<class NodePtrCompare, class H_Alpha>
  181. static node_ptr insert_equal
  182. (node_ptr header, node_ptr hint, node_ptr new_node, NodePtrCompare comp
  183. ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
  184. {
  185. std::size_t depth;
  186. bstree_algo::insert_equal(header, hint, new_node, comp, &depth);
  187. rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
  188. return new_node;
  189. }
  190. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_before(node_ptr,node_ptr,node_ptr)
  191. template<class H_Alpha>
  192. static node_ptr insert_before
  193. (node_ptr header, node_ptr pos, node_ptr new_node
  194. ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
  195. {
  196. std::size_t depth;
  197. bstree_algo::insert_before(header, pos, new_node, &depth);
  198. rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
  199. return new_node;
  200. }
  201. //! @copydoc ::boost::intrusive::bstree_algorithms::push_back(node_ptr,node_ptr)
  202. template<class H_Alpha>
  203. static void push_back(node_ptr header, node_ptr new_node
  204. ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
  205. {
  206. std::size_t depth;
  207. bstree_algo::push_back(header, new_node, &depth);
  208. rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
  209. }
  210. //! @copydoc ::boost::intrusive::bstree_algorithms::push_front(node_ptr,node_ptr)
  211. template<class H_Alpha>
  212. static void push_front(node_ptr header, node_ptr new_node
  213. ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
  214. {
  215. std::size_t depth;
  216. bstree_algo::push_front(header, new_node, &depth);
  217. rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
  218. }
  219. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
  220. template<class KeyType, class KeyNodePtrCompare>
  221. static std::pair<node_ptr, bool> insert_unique_check
  222. (const_node_ptr header, const KeyType &key
  223. ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
  224. {
  225. std::size_t depth;
  226. std::pair<node_ptr, bool> ret =
  227. bstree_algo::insert_unique_check(header, key, comp, commit_data, &depth);
  228. commit_data.depth = depth;
  229. return ret;
  230. }
  231. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
  232. template<class KeyType, class KeyNodePtrCompare>
  233. static std::pair<node_ptr, bool> insert_unique_check
  234. (const_node_ptr header, node_ptr hint, const KeyType &key
  235. ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
  236. {
  237. std::size_t depth;
  238. std::pair<node_ptr, bool> ret =
  239. bstree_algo::insert_unique_check
  240. (header, hint, key, comp, commit_data, &depth);
  241. commit_data.depth = depth;
  242. return ret;
  243. }
  244. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_commit(node_ptr,node_ptr,const insert_commit_data&)
  245. template<class H_Alpha>
  246. BOOST_INTRUSIVE_FORCEINLINE static void insert_unique_commit
  247. (node_ptr header, node_ptr new_value, const insert_commit_data &commit_data
  248. ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
  249. { return insert_commit(header, new_value, commit_data, tree_size, h_alpha, max_tree_size); }
  250. //! @copydoc ::boost::intrusive::bstree_algorithms::transfer_unique
  251. template<class NodePtrCompare, class H_Alpha, class AlphaByMaxSize>
  252. static bool transfer_unique
  253. ( node_ptr header1, NodePtrCompare comp, std::size_t tree1_size, std::size_t &max_tree1_size
  254. , node_ptr header2, node_ptr z, std::size_t tree2_size, std::size_t &max_tree2_size
  255. ,H_Alpha h_alpha, AlphaByMaxSize alpha_by_maxsize)
  256. {
  257. insert_commit_data commit_data;
  258. bool const transferable = insert_unique_check(header1, z, comp, commit_data).second;
  259. if(transferable){
  260. erase(header2, z, tree2_size, max_tree2_size, alpha_by_maxsize);
  261. insert_commit(header1, z, commit_data, tree1_size, h_alpha, max_tree1_size);
  262. }
  263. return transferable;
  264. }
  265. //! @copydoc ::boost::intrusive::bstree_algorithms::transfer_equal
  266. template<class NodePtrCompare, class H_Alpha, class AlphaByMaxSize>
  267. static void transfer_equal
  268. ( node_ptr header1, NodePtrCompare comp, std::size_t tree1_size, std::size_t &max_tree1_size
  269. , node_ptr header2, node_ptr z, std::size_t tree2_size, std::size_t &max_tree2_size
  270. ,H_Alpha h_alpha, AlphaByMaxSize alpha_by_maxsize)
  271. {
  272. insert_commit_data commit_data;
  273. insert_equal_upper_bound_check(header1, z, comp, commit_data);
  274. erase(header2, z, tree2_size, max_tree2_size, alpha_by_maxsize);
  275. insert_commit(header1, z, commit_data, tree1_size, h_alpha, max_tree1_size);
  276. }
  277. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  278. //! @copydoc ::boost::intrusive::bstree_algorithms::is_header
  279. static bool is_header(const_node_ptr p);
  280. //! @copydoc ::boost::intrusive::bstree_algorithms::is_header
  281. static void rebalance(node_ptr header);
  282. //! @copydoc ::boost::intrusive::bstree_algorithms::rebalance_subtree
  283. static node_ptr rebalance_subtree(node_ptr old_root)
  284. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  285. /// @cond
  286. private:
  287. template<class KeyType, class KeyNodePtrCompare>
  288. static void insert_equal_upper_bound_check
  289. (node_ptr header, const KeyType &key
  290. ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
  291. {
  292. std::size_t depth;
  293. bstree_algo::insert_equal_upper_bound_check(header, key, comp, commit_data, &depth);
  294. commit_data.depth = depth;
  295. }
  296. template<class H_Alpha>
  297. static void insert_commit
  298. (node_ptr header, node_ptr new_value, const insert_commit_data &commit_data
  299. ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
  300. {
  301. bstree_algo::insert_unique_commit(header, new_value, commit_data);
  302. rebalance_after_insertion(new_value, commit_data.depth, tree_size+1, h_alpha, max_tree_size);
  303. }
  304. template<class H_Alpha>
  305. static void rebalance_after_insertion
  306. (node_ptr x, std::size_t depth
  307. , std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
  308. {
  309. if(tree_size > max_tree_size)
  310. max_tree_size = tree_size;
  311. if(tree_size > 2 && //Nothing to do with only the root
  312. //Check if the root node is unbalanced
  313. //Scapegoat paper depth counts root depth as zero and "depth" counts root as 1,
  314. //but since "depth" is the depth of the ancestor of x, i == depth
  315. depth > h_alpha(tree_size)){
  316. //Find the first non height-balanced node
  317. //as described in the section 4.2 of the paper.
  318. //This method is the alternative method described
  319. //in the paper. Authors claim that this method
  320. //may tend to yield more balanced trees on the average
  321. //than the weight balanced method.
  322. node_ptr s = x;
  323. std::size_t size = 1;
  324. for(std::size_t ancestor = 1; ancestor != depth; ++ancestor){
  325. const node_ptr s_parent = NodeTraits::get_parent(s);
  326. const node_ptr s_parent_left = NodeTraits::get_left(s_parent);
  327. //Obtain parent's size (previous size + parent + sibling tree)
  328. const node_ptr s_sibling = s_parent_left == s ? NodeTraits::get_right(s_parent) : s_parent_left;
  329. size += 1 + bstree_algo::subtree_size(s_sibling);
  330. s = s_parent;
  331. if(ancestor > h_alpha(size)){ //is 's' scapegoat?
  332. bstree_algo::rebalance_subtree(s);
  333. return;
  334. }
  335. }
  336. //The whole tree must be rebuilt
  337. max_tree_size = tree_size;
  338. bstree_algo::rebalance_subtree(NodeTraits::get_parent(s));
  339. }
  340. }
  341. /// @endcond
  342. };
  343. /// @cond
  344. template<class NodeTraits>
  345. struct get_algo<SgTreeAlgorithms, NodeTraits>
  346. {
  347. typedef sgtree_algorithms<NodeTraits> type;
  348. };
  349. template <class ValueTraits, class NodePtrCompare, class ExtraChecker>
  350. struct get_node_checker<SgTreeAlgorithms, ValueTraits, NodePtrCompare, ExtraChecker>
  351. {
  352. typedef detail::bstree_node_checker<ValueTraits, NodePtrCompare, ExtraChecker> type;
  353. };
  354. /// @endcond
  355. } //namespace intrusive
  356. } //namespace boost
  357. #include <boost/intrusive/detail/config_end.hpp>
  358. #endif //BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP