1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2007-2014
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)
9 // See http://www.boost.org/libs/intrusive for documentation.
11 /////////////////////////////////////////////////////////////////////////////
13 // Scapegoat tree algorithms are taken from the paper titled:
14 // "Scapegoat Trees" by Igal Galperin Ronald L. Rivest.
16 /////////////////////////////////////////////////////////////////////////////
17 #ifndef BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP
18 #define BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP
24 #include <boost/intrusive/detail/config_begin.hpp>
25 #include <boost/intrusive/intrusive_fwd.hpp>
28 #include <boost/intrusive/detail/algo_type.hpp>
29 #include <boost/intrusive/bstree_algorithms.hpp>
36 //! sgtree_algorithms is configured with a NodeTraits class, which encapsulates the
37 //! information about the node to be manipulated. NodeTraits must support the
38 //! following interface:
42 //! <tt>node</tt>: The type of the node that forms the binary search tree
44 //! <tt>node_ptr</tt>: A pointer to a node
46 //! <tt>const_node_ptr</tt>: A pointer to a const node
48 //! <b>Static functions</b>:
50 //! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
52 //! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
54 //! <tt>static node_ptr get_left(const_node_ptr n);</tt>
56 //! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
58 //! <tt>static node_ptr get_right(const_node_ptr n);</tt>
60 //! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
61 template<class NodeTraits>
62 class sgtree_algorithms
63 #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
64 : public bstree_algorithms<NodeTraits>
68 typedef typename NodeTraits::node node;
69 typedef NodeTraits node_traits;
70 typedef typename NodeTraits::node_ptr node_ptr;
71 typedef typename NodeTraits::const_node_ptr const_node_ptr;
76 typedef bstree_algorithms<NodeTraits> bstree_algo;
81 //! This type is the information that will be
82 //! filled by insert_unique_check
83 struct insert_commit_data
84 : bstree_algo::insert_commit_data
89 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
90 //! @copydoc ::boost::intrusive::bstree_algorithms::get_header(const const_node_ptr&)
91 static node_ptr get_header(const const_node_ptr & n);
93 //! @copydoc ::boost::intrusive::bstree_algorithms::begin_node
94 static node_ptr begin_node(const const_node_ptr & header);
96 //! @copydoc ::boost::intrusive::bstree_algorithms::end_node
97 static node_ptr end_node(const const_node_ptr & header);
99 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_tree
100 static void swap_tree(const node_ptr & header1, const node_ptr & header2);
102 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&)
103 static void swap_nodes(const node_ptr & node1, const node_ptr & node2);
105 //! @copydoc ::boost::intrusive::bstree_algorithms::swap_nodes(const node_ptr&,const node_ptr&,const node_ptr&,const node_ptr&)
106 static void swap_nodes(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2);
108 //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&)
109 static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & new_node);
111 //! @copydoc ::boost::intrusive::bstree_algorithms::replace_node(const node_ptr&,const node_ptr&,const node_ptr&)
112 static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node);
114 //Unlink is not possible since tree metadata is needed to update the tree
115 //!static void unlink(const node_ptr & node);
117 //! @copydoc ::boost::intrusive::bstree_algorithms::unlink_leftmost_without_rebalance
118 static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header);
120 //! @copydoc ::boost::intrusive::bstree_algorithms::unique(const const_node_ptr&)
121 static bool unique(const const_node_ptr & node);
123 //! @copydoc ::boost::intrusive::bstree_algorithms::size(const const_node_ptr&)
124 static std::size_t size(const const_node_ptr & header);
126 //! @copydoc ::boost::intrusive::bstree_algorithms::next_node(const node_ptr&)
127 static node_ptr next_node(const node_ptr & node);
129 //! @copydoc ::boost::intrusive::bstree_algorithms::prev_node(const node_ptr&)
130 static node_ptr prev_node(const node_ptr & node);
132 //! @copydoc ::boost::intrusive::bstree_algorithms::init(const node_ptr&)
133 static void init(const node_ptr & node);
135 //! @copydoc ::boost::intrusive::bstree_algorithms::init_header(const node_ptr&)
136 static void init_header(const node_ptr & header);
137 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
139 //! @copydoc ::boost::intrusive::bstree_algorithms::erase(const node_ptr&,const node_ptr&)
140 template<class AlphaByMaxSize>
141 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)
143 //typename bstree_algo::data_for_rebalance info;
144 bstree_algo::erase(header, z);
147 tree_size < alpha_by_maxsize(max_tree_size)){
148 bstree_algo::rebalance(header);
149 max_tree_size = tree_size;
154 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
155 //! @copydoc ::boost::intrusive::bstree_algorithms::clone(const const_node_ptr&,const node_ptr&,Cloner,Disposer)
156 template <class Cloner, class Disposer>
158 (const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer);
160 //! @copydoc ::boost::intrusive::bstree_algorithms::clear_and_dispose(const node_ptr&,Disposer)
161 template<class Disposer>
162 static void clear_and_dispose(const node_ptr & header, Disposer disposer);
164 //! @copydoc ::boost::intrusive::bstree_algorithms::lower_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
165 template<class KeyType, class KeyNodePtrCompare>
166 static node_ptr lower_bound
167 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
169 //! @copydoc ::boost::intrusive::bstree_algorithms::upper_bound(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
170 template<class KeyType, class KeyNodePtrCompare>
171 static node_ptr upper_bound
172 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
174 //! @copydoc ::boost::intrusive::bstree_algorithms::find(const const_node_ptr&, const KeyType&,KeyNodePtrCompare)
175 template<class KeyType, class KeyNodePtrCompare>
177 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
179 //! @copydoc ::boost::intrusive::bstree_algorithms::equal_range(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
180 template<class KeyType, class KeyNodePtrCompare>
181 static std::pair<node_ptr, node_ptr> equal_range
182 (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
184 //! @copydoc ::boost::intrusive::bstree_algorithms::bounded_range(const const_node_ptr&,const KeyType&,const KeyType&,KeyNodePtrCompare,bool,bool)
185 template<class KeyType, class KeyNodePtrCompare>
186 static std::pair<node_ptr, node_ptr> bounded_range
187 (const const_node_ptr & header, const KeyType &lower_key, const KeyType &upper_key, KeyNodePtrCompare comp
188 , bool left_closed, bool right_closed);
190 //! @copydoc ::boost::intrusive::bstree_algorithms::count(const const_node_ptr&,const KeyType&,KeyNodePtrCompare)
191 template<class KeyType, class KeyNodePtrCompare>
192 static std::size_t count(const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
194 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
196 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_upper_bound(const node_ptr&,const node_ptr&,NodePtrCompare)
197 template<class NodePtrCompare, class H_Alpha>
198 static node_ptr insert_equal_upper_bound
199 (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp
200 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
203 bstree_algo::insert_equal_upper_bound(h, new_node, comp, &depth);
204 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
208 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal_lower_bound(const node_ptr&,const node_ptr&,NodePtrCompare)
209 template<class NodePtrCompare, class H_Alpha>
210 static node_ptr insert_equal_lower_bound
211 (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp
212 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
215 bstree_algo::insert_equal_lower_bound(h, new_node, comp, &depth);
216 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
220 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_equal(const node_ptr&,const node_ptr&,const node_ptr&,NodePtrCompare)
221 template<class NodePtrCompare, class H_Alpha>
222 static node_ptr insert_equal
223 (const node_ptr & header, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp
224 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
227 bstree_algo::insert_equal(header, hint, new_node, comp, &depth);
228 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
232 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_before(const node_ptr&,const node_ptr&,const node_ptr&)
233 template<class H_Alpha>
234 static node_ptr insert_before
235 (const node_ptr & header, const node_ptr & pos, const node_ptr & new_node
236 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
239 bstree_algo::insert_before(header, pos, new_node, &depth);
240 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
244 //! @copydoc ::boost::intrusive::bstree_algorithms::push_back(const node_ptr&,const node_ptr&)
245 template<class H_Alpha>
246 static void push_back(const node_ptr & header, const node_ptr & new_node
247 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
250 bstree_algo::push_back(header, new_node, &depth);
251 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
254 //! @copydoc ::boost::intrusive::bstree_algorithms::push_front(const node_ptr&,const node_ptr&)
255 template<class H_Alpha>
256 static void push_front(const node_ptr & header, const node_ptr & new_node
257 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
260 bstree_algo::push_front(header, new_node, &depth);
261 rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
264 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
265 template<class KeyType, class KeyNodePtrCompare>
266 static std::pair<node_ptr, bool> insert_unique_check
267 (const const_node_ptr & header, const KeyType &key
268 ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
271 std::pair<node_ptr, bool> ret =
272 bstree_algo::insert_unique_check(header, key, comp, commit_data, &depth);
273 commit_data.depth = depth;
277 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_check(const const_node_ptr&,const node_ptr&,const KeyType&,KeyNodePtrCompare,insert_commit_data&)
278 template<class KeyType, class KeyNodePtrCompare>
279 static std::pair<node_ptr, bool> insert_unique_check
280 (const const_node_ptr & header, const node_ptr &hint, const KeyType &key
281 ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
284 std::pair<node_ptr, bool> ret =
285 bstree_algo::insert_unique_check
286 (header, hint, key, comp, commit_data, &depth);
287 commit_data.depth = depth;
291 //! @copydoc ::boost::intrusive::bstree_algorithms::insert_unique_commit(const node_ptr&,const node_ptr&,const insert_commit_data&)
292 template<class H_Alpha>
293 static void insert_unique_commit
294 (const node_ptr & header, const node_ptr & new_value, const insert_commit_data &commit_data
295 ,std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
297 bstree_algo::insert_unique_commit(header, new_value, commit_data);
298 rebalance_after_insertion(new_value, commit_data.depth, tree_size+1, h_alpha, max_tree_size);
301 #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
302 //! @copydoc ::boost::intrusive::bstree_algorithms::is_header
303 static bool is_header(const const_node_ptr & p);
305 //! @copydoc ::boost::intrusive::bstree_algorithms::is_header
306 static void rebalance(const node_ptr & header);
308 //! @copydoc ::boost::intrusive::bstree_algorithms::rebalance_subtree
309 static node_ptr rebalance_subtree(const node_ptr & old_root)
310 #endif //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
315 template<class H_Alpha>
316 static void rebalance_after_insertion
317 (const node_ptr &x, std::size_t depth
318 , std::size_t tree_size, H_Alpha h_alpha, std::size_t &max_tree_size)
320 if(tree_size > max_tree_size)
321 max_tree_size = tree_size;
323 if(tree_size > 2 && //Nothing to do with only the root
324 //Check if the root node is unbalanced
325 //Scapegoat paper depth counts root depth as zero and "depth" counts root as 1,
326 //but since "depth" is the depth of the ancestor of x, i == depth
327 depth > h_alpha(tree_size)){
329 //Find the first non height-balanced node
330 //as described in the section 4.2 of the paper.
331 //This method is the alternative method described
332 //in the paper. Authors claim that this method
333 //may tend to yield more balanced trees on the average
334 //than the weight balanced method.
336 std::size_t size = 1;
337 for(std::size_t ancestor = 1; ancestor != depth; ++ancestor){
338 const node_ptr s_parent = NodeTraits::get_parent(s);
339 const node_ptr s_parent_left = NodeTraits::get_left(s_parent);
340 //Obtain parent's size (previous size + parent + sibling tree)
341 const node_ptr s_sibling = s_parent_left == s ? NodeTraits::get_right(s_parent) : s_parent_left;
342 size += 1 + bstree_algo::subtree_size(s_sibling);
344 if(ancestor > h_alpha(size)){ //is 's' scapegoat?
345 bstree_algo::rebalance_subtree(s);
349 //The whole tree must be rebuilt
350 max_tree_size = tree_size;
351 bstree_algo::rebalance_subtree(NodeTraits::get_parent(s));
359 template<class NodeTraits>
360 struct get_algo<SgTreeAlgorithms, NodeTraits>
362 typedef sgtree_algorithms<NodeTraits> type;
365 template <class ValueTraits, class NodePtrCompare, class ExtraChecker>
366 struct get_node_checker<SgTreeAlgorithms, ValueTraits, NodePtrCompare, ExtraChecker>
368 typedef detail::bstree_node_checker<ValueTraits, NodePtrCompare, ExtraChecker> type;
373 } //namespace intrusive
376 #include <boost/intrusive/detail/config_end.hpp>
378 #endif //BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP