Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / contrib / gcc-4.1 / libstdc++-v3 / include / ext / pb_assoc / detail / bin_search_tree_ / bin_search_tree_.hpp
1 // -*- C++ -*-
2
3 // Copyright (C) 2005 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
31
32 // Permission to use, copy, modify, sell, and distribute this software
33 // is hereby granted without fee, provided that the above copyright
34 // notice appears in all copies, and that both that copyright notice and
35 // this permission notice appear in supporting documentation. None of
36 // the above authors, nor IBM Haifa Research Laboratories, make any
37 // representation about the suitability of this software for any
38 // purpose. It is provided "as is" without express or implied warranty.
39
40 /*
41  * @file bin_search_tree_.hpp
42  * Contains an implementation class for bin_search_tree_.
43  */
44 /*
45  * This implementation uses an idea from the SGI STL (using a "header" node
46  *      which is needed for efficient iteration).
47  */
48
49 #include <ext/pb_assoc/exception.hpp>
50 #include <ext/pb_assoc/detail/eq_fn/eq_by_less.hpp>
51 #include <ext/pb_assoc/detail/types_traits.hpp>
52 #include <ext/pb_assoc/detail/map_debug_base.hpp>
53 #include <ext/pb_assoc/tree_policy.hpp>
54 #include <ext/pb_assoc/detail/cond_dealtor.hpp>
55 #include <ext/pb_assoc/detail/type_utils.hpp>
56 #include <utility>
57 #include <functional>
58 #include <assert.h>
59
60 namespace pb_assoc
61 {
62
63   namespace detail
64   {
65
66 #ifdef PB_ASSOC_BIN_SEARCH_TREE_DEBUG_
67 #define PB_ASSOC_DBG_ASSERT(X) assert(X)
68 #define PB_ASSOC_DBG_VERIFY(X) assert(X)
69 #define PB_ASSOC_DBG_ONLY(X) X
70 #else // #ifdef PB_ASSOC_BIN_SEARCH_TREE_DEBUG_
71 #define PB_ASSOC_DBG_ASSERT(X)
72 #define PB_ASSOC_DBG_VERIFY(X) {if((X)==0);}
73 #define PB_ASSOC_DBG_ONLY(X) ;
74 #endif // #ifdef PB_ASSOC_BIN_SEARCH_TREE_DEBUG_
75
76 #define PB_ASSOC_CLASS_T_DEC \
77         template< \
78                 typename Key, \
79                 typename Data, \
80                 class Node, \
81                 class Cmp_Fn, \
82                 class Allocator, \
83                 class Node_Updator>
84
85 #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
86 #define PB_ASSOC_CLASS_NAME \
87         bin_search_tree_data_
88 #endif // #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
89
90 #ifdef PB_ASSOC_DATA_FALSE_INDICATOR
91 #define PB_ASSOC_CLASS_NAME \
92         bin_search_tree_no_data_
93 #endif // #ifdef PB_ASSOC_DATA_FALSE_INDICATOR
94
95 #define PB_ASSOC_CLASS_C_DEC \
96         PB_ASSOC_CLASS_NAME< \
97                 Key, \
98                 Data, \
99                 Node, \
100                 Cmp_Fn, \
101                 Allocator, \
102                 Node_Updator>
103
104 #define PB_ASSOC_TYPES_TRAITS_C_DEC \
105         pb_assoc::detail::types_traits< \
106                 Key, \
107                 Data, \
108                 Allocator>
109
110 #ifdef PB_ASSOC_USE_MAP_DEBUG_BASE
111 #define PB_ASSOC_MAP_DEBUG_BASE_C_DEC \
112         pb_assoc::detail::map_debug_base< \
113                 Key, \
114                 eq_by_less<Key, Cmp_Fn> >
115 #endif // #ifdef PB_ASSOC_USE_MAP_DEBUG_BASE
116
117 #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
118 #define PB_ASSOC_V2F(X) (X).first
119 #define PB_ASSOC_V2S(X) (X).second
120 #define PB_ASSOC_EP2VP(X)& ((X)->m_value)
121 #endif // #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
122
123 #ifdef PB_ASSOC_DATA_FALSE_INDICATOR
124 #define PB_ASSOC_V2F(X) (X)
125 #define PB_ASSOC_V2S(X) Mapped_Data()
126 #define PB_ASSOC_EP2VP(X)& ((X)->m_value.first)
127 #endif // #ifdef PB_ASSOC_DATA_FALSE_INDICATOR
128
129     template<typename Key,
130              typename Data,
131              class Node,
132              class Cmp_Fn,
133              class Allocator,
134              class Node_Updator>
135     class PB_ASSOC_CLASS_NAME :
136 #ifdef PB_ASSOC_BIN_SEARCH_TREE_DEBUG_
137       protected PB_ASSOC_MAP_DEBUG_BASE_C_DEC,
138 #endif // #ifdef PB_ASSOC_BIN_SEARCH_TREE_DEBUG_
139       protected Cmp_Fn,
140       public PB_ASSOC_TYPES_TRAITS_C_DEC,
141       public Node_Updator
142     {
143
144     protected:
145
146       typedef typename Allocator::size_type size_type;
147
148       typedef
149       typename PB_ASSOC_TYPES_TRAITS_C_DEC::const_key_reference
150       const_key_reference;
151
152       typedef typename PB_ASSOC_TYPES_TRAITS_C_DEC::data_type data_type;
153
154       typedef
155       typename PB_ASSOC_TYPES_TRAITS_C_DEC::data_reference
156       data_reference;
157
158       typedef
159       typename PB_ASSOC_TYPES_TRAITS_C_DEC::const_data_reference
160       const_data_reference;
161
162       typedef typename PB_ASSOC_TYPES_TRAITS_C_DEC::value_type value_type;
163
164       typedef typename PB_ASSOC_TYPES_TRAITS_C_DEC::pointer pointer;
165
166       typedef
167       typename PB_ASSOC_TYPES_TRAITS_C_DEC::const_pointer
168       const_pointer;
169
170       typedef typename PB_ASSOC_TYPES_TRAITS_C_DEC::reference reference;
171
172       typedef
173       typename PB_ASSOC_TYPES_TRAITS_C_DEC::const_reference
174       const_reference;
175
176       typedef PB_ASSOC_TYPES_TRAITS_C_DEC my_traits_base;
177
178       typedef Node node;
179
180       typedef
181       pb_assoc::detail::cond_dealtor<
182         node,
183         Allocator>
184       cond_dealtor_t;
185
186       typedef
187       typename Allocator::template rebind<node>::other
188       node_allocator;
189
190       typedef typename node_allocator::value_type node_type;
191
192       typedef typename node_allocator::pointer node_pointer;
193
194       typedef value_type mapped_value_type;
195
196       typedef reference mapped_reference;
197
198       typedef const_reference const_mapped_reference;
199
200       typedef pointer mapped_pointer;
201
202       typedef const_pointer const_mapped_pointer;
203
204 #include <ext/pb_assoc/detail/bin_search_tree_/find_iterators.hpp>
205
206       typedef const_it_<true> const_find_iterator;
207
208       typedef it_<true> find_iterator;
209
210       typedef const_find_iterator const_iterator;
211
212       typedef find_iterator iterator;
213
214       typedef const_it_<false> const_reverse_iterator;
215
216       typedef it_<false> reverse_iterator;
217
218 #include <ext/pb_assoc/detail/bin_search_tree_/node_iterators.hpp>
219
220       typedef const_node_it_ const_node_iterator;
221
222       typedef node_it_ node_iterator;
223
224       typedef Cmp_Fn cmp_fn;
225
226       typedef Allocator allocator;
227
228     private:
229
230 #ifdef PB_ASSOC_BIN_SEARCH_TREE_DEBUG_
231       typedef PB_ASSOC_MAP_DEBUG_BASE_C_DEC my_map_debug_base;
232 #endif // #ifdef PB_ASSOC_BIN_SEARCH_TREE_DEBUG_
233
234     protected:
235
236       PB_ASSOC_CLASS_NAME();
237
238       PB_ASSOC_CLASS_NAME(const Cmp_Fn& r_cmp_fn);
239
240       PB_ASSOC_CLASS_NAME(const Cmp_Fn& r_cmp_fn, const Node_Updator& r_updator);
241
242       PB_ASSOC_CLASS_NAME(const PB_ASSOC_CLASS_C_DEC& r_other);
243
244       void
245       swap(PB_ASSOC_CLASS_C_DEC& r_other);
246
247       ~PB_ASSOC_CLASS_NAME();
248
249       void
250       initialize_min_max();
251
252       template<class Other_Map_Type>
253       bool
254       cmp_with_other(const Other_Map_Type& r_other) const;
255
256       inline bool
257       empty() const;
258
259       inline size_type
260       size() const;
261
262       inline size_type
263       max_size() const;
264
265       Cmp_Fn& 
266       get_cmp_fn();
267
268       const Cmp_Fn& 
269       get_cmp_fn() const;
270
271       inline std::pair<find_iterator, bool>
272       insert_leaf(const_reference r_value);
273
274       inline find_iterator
275       lower_bound(const_key_reference r_key);
276
277       inline const_find_iterator
278       lower_bound(const_key_reference r_key) const;
279
280       inline find_iterator
281       upper_bound(const_key_reference r_key);
282
283       inline const_find_iterator
284       upper_bound(const_key_reference r_key) const;
285
286       inline find_iterator
287       find(const_key_reference r_key);
288
289       inline const_find_iterator
290       find(const_key_reference r_key) const;
291
292       inline void
293       update_min_max_for_erased_node(node_pointer p_nd);
294
295       inline void
296       actual_erase_node(node_pointer p_nd);
297
298       void
299       clear();
300
301       inline void
302       rotate_left(node_pointer p_x);
303
304       inline void
305       rotate_right(node_pointer p_y);
306
307       inline void
308       rotate_parent(node_pointer p_nd);
309
310       inline void
311       apply_update(node_pointer p_nd, pb_assoc::null_node_updator* );
312
313       template<class Node_Updator_>
314       inline void
315       apply_update(node_pointer p_nd, Node_Updator_* p_updator);
316
317       template<class Node_Updator_>
318       inline void
319       update_to_top(node_pointer p_nd, Node_Updator_* p_updator);
320
321       inline void
322       update_to_top(node_pointer p_nd, pb_assoc::null_node_updator* );
323
324       inline iterator
325       begin();
326
327       inline const_iterator
328       begin() const;
329
330       inline iterator
331       find_end();
332
333       inline const_iterator
334       find_end() const;
335
336       inline iterator
337       end();
338
339       inline const_iterator
340       end() const;
341
342       inline reverse_iterator
343       rbegin()
344       {
345         return (reverse_iterator(m_p_head->m_p_right));
346       }
347
348       inline const_reverse_iterator
349       rbegin() const;
350
351       inline reverse_iterator
352       find_rend();
353
354       inline const_reverse_iterator
355       find_rend() const;
356
357       inline reverse_iterator
358       rend();
359
360       inline const_reverse_iterator
361       rend() const;
362
363       bool
364       join_prep(PB_ASSOC_CLASS_C_DEC& r_other);
365
366       void
367       join_finish(PB_ASSOC_CLASS_C_DEC& r_other);
368
369       bool
370       split_prep(const_key_reference r_key, PB_ASSOC_CLASS_C_DEC& r_other);
371
372       void
373       split_finish(PB_ASSOC_CLASS_C_DEC& r_other);
374
375       size_type
376       recursive_count(node_pointer p_nd) const;
377
378       inline const_node_iterator
379       node_begin() const;
380
381       inline node_iterator
382       node_begin();
383
384       inline const_node_iterator
385       node_end() const;
386
387       inline node_iterator
388       node_end();
389
390     private:
391
392       inline std::pair<node_pointer, bool>
393       erase(node_pointer p_nd);
394
395 #ifdef PB_ASSOC_BIN_SEARCH_TREE_DEBUG_
396
397       void
398       assert_valid(bool check_iterators, bool check_metadata) const;
399
400       std::pair<const_pointer, const_pointer>
401       assert_node_consistent(const node_pointer p_nd) const
402       {
403         if (p_nd == NULL)
404           return (std::make_pair((const_pointer)NULL,(const_pointer)NULL));
405
406         assert_node_consistent_with_left(p_nd);
407         assert_node_consistent_with_right(p_nd);
408
409         const std::pair<const_pointer, const_pointer>
410           l_range =
411           assert_node_consistent(p_nd->m_p_left);
412
413         if (l_range.second != NULL)
414           PB_ASSOC_DBG_ASSERT(Cmp_Fn::operator()(
415                                                  PB_ASSOC_V2F(*l_range.second),
416                                                  PB_ASSOC_V2F(p_nd->m_value)));
417
418         const std::pair<const_pointer, const_pointer>
419           r_range =
420           assert_node_consistent(p_nd->m_p_right);
421
422         if (r_range.first != NULL)
423           PB_ASSOC_DBG_ASSERT(Cmp_Fn::operator()(
424                                                  PB_ASSOC_V2F(p_nd->m_value),
425                                                  PB_ASSOC_V2F(*r_range.first)));
426
427         return (std::make_pair((l_range.first != NULL)? l_range.first :& p_nd->m_value,(r_range.second != NULL)? r_range.second :& p_nd->m_value));
428       }
429
430       void
431       assert_consistent_with_debug_base() const;
432
433       void
434       assert_node_consistent_with_left(const node_pointer p_nd) const;
435
436       void
437       assert_node_consistent_with_right(const node_pointer p_nd) const;
438
439       void
440       assert_consistent_with_debug_base(const node_pointer p_nd) const;
441
442       void
443       assert_min() const;
444
445       void
446       assert_min_imp(const node_pointer p_nd) const;
447
448       void
449       assert_max() const;
450
451       void
452       assert_max_imp(const node_pointer p_nd) const;
453
454       void
455       assert_iterators() const;
456
457       void
458       assert_size() const;
459
460 #endif // #ifdef PB_ASSOC_BIN_SEARCH_TREE_DEBUG_
461
462       void
463       initialize();
464
465       node_pointer
466       recursive_copy_node(const node_pointer p_nd);
467
468       inline node_pointer
469       get_new_node_for_leaf_insert(const_reference r_val, pb_assoc::detail::int_to_type<false>);
470
471       inline node_pointer
472       get_new_node_for_leaf_insert(const_reference r_val, pb_assoc::detail::int_to_type<true>);
473
474       inline iterator
475       insert_imp_empty(const_reference r_value);
476
477       inline iterator
478       insert_leaf_new(const_reference r_value, node_pointer p_nd, bool left_nd);
479
480       static void
481       clear_imp(node_pointer p_nd);
482
483     protected:
484       node_pointer m_p_head;
485
486       iterator m_end_it;
487
488       reverse_iterator m_rend_it;
489
490       size_type m_size;
491
492       static node_allocator s_node_allocator;
493     };
494
495 #include <ext/pb_assoc/detail/bin_search_tree_/constructors_destructor_fn_imps.hpp>
496 #include <ext/pb_assoc/detail/bin_search_tree_/iterators_fn_imps.hpp>
497 #include <ext/pb_assoc/detail/bin_search_tree_/debug_fn_imps.hpp>
498 #include <ext/pb_assoc/detail/bin_search_tree_/insert_fn_imps.hpp>
499 #include <ext/pb_assoc/detail/bin_search_tree_/erase_fn_imps.hpp>
500 #include <ext/pb_assoc/detail/bin_search_tree_/find_fn_imps.hpp>
501 #include <ext/pb_assoc/detail/bin_search_tree_/info_fn_imps.hpp>
502 #include <ext/pb_assoc/detail/bin_search_tree_/split_join_fn_imps.hpp>
503 #include <ext/pb_assoc/detail/bin_search_tree_/rotate_fn_imps.hpp>
504
505 #undef PB_ASSOC_CLASS_C_DEC
506
507 #undef PB_ASSOC_CLASS_T_DEC
508
509 #undef PB_ASSOC_CLASS_NAME
510
511 #undef PB_ASSOC_TYPES_TRAITS_C_DEC
512
513 #undef PB_ASSOC_MAP_DEBUG_BASE_C_DEC
514
515 #undef PB_ASSOC_V2F
516 #undef PB_ASSOC_EP2VP
517 #undef PB_ASSOC_V2S
518
519 #undef PB_ASSOC_DBG_ASSERT
520 #undef PB_ASSOC_DBG_VERIFY
521 #undef PB_ASSOC_DBG_ONLY
522
523   } // namespace detail
524
525 } // namespace pb_assoc