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