]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/ext/pb_ds/detail/ov_tree_map_/ov_tree_map_.hpp
re PR target/32338 (Error: .prologue within prologue)
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / ov_tree_map_ / ov_tree_map_.hpp
CommitLineData
4569a895
AT
1// -*- C++ -*-
2
3// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
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
8// Foundation; either version 2, or (at your option) any later
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
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.
20
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
29// Public License.
30
31// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32
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
40// warranty.
41
42/**
43 * @file ov_tree_map_.hpp
44 * Contains an implementation class for ov_tree_.
45 */
46
47#include <map>
48#include <set>
49#include <ext/pb_ds/tree_policy.hpp>
50#include <ext/pb_ds/detail/eq_fn/eq_by_less.hpp>
51#include <ext/pb_ds/detail/types_traits.hpp>
551fe1a2 52#include <ext/pb_ds/detail/debug_map_base.hpp>
4569a895
AT
53#include <ext/pb_ds/detail/type_utils.hpp>
54#include <ext/pb_ds/exception.hpp>
55#include <ext/pb_ds/detail/tree_trace_base.hpp>
56#include <utility>
57#include <functional>
58#include <algorithm>
59#include <vector>
60#include <assert.h>
47bea7b8 61#include <debug/debug.h>
4569a895
AT
62
63namespace pb_ds
64{
65 namespace detail
66 {
47bea7b8
BK
67#define PB_DS_CLASS_T_DEC \
68 template<typename Key, typename Mapped, class Cmp_Fn, \
69 class Node_And_It_Traits, class Allocator>
4569a895
AT
70
71#ifdef PB_DS_DATA_TRUE_INDICATOR
47bea7b8
BK
72#define PB_DS_OV_TREE_CLASS_NAME ov_tree_data_
73#endif
4569a895
AT
74
75#ifdef PB_DS_DATA_FALSE_INDICATOR
47bea7b8
BK
76#define PB_DS_OV_TREE_CLASS_NAME ov_tree_no_data_
77#endif
4569a895
AT
78
79#ifdef PB_DS_DATA_TRUE_INDICATOR
80#define PB_DS_CONST_NODE_ITERATOR_NAME ov_tree_const_node_iterator_data_
47bea7b8 81#else
4569a895 82#define PB_DS_CONST_NODE_ITERATOR_NAME ov_tree_const_node_iterator_no_data_
47bea7b8
BK
83#endif
84
85#define PB_DS_CLASS_C_DEC \
86 PB_DS_OV_TREE_CLASS_NAME<Key, Mapped, Cmp_Fn, Node_And_It_Traits, Allocator>
87
88#define PB_DS_TYPES_TRAITS_C_DEC \
89 types_traits<Key, Mapped, Allocator, false>
90
91#ifdef _GLIBCXX_DEBUG
551fe1a2
BK
92#define PB_DS_DEBUG_MAP_BASE_C_DEC \
93 debug_map_base<Key, eq_by_less<Key, Cmp_Fn>, \
47bea7b8
BK
94 typename Allocator::template rebind<Key>::other::const_reference>
95#endif
4569a895
AT
96
97#ifdef PB_DS_DATA_TRUE_INDICATOR
98#define PB_DS_V2F(X) (X).first
99#define PB_DS_V2S(X) (X).second
100#define PB_DS_EP2VP(X)& ((X)->m_value)
47bea7b8 101#endif
4569a895
AT
102
103#ifdef PB_DS_DATA_FALSE_INDICATOR
104#define PB_DS_V2F(X) (X)
105#define PB_DS_V2S(X) Mapped_Data()
106#define PB_DS_EP2VP(X)& ((X)->m_value.first)
47bea7b8 107#endif
4569a895
AT
108
109#ifdef PB_DS_TREE_TRACE
47bea7b8
BK
110#define PB_DS_TREE_TRACE_BASE_C_DEC \
111 tree_trace_base<typename Node_And_It_Traits::const_node_iterator, \
112 typename Node_And_It_Traits::node_iterator, \
113 Cmp_Fn, false, Allocator>
114#endif
4569a895
AT
115
116 // Ordered-vector tree associative-container.
47bea7b8
BK
117 template<typename Key, typename Mapped, class Cmp_Fn,
118 class Node_And_It_Traits, class Allocator>
4569a895 119 class PB_DS_OV_TREE_CLASS_NAME :
47bea7b8 120#ifdef _GLIBCXX_DEBUG
551fe1a2 121 protected PB_DS_DEBUG_MAP_BASE_C_DEC,
47bea7b8 122#endif
4569a895
AT
123#ifdef PB_DS_TREE_TRACE
124 public PB_DS_TREE_TRACE_BASE_C_DEC,
47bea7b8 125#endif
4569a895
AT
126 public Cmp_Fn,
127 public Node_And_It_Traits::node_update,
128 public PB_DS_TYPES_TRAITS_C_DEC
129 {
4569a895 130 private:
1b24692f 131 typedef PB_DS_TYPES_TRAITS_C_DEC traits_base;
4569a895 132
1b24692f 133 typedef typename remove_const<typename traits_base::value_type>::type non_const_value_type;
4569a895 134
1b24692f 135 typedef typename Allocator::template rebind<non_const_value_type>::other value_allocator;
4569a895
AT
136 typedef typename value_allocator::pointer value_vector;
137
4569a895
AT
138
139 typedef Cmp_Fn cmp_fn_base;
140
47bea7b8 141#ifdef _GLIBCXX_DEBUG
551fe1a2 142 typedef PB_DS_DEBUG_MAP_BASE_C_DEC debug_base;
47bea7b8 143#endif
4569a895 144
1b24692f
BK
145 typedef typename traits_base::pointer mapped_pointer_;
146 typedef typename traits_base::const_pointer const_mapped_pointer_;
4569a895
AT
147
148 typedef typename Node_And_It_Traits::metadata_type metadata_type;
149
1b24692f 150 typedef typename Allocator::template rebind<metadata_type>::other metadata_allocator;
4569a895 151 typedef typename metadata_allocator::pointer metadata_pointer;
1b24692f 152 typedef typename metadata_allocator::const_reference const_metadata_reference;
4569a895
AT
153 typedef typename metadata_allocator::reference metadata_reference;
154
155 typedef
156 typename Node_And_It_Traits::null_node_update_pointer
157 null_node_update_pointer;
158
159 public:
160
1b24692f 161 typedef Allocator allocator;
4569a895 162 typedef typename Allocator::size_type size_type;
4569a895
AT
163 typedef typename Allocator::difference_type difference_type;
164
165 typedef Cmp_Fn cmp_fn;
166
167 typedef typename Node_And_It_Traits::node_update node_update;
168
1b24692f
BK
169 typedef typename traits_base::key_type key_type;
170 typedef typename traits_base::key_pointer key_pointer;
171 typedef typename traits_base::const_key_pointer const_key_pointer;
172 typedef typename traits_base::key_reference key_reference;
173 typedef typename traits_base::const_key_reference const_key_reference;
174 typedef typename traits_base::mapped_type mapped_type;
175 typedef typename traits_base::mapped_pointer mapped_pointer;
176 typedef typename traits_base::const_mapped_pointer const_mapped_pointer;
177 typedef typename traits_base::mapped_reference mapped_reference;
178 typedef typename traits_base::const_mapped_reference const_mapped_reference;
179 typedef typename traits_base::value_type value_type;
180 typedef typename traits_base::pointer pointer;
181 typedef typename traits_base::const_pointer const_pointer;
182 typedef typename traits_base::reference reference;
183 typedef typename traits_base::const_reference const_reference;
4569a895
AT
184
185 typedef const_pointer const_point_iterator;
186
187#ifdef PB_DS_DATA_TRUE_INDICATOR
188 typedef pointer point_iterator;
47bea7b8 189#else
4569a895 190 typedef const_point_iterator point_iterator;
47bea7b8 191#endif
4569a895
AT
192
193 typedef const_point_iterator const_iterator;
194
195 typedef point_iterator iterator;
196
197#include <ext/pb_ds/detail/ov_tree_map_/cond_dtor.hpp>
198
199 typedef
200 typename Node_And_It_Traits::const_node_iterator
201 const_node_iterator;
202
203 typedef typename Node_And_It_Traits::node_iterator node_iterator;
204
205 public:
206
207 PB_DS_OV_TREE_CLASS_NAME();
208
1b24692f 209 PB_DS_OV_TREE_CLASS_NAME(const Cmp_Fn&);
4569a895 210
1b24692f 211 PB_DS_OV_TREE_CLASS_NAME(const Cmp_Fn&, const node_update&);
4569a895 212
1b24692f 213 PB_DS_OV_TREE_CLASS_NAME(const PB_DS_CLASS_C_DEC&);
4569a895
AT
214
215 ~PB_DS_OV_TREE_CLASS_NAME();
216
217 void
1b24692f 218 swap(PB_DS_CLASS_C_DEC&);
4569a895
AT
219
220 template<typename It>
221 void
1b24692f 222 copy_from_range(It, It);
4569a895
AT
223
224 inline size_type
225 max_size() const;
226
227 inline bool
228 empty() const;
229
230 inline size_type
231 size() const;
232
233 Cmp_Fn&
234 get_cmp_fn();
235
236 const Cmp_Fn&
237 get_cmp_fn() const;
238
239 inline mapped_reference
240 operator[](const_key_reference r_key)
241 {
242#ifdef PB_DS_DATA_TRUE_INDICATOR
47bea7b8
BK
243 _GLIBCXX_DEBUG_ONLY(assert_valid();)
244 point_iterator it = lower_bound(r_key);
245 if (it != end() && !Cmp_Fn::operator()(r_key, PB_DS_V2F(*it)))
4569a895 246 {
551fe1a2 247 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(r_key));
47bea7b8
BK
248 _GLIBCXX_DEBUG_ONLY(assert_valid();)
249 return it->second;
4569a895
AT
250 }
251
47bea7b8
BK
252 _GLIBCXX_DEBUG_ONLY(assert_valid();)
253 return (insert_new_val(it, std::make_pair(r_key, mapped_type()))->second);
254#else
4569a895 255 insert(r_key);
47bea7b8
BK
256 return traits_base::s_null_mapped;
257#endif
4569a895
AT
258 }
259
260 inline std::pair<point_iterator, bool>
261 insert(const_reference r_value)
262 {
47bea7b8
BK
263 _GLIBCXX_DEBUG_ONLY(assert_valid();)
264 const_key_reference r_key = PB_DS_V2F(r_value);
4569a895
AT
265 point_iterator it = lower_bound(r_key);
266
47bea7b8 267 if (it != end()&& !Cmp_Fn::operator()(r_key, PB_DS_V2F(*it)))
4569a895 268 {
47bea7b8 269 _GLIBCXX_DEBUG_ONLY(assert_valid();)
551fe1a2 270 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(r_key));
47bea7b8 271 return std::make_pair(it, false);
4569a895
AT
272 }
273
47bea7b8
BK
274 _GLIBCXX_DEBUG_ONLY(assert_valid();)
275 return std::make_pair(insert_new_val(it, r_value), true);
4569a895
AT
276 }
277
278 inline point_iterator
279 lower_bound(const_key_reference r_key)
280 {
281 pointer it = m_a_values;
4569a895 282 pointer e_it = m_a_values + m_size;
4569a895
AT
283 while (it != e_it)
284 {
285 pointer mid_it = it + ((e_it - it) >> 1);
47bea7b8 286 if (cmp_fn_base::operator()(PB_DS_V2F(*mid_it), r_key))
4569a895
AT
287 it = ++mid_it;
288 else
289 e_it = mid_it;
290 }
47bea7b8 291 return it;
4569a895
AT
292 }
293
294 inline const_point_iterator
295 lower_bound(const_key_reference r_key) const
1b24692f 296 { return const_cast<PB_DS_CLASS_C_DEC& >(*this).lower_bound(r_key); }
4569a895
AT
297
298 inline point_iterator
299 upper_bound(const_key_reference r_key)
300 {
301 iterator pot_it = lower_bound(r_key);
47bea7b8 302 if (pot_it != end()&& !Cmp_Fn::operator()(r_key, PB_DS_V2F(*pot_it)))
4569a895 303 {
551fe1a2 304 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(r_key));
3441f106 305 return ++pot_it;
4569a895
AT
306 }
307
551fe1a2 308 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(r_key));
47bea7b8 309 return pot_it;
4569a895
AT
310 }
311
312 inline const_point_iterator
313 upper_bound(const_key_reference r_key) const
3441f106 314 { return const_cast<PB_DS_CLASS_C_DEC&>(*this).upper_bound(r_key); }
4569a895
AT
315
316 inline point_iterator
317 find(const_key_reference r_key)
318 {
47bea7b8
BK
319 _GLIBCXX_DEBUG_ONLY(assert_valid();)
320 iterator pot_it = lower_bound(r_key);
3441f106 321 if (pot_it != end() && !Cmp_Fn::operator()(r_key, PB_DS_V2F(*pot_it)))
4569a895 322 {
551fe1a2 323 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(r_key));
3441f106 324 return pot_it;
4569a895
AT
325 }
326
551fe1a2 327 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(r_key));
47bea7b8 328 return end();
4569a895
AT
329 }
330
331 inline const_point_iterator
332 find(const_key_reference r_key) const
47bea7b8 333 { return (const_cast<PB_DS_CLASS_C_DEC& >(*this).find(r_key)); }
4569a895
AT
334
335 bool
1b24692f 336 erase(const_key_reference);
4569a895
AT
337
338 template<typename Pred>
339 inline size_type
1b24692f 340 erase_if(Pred);
4569a895
AT
341
342 inline iterator
343 erase(iterator it)
47bea7b8 344 { return erase_imp<iterator>(it); }
4569a895
AT
345
346 void
347 clear();
348
349 void
1b24692f 350 join(PB_DS_CLASS_C_DEC&);
4569a895
AT
351
352 void
1b24692f 353 split(const_key_reference, PB_DS_CLASS_C_DEC&);
4569a895
AT
354
355 inline iterator
356 begin()
47bea7b8 357 { return m_a_values; }
4569a895
AT
358
359 inline const_iterator
360 begin() const
47bea7b8 361 { return m_a_values; }
4569a895
AT
362
363 inline iterator
364 end()
47bea7b8 365 { return m_end_it; }
4569a895
AT
366
367 inline const_iterator
368 end() const
47bea7b8 369 { return m_end_it; }
4569a895
AT
370
371 inline const_node_iterator
372 node_begin() const;
373
374 inline const_node_iterator
375 node_end() const;
376
377 inline node_iterator
378 node_begin();
379
380 inline node_iterator
381 node_end();
382
383 private:
384
385 inline void
386 update(node_iterator /*it*/, null_node_update_pointer);
387
388 template<typename Node_Update>
389 void
1b24692f 390 update(node_iterator, Node_Update*);
4569a895
AT
391
392 void
393 reallocate_metadata(null_node_update_pointer, size_type);
394
395 template<typename Node_Update_>
396 void
1b24692f 397 reallocate_metadata(Node_Update_*, size_type);
4569a895
AT
398
399 template<typename It>
400 void
1b24692f 401 copy_from_ordered_range(It, It);
4569a895
AT
402
403 void
1b24692f 404 value_swap(PB_DS_CLASS_C_DEC&);
4569a895
AT
405
406 template<typename It>
407 void
1b24692f 408 copy_from_ordered_range(It, It, It, It);
4569a895
AT
409
410 template<typename Ptr>
411 inline static Ptr
412 mid_pointer(Ptr p_begin, Ptr p_end)
413 {
47bea7b8 414 _GLIBCXX_DEBUG_ASSERT(p_end >= p_begin);
4569a895
AT
415 return (p_begin + (p_end - p_begin) / 2);
416 }
417
418 inline iterator
419 insert_new_val(iterator it, const_reference r_value)
420 {
47bea7b8 421 _GLIBCXX_DEBUG_ONLY(assert_valid();)
4569a895
AT
422#ifdef PB_DS_REGRESSION
423 typename Allocator::group_throw_prob_adjustor adjust(m_size);
47bea7b8 424#endif
4569a895 425
551fe1a2 426 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(PB_DS_V2F(r_value)));
4569a895
AT
427
428 value_vector a_values = s_value_alloc.allocate(m_size + 1);
429
430 iterator source_it = begin();
431 iterator source_end_it = end();
432 iterator target_it = a_values;
433 iterator ret_it;
434
435 cond_dtor<size_type> cd(a_values, target_it, m_size + 1);
4569a895
AT
436 while (source_it != it)
437 {
47bea7b8 438 new (const_cast<void* >(static_cast<const void* >(target_it)))
4569a895 439 value_type(*source_it++);
4569a895
AT
440 ++target_it;
441 }
442
47bea7b8 443 new (const_cast<void* >(static_cast<const void* >(ret_it = target_it)))
4569a895 444 value_type(r_value);
4569a895
AT
445 ++target_it;
446
447 while (source_it != source_end_it)
448 {
47bea7b8 449 new (const_cast<void* >(static_cast<const void* >(target_it)))
4569a895 450 value_type(*source_it++);
4569a895
AT
451 ++target_it;
452 }
453
454 reallocate_metadata((node_update* )this, m_size + 1);
4569a895 455 cd.set_no_action();
4569a895
AT
456 if (m_size != 0)
457 {
458 cond_dtor<size_type> cd1(m_a_values, m_end_it, m_size);
459 }
460
461 ++m_size;
4569a895 462 m_a_values = a_values;
4569a895 463 m_end_it = m_a_values + m_size;
551fe1a2 464 _GLIBCXX_DEBUG_ONLY(debug_base::insert_new(PB_DS_V2F(r_value)));
4569a895 465 update(node_begin(), (node_update* )this);
47bea7b8
BK
466 _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
467 return ret_it;
4569a895
AT
468 }
469
47bea7b8 470#ifdef _GLIBCXX_DEBUG
4569a895
AT
471 void
472 assert_valid() const;
473
474 void
475 assert_iterators() const;
47bea7b8 476#endif
4569a895
AT
477
478 template<typename It>
479 It
480 erase_imp(It it);
481
482 inline const_node_iterator
483 PB_DS_node_begin_imp() const;
484
485 inline const_node_iterator
486 PB_DS_node_end_imp() const;
487
488 inline node_iterator
489 PB_DS_node_begin_imp();
490
491 inline node_iterator
492 PB_DS_node_end_imp();
493
494 private:
4569a895 495 static value_allocator s_value_alloc;
4569a895
AT
496 static metadata_allocator s_metadata_alloc;
497
1b24692f
BK
498 value_vector m_a_values;
499 metadata_pointer m_a_metadata;
4569a895 500 iterator m_end_it;
4569a895
AT
501 size_type m_size;
502 };
503
504#include <ext/pb_ds/detail/ov_tree_map_/constructors_destructor_fn_imps.hpp>
505#include <ext/pb_ds/detail/ov_tree_map_/iterators_fn_imps.hpp>
506#include <ext/pb_ds/detail/ov_tree_map_/debug_fn_imps.hpp>
507#include <ext/pb_ds/detail/ov_tree_map_/erase_fn_imps.hpp>
508#include <ext/pb_ds/detail/ov_tree_map_/insert_fn_imps.hpp>
509#include <ext/pb_ds/detail/ov_tree_map_/info_fn_imps.hpp>
510#include <ext/pb_ds/detail/ov_tree_map_/split_join_fn_imps.hpp>
511#include <ext/pb_ds/detail/bin_search_tree_/policy_access_fn_imps.hpp>
512
513#undef PB_DS_CLASS_C_DEC
4569a895 514#undef PB_DS_CLASS_T_DEC
4569a895 515#undef PB_DS_OV_TREE_CLASS_NAME
4569a895 516#undef PB_DS_TYPES_TRAITS_C_DEC
551fe1a2 517#undef PB_DS_DEBUG_MAP_BASE_C_DEC
4569a895
AT
518#ifdef PB_DS_TREE_TRACE
519#undef PB_DS_TREE_TRACE_BASE_C_DEC
47bea7b8 520#endif
4569a895
AT
521
522#undef PB_DS_V2F
523#undef PB_DS_EP2VP
524#undef PB_DS_V2S
4569a895
AT
525#undef PB_DS_CONST_NODE_ITERATOR_NAME
526
527 } // namespace detail
528} // namespace pb_ds