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>
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>
65 #include <ext/pb_ds/detail/standard_policies.hpp>
66 #include <ext/pb_ds/detail/basic_types.hpp>
70 #include <debug/debug.h>
76 #define PB_DS_CLASS_T_DEC \
77 template<typename Key, typename Mapped, typename Cmp_Fn, \
78 typename Node_And_It_Traits, typename Allocator>
80 #ifdef PB_DS_DATA_TRUE_INDICATOR
81 #define PB_DS_CLASS_NAME rb_tree_data_
84 #ifdef PB_DS_DATA_TRUE_INDICATOR
85 #define PB_DS_BASE_CLASS_NAME bin_search_tree_data_
88 #ifdef PB_DS_DATA_FALSE_INDICATOR
89 #define PB_DS_CLASS_NAME rb_tree_no_data_
92 #ifdef PB_DS_DATA_FALSE_INDICATOR
93 #define PB_DS_BASE_CLASS_NAME bin_search_tree_no_data_
96 #define PB_DS_CLASS_C_DEC \
97 PB_DS_CLASS_NAME<Key, Mapped, Cmp_Fn, Node_And_It_Traits, Allocator>
99 #define PB_DS_BASE_C_DEC \
100 PB_DS_BASE_CLASS_NAME<Key, Mapped, Cmp_Fn, Node_And_It_Traits, Allocator>
102 #ifdef PB_DS_DATA_TRUE_INDICATOR
103 #define PB_DS_V2F(X) (X).first
104 #define PB_DS_V2S(X) (X).second
105 #define PB_DS_EP2VP(X)& ((X)->m_value)
108 #ifdef PB_DS_DATA_FALSE_INDICATOR
109 #define PB_DS_V2F(X) (X)
110 #define PB_DS_V2S(X) Mapped_Data()
111 #define PB_DS_EP2VP(X)& ((X)->m_value.first)
114 template<typename Key,
117 typename Node_And_It_Traits,
119 class PB_DS_CLASS_NAME : public PB_DS_BASE_C_DEC
122 typedef PB_DS_BASE_C_DEC base_type;
123 typedef typename base_type::node_pointer node_pointer;
126 typedef Cmp_Fn cmp_fn;
127 typedef Allocator allocator;
128 typedef typename Allocator::size_type size_type;
129 typedef typename Allocator::difference_type difference_type;
130 typedef typename base_type::key_type key_type;
131 typedef typename base_type::key_pointer key_pointer;
132 typedef typename base_type::const_key_pointer const_key_pointer;
133 typedef typename base_type::key_reference key_reference;
134 typedef typename base_type::const_key_reference const_key_reference;
135 typedef typename base_type::mapped_type mapped_type;
136 typedef typename base_type::mapped_pointer mapped_pointer;
137 typedef typename base_type::const_mapped_pointer const_mapped_pointer;
138 typedef typename base_type::mapped_reference mapped_reference;
139 typedef typename base_type::const_mapped_reference const_mapped_reference;
140 typedef typename base_type::value_type value_type;
141 typedef typename base_type::pointer pointer;
142 typedef typename base_type::const_pointer const_pointer;
143 typedef typename base_type::reference reference;
144 typedef typename base_type::const_reference const_reference;
145 typedef typename base_type::point_iterator point_iterator;
146 typedef typename base_type::const_iterator const_point_iterator;
147 typedef typename base_type::iterator iterator;
148 typedef typename base_type::const_iterator const_iterator;
149 typedef typename base_type::reverse_iterator reverse_iterator;
150 typedef typename base_type::const_reverse_iterator const_reverse_iterator;
151 typedef typename base_type::node_update node_update;
156 PB_DS_CLASS_NAME(const Cmp_Fn&);
158 PB_DS_CLASS_NAME(const Cmp_Fn&, const node_update&);
160 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC&);
163 swap(PB_DS_CLASS_C_DEC&);
165 template<typename It>
167 copy_from_range(It, It);
169 inline std::pair<point_iterator, bool>
170 insert(const_reference);
172 inline mapped_reference
173 operator[](const_key_reference r_key)
175 #ifdef PB_DS_DATA_TRUE_INDICATOR
176 _GLIBCXX_DEBUG_ONLY(assert_valid();)
177 std::pair<point_iterator, bool> ins_pair =
178 base_type::insert_leaf(value_type(r_key, mapped_type()));
180 if (ins_pair.second == true)
182 ins_pair.first.m_p_nd->m_red = true;
183 _GLIBCXX_DEBUG_ONLY(this->structure_only_assert_valid();)
184 insert_fixup(ins_pair.first.m_p_nd);
186 _GLIBCXX_DEBUG_ONLY(assert_valid();)
187 return ins_pair.first.m_p_nd->m_value.second;
190 return base_type::s_null_mapped;
195 erase(const_key_reference);
200 inline reverse_iterator
201 erase(reverse_iterator);
203 template<typename Pred>
208 join(PB_DS_CLASS_C_DEC&);
211 split(const_key_reference, PB_DS_CLASS_C_DEC&);
217 #ifdef _GLIBCXX_DEBUG
219 assert_valid() const;
222 assert_node_consistent(const node_pointer) const;
226 is_effectively_black(const node_pointer);
232 insert_fixup(node_pointer);
235 erase_node(node_pointer);
238 remove_node(node_pointer);
241 remove_fixup(node_pointer, node_pointer);
244 split_imp(node_pointer, PB_DS_CLASS_C_DEC&);
249 std::pair<node_pointer, node_pointer>
253 join_imp(node_pointer, node_pointer);
255 std::pair<node_pointer, node_pointer>
256 find_join_pos_right(node_pointer, size_type, size_type);
258 std::pair<node_pointer, node_pointer>
259 find_join_pos_left(node_pointer, size_type, size_type);
262 black_height(node_pointer);
265 split_at_node(node_pointer, PB_DS_CLASS_C_DEC&);
268 #include <ext/pb_ds/detail/rb_tree_map_/constructors_destructor_fn_imps.hpp>
269 #include <ext/pb_ds/detail/rb_tree_map_/insert_fn_imps.hpp>
270 #include <ext/pb_ds/detail/rb_tree_map_/erase_fn_imps.hpp>
271 #include <ext/pb_ds/detail/rb_tree_map_/debug_fn_imps.hpp>
272 #include <ext/pb_ds/detail/rb_tree_map_/split_join_fn_imps.hpp>
273 #include <ext/pb_ds/detail/rb_tree_map_/info_fn_imps.hpp>
275 #undef PB_DS_CLASS_T_DEC
276 #undef PB_DS_CLASS_C_DEC
277 #undef PB_DS_CLASS_NAME
278 #undef PB_DS_BASE_CLASS_NAME
279 #undef PB_DS_BASE_C_DEC
284 } // namespace detail