3 // Copyright (C) 2005, 2006, 2009 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 3, 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 // 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
38 * Contains a list update map.
43 #include <ext/pb_ds/detail/cond_dealtor.hpp>
44 #include <ext/pb_ds/tag_and_trait.hpp>
45 #include <ext/pb_ds/detail/types_traits.hpp>
46 #include <ext/pb_ds/detail/list_update_map_/entry_metadata_base.hpp>
47 #include <ext/pb_ds/exception.hpp>
49 #include <ext/pb_ds/detail/debug_map_base.hpp>
51 #ifdef PB_DS_LU_MAP_TRACE_
54 #include <debug/debug.h>
60 #define PB_DS_CLASS_T_DEC \
61 template<typename Key, typename Mapped, class Eq_Fn, \
62 class Allocator, class Update_Policy>
64 #ifdef PB_DS_DATA_TRUE_INDICATOR
65 #define PB_DS_CLASS_NAME lu_map_data_
68 #ifdef PB_DS_DATA_FALSE_INDICATOR
69 #define PB_DS_CLASS_NAME lu_map_no_data_
72 #define PB_DS_CLASS_C_DEC \
73 PB_DS_CLASS_NAME<Key, Mapped, Eq_Fn, Allocator, Update_Policy>
75 #define PB_DS_TYPES_TRAITS_C_DEC \
76 types_traits<Key, Mapped, Allocator, false>
79 #define PB_DS_DEBUG_MAP_BASE_C_DEC \
80 debug_map_base<Key, Eq_Fn, \
81 typename Allocator::template rebind<Key>::other::const_reference>
84 #ifdef PB_DS_DATA_TRUE_INDICATOR
85 #define PB_DS_V2F(X) (X).first
86 #define PB_DS_V2S(X) (X).second
87 #define PB_DS_EP2VP(X)& ((X)->m_value)
90 #ifdef PB_DS_DATA_FALSE_INDICATOR
91 #define PB_DS_V2F(X) (X)
92 #define PB_DS_V2S(X) Mapped_Data()
93 #define PB_DS_EP2VP(X)& ((X)->m_value.first)
96 /* Skip to the lu, my darling. */
97 // list-based (with updates) associative container.
98 template<typename Key,
103 class PB_DS_CLASS_NAME :
104 #ifdef _GLIBCXX_DEBUG
105 protected PB_DS_DEBUG_MAP_BASE_C_DEC,
107 public PB_DS_TYPES_TRAITS_C_DEC
110 typedef PB_DS_TYPES_TRAITS_C_DEC traits_base;
113 : public lu_map_entry_metadata_base<typename Update_Policy::metadata_type>
115 typename traits_base::value_type m_value;
116 typename Allocator::template rebind<entry>::other::pointer m_p_next;
119 typedef typename Allocator::template rebind<entry>::other entry_allocator;
120 typedef typename entry_allocator::pointer entry_pointer;
121 typedef typename entry_allocator::const_pointer const_entry_pointer;
122 typedef typename entry_allocator::reference entry_reference;
123 typedef typename entry_allocator::const_reference const_entry_reference;
125 typedef typename Allocator::template rebind<entry_pointer>::other entry_pointer_allocator;
126 typedef typename entry_pointer_allocator::pointer entry_pointer_array;
128 typedef typename traits_base::value_type value_type_;
129 typedef typename traits_base::pointer pointer_;
130 typedef typename traits_base::const_pointer const_pointer_;
131 typedef typename traits_base::reference reference_;
132 typedef typename traits_base::const_reference const_reference_;
134 #define PB_DS_GEN_POS entry_pointer
136 #include <ext/pb_ds/detail/unordered_iterator/const_point_iterator.hpp>
137 #include <ext/pb_ds/detail/unordered_iterator/point_iterator.hpp>
138 #include <ext/pb_ds/detail/unordered_iterator/const_iterator.hpp>
139 #include <ext/pb_ds/detail/unordered_iterator/iterator.hpp>
144 #ifdef _GLIBCXX_DEBUG
145 typedef PB_DS_DEBUG_MAP_BASE_C_DEC debug_base;
148 typedef cond_dealtor<entry, Allocator> cond_dealtor_t;
151 typedef Allocator allocator_type;
152 typedef typename Allocator::size_type size_type;
153 typedef typename Allocator::difference_type difference_type;
155 typedef Update_Policy update_policy;
156 typedef typename Update_Policy::metadata_type update_metadata;
157 typedef typename traits_base::key_type key_type;
158 typedef typename traits_base::key_pointer key_pointer;
159 typedef typename traits_base::const_key_pointer const_key_pointer;
160 typedef typename traits_base::key_reference key_reference;
161 typedef typename traits_base::const_key_reference const_key_reference;
162 typedef typename traits_base::mapped_type mapped_type;
163 typedef typename traits_base::mapped_pointer mapped_pointer;
164 typedef typename traits_base::const_mapped_pointer const_mapped_pointer;
165 typedef typename traits_base::mapped_reference mapped_reference;
166 typedef typename traits_base::const_mapped_reference const_mapped_reference;
167 typedef typename traits_base::value_type value_type;
168 typedef typename traits_base::pointer pointer;
169 typedef typename traits_base::const_pointer const_pointer;
170 typedef typename traits_base::reference reference;
171 typedef typename traits_base::const_reference const_reference;
173 #ifdef PB_DS_DATA_TRUE_INDICATOR
174 typedef point_iterator_ point_iterator;
177 #ifdef PB_DS_DATA_FALSE_INDICATOR
178 typedef const_point_iterator_ point_iterator;
181 typedef const_point_iterator_ const_point_iterator;
183 #ifdef PB_DS_DATA_TRUE_INDICATOR
184 typedef iterator_ iterator;
187 #ifdef PB_DS_DATA_FALSE_INDICATOR
188 typedef const_iterator_ iterator;
191 typedef const_iterator_ const_iterator;
196 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC&);
201 template<typename It>
202 PB_DS_CLASS_NAME(It first_it, It last_it);
205 swap(PB_DS_CLASS_C_DEC&);
216 inline mapped_reference
217 operator[](const_key_reference r_key)
219 #ifdef PB_DS_DATA_TRUE_INDICATOR
220 _GLIBCXX_DEBUG_ONLY(assert_valid();)
221 return insert(std::make_pair(r_key, mapped_type())).first->second;
224 return traits_base::s_null_mapped;
228 inline std::pair<point_iterator, bool>
229 insert(const_reference);
231 inline point_iterator
232 find(const_key_reference r_key)
234 _GLIBCXX_DEBUG_ONLY(assert_valid();)
235 entry_pointer p_e = find_imp(r_key);
236 return point_iterator(p_e == NULL ? NULL: &p_e->m_value);
239 inline const_point_iterator
240 find(const_key_reference r_key) const
242 _GLIBCXX_DEBUG_ONLY(assert_valid();)
243 entry_pointer p_e = find_imp(r_key);
244 return const_point_iterator(p_e == NULL ? NULL: &p_e->m_value);
248 erase(const_key_reference);
250 template<typename Pred>
260 inline const_iterator
266 inline const_iterator
269 #ifdef _GLIBCXX_DEBUG
271 assert_valid() const;
274 #ifdef PB_DS_LU_MAP_TRACE_
281 template<typename It>
283 copy_from_range(It, It);
286 #ifdef PB_DS_DATA_TRUE_INDICATOR
287 friend class iterator_;
290 friend class const_iterator_;
293 allocate_new_entry(const_reference, false_type);
296 allocate_new_entry(const_reference, true_type);
298 template<typename Metadata>
300 init_entry_metadata(entry_pointer, type_to_type<Metadata>);
303 init_entry_metadata(entry_pointer, type_to_type<null_lu_metadata>);
309 erase_next(entry_pointer);
312 actual_erase_entry(entry_pointer);
315 inc_it_state(const_pointer& r_p_value, entry_pointer& r_pos) const
317 r_pos = r_pos->m_p_next;
318 r_p_value = (r_pos == NULL) ? NULL : &r_pos->m_value;
321 template<typename Metadata>
323 apply_update(entry_pointer, type_to_type<Metadata>);
326 apply_update(entry_pointer, type_to_type<null_lu_metadata>);
329 find_imp(const_key_reference) const;
331 static entry_allocator s_entry_allocator;
332 static Eq_Fn s_eq_fn;
333 static Update_Policy s_update_policy;
334 static type_to_type<update_metadata> s_metadata_type_indicator;
335 static null_lu_metadata s_null_lu_metadata;
337 mutable entry_pointer m_p_l;
340 #include <ext/pb_ds/detail/list_update_map_/constructor_destructor_fn_imps.hpp>
341 #include <ext/pb_ds/detail/list_update_map_/info_fn_imps.hpp>
342 #include <ext/pb_ds/detail/list_update_map_/debug_fn_imps.hpp>
343 #include <ext/pb_ds/detail/list_update_map_/iterators_fn_imps.hpp>
344 #include <ext/pb_ds/detail/list_update_map_/erase_fn_imps.hpp>
345 #include <ext/pb_ds/detail/list_update_map_/find_fn_imps.hpp>
346 #include <ext/pb_ds/detail/list_update_map_/insert_fn_imps.hpp>
347 #include <ext/pb_ds/detail/list_update_map_/trace_fn_imps.hpp>
349 #undef PB_DS_CLASS_T_DEC
350 #undef PB_DS_CLASS_C_DEC
351 #undef PB_DS_TYPES_TRAITS_C_DEC
352 #undef PB_DS_DEBUG_MAP_BASE_C_DEC
353 #undef PB_DS_CLASS_NAME
358 } // namespace detail
359 } // namespace __gnu_pbds