~ubuntu-branches/debian/sid/boost1.49/sid

« back to all changes in this revision

Viewing changes to boost/intrusive/avltree_algorithms.hpp

  • Committer: Package Import Robot
  • Author(s): Steve M. Robbins
  • Date: 2012-02-26 00:31:44 UTC
  • Revision ID: package-import@ubuntu.com-20120226003144-eaytp12cbf6ubpms
Tags: upstream-1.49.0
ImportĀ upstreamĀ versionĀ 1.49.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/////////////////////////////////////////////////////////////////////////////
 
2
//
 
3
// (C) Copyright Daniel K. O. 2005.
 
4
// (C) Copyright Ion Gaztanaga 2007.
 
5
//
 
6
// Distributed under the Boost Software License, Version 1.0.
 
7
//    (See accompanying file LICENSE_1_0.txt or copy at
 
8
//          http://www.boost.org/LICENSE_1_0.txt)
 
9
//
 
10
// See http://www.boost.org/libs/intrusive for documentation.
 
11
//
 
12
/////////////////////////////////////////////////////////////////////////////
 
13
 
 
14
#ifndef BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP
 
15
#define BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP
 
16
 
 
17
#include <boost/intrusive/detail/config_begin.hpp>
 
18
 
 
19
#include <cstddef>
 
20
#include <boost/intrusive/intrusive_fwd.hpp>
 
21
 
 
22
#include <boost/intrusive/detail/assert.hpp>
 
23
#include <boost/intrusive/detail/utilities.hpp>
 
24
#include <boost/intrusive/detail/tree_algorithms.hpp>
 
25
#include <boost/intrusive/pointer_traits.hpp>
 
26
 
 
27
 
 
28
namespace boost {
 
29
namespace intrusive {
 
30
 
 
31
//! avltree_algorithms is configured with a NodeTraits class, which encapsulates the
 
32
//! information about the node to be manipulated. NodeTraits must support the
 
33
//! following interface:
 
34
//!
 
35
//! <b>Typedefs</b>:
 
36
//!
 
37
//! <tt>node</tt>: The type of the node that forms the circular list
 
38
//!
 
39
//! <tt>node_ptr</tt>: A pointer to a node
 
40
//!
 
41
//! <tt>const_node_ptr</tt>: A pointer to a const node
 
42
//!
 
43
//! <tt>balance</tt>: The type of the balance factor
 
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
//! 
 
59
//! <tt>static balance get_balance(const_node_ptr n);</tt>
 
60
//! 
 
61
//! <tt>static void set_balance(node_ptr n, balance b);</tt>
 
62
//! 
 
63
//! <tt>static balance negative();</tt>
 
64
//! 
 
65
//! <tt>static balance zero();</tt>
 
66
//! 
 
67
//! <tt>static balance positive();</tt>
 
68
template<class NodeTraits>
 
69
class avltree_algorithms
 
70
{
 
71
   public:
 
72
   typedef typename NodeTraits::node            node;
 
73
   typedef NodeTraits                           node_traits;
 
74
   typedef typename NodeTraits::node_ptr        node_ptr;
 
75
   typedef typename NodeTraits::const_node_ptr  const_node_ptr;
 
76
   typedef typename NodeTraits::balance         balance;
 
77
 
 
78
   /// @cond
 
79
   private:
 
80
   typedef detail::tree_algorithms<NodeTraits>  tree_algorithms;
 
81
 
 
82
   template<class F>
 
83
   struct avltree_node_cloner
 
84
      :  private detail::ebo_functor_holder<F>
 
85
   {
 
86
      typedef detail::ebo_functor_holder<F>                 base_t;
 
87
 
 
88
      avltree_node_cloner(F f)
 
89
         :  base_t(f)
 
90
      {}
 
91
      
 
92
      node_ptr operator()(const node_ptr &p)
 
93
      {
 
94
         node_ptr n = base_t::get()(p);
 
95
         NodeTraits::set_balance(n, NodeTraits::get_balance(p));
 
96
         return n;
 
97
      }
 
98
   };
 
99
 
 
100
   struct avltree_erase_fixup
 
101
   {
 
102
      void operator()(const node_ptr &to_erase, const node_ptr &successor)
 
103
      {  NodeTraits::set_balance(successor, NodeTraits::get_balance(to_erase));  }
 
104
   };
 
105
 
 
106
   static node_ptr uncast(const const_node_ptr & ptr)
 
107
   {  return pointer_traits<node_ptr>::const_cast_from(ptr);  }
 
108
   /// @endcond
 
109
 
 
110
   public:
 
111
   static node_ptr begin_node(const const_node_ptr & header)
 
112
   {  return tree_algorithms::begin_node(header);   }
 
113
 
 
114
   static node_ptr end_node(const const_node_ptr & header)
 
115
   {  return tree_algorithms::end_node(header);   }
 
116
 
 
117
   //! This type is the information that will be
 
118
   //! filled by insert_unique_check
 
119
   typedef typename tree_algorithms::insert_commit_data insert_commit_data;
 
120
 
 
121
   //! <b>Requires</b>: header1 and header2 must be the header nodes
 
122
   //!  of two trees.
 
123
   //! 
 
124
   //! <b>Effects</b>: Swaps two trees. After the function header1 will contain 
 
125
   //!   links to the second tree and header2 will have links to the first tree.
 
126
   //! 
 
127
   //! <b>Complexity</b>: Constant. 
 
128
   //! 
 
129
   //! <b>Throws</b>: Nothing.
 
130
   static void swap_tree(const node_ptr & header1, const node_ptr & header2)
 
131
   {  return tree_algorithms::swap_tree(header1, header2);  }
 
132
 
 
133
   //! <b>Requires</b>: node1 and node2 can't be header nodes
 
134
   //!  of two trees.
 
135
   //! 
 
136
   //! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
 
137
   //!   in the position node2 before the function. node2 will be inserted in the
 
138
   //!   position node1 had before the function.
 
139
   //! 
 
140
   //! <b>Complexity</b>: Logarithmic. 
 
141
   //! 
 
142
   //! <b>Throws</b>: Nothing.
 
143
   //! 
 
144
   //! <b>Note</b>: This function will break container ordering invariants if
 
145
   //!   node1 and node2 are not equivalent according to the ordering rules.
 
146
   //!
 
147
   //!Experimental function
 
148
   static void swap_nodes(const node_ptr & node1, const node_ptr & node2)
 
149
   {
 
150
      if(node1 == node2)
 
151
         return;
 
152
   
 
153
      node_ptr header1(tree_algorithms::get_header(node1)), header2(tree_algorithms::get_header(node2));
 
154
      swap_nodes(node1, header1, node2, header2);
 
155
   }
 
156
 
 
157
   //! <b>Requires</b>: node1 and node2 can't be header nodes
 
158
   //!  of two trees with header header1 and header2.
 
159
   //! 
 
160
   //! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
 
161
   //!   in the position node2 before the function. node2 will be inserted in the
 
162
   //!   position node1 had before the function.
 
163
   //! 
 
164
   //! <b>Complexity</b>: Constant. 
 
165
   //! 
 
166
   //! <b>Throws</b>: Nothing.
 
167
   //! 
 
168
   //! <b>Note</b>: This function will break container ordering invariants if
 
169
   //!   node1 and node2 are not equivalent according to the ordering rules.
 
170
   //!
 
171
   //!Experimental function
 
172
   static void swap_nodes(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2)
 
173
   {
 
174
      if(node1 == node2)   return;
 
175
 
 
176
      tree_algorithms::swap_nodes(node1, header1, node2, header2);
 
177
      //Swap balance
 
178
      balance c = NodeTraits::get_balance(node1);
 
179
      NodeTraits::set_balance(node1, NodeTraits::get_balance(node2)); 
 
180
      NodeTraits::set_balance(node2, c); 
 
181
   }
 
182
 
 
183
   //! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
 
184
   //!   and new_node must not be inserted in a tree.
 
185
   //! 
 
186
   //! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
 
187
   //!   tree with new_node. The tree does not need to be rebalanced
 
188
   //! 
 
189
   //! <b>Complexity</b>: Logarithmic. 
 
190
   //! 
 
191
   //! <b>Throws</b>: Nothing.
 
192
   //! 
 
193
   //! <b>Note</b>: This function will break container ordering invariants if
 
194
   //!   new_node is not equivalent to node_to_be_replaced according to the
 
195
   //!   ordering rules. This function is faster than erasing and inserting
 
196
   //!   the node, since no rebalancing and comparison is needed.
 
197
   //!
 
198
   //!Experimental function
 
199
   static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & new_node)
 
200
   {
 
201
      if(node_to_be_replaced == new_node)
 
202
         return;
 
203
      replace_node(node_to_be_replaced, tree_algorithms::get_header(node_to_be_replaced), new_node);
 
204
   }
 
205
 
 
206
   //! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
 
207
   //!   with header "header" and new_node must not be inserted in a tree.
 
208
   //! 
 
209
   //! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
 
210
   //!   tree with new_node. The tree does not need to be rebalanced
 
211
   //! 
 
212
   //! <b>Complexity</b>: Constant. 
 
213
   //! 
 
214
   //! <b>Throws</b>: Nothing.
 
215
   //! 
 
216
   //! <b>Note</b>: This function will break container ordering invariants if
 
217
   //!   new_node is not equivalent to node_to_be_replaced according to the
 
218
   //!   ordering rules. This function is faster than erasing and inserting
 
219
   //!   the node, since no rebalancing or comparison is needed.
 
220
   //!
 
221
   //!Experimental function
 
222
   static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node)
 
223
   {
 
224
      tree_algorithms::replace_node(node_to_be_replaced, header, new_node);
 
225
      NodeTraits::set_balance(new_node, NodeTraits::get_balance(node_to_be_replaced)); 
 
226
   }
 
227
 
 
228
   //! <b>Requires</b>: node is a tree node but not the header.
 
229
   //! 
 
230
   //! <b>Effects</b>: Unlinks the node and rebalances the tree.
 
231
   //! 
 
232
   //! <b>Complexity</b>: Average complexity is constant time.
 
233
   //! 
 
234
   //! <b>Throws</b>: Nothing.
 
235
   static void unlink(const node_ptr & node)
 
236
   {
 
237
      node_ptr x = NodeTraits::get_parent(node);
 
238
      if(x){
 
239
         while(!is_header(x))
 
240
            x = NodeTraits::get_parent(x);
 
241
         erase(x, node);
 
242
      }
 
243
   }
 
244
 
 
245
   //! <b>Requires</b>: header is the header of a tree.
 
246
   //! 
 
247
   //! <b>Effects</b>: Unlinks the leftmost node from the tree, and
 
248
   //!   updates the header link to the new leftmost node.
 
249
   //! 
 
250
   //! <b>Complexity</b>: Average complexity is constant time.
 
251
   //! 
 
252
   //! <b>Throws</b>: Nothing.
 
253
   //! 
 
254
   //! <b>Notes</b>: This function breaks the tree and the tree can
 
255
   //!   only be used for more unlink_leftmost_without_rebalance calls.
 
256
   //!   This function is normally used to achieve a step by step
 
257
   //!   controlled destruction of the tree.
 
258
   static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header)
 
259
   {  return tree_algorithms::unlink_leftmost_without_rebalance(header);   }
 
260
 
 
261
   //! <b>Requires</b>: node is a node of the tree or an node initialized
 
262
   //!   by init(...).
 
263
   //! 
 
264
   //! <b>Effects</b>: Returns true if the node is initialized by init().
 
265
   //! 
 
266
   //! <b>Complexity</b>: Constant time.
 
267
   //! 
 
268
   //! <b>Throws</b>: Nothing.
 
269
   static bool unique(const const_node_ptr & node)
 
270
   {  return tree_algorithms::unique(node);  }
 
271
 
 
272
   //! <b>Requires</b>: node is a node of the tree but it's not the header.
 
273
   //! 
 
274
   //! <b>Effects</b>: Returns the number of nodes of the subtree.
 
275
   //! 
 
276
   //! <b>Complexity</b>: Linear time.
 
277
   //! 
 
278
   //! <b>Throws</b>: Nothing.
 
279
   static std::size_t count(const const_node_ptr & node)
 
280
   {  return tree_algorithms::count(node);   }
 
281
 
 
282
   //! <b>Requires</b>: header is the header node of the tree.
 
283
   //! 
 
284
   //! <b>Effects</b>: Returns the number of nodes above the header.
 
285
   //! 
 
286
   //! <b>Complexity</b>: Linear time.
 
287
   //! 
 
288
   //! <b>Throws</b>: Nothing.
 
289
   static std::size_t size(const const_node_ptr & header)
 
290
   {  return tree_algorithms::size(header);   }
 
291
 
 
292
   //! <b>Requires</b>: p is a node from the tree except the header.
 
293
   //! 
 
294
   //! <b>Effects</b>: Returns the next node of the tree.
 
295
   //! 
 
296
   //! <b>Complexity</b>: Average constant time.
 
297
   //! 
 
298
   //! <b>Throws</b>: Nothing.
 
299
   static node_ptr next_node(const node_ptr & p)
 
300
   {  return tree_algorithms::next_node(p); }
 
301
 
 
302
   //! <b>Requires</b>: p is a node from the tree except the leftmost node.
 
303
   //! 
 
304
   //! <b>Effects</b>: Returns the previous node of the tree.
 
305
   //! 
 
306
   //! <b>Complexity</b>: Average constant time.
 
307
   //! 
 
308
   //! <b>Throws</b>: Nothing.
 
309
   static node_ptr prev_node(const node_ptr & p)
 
310
   {  return tree_algorithms::prev_node(p); }
 
311
 
 
312
   //! <b>Requires</b>: node must not be part of any tree.
 
313
   //!
 
314
   //! <b>Effects</b>: After the function unique(node) == true.
 
315
   //! 
 
316
   //! <b>Complexity</b>: Constant.
 
317
   //! 
 
318
   //! <b>Throws</b>: Nothing.
 
319
   //!
 
320
   //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
 
321
   static void init(const node_ptr & node)
 
322
   {  tree_algorithms::init(node);  }
 
323
 
 
324
   //! <b>Requires</b>: node must not be part of any tree.
 
325
   //!
 
326
   //! <b>Effects</b>: Initializes the header to represent an empty tree.
 
327
   //!   unique(header) == true.
 
328
   //! 
 
329
   //! <b>Complexity</b>: Constant.
 
330
   //! 
 
331
   //! <b>Throws</b>: Nothing.
 
332
   //!
 
333
   //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
 
334
   static void init_header(const node_ptr & header)
 
335
   {
 
336
      tree_algorithms::init_header(header);
 
337
      NodeTraits::set_balance(header, NodeTraits::zero()); 
 
338
   }
 
339
 
 
340
   //! <b>Requires</b>: header must be the header of a tree, z a node
 
341
   //!    of that tree and z != header.
 
342
   //!
 
343
   //! <b>Effects</b>: Erases node "z" from the tree with header "header".
 
344
   //! 
 
345
   //! <b>Complexity</b>: Amortized constant time.
 
346
   //! 
 
347
   //! <b>Throws</b>: Nothing.
 
348
   static node_ptr erase(const node_ptr & header, const node_ptr & z)
 
349
   {
 
350
      typename tree_algorithms::data_for_rebalance info;
 
351
      tree_algorithms::erase(header, z, avltree_erase_fixup(), info);
 
352
      node_ptr x = info.x;
 
353
      node_ptr x_parent = info.x_parent;
 
354
 
 
355
      //Rebalance avltree
 
356
      rebalance_after_erasure(header, x, x_parent);
 
357
      return z;
 
358
   }
 
359
 
 
360
   //! <b>Requires</b>: "cloner" must be a function
 
361
   //!   object taking a node_ptr and returning a new cloned node of it. "disposer" must
 
362
   //!   take a node_ptr and shouldn't throw.
 
363
   //!
 
364
   //! <b>Effects</b>: First empties target tree calling 
 
365
   //!   <tt>void disposer::operator()(const node_ptr &)</tt> for every node of the tree
 
366
   //!    except the header.
 
367
   //!    
 
368
   //!   Then, duplicates the entire tree pointed by "source_header" cloning each
 
369
   //!   source node with <tt>node_ptr Cloner::operator()(const node_ptr &)</tt> to obtain 
 
370
   //!   the nodes of the target tree. If "cloner" throws, the cloned target nodes
 
371
   //!   are disposed using <tt>void disposer(const node_ptr &)</tt>.
 
372
   //! 
 
373
   //! <b>Complexity</b>: Linear to the number of element of the source tree plus the.
 
374
   //!   number of elements of tree target tree when calling this function.
 
375
   //! 
 
376
   //! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
 
377
   template <class Cloner, class Disposer>
 
378
   static void clone
 
379
      (const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer)
 
380
   {
 
381
      avltree_node_cloner<Cloner> new_cloner(cloner);
 
382
      tree_algorithms::clone(source_header, target_header, new_cloner, disposer);
 
383
   }
 
384
 
 
385
   //! <b>Requires</b>: "disposer" must be an object function
 
386
   //!   taking a node_ptr parameter and shouldn't throw.
 
387
   //!
 
388
   //! <b>Effects</b>: Empties the target tree calling 
 
389
   //!   <tt>void disposer::operator()(const node_ptr &)</tt> for every node of the tree
 
390
   //!    except the header.
 
391
   //! 
 
392
   //! <b>Complexity</b>: Linear to the number of element of the source tree plus the.
 
393
   //!   number of elements of tree target tree when calling this function.
 
394
   //! 
 
395
   //! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
 
396
   template<class Disposer>
 
397
   static void clear_and_dispose(const node_ptr & header, Disposer disposer)
 
398
   {  tree_algorithms::clear_and_dispose(header, disposer); }
 
399
 
 
400
   //! <b>Requires</b>: "header" must be the header node of a tree.
 
401
   //!   KeyNodePtrCompare is a function object that induces a strict weak
 
402
   //!   ordering compatible with the strict weak ordering used to create the
 
403
   //!   the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
 
404
   //!
 
405
   //! <b>Effects</b>: Returns an node_ptr to the first element that is
 
406
   //!   not less than "key" according to "comp" or "header" if that element does
 
407
   //!   not exist.
 
408
   //!
 
409
   //! <b>Complexity</b>: Logarithmic.
 
410
   //! 
 
411
   //! <b>Throws</b>: If "comp" throws.
 
412
   template<class KeyType, class KeyNodePtrCompare>
 
413
   static node_ptr lower_bound
 
414
      (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
 
415
   {  return tree_algorithms::lower_bound(header, key, comp);  }
 
416
 
 
417
   //! <b>Requires</b>: "header" must be the header node of a tree.
 
418
   //!   KeyNodePtrCompare is a function object that induces a strict weak
 
419
   //!   ordering compatible with the strict weak ordering used to create the
 
420
   //!   the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
 
421
   //!
 
422
   //! <b>Effects</b>: Returns an node_ptr to the first element that is greater
 
423
   //!   than "key" according to "comp" or "header" if that element does not exist.
 
424
   //!
 
425
   //! <b>Complexity</b>: Logarithmic.
 
426
   //! 
 
427
   //! <b>Throws</b>: If "comp" throws.
 
428
   template<class KeyType, class KeyNodePtrCompare>
 
429
   static node_ptr upper_bound
 
430
      (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
 
431
   {  return tree_algorithms::upper_bound(header, key, comp);  }
 
432
 
 
433
   //! <b>Requires</b>: "header" must be the header node of a tree.
 
434
   //!   KeyNodePtrCompare is a function object that induces a strict weak
 
435
   //!   ordering compatible with the strict weak ordering used to create the
 
436
   //!   the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
 
437
   //!
 
438
   //! <b>Effects</b>: Returns an node_ptr to the element that is equivalent to
 
439
   //!   "key" according to "comp" or "header" if that element does not exist.
 
440
   //!
 
441
   //! <b>Complexity</b>: Logarithmic.
 
442
   //! 
 
443
   //! <b>Throws</b>: If "comp" throws.
 
444
   template<class KeyType, class KeyNodePtrCompare>
 
445
   static node_ptr find
 
446
      (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
 
447
   {  return tree_algorithms::find(header, key, comp);  }
 
448
 
 
449
   //! <b>Requires</b>: "header" must be the header node of a tree.
 
450
   //!   KeyNodePtrCompare is a function object that induces a strict weak
 
451
   //!   ordering compatible with the strict weak ordering used to create the
 
452
   //!   the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
 
453
   //!
 
454
   //! <b>Effects</b>: Returns an a pair of node_ptr delimiting a range containing
 
455
   //!   all elements that are equivalent to "key" according to "comp" or an
 
456
   //!   empty range that indicates the position where those elements would be
 
457
   //!   if they there are no equivalent elements.
 
458
   //!
 
459
   //! <b>Complexity</b>: Logarithmic.
 
460
   //! 
 
461
   //! <b>Throws</b>: If "comp" throws.
 
462
   template<class KeyType, class KeyNodePtrCompare>
 
463
   static std::pair<node_ptr, node_ptr> equal_range
 
464
      (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
 
465
   {  return tree_algorithms::equal_range(header, key, comp);  }
 
466
 
 
467
   //! <b>Requires</b>: "h" must be the header node of a tree.
 
468
   //!   NodePtrCompare is a function object that induces a strict weak
 
469
   //!   ordering compatible with the strict weak ordering used to create the
 
470
   //!   the tree. NodePtrCompare compares two node_ptrs.
 
471
   //!
 
472
   //! <b>Effects</b>: Inserts new_node into the tree before the upper bound
 
473
   //!   according to "comp".
 
474
   //! 
 
475
   //! <b>Complexity</b>: Average complexity for insert element is at
 
476
   //!   most logarithmic.
 
477
   //! 
 
478
   //! <b>Throws</b>: If "comp" throws.
 
479
   template<class NodePtrCompare>
 
480
   static node_ptr insert_equal_upper_bound
 
481
      (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp)
 
482
   {
 
483
      tree_algorithms::insert_equal_upper_bound(h, new_node, comp);
 
484
      rebalance_after_insertion(h, new_node);
 
485
      return new_node;
 
486
   }
 
487
 
 
488
   //! <b>Requires</b>: "h" must be the header node of a tree.
 
489
   //!   NodePtrCompare is a function object that induces a strict weak
 
490
   //!   ordering compatible with the strict weak ordering used to create the
 
491
   //!   the tree. NodePtrCompare compares two node_ptrs.
 
492
   //!
 
493
   //! <b>Effects</b>: Inserts new_node into the tree before the lower bound
 
494
   //!   according to "comp".
 
495
   //! 
 
496
   //! <b>Complexity</b>: Average complexity for insert element is at
 
497
   //!   most logarithmic.
 
498
   //! 
 
499
   //! <b>Throws</b>: If "comp" throws.
 
500
   template<class NodePtrCompare>
 
501
   static node_ptr insert_equal_lower_bound
 
502
      (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp)
 
503
   {
 
504
      tree_algorithms::insert_equal_lower_bound(h, new_node, comp);
 
505
      rebalance_after_insertion(h, new_node);
 
506
      return new_node;
 
507
   }
 
508
 
 
509
   //! <b>Requires</b>: "header" must be the header node of a tree.
 
510
   //!   NodePtrCompare is a function object that induces a strict weak
 
511
   //!   ordering compatible with the strict weak ordering used to create the
 
512
   //!   the tree. NodePtrCompare compares two node_ptrs. "hint" is node from
 
513
   //!   the "header"'s tree.
 
514
   //!   
 
515
   //! <b>Effects</b>: Inserts new_node into the tree, using "hint" as a hint to
 
516
   //!   where it will be inserted. If "hint" is the upper_bound
 
517
   //!   the insertion takes constant time (two comparisons in the worst case).
 
518
   //!
 
519
   //! <b>Complexity</b>: Logarithmic in general, but it is amortized
 
520
   //!   constant time if new_node is inserted immediately before "hint".
 
521
   //! 
 
522
   //! <b>Throws</b>: If "comp" throws.
 
523
   template<class NodePtrCompare>
 
524
   static node_ptr insert_equal
 
525
      (const node_ptr & header, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp)
 
526
   {
 
527
      tree_algorithms::insert_equal(header, hint, new_node, comp);
 
528
      rebalance_after_insertion(header, new_node);
 
529
      return new_node;
 
530
   }
 
531
 
 
532
   //! <b>Requires</b>: "header" must be the header node of a tree.
 
533
   //!   "pos" must be a valid iterator or header (end) node.
 
534
   //!   "pos" must be an iterator pointing to the successor to "new_node"
 
535
   //!   once inserted according to the order of already inserted nodes. This function does not
 
536
   //!   check "pos" and this precondition must be guaranteed by the caller.
 
537
   //!   
 
538
   //! <b>Effects</b>: Inserts new_node into the tree before "pos".
 
539
   //!
 
540
   //! <b>Complexity</b>: Constant-time.
 
541
   //! 
 
542
   //! <b>Throws</b>: Nothing.
 
543
   //! 
 
544
   //! <b>Note</b>: If "pos" is not the successor of the newly inserted "new_node"
 
545
   //! tree invariants might be broken.
 
546
   static node_ptr insert_before
 
547
      (const node_ptr & header, const node_ptr & pos, const node_ptr & new_node)
 
548
   {
 
549
      tree_algorithms::insert_before(header, pos, new_node);
 
550
      rebalance_after_insertion(header, new_node);
 
551
      return new_node;
 
552
   }
 
553
 
 
554
   //! <b>Requires</b>: "header" must be the header node of a tree.
 
555
   //!   "new_node" must be, according to the used ordering no less than the
 
556
   //!   greatest inserted key.
 
557
   //!   
 
558
   //! <b>Effects</b>: Inserts new_node into the tree before "pos".
 
559
   //!
 
560
   //! <b>Complexity</b>: Constant-time.
 
561
   //! 
 
562
   //! <b>Throws</b>: Nothing.
 
563
   //! 
 
564
   //! <b>Note</b>: If "new_node" is less than the greatest inserted key
 
565
   //! tree invariants are broken. This function is slightly faster than
 
566
   //! using "insert_before".
 
567
   static void push_back(const node_ptr & header, const node_ptr & new_node)
 
568
   {
 
569
      tree_algorithms::push_back(header, new_node);
 
570
      rebalance_after_insertion(header, new_node);
 
571
   }
 
572
 
 
573
   //! <b>Requires</b>: "header" must be the header node of a tree.
 
574
   //!   "new_node" must be, according to the used ordering, no greater than the
 
575
   //!   lowest inserted key.
 
576
   //!   
 
577
   //! <b>Effects</b>: Inserts new_node into the tree before "pos".
 
578
   //!
 
579
   //! <b>Complexity</b>: Constant-time.
 
580
   //! 
 
581
   //! <b>Throws</b>: Nothing.
 
582
   //! 
 
583
   //! <b>Note</b>: If "new_node" is greater than the lowest inserted key
 
584
   //! tree invariants are broken. This function is slightly faster than
 
585
   //! using "insert_before".
 
586
   static void push_front(const node_ptr & header, const node_ptr & new_node)
 
587
   {
 
588
      tree_algorithms::push_front(header, new_node);
 
589
      rebalance_after_insertion(header, new_node);
 
590
   }
 
591
 
 
592
   //! <b>Requires</b>: "header" must be the header node of a tree.
 
593
   //!   KeyNodePtrCompare is a function object that induces a strict weak
 
594
   //!   ordering compatible with the strict weak ordering used to create the
 
595
   //!   the tree. NodePtrCompare compares KeyType with a node_ptr.
 
596
   //! 
 
597
   //! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
 
598
   //!   tree according to "comp" and obtains the needed information to realize
 
599
   //!   a constant-time node insertion if there is no equivalent node.
 
600
   //!
 
601
   //! <b>Returns</b>: If there is an equivalent value
 
602
   //!   returns a pair containing a node_ptr to the already present node
 
603
   //!   and false. If there is not equivalent key can be inserted returns true
 
604
   //!   in the returned pair's boolean and fills "commit_data" that is meant to
 
605
   //!   be used with the "insert_commit" function to achieve a constant-time
 
606
   //!   insertion function.
 
607
   //! 
 
608
   //! <b>Complexity</b>: Average complexity is at most logarithmic.
 
609
   //!
 
610
   //! <b>Throws</b>: If "comp" throws.
 
611
   //! 
 
612
   //! <b>Notes</b>: This function is used to improve performance when constructing
 
613
   //!   a node is expensive and the user does not want to have two equivalent nodes
 
614
   //!   in the tree: if there is an equivalent value
 
615
   //!   the constructed object must be discarded. Many times, the part of the
 
616
   //!   node that is used to impose the order is much cheaper to construct
 
617
   //!   than the node and this function offers the possibility to use that part
 
618
   //!   to check if the insertion will be successful.
 
619
   //!
 
620
   //!   If the check is successful, the user can construct the node and use
 
621
   //!   "insert_commit" to insert the node in constant-time. This gives a total
 
622
   //!   logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
 
623
   //!
 
624
   //!   "commit_data" remains valid for a subsequent "insert_unique_commit" only
 
625
   //!   if no more objects are inserted or erased from the set.
 
626
   template<class KeyType, class KeyNodePtrCompare>
 
627
   static std::pair<node_ptr, bool> insert_unique_check
 
628
      (const const_node_ptr & header,  const KeyType &key
 
629
      ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
 
630
   {  return tree_algorithms::insert_unique_check(header, key, comp, commit_data);  }
 
631
 
 
632
   //! <b>Requires</b>: "header" must be the header node of a tree.
 
633
   //!   KeyNodePtrCompare is a function object that induces a strict weak
 
634
   //!   ordering compatible with the strict weak ordering used to create the
 
635
   //!   the tree. NodePtrCompare compares KeyType with a node_ptr.
 
636
   //!   "hint" is node from the "header"'s tree.
 
637
   //! 
 
638
   //! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
 
639
   //!   tree according to "comp" using "hint" as a hint to where it should be
 
640
   //!   inserted and obtains the needed information to realize
 
641
   //!   a constant-time node insertion if there is no equivalent node. 
 
642
   //!   If "hint" is the upper_bound the function has constant time 
 
643
   //!   complexity (two comparisons in the worst case).
 
644
   //!
 
645
   //! <b>Returns</b>: If there is an equivalent value
 
646
   //!   returns a pair containing a node_ptr to the already present node
 
647
   //!   and false. If there is not equivalent key can be inserted returns true
 
648
   //!   in the returned pair's boolean and fills "commit_data" that is meant to
 
649
   //!   be used with the "insert_commit" function to achieve a constant-time
 
650
   //!   insertion function.
 
651
   //! 
 
652
   //! <b>Complexity</b>: Average complexity is at most logarithmic, but it is
 
653
   //!   amortized constant time if new_node should be inserted immediately before "hint".
 
654
   //!
 
655
   //! <b>Throws</b>: If "comp" throws.
 
656
   //! 
 
657
   //! <b>Notes</b>: This function is used to improve performance when constructing
 
658
   //!   a node is expensive and the user does not want to have two equivalent nodes
 
659
   //!   in the tree: if there is an equivalent value
 
660
   //!   the constructed object must be discarded. Many times, the part of the
 
661
   //!   node that is used to impose the order is much cheaper to construct
 
662
   //!   than the node and this function offers the possibility to use that part
 
663
   //!   to check if the insertion will be successful.
 
664
   //!
 
665
   //!   If the check is successful, the user can construct the node and use
 
666
   //!   "insert_commit" to insert the node in constant-time. This gives a total
 
667
   //!   logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
 
668
   //!
 
669
   //!   "commit_data" remains valid for a subsequent "insert_unique_commit" only
 
670
   //!   if no more objects are inserted or erased from the set.
 
671
   template<class KeyType, class KeyNodePtrCompare>
 
672
   static std::pair<node_ptr, bool> insert_unique_check
 
673
      (const const_node_ptr & header, const node_ptr &hint, const KeyType &key
 
674
      ,KeyNodePtrCompare comp, insert_commit_data &commit_data)
 
675
   {  return tree_algorithms::insert_unique_check(header, hint, key, comp, commit_data);  }
 
676
 
 
677
   //! <b>Requires</b>: "header" must be the header node of a tree.
 
678
   //!   "commit_data" must have been obtained from a previous call to
 
679
   //!   "insert_unique_check". No objects should have been inserted or erased
 
680
   //!   from the set between the "insert_unique_check" that filled "commit_data"
 
681
   //!   and the call to "insert_commit". 
 
682
   //! 
 
683
   //! 
 
684
   //! <b>Effects</b>: Inserts new_node in the set using the information obtained
 
685
   //!   from the "commit_data" that a previous "insert_check" filled.
 
686
   //!
 
687
   //! <b>Complexity</b>: Constant time.
 
688
   //!
 
689
   //! <b>Throws</b>: Nothing.
 
690
   //! 
 
691
   //! <b>Notes</b>: This function has only sense if a "insert_unique_check" has been
 
692
   //!   previously executed to fill "commit_data". No value should be inserted or
 
693
   //!   erased between the "insert_check" and "insert_commit" calls.
 
694
   static void insert_unique_commit
 
695
      (const node_ptr & header, const node_ptr & new_value, const insert_commit_data &commit_data)
 
696
   {
 
697
      tree_algorithms::insert_unique_commit(header, new_value, commit_data);
 
698
      rebalance_after_insertion(header, new_value);
 
699
   }
 
700
 
 
701
   //! <b>Requires</b>: "n" must be a node inserted in a tree.
 
702
   //!
 
703
   //! <b>Effects</b>: Returns a pointer to the header node of the tree.
 
704
   //!
 
705
   //! <b>Complexity</b>: Logarithmic.
 
706
   //! 
 
707
   //! <b>Throws</b>: Nothing.
 
708
   static node_ptr get_header(const node_ptr & n)
 
709
   {  return tree_algorithms::get_header(n);   }
 
710
 
 
711
   /// @cond
 
712
   private:
 
713
 
 
714
   //! <b>Requires</b>: p is a node of a tree.
 
715
   //! 
 
716
   //! <b>Effects</b>: Returns true if p is the header of the tree.
 
717
   //! 
 
718
   //! <b>Complexity</b>: Constant.
 
719
   //! 
 
720
   //! <b>Throws</b>: Nothing.
 
721
   static bool is_header(const const_node_ptr & p)
 
722
   {  return NodeTraits::get_balance(p) == NodeTraits::zero() && tree_algorithms::is_header(p);  }
 
723
 
 
724
   static void rebalance_after_erasure(const node_ptr & header, const node_ptr & xnode, const node_ptr & xnode_parent)
 
725
   {
 
726
      node_ptr x(xnode), x_parent(xnode_parent);
 
727
      for (node_ptr root = NodeTraits::get_parent(header); x != root; root = NodeTraits::get_parent(header)) {
 
728
         const balance x_parent_balance = NodeTraits::get_balance(x_parent);
 
729
         if(x_parent_balance == NodeTraits::zero()){
 
730
            NodeTraits::set_balance(x_parent, 
 
731
               (x == NodeTraits::get_right(x_parent) ? NodeTraits::negative() : NodeTraits::positive()));
 
732
            break;       // the height didn't change, let's stop here
 
733
         }
 
734
         else if(x_parent_balance == NodeTraits::negative()){
 
735
            if (x == NodeTraits::get_left(x_parent)) {
 
736
               NodeTraits::set_balance(x_parent, NodeTraits::zero()); // balanced
 
737
               x = x_parent;
 
738
               x_parent = NodeTraits::get_parent(x_parent);
 
739
            }
 
740
            else {
 
741
               // x is right child
 
742
               // a is left child
 
743
               node_ptr a = NodeTraits::get_left(x_parent);
 
744
               BOOST_INTRUSIVE_INVARIANT_ASSERT(a);
 
745
               if (NodeTraits::get_balance(a) == NodeTraits::positive()) {
 
746
                  // a MUST have a right child
 
747
                  BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(a));
 
748
                  rotate_left_right(x_parent, header);
 
749
                  x = NodeTraits::get_parent(x_parent);
 
750
                  x_parent = NodeTraits::get_parent(x);
 
751
               }
 
752
               else {
 
753
                  rotate_right(x_parent, header);
 
754
                  x = NodeTraits::get_parent(x_parent);
 
755
                  x_parent = NodeTraits::get_parent(x);
 
756
               }
 
757
 
 
758
               // if changed from negative to NodeTraits::positive(), no need to check above
 
759
               if (NodeTraits::get_balance(x) == NodeTraits::positive()){
 
760
                  break;
 
761
               }
 
762
            }
 
763
         }
 
764
         else if(x_parent_balance == NodeTraits::positive()){
 
765
            if (x == NodeTraits::get_right(x_parent)) {
 
766
               NodeTraits::set_balance(x_parent, NodeTraits::zero()); // balanced
 
767
               x = x_parent;
 
768
               x_parent = NodeTraits::get_parent(x_parent);
 
769
            }
 
770
            else {
 
771
               // x is left child
 
772
               // a is right child
 
773
               node_ptr a = NodeTraits::get_right(x_parent);
 
774
               BOOST_INTRUSIVE_INVARIANT_ASSERT(a);
 
775
               if (NodeTraits::get_balance(a) == NodeTraits::negative()) {
 
776
                  // a MUST have then a left child
 
777
                  BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(a));
 
778
                  rotate_right_left(x_parent, header);
 
779
 
 
780
                  x = NodeTraits::get_parent(x_parent);
 
781
                  x_parent = NodeTraits::get_parent(x);
 
782
               }
 
783
               else {
 
784
                  rotate_left(x_parent, header);
 
785
                  x = NodeTraits::get_parent(x_parent);
 
786
                  x_parent = NodeTraits::get_parent(x);
 
787
               }
 
788
               // if changed from NodeTraits::positive() to negative, no need to check above
 
789
               if (NodeTraits::get_balance(x) == NodeTraits::negative()){
 
790
                  break;
 
791
               }
 
792
            }
 
793
         }
 
794
         else{
 
795
            BOOST_INTRUSIVE_INVARIANT_ASSERT(false);  // never reached
 
796
         }
 
797
      }
 
798
   }
 
799
 
 
800
   static void rebalance_after_insertion(const node_ptr & header, const node_ptr & xnode)
 
801
   {
 
802
      node_ptr x(xnode);
 
803
      NodeTraits::set_balance(x, NodeTraits::zero());
 
804
      // Rebalance.
 
805
      for(node_ptr root = NodeTraits::get_parent(header); x != root; root = NodeTraits::get_parent(header)){
 
806
         const balance x_parent_balance = NodeTraits::get_balance(NodeTraits::get_parent(x));
 
807
 
 
808
         if(x_parent_balance == NodeTraits::zero()){
 
809
            // if x is left, parent will have parent->bal_factor = negative
 
810
            // else, parent->bal_factor = NodeTraits::positive()
 
811
            NodeTraits::set_balance( NodeTraits::get_parent(x)
 
812
                                    , x == NodeTraits::get_left(NodeTraits::get_parent(x))
 
813
                                       ? NodeTraits::negative() : NodeTraits::positive()  );
 
814
            x = NodeTraits::get_parent(x);
 
815
         }
 
816
         else if(x_parent_balance == NodeTraits::positive()){
 
817
            // if x is a left child, parent->bal_factor = zero
 
818
            if (x == NodeTraits::get_left(NodeTraits::get_parent(x)))
 
819
               NodeTraits::set_balance(NodeTraits::get_parent(x), NodeTraits::zero());
 
820
            else{        // x is a right child, needs rebalancing
 
821
               if (NodeTraits::get_balance(x) == NodeTraits::negative())
 
822
                  rotate_right_left(NodeTraits::get_parent(x), header);
 
823
               else
 
824
                  rotate_left(NodeTraits::get_parent(x), header);
 
825
            }
 
826
            break;
 
827
         }
 
828
         else if(x_parent_balance == NodeTraits::negative()){
 
829
            // if x is a left child, needs rebalancing
 
830
            if (x == NodeTraits::get_left(NodeTraits::get_parent(x))) {
 
831
               if (NodeTraits::get_balance(x) == NodeTraits::positive())
 
832
                  rotate_left_right(NodeTraits::get_parent(x), header);
 
833
               else
 
834
                  rotate_right(NodeTraits::get_parent(x), header);
 
835
            }
 
836
            else
 
837
               NodeTraits::set_balance(NodeTraits::get_parent(x), NodeTraits::zero());
 
838
            break;
 
839
         }
 
840
         else{
 
841
            BOOST_INTRUSIVE_INVARIANT_ASSERT(false);  // never reached
 
842
         }
 
843
      }
 
844
   }
 
845
 
 
846
   static void left_right_balancing(const node_ptr & a, const node_ptr & b, const node_ptr & c)
 
847
   {
 
848
      // balancing...
 
849
      const balance c_balance = NodeTraits::get_balance(c);
 
850
      const balance zero_balance = NodeTraits::zero();
 
851
      NodeTraits::set_balance(c, zero_balance);
 
852
      if(c_balance == NodeTraits::negative()){
 
853
         NodeTraits::set_balance(a, NodeTraits::positive());
 
854
         NodeTraits::set_balance(b, zero_balance);
 
855
      }
 
856
      else if(c_balance == zero_balance){
 
857
         NodeTraits::set_balance(a, zero_balance);
 
858
         NodeTraits::set_balance(b, zero_balance);
 
859
      }
 
860
      else if(c_balance == NodeTraits::positive()){
 
861
         NodeTraits::set_balance(a, zero_balance);
 
862
         NodeTraits::set_balance(b, NodeTraits::negative());
 
863
      }
 
864
      else{
 
865
         BOOST_INTRUSIVE_INVARIANT_ASSERT(false); // never reached
 
866
      }
 
867
   }
 
868
 
 
869
   static void rotate_left_right(const node_ptr a, const node_ptr & hdr)
 
870
   {
 
871
      //             |                               |         //
 
872
      //             a(-2)                           c         //
 
873
      //            / \                             / \        //
 
874
      //           /   \        ==>                /   \       //
 
875
      //      (pos)b    [g]                       b     a      //
 
876
      //          / \                            / \   / \     //
 
877
      //        [d]  c                         [d]  e f  [g]   //
 
878
      //           / \                                         //
 
879
      //          e   f                                        //
 
880
      node_ptr b = NodeTraits::get_left(a), c = NodeTraits::get_right(b);
 
881
      tree_algorithms::rotate_left(b, hdr);
 
882
      tree_algorithms::rotate_right(a, hdr);
 
883
      left_right_balancing(a, b, c);
 
884
   }
 
885
 
 
886
   static void rotate_right_left(const node_ptr a, const node_ptr & hdr)
 
887
   {
 
888
      //              |                               |           //
 
889
      //              a(pos)                          c           //
 
890
      //             / \                             / \          //
 
891
      //            /   \                           /   \         //
 
892
      //          [d]   b(neg)         ==>         a     b        //
 
893
      //               / \                        / \   / \       //
 
894
      //              c  [g]                    [d] e  f  [g]     //
 
895
      //             / \                                          //
 
896
      //            e   f                                         //
 
897
      node_ptr b = NodeTraits::get_right(a), c = NodeTraits::get_left(b);
 
898
      tree_algorithms::rotate_right(b, hdr);
 
899
      tree_algorithms::rotate_left(a, hdr);
 
900
      left_right_balancing(b, a, c);
 
901
   }
 
902
 
 
903
   static void rotate_left(const node_ptr x, const node_ptr & hdr)
 
904
   {
 
905
      const node_ptr y = NodeTraits::get_right(x);
 
906
      tree_algorithms::rotate_left(x, hdr);
 
907
 
 
908
      // reset the balancing factor
 
909
      if (NodeTraits::get_balance(y) == NodeTraits::positive()) {
 
910
         NodeTraits::set_balance(x, NodeTraits::zero());
 
911
         NodeTraits::set_balance(y, NodeTraits::zero());
 
912
      }
 
913
      else {        // this doesn't happen during insertions
 
914
         NodeTraits::set_balance(x, NodeTraits::positive());
 
915
         NodeTraits::set_balance(y, NodeTraits::negative());
 
916
      }
 
917
   }
 
918
 
 
919
   static void rotate_right(const node_ptr x, const node_ptr & hdr)
 
920
   {
 
921
      const node_ptr y = NodeTraits::get_left(x);
 
922
      tree_algorithms::rotate_right(x, hdr);
 
923
 
 
924
      // reset the balancing factor
 
925
      if (NodeTraits::get_balance(y) == NodeTraits::negative()) {
 
926
         NodeTraits::set_balance(x, NodeTraits::zero());
 
927
         NodeTraits::set_balance(y, NodeTraits::zero());
 
928
      }
 
929
      else {        // this doesn't happen during insertions
 
930
         NodeTraits::set_balance(x, NodeTraits::negative());
 
931
         NodeTraits::set_balance(y, NodeTraits::positive());
 
932
      }
 
933
   }
 
934
 
 
935
   /// @endcond
 
936
};
 
937
 
 
938
} //namespace intrusive 
 
939
} //namespace boost 
 
940
 
 
941
#include <boost/intrusive/detail/config_end.hpp>
 
942
 
 
943
#endif //BOOST_INTRUSIVE_AVLTREE_ALGORITHMS_HPP