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