3 // Copyright (C) 2005, 2006 Free Software Foundation, Inc.
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
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 // 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.
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
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
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
43 * @file ov_tree_map_.hpp
44 * Contains an implementation class for ov_tree_.
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>
61 #include <debug/debug.h>
68 #define PB_DS_CLASS_T_DEC \
69 template<typename Key, typename Mapped, class Cmp_Fn, \
70 class Node_And_It_Traits, class Allocator>
72 #ifdef PB_DS_DATA_TRUE_INDICATOR
73 #define PB_DS_OV_TREE_CLASS_NAME ov_tree_data_
76 #ifdef PB_DS_DATA_FALSE_INDICATOR
77 #define PB_DS_OV_TREE_CLASS_NAME ov_tree_no_data_
80 #ifdef PB_DS_DATA_TRUE_INDICATOR
81 #define PB_DS_CONST_NODE_ITERATOR_NAME ov_tree_const_node_iterator_data_
83 #define PB_DS_CONST_NODE_ITERATOR_NAME ov_tree_const_node_iterator_no_data_
86 #define PB_DS_CLASS_C_DEC \
87 PB_DS_OV_TREE_CLASS_NAME<Key, Mapped, Cmp_Fn, Node_And_It_Traits, Allocator>
89 #define PB_DS_TYPES_TRAITS_C_DEC \
90 types_traits<Key, Mapped, Allocator, false>
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>
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)
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)
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>
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,
124 #ifdef PB_DS_TREE_TRACE
125 public PB_DS_TREE_TRACE_BASE_C_DEC,
128 public Node_And_It_Traits::node_update,
129 public PB_DS_TYPES_TRAITS_C_DEC
134 typename remove_const<
135 typename PB_DS_TYPES_TRAITS_C_DEC::value_type>::type
136 non_const_value_type;
139 typename Allocator::template rebind<
140 non_const_value_type>::other
143 typedef typename value_allocator::pointer value_vector;
145 typedef PB_DS_TYPES_TRAITS_C_DEC traits_base;
147 typedef Cmp_Fn cmp_fn_base;
149 #ifdef _GLIBCXX_DEBUG
150 typedef PB_DS_MAP_DEBUG_BASE_C_DEC map_debug_base;
153 typedef typename PB_DS_TYPES_TRAITS_C_DEC::pointer mapped_pointer_;
156 typename PB_DS_TYPES_TRAITS_C_DEC::const_pointer
157 const_mapped_pointer_;
159 typedef typename Node_And_It_Traits::metadata_type metadata_type;
162 typename Allocator::template rebind<
163 metadata_type>::other
166 typedef typename metadata_allocator::pointer metadata_pointer;
169 typename metadata_allocator::const_reference
170 const_metadata_reference;
172 typedef typename metadata_allocator::reference metadata_reference;
175 typename Node_And_It_Traits::null_node_update_pointer
176 null_node_update_pointer;
180 typedef typename Allocator::size_type size_type;
182 typedef typename Allocator::difference_type difference_type;
184 typedef Cmp_Fn cmp_fn;
186 typedef typename Node_And_It_Traits::node_update node_update;
188 typedef Allocator allocator;
190 typedef typename PB_DS_TYPES_TRAITS_C_DEC::key_type key_type;
192 typedef typename PB_DS_TYPES_TRAITS_C_DEC::key_pointer key_pointer;
195 typename PB_DS_TYPES_TRAITS_C_DEC::const_key_pointer
198 typedef typename PB_DS_TYPES_TRAITS_C_DEC::key_reference key_reference;
201 typename PB_DS_TYPES_TRAITS_C_DEC::const_key_reference
204 typedef typename PB_DS_TYPES_TRAITS_C_DEC::mapped_type mapped_type;
207 typename PB_DS_TYPES_TRAITS_C_DEC::mapped_pointer
211 typename PB_DS_TYPES_TRAITS_C_DEC::const_mapped_pointer
212 const_mapped_pointer;
215 typename PB_DS_TYPES_TRAITS_C_DEC::mapped_reference
219 typename PB_DS_TYPES_TRAITS_C_DEC::const_mapped_reference
220 const_mapped_reference;
222 typedef typename PB_DS_TYPES_TRAITS_C_DEC::value_type value_type;
224 typedef typename PB_DS_TYPES_TRAITS_C_DEC::pointer pointer;
226 typedef typename PB_DS_TYPES_TRAITS_C_DEC::const_pointer const_pointer;
228 typedef typename PB_DS_TYPES_TRAITS_C_DEC::reference reference;
231 typename PB_DS_TYPES_TRAITS_C_DEC::const_reference
234 typedef const_pointer const_point_iterator;
236 #ifdef PB_DS_DATA_TRUE_INDICATOR
237 typedef pointer point_iterator;
239 typedef const_point_iterator point_iterator;
242 typedef const_point_iterator const_iterator;
244 typedef point_iterator iterator;
246 #include <ext/pb_ds/detail/ov_tree_map_/cond_dtor.hpp>
249 typename Node_And_It_Traits::const_node_iterator
252 typedef typename Node_And_It_Traits::node_iterator node_iterator;
256 PB_DS_OV_TREE_CLASS_NAME();
258 PB_DS_OV_TREE_CLASS_NAME(const Cmp_Fn& r_cmp_fn);
260 PB_DS_OV_TREE_CLASS_NAME(const Cmp_Fn& r_cmp_fn, const node_update& r_node_update);
262 PB_DS_OV_TREE_CLASS_NAME(const PB_DS_CLASS_C_DEC& other);
264 ~PB_DS_OV_TREE_CLASS_NAME();
267 swap(PB_DS_CLASS_C_DEC& other);
269 template<typename It>
271 copy_from_range(It first_it, It last_it);
288 inline mapped_reference
289 operator[](const_key_reference r_key)
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)))
296 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_exists(r_key));
297 _GLIBCXX_DEBUG_ONLY(assert_valid();)
301 _GLIBCXX_DEBUG_ONLY(assert_valid();)
302 return (insert_new_val(it, std::make_pair(r_key, mapped_type()))->second);
305 return traits_base::s_null_mapped;
309 inline std::pair<point_iterator, bool>
310 insert(const_reference r_value)
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);
316 if (it != end()&& !Cmp_Fn::operator()(r_key, PB_DS_V2F(*it)))
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);
323 _GLIBCXX_DEBUG_ONLY(assert_valid();)
324 return std::make_pair(insert_new_val(it, r_value), true);
327 inline point_iterator
328 lower_bound(const_key_reference r_key)
330 pointer it = m_a_values;
331 pointer e_it = m_a_values + m_size;
334 pointer mid_it = it + ((e_it - it) >> 1);
335 if (cmp_fn_base::operator()(PB_DS_V2F(*mid_it), r_key))
343 inline const_point_iterator
344 lower_bound(const_key_reference r_key) const
346 return (const_cast<PB_DS_CLASS_C_DEC& >(*this).lower_bound(r_key));
349 inline point_iterator
350 upper_bound(const_key_reference r_key)
352 iterator pot_it = lower_bound(r_key);
353 if (pot_it != end()&& !Cmp_Fn::operator()(r_key, PB_DS_V2F(*pot_it)))
355 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_exists(r_key));
359 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_does_not_exist(r_key));
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); }
367 inline point_iterator
368 find(const_key_reference r_key)
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)))
374 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_exists(r_key));
378 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_does_not_exist(r_key));
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)); }
387 erase(const_key_reference r_key);
389 template<typename Pred>
395 { return erase_imp<iterator>(it); }
401 join(PB_DS_CLASS_C_DEC& other);
404 split(const_key_reference r_key, PB_DS_CLASS_C_DEC& other);
408 { return m_a_values; }
410 inline const_iterator
412 { return m_a_values; }
418 inline const_iterator
422 inline const_node_iterator
425 inline const_node_iterator
437 update(node_iterator /*it*/, null_node_update_pointer);
439 template<typename Node_Update>
441 update(node_iterator it, Node_Update* p_update);
444 reallocate_metadata(null_node_update_pointer, size_type);
446 template<typename Node_Update_>
448 reallocate_metadata(Node_Update_* p_update, size_type new_size);
450 template<typename It>
452 copy_from_ordered_range(It first_it, It last_it);
455 value_swap(PB_DS_CLASS_C_DEC& other);
457 template<typename It>
459 copy_from_ordered_range(It first_it, It last_it, It other_first_it, It other_last_it);
461 template<typename Ptr>
463 mid_pointer(Ptr p_begin, Ptr p_end)
465 _GLIBCXX_DEBUG_ASSERT(p_end >= p_begin);
466 return (p_begin + (p_end - p_begin) / 2);
470 insert_new_val(iterator it, const_reference r_value)
472 _GLIBCXX_DEBUG_ONLY(assert_valid();)
473 #ifdef PB_DS_REGRESSION
474 typename Allocator::group_throw_prob_adjustor adjust(m_size);
477 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_does_not_exist(PB_DS_V2F(r_value)));
479 value_vector a_values = s_value_alloc.allocate(m_size + 1);
481 iterator source_it = begin();
482 iterator source_end_it = end();
483 iterator target_it = a_values;
486 cond_dtor<size_type> cd(a_values, target_it, m_size + 1);
487 while (source_it != it)
489 new (const_cast<void* >(static_cast<const void* >(target_it)))
490 value_type(*source_it++);
494 new (const_cast<void* >(static_cast<const void* >(ret_it = target_it)))
498 while (source_it != source_end_it)
500 new (const_cast<void* >(static_cast<const void* >(target_it)))
501 value_type(*source_it++);
505 reallocate_metadata((node_update* )this, m_size + 1);
509 cond_dtor<size_type> cd1(m_a_values, m_end_it, 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();)
521 #ifdef _GLIBCXX_DEBUG
523 assert_valid() const;
526 assert_iterators() const;
529 template<typename It>
533 inline const_node_iterator
534 PB_DS_node_begin_imp() const;
536 inline const_node_iterator
537 PB_DS_node_end_imp() const;
540 PB_DS_node_begin_imp();
543 PB_DS_node_end_imp();
546 value_vector m_a_values;
548 static value_allocator s_value_alloc;
550 metadata_pointer m_a_metadata;
552 static metadata_allocator s_metadata_alloc;
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>
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
581 #undef PB_DS_CONST_NODE_ITERATOR_NAME
583 } // namespace detail