]>
Commit | Line | Data |
---|---|---|
4569a895 AT |
1 | // -*- C++ -*- |
2 | ||
f5886803 | 3 | // Copyright (C) 2005, 2006, 2009, 2010, 2011 Free Software Foundation, Inc. |
4569a895 AT |
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 terms | |
7 | // of the GNU General Public License as published by the Free Software | |
748086b7 | 8 | // Foundation; either version 3, or (at your option) any later |
4569a895 AT |
9 | // version. |
10 | ||
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. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
4569a895 | 19 | |
748086b7 JJ |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
4569a895 AT |
24 | |
25 | // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. | |
26 | ||
27 | // Permission to use, copy, modify, sell, and distribute this software | |
28 | // is hereby granted without fee, provided that the above copyright | |
29 | // notice appears in all copies, and that both that copyright notice | |
30 | // and this permission notice appear in supporting documentation. None | |
31 | // of the above authors, nor IBM Haifa Research Laboratories, make any | |
32 | // representation about the suitability of this software for any | |
33 | // purpose. It is provided "as is" without express or implied | |
34 | // warranty. | |
35 | ||
36 | /** | |
a345e45d BK |
37 | * @file rb_tree_map_/rb_tree_.hpp |
38 | * Contains an implementation for Red Black trees. | |
4569a895 | 39 | */ |
4569a895 AT |
40 | |
41 | #include <ext/pb_ds/detail/standard_policies.hpp> | |
4569a895 AT |
42 | #include <utility> |
43 | #include <vector> | |
44 | #include <assert.h> | |
47bea7b8 | 45 | #include <debug/debug.h> |
4569a895 | 46 | |
5e11f978 | 47 | namespace __gnu_pbds |
4569a895 AT |
48 | { |
49 | namespace detail | |
50 | { | |
1b24692f BK |
51 | #define PB_DS_CLASS_T_DEC \ |
52 | template<typename Key, typename Mapped, typename Cmp_Fn, \ | |
a345e45d | 53 | typename Node_And_It_Traits, typename _Alloc> |
4569a895 AT |
54 | |
55 | #ifdef PB_DS_DATA_TRUE_INDICATOR | |
a345e45d BK |
56 | # define PB_DS_RB_TREE_NAME rb_tree_map |
57 | # define PB_DS_RB_TREE_BASE_NAME bin_search_tree_map | |
58 | #endif | |
4569a895 AT |
59 | |
60 | #ifdef PB_DS_DATA_FALSE_INDICATOR | |
a345e45d BK |
61 | # define PB_DS_RB_TREE_NAME rb_tree_set |
62 | # define PB_DS_RB_TREE_BASE_NAME bin_search_tree_set | |
63 | #endif | |
4569a895 | 64 | |
1b24692f | 65 | #define PB_DS_CLASS_C_DEC \ |
a345e45d | 66 | PB_DS_RB_TREE_NAME<Key, Mapped, Cmp_Fn, Node_And_It_Traits, _Alloc> |
4569a895 | 67 | |
a345e45d BK |
68 | #define PB_DS_RB_TREE_BASE \ |
69 | PB_DS_RB_TREE_BASE_NAME<Key, Mapped, Cmp_Fn, Node_And_It_Traits, _Alloc> | |
4569a895 | 70 | |
4569a895 | 71 | |
a345e45d BK |
72 | /* |
73 | * @brief Red-Black tree. | |
74 | * | |
75 | * This implementation uses an idea from the SGI STL (using a | |
76 | * @a header node which is needed for efficient iteration). | |
77 | */ | |
4569a895 AT |
78 | template<typename Key, |
79 | typename Mapped, | |
1b24692f BK |
80 | typename Cmp_Fn, |
81 | typename Node_And_It_Traits, | |
a345e45d BK |
82 | typename _Alloc> |
83 | class PB_DS_RB_TREE_NAME : public PB_DS_RB_TREE_BASE | |
4569a895 | 84 | { |
4569a895 | 85 | private: |
a345e45d BK |
86 | typedef PB_DS_RB_TREE_BASE base_type; |
87 | typedef typename base_type::node_pointer node_pointer; | |
4569a895 AT |
88 | |
89 | public: | |
a345e45d BK |
90 | typedef rb_tree_tag container_category; |
91 | typedef Cmp_Fn cmp_fn; | |
92 | typedef _Alloc allocator_type; | |
93 | typedef typename _Alloc::size_type size_type; | |
94 | typedef typename _Alloc::difference_type difference_type; | |
95 | typedef typename base_type::key_type key_type; | |
96 | typedef typename base_type::key_pointer key_pointer; | |
97 | typedef typename base_type::key_const_pointer key_const_pointer; | |
98 | typedef typename base_type::key_reference key_reference; | |
99 | typedef typename base_type::key_const_reference key_const_reference; | |
100 | typedef typename base_type::mapped_type mapped_type; | |
101 | typedef typename base_type::mapped_pointer mapped_pointer; | |
102 | typedef typename base_type::mapped_const_pointer mapped_const_pointer; | |
103 | typedef typename base_type::mapped_reference mapped_reference; | |
104 | typedef typename base_type::mapped_const_reference mapped_const_reference; | |
105 | typedef typename base_type::value_type value_type; | |
106 | typedef typename base_type::pointer pointer; | |
107 | typedef typename base_type::const_pointer const_pointer; | |
108 | typedef typename base_type::reference reference; | |
109 | typedef typename base_type::const_reference const_reference; | |
110 | typedef typename base_type::point_iterator point_iterator; | |
111 | typedef typename base_type::const_iterator point_const_iterator; | |
112 | typedef typename base_type::iterator iterator; | |
113 | typedef typename base_type::const_iterator const_iterator; | |
114 | typedef typename base_type::reverse_iterator reverse_iterator; | |
1b24692f | 115 | typedef typename base_type::const_reverse_iterator const_reverse_iterator; |
a345e45d | 116 | typedef typename base_type::node_update node_update; |
4569a895 | 117 | |
a345e45d | 118 | PB_DS_RB_TREE_NAME(); |
4569a895 | 119 | |
a345e45d | 120 | PB_DS_RB_TREE_NAME(const Cmp_Fn&); |
4569a895 | 121 | |
a345e45d | 122 | PB_DS_RB_TREE_NAME(const Cmp_Fn&, const node_update&); |
4569a895 | 123 | |
a345e45d | 124 | PB_DS_RB_TREE_NAME(const PB_DS_CLASS_C_DEC&); |
4569a895 AT |
125 | |
126 | void | |
1b24692f | 127 | swap(PB_DS_CLASS_C_DEC&); |
4569a895 AT |
128 | |
129 | template<typename It> | |
130 | void | |
1b24692f | 131 | copy_from_range(It, It); |
4569a895 | 132 | |
47bea7b8 | 133 | inline std::pair<point_iterator, bool> |
1b24692f | 134 | insert(const_reference); |
4569a895 AT |
135 | |
136 | inline mapped_reference | |
a345e45d | 137 | operator[](key_const_reference r_key) |
4569a895 AT |
138 | { |
139 | #ifdef PB_DS_DATA_TRUE_INDICATOR | |
f5886803 | 140 | _GLIBCXX_DEBUG_ONLY(assert_valid(__FILE__, __LINE__);) |
47bea7b8 | 141 | std::pair<point_iterator, bool> ins_pair = |
1b24692f | 142 | base_type::insert_leaf(value_type(r_key, mapped_type())); |
4569a895 AT |
143 | |
144 | if (ins_pair.second == true) | |
145 | { | |
146 | ins_pair.first.m_p_nd->m_red = true; | |
f5886803 | 147 | _GLIBCXX_DEBUG_ONLY(this->structure_only_assert_valid(__FILE__, __LINE__);) |
47bea7b8 | 148 | insert_fixup(ins_pair.first.m_p_nd); |
4569a895 | 149 | } |
f5886803 | 150 | _GLIBCXX_DEBUG_ONLY(assert_valid(__FILE__, __LINE__);) |
1b24692f | 151 | return ins_pair.first.m_p_nd->m_value.second; |
a345e45d | 152 | #else |
4569a895 | 153 | insert(r_key); |
a345e45d BK |
154 | return base_type::s_null_type; |
155 | #endif | |
4569a895 AT |
156 | } |
157 | ||
158 | inline bool | |
a345e45d | 159 | erase(key_const_reference); |
4569a895 AT |
160 | |
161 | inline iterator | |
1b24692f | 162 | erase(iterator); |
4569a895 AT |
163 | |
164 | inline reverse_iterator | |
1b24692f | 165 | erase(reverse_iterator); |
4569a895 AT |
166 | |
167 | template<typename Pred> | |
168 | inline size_type | |
1b24692f | 169 | erase_if(Pred); |
4569a895 AT |
170 | |
171 | void | |
1b24692f | 172 | join(PB_DS_CLASS_C_DEC&); |
4569a895 AT |
173 | |
174 | void | |
a345e45d | 175 | split(key_const_reference, PB_DS_CLASS_C_DEC&); |
4569a895 AT |
176 | |
177 | private: | |
178 | ||
47bea7b8 | 179 | #ifdef _GLIBCXX_DEBUG |
4569a895 | 180 | void |
a345e45d | 181 | assert_valid(const char*, int) const; |
4569a895 AT |
182 | |
183 | size_type | |
a345e45d BK |
184 | assert_node_consistent(const node_pointer, const char*, int) const; |
185 | #endif | |
4569a895 AT |
186 | |
187 | inline static bool | |
1b24692f | 188 | is_effectively_black(const node_pointer); |
4569a895 AT |
189 | |
190 | void | |
191 | initialize(); | |
192 | ||
193 | void | |
1b24692f | 194 | insert_fixup(node_pointer); |
4569a895 AT |
195 | |
196 | void | |
1b24692f | 197 | erase_node(node_pointer); |
4569a895 AT |
198 | |
199 | void | |
1b24692f | 200 | remove_node(node_pointer); |
4569a895 AT |
201 | |
202 | void | |
1b24692f | 203 | remove_fixup(node_pointer, node_pointer); |
4569a895 AT |
204 | |
205 | void | |
1b24692f | 206 | split_imp(node_pointer, PB_DS_CLASS_C_DEC&); |
4569a895 AT |
207 | |
208 | inline node_pointer | |
209 | split_min(); | |
210 | ||
211 | std::pair<node_pointer, node_pointer> | |
212 | split_min_imp(); | |
213 | ||
214 | void | |
1b24692f | 215 | join_imp(node_pointer, node_pointer); |
4569a895 AT |
216 | |
217 | std::pair<node_pointer, node_pointer> | |
1b24692f | 218 | find_join_pos_right(node_pointer, size_type, size_type); |
4569a895 AT |
219 | |
220 | std::pair<node_pointer, node_pointer> | |
1b24692f | 221 | find_join_pos_left(node_pointer, size_type, size_type); |
4569a895 AT |
222 | |
223 | inline size_type | |
1b24692f | 224 | black_height(node_pointer); |
4569a895 AT |
225 | |
226 | void | |
1b24692f | 227 | split_at_node(node_pointer, PB_DS_CLASS_C_DEC&); |
4569a895 AT |
228 | }; |
229 | ||
f5886803 FD |
230 | #define PB_DS_STRUCT_ONLY_ASSERT_VALID(X) \ |
231 | _GLIBCXX_DEBUG_ONLY(X.structure_only_assert_valid(__FILE__, __LINE__);) | |
232 | ||
4569a895 AT |
233 | #include <ext/pb_ds/detail/rb_tree_map_/constructors_destructor_fn_imps.hpp> |
234 | #include <ext/pb_ds/detail/rb_tree_map_/insert_fn_imps.hpp> | |
235 | #include <ext/pb_ds/detail/rb_tree_map_/erase_fn_imps.hpp> | |
236 | #include <ext/pb_ds/detail/rb_tree_map_/debug_fn_imps.hpp> | |
237 | #include <ext/pb_ds/detail/rb_tree_map_/split_join_fn_imps.hpp> | |
238 | #include <ext/pb_ds/detail/rb_tree_map_/info_fn_imps.hpp> | |
239 | ||
f5886803 | 240 | #undef PB_DS_STRUCT_ONLY_ASSERT_VALID |
4569a895 | 241 | #undef PB_DS_CLASS_T_DEC |
4569a895 | 242 | #undef PB_DS_CLASS_C_DEC |
a345e45d BK |
243 | #undef PB_DS_RB_TREE_NAME |
244 | #undef PB_DS_RB_TREE_BASE_NAME | |
245 | #undef PB_DS_RB_TREE_BASE | |
4569a895 | 246 | } // namespace detail |
5e11f978 | 247 | } // namespace __gnu_pbds |