3 // Copyright (C) 2005 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
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32 // Permission to use, copy, modify, sell, and distribute this software
33 // is hereby granted without fee, provided that the above copyright
34 // notice appears in all copies, and that both that copyright notice and
35 // this permission notice appear in supporting documentation. None of
36 // the above authors, nor IBM Haifa Research Laboratories, make any
37 // representation about the suitability of this software for any
38 // purpose. It is provided "as is" without express or implied warranty.
41 * @file cc_ht_map_.hpp
42 * Contains an implementation class for cc_ht_map_.
47 #include <ext/pb_assoc/detail/cond_dealtor.hpp>
48 #include <ext/pb_assoc/trivial_iterator_def.hpp>
49 #include <ext/pb_assoc/detail/hash_fn/ranged_hash_fn.hpp>
50 #include <ext/pb_assoc/detail/hash_types_traits.hpp>
51 #include <ext/pb_assoc/detail/types_traits.hpp>
52 #include <ext/pb_assoc/exception.hpp>
53 #include <ext/pb_assoc/detail/map_debug_base.hpp>
54 #include <ext/pb_assoc/detail/eq_fn/hash_eq_fn.hpp>
62 #ifdef PB_ASSOC_CC_HT_MAP_DEBUG
63 #define PB_ASSOC_DBG_ASSERT(X) assert(X)
64 #define PB_ASSOC_DBG_VERIFY(X) assert(X)
65 #define PB_ASSOC_DBG_ONLY(X) X
66 #else // #ifdef PB_ASSOC_CC_HT_MAP_DEBUG
67 #define PB_ASSOC_DBG_ASSERT(X)
68 #define PB_ASSOC_DBG_VERIFY(X) {if((X)==0);}
69 #define PB_ASSOC_DBG_ONLY(X) ;
70 #endif // #ifdef PB_ASSOC_CC_HT_MAP_DEBUG
72 #define PB_ASSOC_CLASS_T_DEC \
83 #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
84 #define PB_ASSOC_CLASS_NAME \
86 #endif // #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
88 #ifdef PB_ASSOC_DATA_FALSE_INDICATOR
89 #define PB_ASSOC_CLASS_NAME \
91 #endif // #ifdef PB_ASSOC_DATA_FALSE_INDICATOR
93 #define PB_ASSOC_CLASS_C_DEC \
94 PB_ASSOC_CLASS_NAME< \
104 #define PB_ASSOC_HASH_EQ_FN_C_DEC \
105 pb_assoc::detail::hash_eq_fn< \
111 #define PB_ASSOC_RANGED_HASH_FN_C_DEC \
112 pb_assoc::detail::ranged_hash_fn< \
119 #define PB_ASSOC_TYPES_TRAITS_C_DEC \
125 #define PB_ASSOC_HASH_TYPES_TRAITS_C_DEC \
127 typename Allocator::size_type, \
130 #ifdef PB_ASSOC_USE_MAP_DEBUG_BASE
131 #define PB_ASSOC_MAP_DEBUG_BASE_C_DEC \
132 pb_assoc::detail::map_debug_base< \
135 #endif // #ifdef PB_ASSOC_USE_MAP_DEBUG_BASE
137 #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
138 #define PB_ASSOC_V2F(X) (X).first
139 #define PB_ASSOC_V2S(X) (X).second
140 #endif // #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
142 #ifdef PB_ASSOC_DATA_FALSE_INDICATOR
143 #define PB_ASSOC_V2F(X) (X)
144 #define PB_ASSOC_V2S(X) Mapped_Data()
145 #endif // #ifdef PB_ASSOC_DATA_FALSE_INDICATOR
147 #define PB_ASSOC_STATIC_ASSERT(UNIQUE, E) \
149 pb_assoc::detail::static_assert_dummy_class< \
150 sizeof(pb_assoc::detail::static_assert<(bool)(E)>)> \
151 UNIQUE##static_assert_type
153 template<typename Key,
160 class Resize_Policy >
161 class PB_ASSOC_CLASS_NAME:
162 #ifdef PB_ASSOC_CC_HT_MAP_DEBUG
163 protected PB_ASSOC_MAP_DEBUG_BASE_C_DEC,
164 #endif // #ifdef PB_ASSOC_CC_HT_MAP_DEBUG
165 public PB_ASSOC_HASH_EQ_FN_C_DEC,
166 public Resize_Policy,
167 public PB_ASSOC_RANGED_HASH_FN_C_DEC,
168 public PB_ASSOC_TYPES_TRAITS_C_DEC,
169 public PB_ASSOC_HASH_TYPES_TRAITS_C_DEC
174 typedef typename Allocator::size_type size_type;
177 typename PB_ASSOC_TYPES_TRAITS_C_DEC::const_key_reference
180 typedef typename PB_ASSOC_TYPES_TRAITS_C_DEC::data_type data_type;
183 typename PB_ASSOC_TYPES_TRAITS_C_DEC::data_reference
187 typename PB_ASSOC_TYPES_TRAITS_C_DEC::const_data_reference
188 const_data_reference;
190 typedef typename PB_ASSOC_TYPES_TRAITS_C_DEC::value_type value_type;
192 typedef typename PB_ASSOC_TYPES_TRAITS_C_DEC::pointer pointer;
195 typename PB_ASSOC_TYPES_TRAITS_C_DEC::const_pointer
198 typedef typename PB_ASSOC_TYPES_TRAITS_C_DEC::reference reference;
201 typename PB_ASSOC_TYPES_TRAITS_C_DEC::const_reference
206 typedef typename PB_ASSOC_HASH_TYPES_TRAITS_C_DEC::comp_hash comp_hash;
208 struct no_store_hash_entry
212 typename Allocator::template rebind<
213 no_store_hash_entry>::other::pointer m_p_next;
216 struct store_hash_entry
222 typename Allocator::template rebind<
223 store_hash_entry>::other::pointer m_p_next;
230 no_store_hash_entry>::type
234 typename Allocator::template rebind<entry>::other
237 typedef typename entry_allocator::pointer entry_pointer;
239 typedef typename entry_allocator::const_pointer const_entry_pointer;
241 typedef typename entry_allocator::reference entry_reference;
244 typename entry_allocator::const_reference
245 const_entry_reference;
248 typename Allocator::template rebind<entry_pointer>::other
249 entry_pointer_allocator;
251 typedef typename entry_pointer_allocator::pointer entry_pointer_array;
253 typedef value_type mapped_value_type;
255 typedef pointer mapped_pointer;
257 typedef const_pointer const_mapped_pointer;
259 typedef reference mapped_reference;
261 typedef const_reference const_mapped_reference;
263 #define PB_ASSOC_GEN_POS std::pair<entry_pointer, size_type>
265 #include <ext/pb_assoc/detail/unordered_iterator/const_find_iterator.hpp>
266 #include <ext/pb_assoc/detail/unordered_iterator/find_iterator.hpp>
267 #include <ext/pb_assoc/detail/unordered_iterator/const_iterator.hpp>
268 #include <ext/pb_assoc/detail/unordered_iterator/iterator.hpp>
270 #undef PB_ASSOC_GEN_POS
272 typedef find_iterator_ find_iterator;
274 typedef const_find_iterator_ const_find_iterator;
276 typedef iterator_ iterator;
278 typedef const_iterator_ const_iterator;
280 typedef Hash_Fn hash_fn;
284 typedef Allocator allocator;
286 typedef Resize_Policy resize_policy;
290 PB_ASSOC_CLASS_NAME();
292 PB_ASSOC_CLASS_NAME(const Hash_Fn& r_hash_fn);
294 PB_ASSOC_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn);
296 PB_ASSOC_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn, const Comb_Hash_Fn& r_comb_hash_fn);
298 PB_ASSOC_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn, const Comb_Hash_Fn& r_comb_hash_fn, const Resize_Policy& r_resize_policy);
300 PB_ASSOC_CLASS_NAME(const PB_ASSOC_CLASS_C_DEC& r_other);
303 ~PB_ASSOC_CLASS_NAME();
306 swap(PB_ASSOC_CLASS_C_DEC& r_other);
310 copy_from_range(It first_it, It last_it);
337 get_comb_hash_fn() const;
343 get_resize_policy() const;
345 inline std::pair<find_iterator, bool>
346 insert(const_reference r_val);
348 inline data_reference
349 subscript_imp(const_key_reference r_key);
352 find(const_key_reference r_key);
354 inline const_find_iterator
355 find(const_key_reference r_key) const;
360 inline const_find_iterator
365 erase(T r_t, bool erase_entry_if_last, pb_assoc::detail::int_to_type<false>);
369 erase(T r_t, bool erase_entry_if_last, pb_assoc::detail::int_to_type<true>);
373 erase_if(Pred& r_pred);
381 inline const_iterator
387 inline const_iterator
390 #ifdef PB_ASSOC_CC_HT_MAP_DEBUG
393 assert_valid() const;
395 #endif // #ifdef PB_ASSOC_CC_HT_MAP_DEBUG
398 do_resize(size_type new_size);
402 typedef PB_ASSOC_TYPES_TRAITS_C_DEC my_traits_base;
404 typedef PB_ASSOC_HASH_TYPES_TRAITS_C_DEC my_hash_traits_base;
406 typedef PB_ASSOC_RANGED_HASH_FN_C_DEC my_ranged_hash_fn_base;
408 typedef PB_ASSOC_HASH_EQ_FN_C_DEC my_hash_eq_fn_base;
410 typedef Resize_Policy my_resize_base;
412 #ifdef PB_ASSOC_USE_MAP_DEBUG_BASE
413 typedef PB_ASSOC_MAP_DEBUG_BASE_C_DEC my_map_debug_base;
414 #endif // #ifdef PB_ASSOC_USE_MAP_DEBUG_BASE
419 do_resize_if_needed();
422 do_resize_if_needed_no_throw();
425 resize_imp_no_exceptions(size_type new_size, entry_pointer_array a_p_entries_resized, size_type old_size);
428 resize_imp_no_exceptions_reassign_pointer(entry_pointer p_e, entry_pointer_array a_p_entries_resized, int_to_type<false>);
431 resize_imp_no_exceptions_reassign_pointer(entry_pointer p_e, entry_pointer_array a_p_entries_resized, int_to_type<true>);
433 template<class For_Each_Fn>
435 do_for_each(For_Each_Fn fn);
438 deallocate_links_in_list(entry_pointer p_e);
441 get_entry(const_reference r_val, int_to_type<false>);
444 get_entry(const_reference r_val, int_to_type<true>);
447 rels_entry(entry_pointer p_e);
450 constructor_insert_new_imp(const_reference r_val, size_type pos, int_to_type<false>);
453 constructor_insert_new_imp(const_reference r_val, size_type pos, int_to_type<true>);
458 inline data_reference
459 subscript_imp(const_key_reference r_key, int_to_type<false>);
461 inline data_reference
462 subscript_imp(const_key_reference r_key, int_to_type<true>);
464 inline std::pair<find_iterator, bool>
465 insert_imp(const_reference r_val, int_to_type<false>);
467 inline std::pair<find_iterator, bool>
468 insert_imp(const_reference r_val, int_to_type<true>);
471 insert_new_imp(const_reference r_val, size_type pos);
474 insert_new_imp(const_reference r_val, comp_hash& r_pos_hash_pair);
476 inline const_data_reference
477 const_subscript_imp(const_key_reference r_key, int_to_type<false>) const;
479 inline const_data_reference
480 const_subscript_imp(const_key_reference r_key, int_to_type<true>) const;
483 find_key_pointer(const_key_reference r_key, int_to_type<false>);
486 find_key_pointer(const_key_reference r_key, int_to_type<true>);
490 erase_in_pos_imp(T r_t, bool erase_entry_if_last, size_type pos);
494 erase_in_pos_imp(T r_t, bool erase_entry_if_last, const comp_hash& r_pos_hash_pair);
497 erase_entry_pointer(entry_pointer& r_p_e);
499 #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
501 inc_it_state(pointer& r_p_value, std::pair<entry_pointer, size_type>& r_pos) const;
502 #endif // #ifdef PB_ASSOC_DATA_TRUE_INDICATOR
505 inc_it_state(const_pointer& r_p_value, std::pair<entry_pointer, size_type>& r_pos) const;
508 get_start_it_state(pointer& r_p_value, std::pair<entry_pointer, size_type>& r_pos) const;
510 #ifdef PB_ASSOC_CC_HT_MAP_DEBUG
513 assert_entry_pointer_array_valid(const entry_pointer_array a_p_entries) const;
516 assert_entry_pointer_valid(const entry_pointer p_e, store_hash_true_indicator) const;
519 assert_entry_pointer_valid(const entry_pointer p_e, store_hash_false_indicator) const;
521 #endif // #ifdef PB_ASSOC_CC_HT_MAP_DEBUG
524 static entry_allocator s_entry_allocator;
526 static entry_pointer_allocator s_entry_pointer_allocator;
529 pb_assoc::detail::cond_dealtor<
534 entry_pointer_array m_a_p_entries;
538 size_type m_num_used_e;
540 friend class iterator_;
542 friend class const_iterator_;
544 static iterator s_end_it;
546 static const_iterator s_const_end_it;
548 static find_iterator s_find_end_it;
550 static const_find_iterator s_const_find_end_it;
556 !pb_assoc::detail::is_same_type<
558 pb_assoc::null_hash_fn>::value
561 PB_ASSOC_STATIC_ASSERT(sth, store_hash_ok);
564 #include <ext/pb_assoc/detail/cc_ht_map_/constructor_destructor_fn_imps.hpp>
565 #include <ext/pb_assoc/detail/cc_ht_map_/entry_list_fn_imps.hpp>
566 #include <ext/pb_assoc/detail/cc_ht_map_/find_fn_imps.hpp>
567 #include <ext/pb_assoc/detail/cc_ht_map_/resize_fn_imps.hpp>
568 #include <ext/pb_assoc/detail/cc_ht_map_/debug_fn_imps.hpp>
569 #include <ext/pb_assoc/detail/cc_ht_map_/size_fn_imps.hpp>
570 #include <ext/pb_assoc/detail/cc_ht_map_/policy_access_fn_imps.hpp>
571 #include <ext/pb_assoc/detail/cc_ht_map_/erase_fn_imps.hpp>
572 #include <ext/pb_assoc/detail/cc_ht_map_/iterators_fn_imps.hpp>
573 #include <ext/pb_assoc/detail/cc_ht_map_/insert_fn_imps.hpp>
575 #undef PB_ASSOC_CLASS_T_DEC
577 #undef PB_ASSOC_CLASS_C_DEC
579 #undef PB_ASSOC_HASH_EQ_FN_C_DEC
581 #undef PB_ASSOC_RANGED_HASH_FN_C_DEC
583 #undef PB_ASSOC_TYPES_TRAITS_C_DEC
585 #undef PB_ASSOC_HASH_TYPES_TRAITS_C_DEC
587 #undef PB_ASSOC_MAP_DEBUG_BASE_C_DEC
589 #undef PB_ASSOC_CLASS_NAME
594 #undef PB_ASSOC_DBG_ASSERT
595 #undef PB_ASSOC_DBG_VERIFY
596 #undef PB_ASSOC_DBG_ONLY
598 #undef PB_ASSOC_STATIC_ASSERT
600 } // namespace detail
602 } // namespace pb_assoc