2011-05-11 François Dumont <francois.cppdevs@free.fr>
[official-gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / ov_tree_map_ / ov_tree_map_.hpp
blob06436591ef5d642eec0bbbb5c49317d7e50c2e75
1 // -*- C++ -*-
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.
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.
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.
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/>.
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
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.
36 /**
37 * @file ov_tree_map_.hpp
38 * Contains an implementation class for ov_tree_.
41 #include <map>
42 #include <set>
43 #include <ext/pb_ds/tree_policy.hpp>
44 #include <ext/pb_ds/detail/eq_fn/eq_by_less.hpp>
45 #include <ext/pb_ds/detail/types_traits.hpp>
46 #include <ext/pb_ds/detail/debug_map_base.hpp>
47 #include <ext/pb_ds/detail/type_utils.hpp>
48 #include <ext/pb_ds/exception.hpp>
49 #include <ext/pb_ds/detail/tree_trace_base.hpp>
50 #include <utility>
51 #include <functional>
52 #include <algorithm>
53 #include <vector>
54 #include <assert.h>
55 #include <debug/debug.h>
57 namespace __gnu_pbds
59 namespace detail
61 #define PB_DS_CLASS_T_DEC \
62 template<typename Key, typename Mapped, class Cmp_Fn, \
63 class Node_And_It_Traits, class Allocator>
65 #ifdef PB_DS_DATA_TRUE_INDICATOR
66 #define PB_DS_OV_TREE_CLASS_NAME ov_tree_data_
67 #endif
69 #ifdef PB_DS_DATA_FALSE_INDICATOR
70 #define PB_DS_OV_TREE_CLASS_NAME ov_tree_no_data_
71 #endif
73 #ifdef PB_DS_DATA_TRUE_INDICATOR
74 #define PB_DS_CONST_NODE_ITERATOR_NAME ov_tree_const_node_iterator_data_
75 #else
76 #define PB_DS_CONST_NODE_ITERATOR_NAME ov_tree_const_node_iterator_no_data_
77 #endif
79 #define PB_DS_CLASS_C_DEC \
80 PB_DS_OV_TREE_CLASS_NAME<Key, Mapped, Cmp_Fn, Node_And_It_Traits, Allocator>
82 #define PB_DS_TYPES_TRAITS_C_DEC \
83 types_traits<Key, Mapped, Allocator, false>
85 #ifdef _GLIBCXX_DEBUG
86 #define PB_DS_DEBUG_MAP_BASE_C_DEC \
87 debug_map_base<Key, eq_by_less<Key, Cmp_Fn>, \
88 typename Allocator::template rebind<Key>::other::const_reference>
89 #endif
91 #ifdef PB_DS_DATA_TRUE_INDICATOR
92 #define PB_DS_V2F(X) (X).first
93 #define PB_DS_V2S(X) (X).second
94 #define PB_DS_EP2VP(X)& ((X)->m_value)
95 #endif
97 #ifdef PB_DS_DATA_FALSE_INDICATOR
98 #define PB_DS_V2F(X) (X)
99 #define PB_DS_V2S(X) Mapped_Data()
100 #define PB_DS_EP2VP(X)& ((X)->m_value.first)
101 #endif
103 #ifdef PB_DS_TREE_TRACE
104 #define PB_DS_TREE_TRACE_BASE_C_DEC \
105 tree_trace_base<typename Node_And_It_Traits::const_node_iterator, \
106 typename Node_And_It_Traits::node_iterator, \
107 Cmp_Fn, false, Allocator>
108 #endif
110 #ifndef PB_DS_CHECK_KEY_EXISTS
111 # error Missing definition
112 #endif
114 // Ordered-vector tree associative-container.
115 template<typename Key, typename Mapped, class Cmp_Fn,
116 class Node_And_It_Traits, class Allocator>
117 class PB_DS_OV_TREE_CLASS_NAME :
118 #ifdef _GLIBCXX_DEBUG
119 protected PB_DS_DEBUG_MAP_BASE_C_DEC,
120 #endif
121 #ifdef PB_DS_TREE_TRACE
122 public PB_DS_TREE_TRACE_BASE_C_DEC,
123 #endif
124 public Cmp_Fn,
125 public Node_And_It_Traits::node_update,
126 public PB_DS_TYPES_TRAITS_C_DEC
128 private:
129 typedef PB_DS_TYPES_TRAITS_C_DEC traits_base;
131 typedef typename remove_const<typename traits_base::value_type>::type non_const_value_type;
133 typedef typename Allocator::template rebind<non_const_value_type>::other value_allocator;
134 typedef typename value_allocator::pointer value_vector;
137 typedef Cmp_Fn cmp_fn_base;
139 #ifdef _GLIBCXX_DEBUG
140 typedef PB_DS_DEBUG_MAP_BASE_C_DEC debug_base;
141 #endif
143 typedef typename traits_base::pointer mapped_pointer_;
144 typedef typename traits_base::const_pointer const_mapped_pointer_;
146 typedef typename Node_And_It_Traits::metadata_type metadata_type;
148 typedef typename Allocator::template rebind<metadata_type>::other metadata_allocator;
149 typedef typename metadata_allocator::pointer metadata_pointer;
150 typedef typename metadata_allocator::const_reference const_metadata_reference;
151 typedef typename metadata_allocator::reference metadata_reference;
153 typedef
154 typename Node_And_It_Traits::null_node_update_pointer
155 null_node_update_pointer;
157 public:
159 typedef Allocator allocator_type;
160 typedef typename Allocator::size_type size_type;
161 typedef typename Allocator::difference_type difference_type;
163 typedef Cmp_Fn cmp_fn;
165 typedef typename Node_And_It_Traits::node_update node_update;
167 typedef typename traits_base::key_type key_type;
168 typedef typename traits_base::key_pointer key_pointer;
169 typedef typename traits_base::const_key_pointer const_key_pointer;
170 typedef typename traits_base::key_reference key_reference;
171 typedef typename traits_base::const_key_reference const_key_reference;
172 typedef typename traits_base::mapped_type mapped_type;
173 typedef typename traits_base::mapped_pointer mapped_pointer;
174 typedef typename traits_base::const_mapped_pointer const_mapped_pointer;
175 typedef typename traits_base::mapped_reference mapped_reference;
176 typedef typename traits_base::const_mapped_reference const_mapped_reference;
177 typedef typename traits_base::value_type value_type;
178 typedef typename traits_base::pointer pointer;
179 typedef typename traits_base::const_pointer const_pointer;
180 typedef typename traits_base::reference reference;
181 typedef typename traits_base::const_reference const_reference;
183 typedef const_pointer const_point_iterator;
185 #ifdef PB_DS_DATA_TRUE_INDICATOR
186 typedef pointer point_iterator;
187 #else
188 typedef const_point_iterator point_iterator;
189 #endif
191 typedef const_point_iterator const_iterator;
193 typedef point_iterator iterator;
195 #include <ext/pb_ds/detail/ov_tree_map_/cond_dtor.hpp>
197 typedef
198 typename Node_And_It_Traits::const_node_iterator
199 const_node_iterator;
201 typedef typename Node_And_It_Traits::node_iterator node_iterator;
203 public:
205 PB_DS_OV_TREE_CLASS_NAME();
207 PB_DS_OV_TREE_CLASS_NAME(const Cmp_Fn&);
209 PB_DS_OV_TREE_CLASS_NAME(const Cmp_Fn&, const node_update&);
211 PB_DS_OV_TREE_CLASS_NAME(const PB_DS_CLASS_C_DEC&);
213 ~PB_DS_OV_TREE_CLASS_NAME();
215 void
216 swap(PB_DS_CLASS_C_DEC&);
218 template<typename It>
219 void
220 copy_from_range(It, It);
222 inline size_type
223 max_size() const;
225 inline bool
226 empty() const;
228 inline size_type
229 size() const;
231 Cmp_Fn&
232 get_cmp_fn();
234 const Cmp_Fn&
235 get_cmp_fn() const;
237 inline mapped_reference
238 operator[](const_key_reference r_key)
240 #ifdef PB_DS_DATA_TRUE_INDICATOR
241 PB_DS_ASSERT_VALID((*this))
242 point_iterator it = lower_bound(r_key);
243 if (it != end() && !Cmp_Fn::operator()(r_key, PB_DS_V2F(*it)))
245 PB_DS_CHECK_KEY_EXISTS(r_key)
246 PB_DS_ASSERT_VALID((*this))
247 return it->second;
250 return (insert_new_val(it, std::make_pair(r_key, mapped_type()))->second);
251 #else
252 insert(r_key);
253 return traits_base::s_null_mapped;
254 #endif
257 inline std::pair<point_iterator, bool>
258 insert(const_reference r_value)
260 PB_DS_ASSERT_VALID((*this))
261 const_key_reference r_key = PB_DS_V2F(r_value);
262 point_iterator it = lower_bound(r_key);
264 if (it != end()&& !Cmp_Fn::operator()(r_key, PB_DS_V2F(*it)))
266 PB_DS_ASSERT_VALID((*this))
267 PB_DS_CHECK_KEY_EXISTS(r_key)
268 return std::make_pair(it, false);
271 return std::make_pair(insert_new_val(it, r_value), true);
274 inline point_iterator
275 lower_bound(const_key_reference r_key)
277 pointer it = m_a_values;
278 pointer e_it = m_a_values + m_size;
279 while (it != e_it)
281 pointer mid_it = it + ((e_it - it) >> 1);
282 if (cmp_fn_base::operator()(PB_DS_V2F(*mid_it), r_key))
283 it = ++mid_it;
284 else
285 e_it = mid_it;
287 return it;
290 inline const_point_iterator
291 lower_bound(const_key_reference r_key) const
292 { return const_cast<PB_DS_CLASS_C_DEC& >(*this).lower_bound(r_key); }
294 inline point_iterator
295 upper_bound(const_key_reference r_key)
297 iterator pot_it = lower_bound(r_key);
298 if (pot_it != end()&& !Cmp_Fn::operator()(r_key, PB_DS_V2F(*pot_it)))
300 PB_DS_CHECK_KEY_EXISTS(r_key)
301 return ++pot_it;
304 PB_DS_CHECK_KEY_DOES_NOT_EXIST(r_key)
305 return pot_it;
308 inline const_point_iterator
309 upper_bound(const_key_reference r_key) const
310 { return const_cast<PB_DS_CLASS_C_DEC&>(*this).upper_bound(r_key); }
312 inline point_iterator
313 find(const_key_reference r_key)
315 PB_DS_ASSERT_VALID((*this))
316 iterator pot_it = lower_bound(r_key);
317 if (pot_it != end() && !Cmp_Fn::operator()(r_key, PB_DS_V2F(*pot_it)))
319 PB_DS_CHECK_KEY_EXISTS(r_key)
320 return pot_it;
323 PB_DS_CHECK_KEY_DOES_NOT_EXIST(r_key)
324 return end();
327 inline const_point_iterator
328 find(const_key_reference r_key) const
329 { return (const_cast<PB_DS_CLASS_C_DEC& >(*this).find(r_key)); }
331 bool
332 erase(const_key_reference);
334 template<typename Pred>
335 inline size_type
336 erase_if(Pred);
338 inline iterator
339 erase(iterator it)
340 { return erase_imp<iterator>(it); }
342 void
343 clear();
345 void
346 join(PB_DS_CLASS_C_DEC&);
348 void
349 split(const_key_reference, PB_DS_CLASS_C_DEC&);
351 inline iterator
352 begin()
353 { return m_a_values; }
355 inline const_iterator
356 begin() const
357 { return m_a_values; }
359 inline iterator
360 end()
361 { return m_end_it; }
363 inline const_iterator
364 end() const
365 { return m_end_it; }
367 inline const_node_iterator
368 node_begin() const;
370 inline const_node_iterator
371 node_end() const;
373 inline node_iterator
374 node_begin();
376 inline node_iterator
377 node_end();
379 private:
381 inline void
382 update(node_iterator /*it*/, null_node_update_pointer);
384 template<typename Node_Update>
385 void
386 update(node_iterator, Node_Update*);
388 void
389 reallocate_metadata(null_node_update_pointer, size_type);
391 template<typename Node_Update_>
392 void
393 reallocate_metadata(Node_Update_*, size_type);
395 template<typename It>
396 void
397 copy_from_ordered_range(It, It);
399 void
400 value_swap(PB_DS_CLASS_C_DEC&);
402 template<typename It>
403 void
404 copy_from_ordered_range(It, It, It, It);
406 template<typename Ptr>
407 inline static Ptr
408 mid_pointer(Ptr p_begin, Ptr p_end)
410 _GLIBCXX_DEBUG_ASSERT(p_end >= p_begin);
411 return (p_begin + (p_end - p_begin) / 2);
414 inline iterator
415 insert_new_val(iterator it, const_reference r_value)
417 #ifdef PB_DS_REGRESSION
418 typename Allocator::group_adjustor adjust(m_size);
419 #endif
421 PB_DS_CHECK_KEY_DOES_NOT_EXIST(PB_DS_V2F(r_value))
423 value_vector a_values = s_value_alloc.allocate(m_size + 1);
425 iterator source_it = begin();
426 iterator source_end_it = end();
427 iterator target_it = a_values;
428 iterator ret_it;
430 cond_dtor<size_type> cd(a_values, target_it, m_size + 1);
431 while (source_it != it)
433 new (const_cast<void* >(static_cast<const void* >(target_it)))
434 value_type(*source_it++);
435 ++target_it;
438 new (const_cast<void* >(static_cast<const void* >(ret_it = target_it)))
439 value_type(r_value);
440 ++target_it;
442 while (source_it != source_end_it)
444 new (const_cast<void* >(static_cast<const void* >(target_it)))
445 value_type(*source_it++);
446 ++target_it;
449 reallocate_metadata((node_update* )this, m_size + 1);
450 cd.set_no_action();
451 if (m_size != 0)
453 cond_dtor<size_type> cd1(m_a_values, m_end_it, m_size);
456 ++m_size;
457 m_a_values = a_values;
458 m_end_it = m_a_values + m_size;
459 _GLIBCXX_DEBUG_ONLY(debug_base::insert_new(PB_DS_V2F(r_value)));
460 update(node_begin(), (node_update* )this);
461 PB_DS_ASSERT_VALID((*this))
462 return ret_it;
465 #ifdef _GLIBCXX_DEBUG
466 void
467 assert_valid(const char* file, int line) const;
469 void
470 assert_iterators(const char* file, int line) const;
471 #endif
473 template<typename It>
475 erase_imp(It it);
477 inline const_node_iterator
478 PB_DS_node_begin_imp() const;
480 inline const_node_iterator
481 PB_DS_node_end_imp() const;
483 inline node_iterator
484 PB_DS_node_begin_imp();
486 inline node_iterator
487 PB_DS_node_end_imp();
489 private:
490 static value_allocator s_value_alloc;
491 static metadata_allocator s_metadata_alloc;
493 value_vector m_a_values;
494 metadata_pointer m_a_metadata;
495 iterator m_end_it;
496 size_type m_size;
499 #include <ext/pb_ds/detail/ov_tree_map_/constructors_destructor_fn_imps.hpp>
500 #include <ext/pb_ds/detail/ov_tree_map_/iterators_fn_imps.hpp>
501 #include <ext/pb_ds/detail/ov_tree_map_/debug_fn_imps.hpp>
502 #include <ext/pb_ds/detail/ov_tree_map_/erase_fn_imps.hpp>
503 #include <ext/pb_ds/detail/ov_tree_map_/insert_fn_imps.hpp>
504 #include <ext/pb_ds/detail/ov_tree_map_/info_fn_imps.hpp>
505 #include <ext/pb_ds/detail/ov_tree_map_/split_join_fn_imps.hpp>
506 #include <ext/pb_ds/detail/bin_search_tree_/policy_access_fn_imps.hpp>
508 #undef PB_DS_CLASS_C_DEC
509 #undef PB_DS_CLASS_T_DEC
510 #undef PB_DS_OV_TREE_CLASS_NAME
511 #undef PB_DS_TYPES_TRAITS_C_DEC
512 #undef PB_DS_DEBUG_MAP_BASE_C_DEC
513 #ifdef PB_DS_TREE_TRACE
514 #undef PB_DS_TREE_TRACE_BASE_C_DEC
515 #endif
517 #undef PB_DS_V2F
518 #undef PB_DS_EP2VP
519 #undef PB_DS_V2S
520 #undef PB_DS_CONST_NODE_ITERATOR_NAME
522 } // namespace detail
523 } // namespace __gnu_pbds