toolchain: switch kernel 2.4 builds to gcc 4.2.4
[tomato.git] / tools / brcm / K24 / hndtools-mipsel-uclibc-4.1.2 / include / c++ / 4.1.2 / ext / pb_assoc / detail / bin_search_tree_ / bin_search_tree_.hpp
blobb450f539807e91a5715570843ebe04542fd331fb
1 // -*- C++ -*-
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.
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.
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.
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.
30 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
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.
41 * @file bin_search_tree_.hpp
42 * Contains an implementation class for bin_search_tree_.
45 * This implementation uses an idea from the SGI STL (using a "header" node
46 * which is needed for efficient iteration).
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>
60 namespace pb_assoc
63 namespace detail
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_
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>
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
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
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>
104 #define PB_ASSOC_TYPES_TRAITS_C_DEC \
105 pb_assoc::detail::types_traits< \
106 Key, \
107 Data, \
108 Allocator>
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
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
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
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
144 protected:
146 typedef typename Allocator::size_type size_type;
148 typedef
149 typename PB_ASSOC_TYPES_TRAITS_C_DEC::const_key_reference
150 const_key_reference;
152 typedef typename PB_ASSOC_TYPES_TRAITS_C_DEC::data_type data_type;
154 typedef
155 typename PB_ASSOC_TYPES_TRAITS_C_DEC::data_reference
156 data_reference;
158 typedef
159 typename PB_ASSOC_TYPES_TRAITS_C_DEC::const_data_reference
160 const_data_reference;
162 typedef typename PB_ASSOC_TYPES_TRAITS_C_DEC::value_type value_type;
164 typedef typename PB_ASSOC_TYPES_TRAITS_C_DEC::pointer pointer;
166 typedef
167 typename PB_ASSOC_TYPES_TRAITS_C_DEC::const_pointer
168 const_pointer;
170 typedef typename PB_ASSOC_TYPES_TRAITS_C_DEC::reference reference;
172 typedef
173 typename PB_ASSOC_TYPES_TRAITS_C_DEC::const_reference
174 const_reference;
176 typedef PB_ASSOC_TYPES_TRAITS_C_DEC my_traits_base;
178 typedef Node node;
180 typedef
181 pb_assoc::detail::cond_dealtor<
182 node,
183 Allocator>
184 cond_dealtor_t;
186 typedef
187 typename Allocator::template rebind<node>::other
188 node_allocator;
190 typedef typename node_allocator::value_type node_type;
192 typedef typename node_allocator::pointer node_pointer;
194 typedef value_type mapped_value_type;
196 typedef reference mapped_reference;
198 typedef const_reference const_mapped_reference;
200 typedef pointer mapped_pointer;
202 typedef const_pointer const_mapped_pointer;
204 #include <ext/pb_assoc/detail/bin_search_tree_/find_iterators.hpp>
206 typedef const_it_<true> const_find_iterator;
208 typedef it_<true> find_iterator;
210 typedef const_find_iterator const_iterator;
212 typedef find_iterator iterator;
214 typedef const_it_<false> const_reverse_iterator;
216 typedef it_<false> reverse_iterator;
218 #include <ext/pb_assoc/detail/bin_search_tree_/node_iterators.hpp>
220 typedef const_node_it_ const_node_iterator;
222 typedef node_it_ node_iterator;
224 typedef Cmp_Fn cmp_fn;
226 typedef Allocator allocator;
228 private:
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_
234 protected:
236 PB_ASSOC_CLASS_NAME();
238 PB_ASSOC_CLASS_NAME(const Cmp_Fn& r_cmp_fn);
240 PB_ASSOC_CLASS_NAME(const Cmp_Fn& r_cmp_fn, const Node_Updator& r_updator);
242 PB_ASSOC_CLASS_NAME(const PB_ASSOC_CLASS_C_DEC& r_other);
244 void
245 swap(PB_ASSOC_CLASS_C_DEC& r_other);
247 ~PB_ASSOC_CLASS_NAME();
249 void
250 initialize_min_max();
252 template<class Other_Map_Type>
253 bool
254 cmp_with_other(const Other_Map_Type& r_other) const;
256 inline bool
257 empty() const;
259 inline size_type
260 size() const;
262 inline size_type
263 max_size() const;
265 Cmp_Fn&
266 get_cmp_fn();
268 const Cmp_Fn&
269 get_cmp_fn() const;
271 inline std::pair<find_iterator, bool>
272 insert_leaf(const_reference r_value);
274 inline find_iterator
275 lower_bound(const_key_reference r_key);
277 inline const_find_iterator
278 lower_bound(const_key_reference r_key) const;
280 inline find_iterator
281 upper_bound(const_key_reference r_key);
283 inline const_find_iterator
284 upper_bound(const_key_reference r_key) const;
286 inline find_iterator
287 find(const_key_reference r_key);
289 inline const_find_iterator
290 find(const_key_reference r_key) const;
292 inline void
293 update_min_max_for_erased_node(node_pointer p_nd);
295 inline void
296 actual_erase_node(node_pointer p_nd);
298 void
299 clear();
301 inline void
302 rotate_left(node_pointer p_x);
304 inline void
305 rotate_right(node_pointer p_y);
307 inline void
308 rotate_parent(node_pointer p_nd);
310 inline void
311 apply_update(node_pointer p_nd, pb_assoc::null_node_updator* );
313 template<class Node_Updator_>
314 inline void
315 apply_update(node_pointer p_nd, Node_Updator_* p_updator);
317 template<class Node_Updator_>
318 inline void
319 update_to_top(node_pointer p_nd, Node_Updator_* p_updator);
321 inline void
322 update_to_top(node_pointer p_nd, pb_assoc::null_node_updator* );
324 inline iterator
325 begin();
327 inline const_iterator
328 begin() const;
330 inline iterator
331 find_end();
333 inline const_iterator
334 find_end() const;
336 inline iterator
337 end();
339 inline const_iterator
340 end() const;
342 inline reverse_iterator
343 rbegin()
345 return (reverse_iterator(m_p_head->m_p_right));
348 inline const_reverse_iterator
349 rbegin() const;
351 inline reverse_iterator
352 find_rend();
354 inline const_reverse_iterator
355 find_rend() const;
357 inline reverse_iterator
358 rend();
360 inline const_reverse_iterator
361 rend() const;
363 bool
364 join_prep(PB_ASSOC_CLASS_C_DEC& r_other);
366 void
367 join_finish(PB_ASSOC_CLASS_C_DEC& r_other);
369 bool
370 split_prep(const_key_reference r_key, PB_ASSOC_CLASS_C_DEC& r_other);
372 void
373 split_finish(PB_ASSOC_CLASS_C_DEC& r_other);
375 size_type
376 recursive_count(node_pointer p_nd) const;
378 inline const_node_iterator
379 node_begin() const;
381 inline node_iterator
382 node_begin();
384 inline const_node_iterator
385 node_end() const;
387 inline node_iterator
388 node_end();
390 private:
392 inline std::pair<node_pointer, bool>
393 erase(node_pointer p_nd);
395 #ifdef PB_ASSOC_BIN_SEARCH_TREE_DEBUG_
397 void
398 assert_valid(bool check_iterators, bool check_metadata) const;
400 std::pair<const_pointer, const_pointer>
401 assert_node_consistent(const node_pointer p_nd) const
403 if (p_nd == NULL)
404 return (std::make_pair((const_pointer)NULL,(const_pointer)NULL));
406 assert_node_consistent_with_left(p_nd);
407 assert_node_consistent_with_right(p_nd);
409 const std::pair<const_pointer, const_pointer>
410 l_range =
411 assert_node_consistent(p_nd->m_p_left);
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)));
418 const std::pair<const_pointer, const_pointer>
419 r_range =
420 assert_node_consistent(p_nd->m_p_right);
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)));
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));
430 void
431 assert_consistent_with_debug_base() const;
433 void
434 assert_node_consistent_with_left(const node_pointer p_nd) const;
436 void
437 assert_node_consistent_with_right(const node_pointer p_nd) const;
439 void
440 assert_consistent_with_debug_base(const node_pointer p_nd) const;
442 void
443 assert_min() const;
445 void
446 assert_min_imp(const node_pointer p_nd) const;
448 void
449 assert_max() const;
451 void
452 assert_max_imp(const node_pointer p_nd) const;
454 void
455 assert_iterators() const;
457 void
458 assert_size() const;
460 #endif // #ifdef PB_ASSOC_BIN_SEARCH_TREE_DEBUG_
462 void
463 initialize();
465 node_pointer
466 recursive_copy_node(const node_pointer p_nd);
468 inline node_pointer
469 get_new_node_for_leaf_insert(const_reference r_val, pb_assoc::detail::int_to_type<false>);
471 inline node_pointer
472 get_new_node_for_leaf_insert(const_reference r_val, pb_assoc::detail::int_to_type<true>);
474 inline iterator
475 insert_imp_empty(const_reference r_value);
477 inline iterator
478 insert_leaf_new(const_reference r_value, node_pointer p_nd, bool left_nd);
480 static void
481 clear_imp(node_pointer p_nd);
483 protected:
484 node_pointer m_p_head;
486 iterator m_end_it;
488 reverse_iterator m_rend_it;
490 size_type m_size;
492 static node_allocator s_node_allocator;
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>
505 #undef PB_ASSOC_CLASS_C_DEC
507 #undef PB_ASSOC_CLASS_T_DEC
509 #undef PB_ASSOC_CLASS_NAME
511 #undef PB_ASSOC_TYPES_TRAITS_C_DEC
513 #undef PB_ASSOC_MAP_DEBUG_BASE_C_DEC
515 #undef PB_ASSOC_V2F
516 #undef PB_ASSOC_EP2VP
517 #undef PB_ASSOC_V2S
519 #undef PB_ASSOC_DBG_ASSERT
520 #undef PB_ASSOC_DBG_VERIFY
521 #undef PB_ASSOC_DBG_ONLY
523 } // namespace detail
525 } // namespace pb_assoc