]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/ext/pb_ds/detail/bin_search_tree_/bin_search_tree_.hpp
re PR libstdc++/37144 (A bug in include/ext/pb_ds/detail/pat_trie_/constructors_destr...
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / bin_search_tree_ / bin_search_tree_.hpp
CommitLineData
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 bin_search_tree_/bin_search_tree_.hpp
38 * Contains an implementation class for binary search tree.
4569a895
AT
39 */
40
41#include <ext/pb_ds/exception.hpp>
a345e45d 42#include <ext/pb_ds/tree_policy.hpp>
4569a895
AT
43#include <ext/pb_ds/detail/eq_fn/eq_by_less.hpp>
44#include <ext/pb_ds/detail/types_traits.hpp>
4569a895
AT
45#include <ext/pb_ds/detail/cond_dealtor.hpp>
46#include <ext/pb_ds/detail/type_utils.hpp>
47#include <ext/pb_ds/detail/tree_trace_base.hpp>
a345e45d
BK
48#ifdef _GLIBCXX_DEBUG
49#include <ext/pb_ds/detail/debug_map_base.hpp>
50#endif
4569a895
AT
51#include <utility>
52#include <functional>
47bea7b8 53#include <debug/debug.h>
4569a895 54
5e11f978 55namespace __gnu_pbds
4569a895
AT
56{
57 namespace detail
58 {
4569a895 59#ifdef PB_DS_DATA_TRUE_INDICATOR
a345e45d
BK
60#define PB_DS_BIN_TREE_NAME bin_search_tree_map
61#endif
4569a895
AT
62
63#ifdef PB_DS_DATA_FALSE_INDICATOR
a345e45d
BK
64#define PB_DS_BIN_TREE_NAME bin_search_tree_set
65#endif
4569a895 66
a345e45d
BK
67#define PB_DS_CLASS_T_DEC \
68 template<typename Key, typename Mapped, typename Cmp_Fn, \
69 typename Node_And_It_Traits, typename _Alloc>
4569a895 70
a345e45d
BK
71#define PB_DS_CLASS_C_DEC \
72 PB_DS_BIN_TREE_NAME<Key, Mapped, Cmp_Fn, Node_And_It_Traits, _Alloc>
4569a895 73
a345e45d
BK
74#define PB_DS_BIN_TREE_TRAITS_BASE \
75 types_traits<Key, Mapped, _Alloc, false>
76
77#ifdef _GLIBCXX_DEBUG
78#define PB_DS_DEBUG_MAP_BASE_C_DEC \
79 debug_map_base<Key, eq_by_less<Key, Cmp_Fn>, \
80 typename _Alloc::template rebind<Key>::other::const_reference>
81#endif
4569a895
AT
82
83#ifdef PB_DS_TREE_TRACE
a345e45d
BK
84#define PB_DS_TREE_TRACE_BASE_C_DEC \
85 tree_trace_base<typename Node_And_It_Traits::node_const_iterator, \
86 typename Node_And_It_Traits::node_iterator, \
87 Cmp_Fn, true, _Alloc>
88#endif
89
90
91 /*
92 * @brief Binary search tree (BST).
93 *
94 * This implementation uses an idea from the SGI STL (using a @a
95 * header node which is needed for efficient iteration).
96 */
97 template<typename Key, typename Mapped, typename Cmp_Fn,
98 typename Node_And_It_Traits, typename _Alloc>
99 class PB_DS_BIN_TREE_NAME :
47bea7b8 100#ifdef _GLIBCXX_DEBUG
551fe1a2 101 public PB_DS_DEBUG_MAP_BASE_C_DEC,
a345e45d 102#endif
4569a895
AT
103#ifdef PB_DS_TREE_TRACE
104 public PB_DS_TREE_TRACE_BASE_C_DEC,
a345e45d 105#endif
4569a895 106 public Cmp_Fn,
a345e45d 107 public PB_DS_BIN_TREE_TRAITS_BASE,
4569a895
AT
108 public Node_And_It_Traits::node_update
109 {
a345e45d 110 typedef Node_And_It_Traits traits_type;
4569a895
AT
111
112 protected:
a345e45d
BK
113 typedef PB_DS_BIN_TREE_TRAITS_BASE traits_base;
114
4569a895 115 typedef
a345e45d 116 typename _Alloc::template rebind<typename traits_type::node>::other
4569a895
AT
117 node_allocator;
118
a345e45d
BK
119 typedef typename node_allocator::value_type node;
120 typedef typename node_allocator::pointer node_pointer;
4569a895 121
a345e45d 122 typedef typename traits_type::null_node_update_pointer
4569a895
AT
123 null_node_update_pointer;
124
125 private:
a345e45d 126 typedef cond_dealtor<node, _Alloc> cond_dealtor_t;
4569a895 127
47bea7b8 128#ifdef _GLIBCXX_DEBUG
a345e45d
BK
129 typedef PB_DS_DEBUG_MAP_BASE_C_DEC debug_base;
130#endif
4569a895
AT
131
132 public:
a345e45d
BK
133 typedef typename _Alloc::size_type size_type;
134 typedef typename _Alloc::difference_type difference_type;
135 typedef typename traits_base::key_type key_type;
136 typedef typename traits_base::key_pointer key_pointer;
137 typedef typename traits_base::key_const_pointer key_const_pointer;
138 typedef typename traits_base::key_reference key_reference;
139 typedef typename traits_base::key_const_reference key_const_reference;
4569a895
AT
140
141#ifdef PB_DS_DATA_TRUE_INDICATOR
a345e45d
BK
142 typedef typename traits_base::mapped_type mapped_type;
143 typedef typename traits_base::mapped_pointer mapped_pointer;
144 typedef typename traits_base::mapped_const_pointer mapped_const_pointer;
145 typedef typename traits_base::mapped_reference mapped_reference;
146 typedef typename traits_base::mapped_const_reference mapped_const_reference;
147#endif
4569a895 148
a345e45d
BK
149 typedef typename traits_base::value_type value_type;
150 typedef typename traits_base::pointer pointer;
151 typedef typename traits_base::const_pointer const_pointer;
152 typedef typename traits_base::reference reference;
153 typedef typename traits_base::const_reference const_reference;
154 typedef typename traits_type::point_const_iterator point_const_iterator;
4569a895 155
a345e45d
BK
156 typedef point_const_iterator const_iterator;
157 typedef typename traits_type::point_iterator point_iterator;
158 typedef point_iterator iterator;
4569a895 159
a345e45d 160 typedef typename traits_type::const_reverse_iterator const_reverse_iterator;
4569a895 161
a345e45d
BK
162 typedef typename traits_type::reverse_iterator reverse_iterator;
163 typedef typename traits_type::node_const_iterator node_const_iterator;
164 typedef typename traits_type::node_iterator node_iterator;
165 typedef typename traits_type::node_update node_update;
4569a895 166
a345e45d
BK
167 typedef Cmp_Fn cmp_fn;
168 typedef _Alloc allocator_type;
4569a895 169
a345e45d 170 PB_DS_BIN_TREE_NAME();
4569a895 171
a345e45d 172 PB_DS_BIN_TREE_NAME(const Cmp_Fn&);
4569a895 173
a345e45d 174 PB_DS_BIN_TREE_NAME(const Cmp_Fn&, const node_update&);
4569a895 175
a345e45d 176 PB_DS_BIN_TREE_NAME(const PB_DS_CLASS_C_DEC&);
4569a895
AT
177
178 void
a345e45d 179 swap(PB_DS_CLASS_C_DEC&);
4569a895 180
a345e45d 181 ~PB_DS_BIN_TREE_NAME();
4569a895
AT
182
183 inline bool
184 empty() const;
185
186 inline size_type
187 size() const;
188
189 inline size_type
190 max_size() const;
191
a345e45d 192 Cmp_Fn&
4569a895
AT
193 get_cmp_fn();
194
a345e45d 195 const Cmp_Fn&
4569a895
AT
196 get_cmp_fn() const;
197
198 inline point_iterator
a345e45d 199 lower_bound(key_const_reference);
4569a895 200
a345e45d
BK
201 inline point_const_iterator
202 lower_bound(key_const_reference) const;
4569a895
AT
203
204 inline point_iterator
a345e45d 205 upper_bound(key_const_reference);
4569a895 206
a345e45d
BK
207 inline point_const_iterator
208 upper_bound(key_const_reference) const;
4569a895
AT
209
210 inline point_iterator
a345e45d 211 find(key_const_reference);
4569a895 212
a345e45d
BK
213 inline point_const_iterator
214 find(key_const_reference) const;
4569a895
AT
215
216 inline iterator
217 begin();
218
219 inline const_iterator
220 begin() const;
221
222 inline iterator
223 end();
224
225 inline const_iterator
226 end() const;
227
228 inline reverse_iterator
229 rbegin();
230
231 inline const_reverse_iterator
232 rbegin() const;
233
234 inline reverse_iterator
235 rend();
236
237 inline const_reverse_iterator
238 rend() const;
239
a345e45d 240 inline node_const_iterator
4569a895
AT
241 node_begin() const;
242
243 inline node_iterator
244 node_begin();
245
a345e45d 246 inline node_const_iterator
4569a895
AT
247 node_end() const;
248
249 inline node_iterator
250 node_end();
251
252 void
253 clear();
254
255 protected:
4569a895 256 void
a345e45d 257 value_swap(PB_DS_CLASS_C_DEC&);
4569a895
AT
258
259 void
260 initialize_min_max();
261
262 inline iterator
a345e45d 263 insert_imp_empty(const_reference);
4569a895
AT
264
265 inline iterator
a345e45d 266 insert_leaf_new(const_reference, node_pointer, bool);
4569a895
AT
267
268 inline node_pointer
a345e45d 269 get_new_node_for_leaf_insert(const_reference, false_type);
4569a895
AT
270
271 inline node_pointer
a345e45d 272 get_new_node_for_leaf_insert(const_reference, true_type);
4569a895
AT
273
274 inline void
a345e45d 275 actual_erase_node(node_pointer);
4569a895
AT
276
277 inline std::pair<node_pointer, bool>
a345e45d 278 erase(node_pointer);
4569a895
AT
279
280 inline void
a345e45d 281 update_min_max_for_erased_node(node_pointer);
4569a895
AT
282
283 static void
a345e45d 284 clear_imp(node_pointer);
4569a895 285
a345e45d
BK
286 inline std::pair<point_iterator, bool>
287 insert_leaf(const_reference);
4569a895
AT
288
289 inline void
a345e45d 290 rotate_left(node_pointer);
4569a895
AT
291
292 inline void
a345e45d 293 rotate_right(node_pointer);
4569a895
AT
294
295 inline void
a345e45d 296 rotate_parent(node_pointer);
4569a895
AT
297
298 inline void
a345e45d 299 apply_update(node_pointer, null_node_update_pointer);
4569a895
AT
300
301 template<typename Node_Update_>
a345e45d
BK
302 inline void
303 apply_update(node_pointer, Node_Update_*);
4569a895
AT
304
305 inline void
a345e45d 306 update_to_top(node_pointer, null_node_update_pointer);
4569a895
AT
307
308 template<typename Node_Update_>
a345e45d
BK
309 inline void
310 update_to_top(node_pointer, Node_Update_*);
4569a895
AT
311
312 bool
a345e45d 313 join_prep(PB_DS_CLASS_C_DEC&);
4569a895
AT
314
315 void
a345e45d 316 join_finish(PB_DS_CLASS_C_DEC&);
4569a895
AT
317
318 bool
a345e45d 319 split_prep(key_const_reference, PB_DS_CLASS_C_DEC&);
4569a895
AT
320
321 void
a345e45d 322 split_finish(PB_DS_CLASS_C_DEC&);
4569a895
AT
323
324 size_type
a345e45d 325 recursive_count(node_pointer) const;
4569a895 326
47bea7b8 327#ifdef _GLIBCXX_DEBUG
4569a895 328 void
a345e45d 329 assert_valid(const char*, int) const;
4569a895
AT
330
331 void
a345e45d 332 structure_only_assert_valid(const char*, int) const;
4569a895
AT
333
334 void
a345e45d
BK
335 assert_node_consistent(const node_pointer, const char*, int) const;
336#endif
4569a895
AT
337
338 private:
47bea7b8 339#ifdef _GLIBCXX_DEBUG
4569a895 340 void
a345e45d 341 assert_iterators(const char*, int) const;
4569a895
AT
342
343 void
a345e45d 344 assert_consistent_with_debug_base(const char*, int) const;
4569a895
AT
345
346 void
a345e45d
BK
347 assert_node_consistent_with_left(const node_pointer,
348 const char*, int) const;
4569a895
AT
349
350 void
a345e45d
BK
351 assert_node_consistent_with_right(const node_pointer,
352 const char*, int) const;
4569a895
AT
353
354 void
a345e45d
BK
355 assert_consistent_with_debug_base(const node_pointer,
356 const char*, int) const;
4569a895
AT
357
358 void
a345e45d 359 assert_min(const char*, int) const;
4569a895
AT
360
361 void
a345e45d 362 assert_min_imp(const node_pointer, const char*, int) const;
4569a895
AT
363
364 void
a345e45d 365 assert_max(const char*, int) const;
4569a895
AT
366
367 void
a345e45d 368 assert_max_imp(const node_pointer, const char*, int) const;
4569a895
AT
369
370 void
a345e45d 371 assert_size(const char*, int) const;
4569a895 372
a345e45d 373 typedef std::pair<const_pointer, const_pointer> node_consistent_t;
4569a895
AT
374
375 node_consistent_t
a345e45d
BK
376 assert_node_consistent_(const node_pointer, const char*, int) const;
377#endif
4569a895
AT
378
379 void
380 initialize();
381
382 node_pointer
a345e45d 383 recursive_copy_node(const node_pointer);
4569a895
AT
384
385 protected:
a345e45d
BK
386 node_pointer m_p_head;
387 size_type m_size;
388 static node_allocator s_node_allocator;
4569a895
AT
389 };
390
f5886803
FD
391#define PB_DS_STRUCT_ONLY_ASSERT_VALID(X) \
392 _GLIBCXX_DEBUG_ONLY(X.structure_only_assert_valid(__FILE__, __LINE__);)
393
394#define PB_DS_ASSERT_NODE_CONSISTENT(_Node) \
395 _GLIBCXX_DEBUG_ONLY(assert_node_consistent(_Node, __FILE__, __LINE__);)
396
4569a895
AT
397#include <ext/pb_ds/detail/bin_search_tree_/constructors_destructor_fn_imps.hpp>
398#include <ext/pb_ds/detail/bin_search_tree_/iterators_fn_imps.hpp>
399#include <ext/pb_ds/detail/bin_search_tree_/debug_fn_imps.hpp>
400#include <ext/pb_ds/detail/bin_search_tree_/insert_fn_imps.hpp>
401#include <ext/pb_ds/detail/bin_search_tree_/erase_fn_imps.hpp>
402#include <ext/pb_ds/detail/bin_search_tree_/find_fn_imps.hpp>
403#include <ext/pb_ds/detail/bin_search_tree_/info_fn_imps.hpp>
404#include <ext/pb_ds/detail/bin_search_tree_/split_join_fn_imps.hpp>
405#include <ext/pb_ds/detail/bin_search_tree_/rotate_fn_imps.hpp>
406#include <ext/pb_ds/detail/bin_search_tree_/policy_access_fn_imps.hpp>
407
f5886803
FD
408#undef PB_DS_ASSERT_NODE_CONSISTENT
409#undef PB_DS_STRUCT_ONLY_ASSERT_VALID
4569a895 410#undef PB_DS_CLASS_C_DEC
4569a895 411#undef PB_DS_CLASS_T_DEC
a345e45d
BK
412#undef PB_DS_BIN_TREE_NAME
413#undef PB_DS_BIN_TREE_TRAITS_BASE
551fe1a2 414#undef PB_DS_DEBUG_MAP_BASE_C_DEC
4569a895
AT
415
416#ifdef PB_DS_TREE_TRACE
417#undef PB_DS_TREE_TRACE_BASE_C_DEC
a345e45d 418#endif
4569a895 419 } // namespace detail
5e11f978 420} // namespace __gnu_pbds