Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / intrusive / sgtree_algorithms.hpp
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
20 #if defined(_MSC_VER)
21 #  pragma once
22 #endif
23
24 #include <boost/intrusive/detail/config_begin.hpp>
25 #include <boost/intrusive/intrusive_fwd.hpp>
26
27 #include <cstddef>
28 #include <boost/intrusive/detail/algo_type.hpp>
29 #include <boost/intrusive/bstree_algorithms.hpp>
30
31
32
33 namespace boost {
34 namespace intrusive {
35
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:
39 //!
40 //! <b>Typedefs</b>:
41 //!
42 //! <tt>node</tt>: The type of the node that forms the binary search tree
43 //!
44 //! <tt>node_ptr</tt>: A pointer to a node
45 //!
46 //! <tt>const_node_ptr</tt>: A pointer to a const node
47 //!
48 //! <b>Static functions</b>:
49 //!
50 //! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
51 //!
52 //! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
53 //!
54 //! <tt>static node_ptr get_left(const_node_ptr n);</tt>
55 //!
56 //! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
57 //!
58 //! <tt>static node_ptr get_right(const_node_ptr n);</tt>
59 //!
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>
65    #endif
66 {
67    public:
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;
72
73    /// @cond
74    private:
75
76    typedef bstree_algorithms<NodeTraits>  bstree_algo;
77
78    /// @endcond
79
80    public:
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
85    {
86       std::size_t depth;
87    };
88
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);
92
93    //! @copydoc ::boost::intrusive::bstree_algorithms::begin_node
94    static node_ptr begin_node(const const_node_ptr & header);
95
96    //! @copydoc ::boost::intrusive::bstree_algorithms::end_node
97    static node_ptr end_node(const const_node_ptr & header);
98
99    //! @copydoc ::boost::intrusive::bstree_algorithms::swap_tree
100    static void swap_tree(const node_ptr & header1, const node_ptr & header2);
101
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);
104
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);
107
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);
110
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);
113
114    //Unlink is not possible since tree metadata is needed to update the tree
115    //!static void unlink(const node_ptr & node);
116
117    //! @copydoc ::boost::intrusive::bstree_algorithms::unlink_leftmost_without_rebalance
118    static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header);
119
120    //! @copydoc ::boost::intrusive::bstree_algorithms::unique(const const_node_ptr&)
121    static bool unique(const const_node_ptr & node);
122
123    //! @copydoc ::boost::intrusive::bstree_algorithms::size(const const_node_ptr&)
124    static std::size_t size(const const_node_ptr & header);
125
126    //! @copydoc ::boost::intrusive::bstree_algorithms::next_node(const node_ptr&)
127    static node_ptr next_node(const node_ptr & node);
128
129    //! @copydoc ::boost::intrusive::bstree_algorithms::prev_node(const node_ptr&)
130    static node_ptr prev_node(const node_ptr & node);
131
132    //! @copydoc ::boost::intrusive::bstree_algorithms::init(const node_ptr&)
133    static void init(const node_ptr & node);
134
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
138
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)
142    {
143       //typename bstree_algo::data_for_rebalance info;
144       bstree_algo::erase(header, z);
145       --tree_size;
146       if (tree_size > 0 &&
147           tree_size < alpha_by_maxsize(max_tree_size)){
148          bstree_algo::rebalance(header);
149          max_tree_size = tree_size;
150       }
151       return z;
152    }
153
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>
157    static void clone
158       (const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer);
159
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);
163
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);
168
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);
173
174    //! @copydoc ::boost::intrusive::bstree_algorithms::find(const const_node_ptr&, const KeyType&,KeyNodePtrCompare)
175    template<class KeyType, class KeyNodePtrCompare>
176    static node_ptr find
177       (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp);
178
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);
183
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);
189
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);
193
194    #endif   //#ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
195
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)
201    {
202       std::size_t depth;
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);
205       return new_node;
206    }
207
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)
213    {
214       std::size_t depth;
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);
217       return new_node;
218    }
219
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)
225    {
226       std::size_t depth;
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);
229       return new_node;
230    }
231
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)
237    {
238       std::size_t depth;
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);
241       return new_node;
242    }
243
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)
248    {
249       std::size_t depth;
250       bstree_algo::push_back(header, new_node, &depth);
251       rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
252    }
253
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)
258    {
259       std::size_t depth;
260       bstree_algo::push_front(header, new_node, &depth);
261       rebalance_after_insertion(new_node, depth, tree_size+1, h_alpha, max_tree_size);
262    }
263
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)
269    {
270       std::size_t depth;
271       std::pair<node_ptr, bool> ret =
272          bstree_algo::insert_unique_check(header, key, comp, commit_data, &depth);
273       commit_data.depth = depth;
274       return ret;
275    }
276
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)
282    {
283       std::size_t depth;
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;
288       return ret;
289    }
290
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)
296    {
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);
299    }
300
301    #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
302    //! @copydoc ::boost::intrusive::bstree_algorithms::is_header
303    static bool is_header(const const_node_ptr & p);
304
305    //! @copydoc ::boost::intrusive::bstree_algorithms::is_header
306    static void rebalance(const node_ptr & header);
307
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
311
312    /// @cond
313    private:
314
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)
319    {
320       if(tree_size > max_tree_size)
321          max_tree_size = tree_size;
322
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)){
328                                           
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.
335          node_ptr s = x;
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);
343             s = s_parent;
344             if(ancestor > h_alpha(size)){ //is 's' scapegoat?
345                bstree_algo::rebalance_subtree(s);
346                return;
347             }
348          }
349          //The whole tree must be rebuilt
350          max_tree_size = tree_size;
351          bstree_algo::rebalance_subtree(NodeTraits::get_parent(s));
352       }
353    }
354    /// @endcond
355 };
356
357 /// @cond
358
359 template<class NodeTraits>
360 struct get_algo<SgTreeAlgorithms, NodeTraits>
361 {
362    typedef sgtree_algorithms<NodeTraits> type;
363 };
364
365 template <class ValueTraits, class NodePtrCompare, class ExtraChecker>
366 struct get_node_checker<SgTreeAlgorithms, ValueTraits, NodePtrCompare, ExtraChecker>
367 {
368    typedef detail::bstree_node_checker<ValueTraits, NodePtrCompare, ExtraChecker> type;
369 };
370
371 /// @endcond
372
373 } //namespace intrusive
374 } //namespace boost
375
376 #include <boost/intrusive/detail/config_end.hpp>
377
378 #endif //BOOST_INTRUSIVE_SGTREE_ALGORITHMS_HPP