3 // Copyright (C) 2005, 2006 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 terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
44 * Contains an implementation for rb_tree_.
47 * This implementation uses an idea from the SGI STL (using a "header" node
48 * which is needed for efficient iteration).
51 #ifdef PB_DS_DATA_TRUE_INDICATOR
52 #ifndef PB_DS_BIN_SEARCH_TREE_HPP__DATA_TRUE_INDICATOR
53 #define PB_DS_BIN_SEARCH_TREE_HPP__DATA_TRUE_INDICATOR
54 #include <ext/pb_ds/detail/bin_search_tree_/bin_search_tree_.hpp>
55 #endif // #ifndef PB_DS_BIN_SEARCH_TREE_HPP__DATA_TRUE_INDICATOR
56 #endif // #ifdef PB_DS_DATA_TRUE_INDICATOR
58 #ifdef PB_DS_DATA_FALSE_INDICATOR
59 #ifndef PB_DS_BIN_SEARCH_TREE_HPP__DATA_FALSE_INDICATOR
60 #define PB_DS_BIN_SEARCH_TREE_HPP__DATA_FALSE_INDICATOR
61 #include <ext/pb_ds/detail/bin_search_tree_/bin_search_tree_.hpp>
62 #endif // #ifndef PB_DS_BIN_SEARCH_TREE_HPP__DATA_FALSE_INDICATOR
63 #endif // #ifdef PB_DS_DATA_FALSE_INDICATOR
65 #include <ext/pb_ds/detail/standard_policies.hpp>
66 #include <ext/pb_ds/detail/basic_types.hpp>
76 #ifdef PB_DS_RB_TREE_DEBUG_
77 #define PB_DS_DBG_ASSERT(X) assert(X)
78 #define PB_DS_DBG_VERIFY(X) assert(X)
79 #define PB_DS_DBG_ONLY(X) X
80 #else // #ifdef PB_DS_RB_TREE_DEBUG_
81 #define PB_DS_DBG_ASSERT(X)
82 #define PB_DS_DBG_VERIFY(X) {if((X)==0);}
83 #define PB_DS_DBG_ONLY(X) ;
84 #endif // #ifdef PB_DS_RB_TREE_DEBUG_
86 #define PB_DS_CLASS_T_DEC \
91 class Node_And_It_Traits, \
94 #ifdef PB_DS_DATA_TRUE_INDICATOR
95 #define PB_DS_CLASS_NAME \
97 #endif // #ifdef PB_DS_DATA_TRUE_INDICATOR
99 #ifdef PB_DS_DATA_FALSE_INDICATOR
100 #define PB_DS_CLASS_NAME \
102 #endif // #ifdef PB_DS_DATA_FALSE_INDICATOR
104 #ifdef PB_DS_DATA_TRUE_INDICATOR
105 #define PB_DS_BASE_CLASS_NAME \
106 bin_search_tree_data_
107 #endif // #ifdef PB_DS_DATA_TRUE_INDICATOR
109 #ifdef PB_DS_DATA_FALSE_INDICATOR
110 #define PB_DS_BASE_CLASS_NAME \
111 bin_search_tree_no_data_
112 #endif // #ifdef PB_DS_DATA_FALSE_INDICATOR
114 #define PB_DS_CLASS_C_DEC \
119 Node_And_It_Traits, \
122 #define PB_DS_BASE_C_DEC \
123 PB_DS_BASE_CLASS_NAME< \
127 Node_And_It_Traits, \
130 #ifdef PB_DS_DATA_TRUE_INDICATOR
131 #define PB_DS_V2F(X) (X).first
132 #define PB_DS_V2S(X) (X).second
133 #define PB_DS_EP2VP(X)& ((X)->m_value)
134 #endif // #ifdef PB_DS_DATA_TRUE_INDICATOR
136 #ifdef PB_DS_DATA_FALSE_INDICATOR
137 #define PB_DS_V2F(X) (X)
138 #define PB_DS_V2S(X) Mapped_Data()
139 #define PB_DS_EP2VP(X)& ((X)->m_value.first)
140 #endif // #ifdef PB_DS_DATA_FALSE_INDICATOR
142 template<typename Key,
145 class Node_And_It_Traits,
147 class PB_DS_CLASS_NAME : public PB_DS_BASE_C_DEC
151 typedef typename PB_DS_BASE_C_DEC::node_pointer node_pointer;
155 typedef typename Allocator::size_type size_type;
157 typedef typename Allocator::difference_type difference_type;
159 typedef typename PB_DS_BASE_C_DEC::key_type key_type;
161 typedef typename PB_DS_BASE_C_DEC::key_pointer key_pointer;
163 typedef typename PB_DS_BASE_C_DEC::const_key_pointer const_key_pointer;
165 typedef typename PB_DS_BASE_C_DEC::key_reference key_reference;
168 typename PB_DS_BASE_C_DEC::const_key_reference
171 typedef typename PB_DS_BASE_C_DEC::mapped_type mapped_type;
173 typedef typename PB_DS_BASE_C_DEC::mapped_pointer mapped_pointer;
176 typename PB_DS_BASE_C_DEC::const_mapped_pointer
177 const_mapped_pointer;
179 typedef typename PB_DS_BASE_C_DEC::mapped_reference mapped_reference;
182 typename PB_DS_BASE_C_DEC::const_mapped_reference
183 const_mapped_reference;
185 typedef typename PB_DS_BASE_C_DEC::value_type value_type;
187 typedef typename PB_DS_BASE_C_DEC::pointer pointer;
189 typedef typename PB_DS_BASE_C_DEC::const_pointer const_pointer;
191 typedef typename PB_DS_BASE_C_DEC::reference reference;
193 typedef typename PB_DS_BASE_C_DEC::const_reference const_reference;
195 typedef typename PB_DS_BASE_C_DEC::point_iterator point_iterator;
197 typedef typename PB_DS_BASE_C_DEC::const_iterator const_point_iterator;
199 typedef typename PB_DS_BASE_C_DEC::iterator iterator;
201 typedef typename PB_DS_BASE_C_DEC::const_iterator const_iterator;
203 typedef typename PB_DS_BASE_C_DEC::reverse_iterator reverse_iterator;
206 typename PB_DS_BASE_C_DEC::const_reverse_iterator
207 const_reverse_iterator;
209 typedef Cmp_Fn cmp_fn;
211 typedef Allocator allocator;
213 typedef typename PB_DS_BASE_C_DEC::node_update node_update;
219 PB_DS_CLASS_NAME(const Cmp_Fn& r_cmp_fn);
221 PB_DS_CLASS_NAME(const Cmp_Fn& r_cmp_fn, const node_update& r_node_update);
223 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC& other);
226 swap(PB_DS_CLASS_C_DEC& other);
228 template<typename It>
230 copy_from_range(It first_it, It last_it);
235 insert(const_reference r_value);
237 inline mapped_reference
238 operator[](const_key_reference r_key)
240 #ifdef PB_DS_DATA_TRUE_INDICATOR
241 PB_DS_DBG_ONLY(assert_valid();)
243 std::pair<point_iterator, bool> ins_pair =
244 PB_DS_BASE_C_DEC::insert_leaf(
249 if (ins_pair.second == true)
251 ins_pair.first.m_p_nd->m_red = true;
253 PB_DS_DBG_ONLY(this->structure_only_assert_valid();)
255 insert_fixup(ins_pair.first.m_p_nd);
258 PB_DS_DBG_ONLY(assert_valid();)
260 return (ins_pair.first.m_p_nd->m_value.second);
261 #else // #ifdef PB_DS_DATA_TRUE_INDICATOR
264 return (PB_DS_BASE_C_DEC::s_null_mapped);
265 #endif // #ifdef PB_DS_DATA_TRUE
269 erase(const_key_reference r_key);
274 inline reverse_iterator
275 erase(reverse_iterator it);
277 template<typename Pred>
282 join(PB_DS_CLASS_C_DEC& other);
285 split(const_key_reference r_key, PB_DS_CLASS_C_DEC& other);
291 #ifdef PB_DS_RB_TREE_DEBUG_
294 assert_valid() const;
297 assert_node_consistent(const node_pointer p_nd) const;
299 #endif // #ifdef PB_DS_RB_TREE_DEBUG_
302 is_effectively_black(const node_pointer p_nd);
308 insert_fixup(node_pointer p_nd);
311 erase_node(node_pointer p_nd);
314 remove_node(node_pointer p_nd);
317 remove_fixup(node_pointer p_x, node_pointer p_new_x_parent);
320 split_imp(node_pointer p_nd, PB_DS_CLASS_C_DEC& other);
325 std::pair<node_pointer, node_pointer>
329 join_imp(node_pointer p_x, node_pointer p_r);
331 std::pair<node_pointer, node_pointer>
332 find_join_pos_right(node_pointer p_l, size_type h_l, size_type h_r);
334 std::pair<node_pointer, node_pointer>
335 find_join_pos_left(node_pointer p_r, size_type h_l, size_type h_r);
338 black_height(node_pointer p_nd);
341 split_at_node(node_pointer p_nd, PB_DS_CLASS_C_DEC& other);
345 #include <ext/pb_ds/detail/rb_tree_map_/constructors_destructor_fn_imps.hpp>
346 #include <ext/pb_ds/detail/rb_tree_map_/insert_fn_imps.hpp>
347 #include <ext/pb_ds/detail/rb_tree_map_/erase_fn_imps.hpp>
348 #include <ext/pb_ds/detail/rb_tree_map_/debug_fn_imps.hpp>
349 #include <ext/pb_ds/detail/rb_tree_map_/split_join_fn_imps.hpp>
350 #include <ext/pb_ds/detail/rb_tree_map_/info_fn_imps.hpp>
352 #undef PB_DS_CLASS_T_DEC
354 #undef PB_DS_CLASS_C_DEC
356 #undef PB_DS_CLASS_NAME
358 #undef PB_DS_BASE_CLASS_NAME
360 #undef PB_DS_BASE_C_DEC
362 #undef PB_DS_DBG_ASSERT
363 #undef PB_DS_DBG_VERIFY
364 #undef PB_DS_DBG_ONLY
370 } // namespace detail