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