3 // Copyright (C) 2005 Free Software Foundation, Inc.
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)
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,
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>
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 \
85 #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
86 #define PB_ASSOC_CLASS_NAME \
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< \
104 #define PB_ASSOC_TYPES_TRAITS_C_DEC \
105 pb_assoc::detail::types_traits< \
110 #ifdef PB_ASSOC_USE_MAP_DEBUG_BASE
111 #define PB_ASSOC_MAP_DEBUG_BASE_C_DEC \
112 pb_assoc::detail::map_debug_base< \
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,
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_
140 public PB_ASSOC_TYPES_TRAITS_C_DEC,
146 typedef typename Allocator::size_type size_type;
149 typename PB_ASSOC_TYPES_TRAITS_C_DEC::const_key_reference
152 typedef typename PB_ASSOC_TYPES_TRAITS_C_DEC::data_type data_type;
155 typename PB_ASSOC_TYPES_TRAITS_C_DEC::data_reference
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;
167 typename PB_ASSOC_TYPES_TRAITS_C_DEC::const_pointer
170 typedef typename PB_ASSOC_TYPES_TRAITS_C_DEC::reference reference;
173 typename PB_ASSOC_TYPES_TRAITS_C_DEC::const_reference
176 typedef PB_ASSOC_TYPES_TRAITS_C_DEC my_traits_base;
181 pb_assoc::detail::cond_dealtor<
187 typename Allocator::template rebind<node>::other
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;
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_
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);
245 swap(PB_ASSOC_CLASS_C_DEC& r_other);
247 ~PB_ASSOC_CLASS_NAME();
250 initialize_min_max();
252 template<class Other_Map_Type>
254 cmp_with_other(const Other_Map_Type& r_other) const;
271 inline std::pair<find_iterator, bool>
272 insert_leaf(const_reference r_value);
275 lower_bound(const_key_reference r_key);
277 inline const_find_iterator
278 lower_bound(const_key_reference r_key) const;
281 upper_bound(const_key_reference r_key);
283 inline const_find_iterator
284 upper_bound(const_key_reference r_key) const;
287 find(const_key_reference r_key);
289 inline const_find_iterator
290 find(const_key_reference r_key) const;
293 update_min_max_for_erased_node(node_pointer p_nd);
296 actual_erase_node(node_pointer p_nd);
302 rotate_left(node_pointer p_x);
305 rotate_right(node_pointer p_y);
308 rotate_parent(node_pointer p_nd);
311 apply_update(node_pointer p_nd, pb_assoc::null_node_updator* );
313 template<class Node_Updator_>
315 apply_update(node_pointer p_nd, Node_Updator_* p_updator);
317 template<class Node_Updator_>
319 update_to_top(node_pointer p_nd, Node_Updator_* p_updator);
322 update_to_top(node_pointer p_nd, pb_assoc::null_node_updator* );
327 inline const_iterator
333 inline const_iterator
339 inline const_iterator
342 inline reverse_iterator
345 return (reverse_iterator(m_p_head->m_p_right));
348 inline const_reverse_iterator
351 inline reverse_iterator
354 inline const_reverse_iterator
357 inline reverse_iterator
360 inline const_reverse_iterator
364 join_prep(PB_ASSOC_CLASS_C_DEC& r_other);
367 join_finish(PB_ASSOC_CLASS_C_DEC& r_other);
370 split_prep(const_key_reference r_key, PB_ASSOC_CLASS_C_DEC& r_other);
373 split_finish(PB_ASSOC_CLASS_C_DEC& r_other);
376 recursive_count(node_pointer p_nd) const;
378 inline const_node_iterator
384 inline const_node_iterator
392 inline std::pair<node_pointer, bool>
393 erase(node_pointer p_nd);
395 #ifdef PB_ASSOC_BIN_SEARCH_TREE_DEBUG_
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
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>
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>
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));
431 assert_consistent_with_debug_base() const;
434 assert_node_consistent_with_left(const node_pointer p_nd) const;
437 assert_node_consistent_with_right(const node_pointer p_nd) const;
440 assert_consistent_with_debug_base(const node_pointer p_nd) const;
446 assert_min_imp(const node_pointer p_nd) const;
452 assert_max_imp(const node_pointer p_nd) const;
455 assert_iterators() const;
460 #endif // #ifdef PB_ASSOC_BIN_SEARCH_TREE_DEBUG_
466 recursive_copy_node(const node_pointer p_nd);
469 get_new_node_for_leaf_insert(const_reference r_val, pb_assoc::detail::int_to_type<false>);
472 get_new_node_for_leaf_insert(const_reference r_val, pb_assoc::detail::int_to_type<true>);
475 insert_imp_empty(const_reference r_value);
478 insert_leaf_new(const_reference r_value, node_pointer p_nd, bool left_nd);
481 clear_imp(node_pointer p_nd);
484 node_pointer m_p_head;
488 reverse_iterator m_rend_it;
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
516 #undef PB_ASSOC_EP2VP
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