sgtree_algorithms.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2007-2013
  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 <cstddef>
  21. #include <boost/intrusive/intrusive_fwd.hpp>
  22. #include <boost/intrusive/detail/assert.hpp>
  23. #include <boost/intrusive/detail/utilities.hpp>
  24. #include <boost/intrusive/bstree_algorithms.hpp>
  25. #include <boost/intrusive/pointer_traits.hpp>
  26. namespace boost {
  27. namespace intrusive {
  28. //! sgtree_algorithms is configured with a NodeTraits class, which encapsulates the
  29. //! information about the node to be manipulated. NodeTraits must support the
  30. //! following interface:
  31. //!
  32. //! <b>Typedefs</b>:
  33. //!
  34. //! <tt>node</tt>: The type of the node that forms the binary search tree
  35. //!
  36. //! <tt>node_ptr</tt>: A pointer to a node
  37. //!
  38. //! <tt>const_node_ptr</tt>: A pointer to a const node
  39. //!
  40. //! <b>Static functions</b>:
  41. //!
  42. //! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
  43. //!
  44. //! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
  45. //!
  46. //! <tt>static node_ptr get_left(const_node_ptr n);</tt>
  47. //!
  48. //! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
  49. //!
  50. //! <tt>static node_ptr get_right(const_node_ptr n);</tt>
  51. //!
  52. //! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
  53. template<class NodeTraits>
  54. class sgtree_algorithms
  55. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  56. : public bstree_algorithms<NodeTraits>
  57. #endif
  58. {
  59. public:
  60. typedef typename NodeTraits::node node;
  61. typedef NodeTraits node_traits;
  62. typedef typename NodeTraits::node_ptr node_ptr;
  63. typedef typename NodeTraits::const_node_ptr const_node_ptr;
  64. /// @cond
  65. private:
  66. typedef bstree_algorithms<NodeTraits> bstree_algo;
  67. /// @endcond
  68. public:
  69. //! This type is the information that will be
  70. //! filled by insert_unique_check
  71. struct insert_commit_data
  72. : bstree_algo::insert_commit_data
  73. {
  74. std::size_t depth;
  75. };
  76. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  77. //! @copydoc ::boost::intrusive::bstree_algorithms::get_header(const const_node_ptr&)
  78. static node_ptr get_header(const const_node_ptr & n);
  79. //! @copydoc ::boost::intrusive::bstree_algorithms::begin_node
  80. static node_ptr begin_node(const const_node_ptr & header);
  81. //! @copydoc ::boost::intrusive::bstree_algorithms::end_node
  82. static node_ptr end_node(const const_node_ptr & header);
  83. //! @copydoc ::boost::intrusive::bstree_algorithms::swap_tree
  84. static void swap_tree(const node_ptr & header1, const node_ptr & header2);
  85. //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&)
  86. static void swap_nodes(const node_ptr & node1, const node_ptr & node2);
  87. //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&,const node_ptr&,const node_ptr&)
  88. static void swap_nodes(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2);
  89. //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&)
  90. static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & new_node);
  91. //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&,const node_ptr&)
  92. static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node);
  93. //Unlink is not possible since tree metadata is needed to update the tree
  94. //!static void unlink(const node_ptr & node);
  95. //! @copydoc ::boost::intrusive::bstree_algorithms::unlink_leftmost_without_rebalance
  96. static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header);
  97. //! @copydoc ::boost::intrusive::bstree_algorithms::unique(const const_node_ptr&)
  98. static bool unique(const const_node_ptr & node);
  99. //! @copydoc ::boost::intrusive::bstree_algorithms::size(const const_node_ptr&)
  100. static std::size_t size(const const_node_ptr & header);
  101. //! @copydoc ::boost::intrusive::bstree_algorithms::next_node(const node_ptr&)
  102. static node_ptr next_node(const node_ptr & node);
  103. //! @copydoc ::boost::intrusive::bstree_algorithms::prev_node(const node_ptr&)
  104. static node_ptr prev_node(const node_ptr & node);
  105. //! @copydoc ::boost::intrusive::bstree_algorithms::init(const node_ptr&)
  106. static void init(const node_ptr & node);
  107. //! @copydoc ::boost::intrusive::bstree_algorithms::init_header(const node_ptr&)
  108. static void init_header(const node_ptr & header);
  109. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  110. //! @copydoc ::boost::intrusive::bstree_algorithms::erase(const node_ptr&,const node_ptr&)
  111. template<class AlphaByMaxSize>
  112. 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)
  113. {
  114. //typename bstree_algo::data_for_rebalance info;
  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&,const node_ptr&,Cloner,Disposer)
  126. template <class Cloner, class Disposer>
  127. static void clone
  128. (const const_node_ptr & source_header, const 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(const 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 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 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 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 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 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 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(const node_ptr&,const node_ptr&,NodePtrCompare)
  158. template<class NodePtrCompare, class H_Alpha>
  159. static node_ptr insert_equal_upper_bound
  160. (const node_ptr & h, const 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(const node_ptr&,const node_ptr&,NodePtrCompare)
  169. template<class NodePtrCompare, class H_Alpha>
  170. static node_ptr insert_equal_lower_bound
  171. (const node_ptr & h, const 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(const node_ptr&,const node_ptr&,const node_ptr&,NodePtrCompare)
  180. template<class NodePtrCompare, class H_Alpha>
  181. static node_ptr insert_equal
  182. (const node_ptr & header, const node_ptr & hint, const 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(const node_ptr&,const node_ptr&,const node_ptr&)
  191. template<class H_Alpha>
  192. static node_ptr insert_before
  193. (const node_ptr & header, const node_ptr & pos, const 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(const node_ptr&,const node_ptr&)
  202. template<class H_Alpha>
  203. static void push_back(const node_ptr & header, const 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(const node_ptr&,const node_ptr&)
  211. template<class H_Alpha>
  212. static void push_front(const node_ptr & header, const 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 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 const_node_ptr & header, const 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(const node_ptr&,const node_ptr&,const insert_commit_data&)
  245. template<class H_Alpha>
  246. static void insert_unique_commit
  247. (const node_ptr & header, const 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. {
  250. bstree_algo::insert_unique_commit(header, new_value, commit_data);
  251. rebalance_after_insertion(new_value, commit_data.depth, tree_size+1, h_alpha, max_tree_size);
  252. }
  253. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  254. //! @copydoc ::boost::intrusive::bstree_algorithms::is_header
  255. static bool is_header(const const_node_ptr & p);
  256. //! @copydoc ::boost::intrusive::bstree_algorithms::is_header
  257. static void rebalance(const node_ptr & header);
  258. //! @copydoc ::boost::intrusive::bstree_algorithms::rebalance_subtree
  259. static node_ptr rebalance_subtree(const node_ptr & old_root)
  260. #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  261. /// @cond
  262. private:
  263. template<class H_Alpha>
  264. static void rebalance_after_insertion
  265. (const node_ptr &x, std::size_t depth
  266. , std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
  267. {
  268. if(tree_size > max_tree_size)
  269. max_tree_size = tree_size;
  270. if(tree_size > 2 && //Nothing to do with only the root
  271. //Check if the root node is unbalanced
  272. //Scapegoat paper depth counts root depth as zero and "depth" counts root as 1,
  273. //but since "depth" is the depth of the ancestor of x, i == depth
  274. depth > h_alpha(tree_size)){
  275. //Find the first non height-balanced node
  276. //as described in the section 4.2 of the paper.
  277. //This method is the alternative method described
  278. //in the paper. Authors claim that this method
  279. //may tend to yield more balanced trees on the average
  280. //than the weight balanced method.
  281. node_ptr s = x;
  282. std::size_t size = 1;
  283. for(std::size_t ancestor = 1; true; ++ancestor){
  284. if(ancestor == depth){ //Check if whole tree must be rebuilt
  285. max_tree_size = tree_size;
  286. bstree_algo::rebalance_subtree(NodeTraits::get_parent(s));
  287. break;
  288. }
  289. else{ //Go to the next scapegoat candidate
  290. const node_ptr s_parent = NodeTraits::get_parent(s);
  291. const node_ptr s_parent_left = NodeTraits::get_left(s_parent);
  292. //Obtain parent's size (previous size + parent + sibling tree)
  293. const node_ptr s_sibling = s_parent_left == s ? NodeTraits::get_right(s_parent) : s_parent_left;
  294. size += 1 + bstree_algo::subtree_size(s_sibling);
  295. s = s_parent;
  296. if(ancestor > h_alpha(size)){ //is 's' scapegoat?
  297. bstree_algo::rebalance_subtree(s);
  298. break;
  299. }
  300. }
  301. }
  302. }
  303. }
  304. /// @endcond
  305. };
  306. /// @cond
  307. template<class NodeTraits>
  308. struct get_algo<SgTreeAlgorithms, NodeTraits>
  309. {
  310. typedef sgtree_algorithms<NodeTraits> type;
  311. };
  312. /// @endcond
  313. } //namespace intrusive
  314. } //namespace boost
  315. #include <boost/intrusive/detail/config_end.hpp>
  316. #endif //BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP