sgtree_algorithms.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 const_node_ptr & n);
  80. //! @copydoc ::boost::intrusive::bstree_algorithms::begin_node
  81. static node_ptr begin_node(const const_node_ptr & header);
  82. //! @copydoc ::boost::intrusive::bstree_algorithms::end_node
  83. static node_ptr end_node(const const_node_ptr & header);
  84. //! @copydoc ::boost::intrusive::bstree_algorithms::swap_tree
  85. static void swap_tree(const node_ptr & header1, const node_ptr & header2);
  86. //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&)
  87. static void swap_nodes(const node_ptr & node1, const node_ptr & node2);
  88. //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&,const node_ptr&,const node_ptr&)
  89. static void swap_nodes(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2);
  90. //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&)
  91. static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & new_node);
  92. //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&,const node_ptr&)
  93. static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node);
  94. //Unlink is not possible since tree metadata is needed to update the tree
  95. //!static void unlink(const node_ptr & node);
  96. //! @copydoc ::boost::intrusive::bstree_algorithms::unlink_leftmost_without_rebalance
  97. static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header);
  98. //! @copydoc ::boost::intrusive::bstree_algorithms::unique(const const_node_ptr&)
  99. static bool unique(const const_node_ptr & node);
  100. //! @copydoc ::boost::intrusive::bstree_algorithms::size(const const_node_ptr&)
  101. static std::size_t size(const const_node_ptr & header);
  102. //! @copydoc ::boost::intrusive::bstree_algorithms::next_node(const node_ptr&)
  103. static node_ptr next_node(const node_ptr & node);
  104. //! @copydoc ::boost::intrusive::bstree_algorithms::prev_node(const node_ptr&)
  105. static node_ptr prev_node(const node_ptr & node);
  106. //! @copydoc ::boost::intrusive::bstree_algorithms::init(const node_ptr&)
  107. static void init(const node_ptr & node);
  108. //! @copydoc ::boost::intrusive::bstree_algorithms::init_header(const node_ptr&)
  109. static void init_header(const node_ptr & header);
  110. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  111. //! @copydoc ::boost::intrusive::bstree_algorithms::erase(const node_ptr&,const node_ptr&)
  112. template<class AlphaByMaxSize>
  113. static node_ptr erase(const node_ptr & header, const node_ptr & z, std::size_t tree_size, std::size_t &max_tree_size, AlphaByMaxSize alpha_by_maxsize)
  114. {
  115. //typename bstree_algo::data_for_rebalance info;
  116. bstree_algo::erase(header, z);
  117. --tree_size;
  118. if (tree_size > 0 &&
  119. tree_size < alpha_by_maxsize(max_tree_size)){
  120. bstree_algo::rebalance(header);
  121. max_tree_size = tree_size;
  122. }
  123. return z;
  124. }
  125. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  126. //! @copydoc ::boost::intrusive::bstree_algorithms::clone(const const_node_ptr&,const node_ptr&,Cloner,Disposer)
  127. template <class Cloner, class Disposer>
  128. static void clone
  129. (const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer);
  130. //! @copydoc ::boost::intrusive::bstree_algorithms::clear_and_dispose(const node_ptr&,Disposer)
  131. template<class Disposer>
  132. static void clear_and_dispose(const node_ptr & header, Disposer disposer);
  133. //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
  134. template<class KeyType, class KeyNodePtrCompare>
  135. static node_ptr lower_bound
  136. (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
  137. //! @copydoc ::boost::intrusive::bstree_algorithms::upper_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
  138. template<class KeyType, class KeyNodePtrCompare>
  139. static node_ptr upper_bound
  140. (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
  141. //! @copydoc ::boost::intrusive::bstree_algorithms::find(const const_node_ptr&, const KeyType&,KeyNodePtrCompare)
  142. template<class KeyType, class KeyNodePtrCompare>
  143. static node_ptr find
  144. (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
  145. //! @copydoc ::boost::intrusive::bstree_algorithms::equal_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
  146. template<class KeyType, class KeyNodePtrCompare>
  147. static std::pair<node_ptr, node_ptr> equal_range
  148. (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
  149. //! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const const_node_ptr&,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool)
  150. template<class KeyType, class KeyNodePtrCompare>
  151. static std::pair<node_ptr, node_ptr> bounded_range
  152. (const const_node_ptr & header, const KeyType &lower_key, const KeyType &upper_key, KeyNodePtrCompare comp
  153. , bool left_closed, bool right_closed);
  154. //! @copydoc ::boost::intrusive::bstree_algorithms::count(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
  155. template<class KeyType, class KeyNodePtrCompare>
  156. static std::size_t count(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
  157. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  158. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_upper_bound(const node_ptr&,const node_ptr&,NodePtrCompare)
  159. template<class NodePtrCompare, class H_Alpha>
  160. static node_ptr insert_equal_upper_bound
  161. (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp
  162. ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
  163. {
  164. std::size_t depth;
  165. bstree_algo::insert_equal_upper_bound(h, new_node, comp, &depth);
  166. rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
  167. return new_node;
  168. }
  169. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_lower_bound(const node_ptr&,const node_ptr&,NodePtrCompare)
  170. template<class NodePtrCompare, class H_Alpha>
  171. static node_ptr insert_equal_lower_bound
  172. (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp
  173. ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
  174. {
  175. std::size_t depth;
  176. bstree_algo::insert_equal_lower_bound(h, new_node, comp, &depth);
  177. rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
  178. return new_node;
  179. }
  180. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal(const node_ptr&,const node_ptr&,const node_ptr&,NodePtrCompare)
  181. template<class NodePtrCompare, class H_Alpha>
  182. static node_ptr insert_equal
  183. (const node_ptr & header, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp
  184. ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
  185. {
  186. std::size_t depth;
  187. bstree_algo::insert_equal(header, hint, new_node, comp, &depth);
  188. rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
  189. return new_node;
  190. }
  191. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_before(const node_ptr&,const node_ptr&,const node_ptr&)
  192. template<class H_Alpha>
  193. static node_ptr insert_before
  194. (const node_ptr & header, const node_ptr & pos, const node_ptr & new_node
  195. ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
  196. {
  197. std::size_t depth;
  198. bstree_algo::insert_before(header, pos, new_node, &depth);
  199. rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
  200. return new_node;
  201. }
  202. //! @copydoc ::boost::intrusive::bstree_algorithms::push_back(const node_ptr&,const node_ptr&)
  203. template<class H_Alpha>
  204. static void push_back(const node_ptr & header, const node_ptr & new_node
  205. ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
  206. {
  207. std::size_t depth;
  208. bstree_algo::push_back(header, new_node, &depth);
  209. rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
  210. }
  211. //! @copydoc ::boost::intrusive::bstree_algorithms::push_front(const node_ptr&,const node_ptr&)
  212. template<class H_Alpha>
  213. static void push_front(const node_ptr & header, const node_ptr & new_node
  214. ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
  215. {
  216. std::size_t depth;
  217. bstree_algo::push_front(header, new_node, &depth);
  218. rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
  219. }
  220. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
  221. template<class KeyType, class KeyNodePtrCompare>
  222. static std::pair<node_ptr, bool> insert_unique_check
  223. (const const_node_ptr & header, const KeyType &key
  224. ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
  225. {
  226. std::size_t depth;
  227. std::pair<node_ptr, bool> ret =
  228. bstree_algo::insert_unique_check(header, key, comp, commit_data, &depth);
  229. commit_data.depth = depth;
  230. return ret;
  231. }
  232. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
  233. template<class KeyType, class KeyNodePtrCompare>
  234. static std::pair<node_ptr, bool> insert_unique_check
  235. (const const_node_ptr & header, const node_ptr &hint, const KeyType &key
  236. ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
  237. {
  238. std::size_t depth;
  239. std::pair<node_ptr, bool> ret =
  240. bstree_algo::insert_unique_check
  241. (header, hint, key, comp, commit_data, &depth);
  242. commit_data.depth = depth;
  243. return ret;
  244. }
  245. //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_commit(const node_ptr&,const node_ptr&,const insert_commit_data&)
  246. template<class H_Alpha>
  247. static void insert_unique_commit
  248. (const node_ptr & header, const node_ptr & new_value, const insert_commit_data &commit_data
  249. ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
  250. {
  251. bstree_algo::insert_unique_commit(header, new_value, commit_data);
  252. rebalance_after_insertion(new_value, commit_data.depth, tree_size+1, h_alpha, max_tree_size);
  253. }
  254. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  255. //! @copydoc ::boost::intrusive::bstree_algorithms::is_header
  256. static bool is_header(const const_node_ptr & p);
  257. //! @copydoc ::boost::intrusive::bstree_algorithms::is_header
  258. static void rebalance(const node_ptr & header);
  259. //! @copydoc ::boost::intrusive::bstree_algorithms::rebalance_subtree
  260. static node_ptr rebalance_subtree(const node_ptr & old_root)
  261. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  262. /// @cond
  263. private:
  264. template<class H_Alpha>
  265. static void rebalance_after_insertion
  266. (const node_ptr &x, std::size_t depth
  267. , std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
  268. {
  269. if(tree_size > max_tree_size)
  270. max_tree_size = tree_size;
  271. if(tree_size > 2 && //Nothing to do with only the root
  272. //Check if the root node is unbalanced
  273. //Scapegoat paper depth counts root depth as zero and "depth" counts root as 1,
  274. //but since "depth" is the depth of the ancestor of x, i == depth
  275. depth > h_alpha(tree_size)){
  276. //Find the first non height-balanced node
  277. //as described in the section 4.2 of the paper.
  278. //This method is the alternative method described
  279. //in the paper. Authors claim that this method
  280. //may tend to yield more balanced trees on the average
  281. //than the weight balanced method.
  282. node_ptr s = x;
  283. std::size_t size = 1;
  284. for(std::size_t ancestor = 1; ancestor != depth; ++ancestor){
  285. const node_ptr s_parent = NodeTraits::get_parent(s);
  286. const node_ptr s_parent_left = NodeTraits::get_left(s_parent);
  287. //Obtain parent's size (previous size + parent + sibling tree)
  288. const node_ptr s_sibling = s_parent_left == s ? NodeTraits::get_right(s_parent) : s_parent_left;
  289. size += 1 + bstree_algo::subtree_size(s_sibling);
  290. s = s_parent;
  291. if(ancestor > h_alpha(size)){ //is 's' scapegoat?
  292. bstree_algo::rebalance_subtree(s);
  293. return;
  294. }
  295. }
  296. //The whole tree must be rebuilt
  297. max_tree_size = tree_size;
  298. bstree_algo::rebalance_subtree(NodeTraits::get_parent(s));
  299. }
  300. }
  301. /// @endcond
  302. };
  303. /// @cond
  304. template<class NodeTraits>
  305. struct get_algo<SgTreeAlgorithms, NodeTraits>
  306. {
  307. typedef sgtree_algorithms<NodeTraits> type;
  308. };
  309. template <class ValueTraits, class NodePtrCompare, class ExtraChecker>
  310. struct get_node_checker<SgTreeAlgorithms, ValueTraits, NodePtrCompare, ExtraChecker>
  311. {
  312. typedef detail::bstree_node_checker<ValueTraits, NodePtrCompare, ExtraChecker> type;
  313. };
  314. /// @endcond
  315. } //namespace intrusive
  316. } //namespace boost
  317. #include <boost/intrusive/detail/config_end.hpp>
  318. #endif //BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP