3 // Copyright (C) 2005-2013 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
37 * @file cc_hash_table_map_/cc_ht_map_.hpp
38 * Contains an implementation class for cc_ht_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/hash_fn/ranged_hash_fn.hpp>
46 #include <ext/pb_ds/detail/types_traits.hpp>
47 #include <ext/pb_ds/exception.hpp>
48 #include <ext/pb_ds/detail/eq_fn/hash_eq_fn.hpp>
50 #include <ext/pb_ds/detail/debug_map_base.hpp>
52 #ifdef PB_DS_HT_MAP_TRACE_
55 #include <debug/debug.h>
61 #ifdef PB_DS_DATA_TRUE_INDICATOR
62 #define PB_DS_CC_HASH_NAME cc_ht_map
65 #ifdef PB_DS_DATA_FALSE_INDICATOR
66 #define PB_DS_CC_HASH_NAME cc_ht_set
69 #define PB_DS_CLASS_T_DEC \
70 template<typename Key, typename Mapped, typename Hash_Fn, \
71 typename Eq_Fn, typename _Alloc, bool Store_Hash, \
72 typename Comb_Hash_Fn, typename Resize_Policy>
74 #define PB_DS_CLASS_C_DEC \
75 PB_DS_CC_HASH_NAME<Key, Mapped, Hash_Fn, Eq_Fn, _Alloc, \
76 Store_Hash, Comb_Hash_Fn, Resize_Policy>
78 #define PB_DS_HASH_EQ_FN_C_DEC \
79 hash_eq_fn<Key, Eq_Fn, _Alloc, Store_Hash>
81 #define PB_DS_RANGED_HASH_FN_C_DEC \
82 ranged_hash_fn<Key, Hash_Fn, _Alloc, Comb_Hash_Fn, Store_Hash>
84 #define PB_DS_CC_HASH_TRAITS_BASE \
85 types_traits<Key, Mapped, _Alloc, Store_Hash>
88 #define PB_DS_DEBUG_MAP_BASE_C_DEC \
89 debug_map_base<Key, Eq_Fn, \
90 typename _Alloc::template rebind<Key>::other::const_reference>
95 * A collision-chaining hash-based container.
98 * @ingroup hash-detail
100 * @tparam Key Key type.
102 * @tparam Mapped Map type.
104 * @tparam Hash_Fn Hashing functor.
105 * Default is __gnu_cxx::hash.
107 * @tparam Eq_Fn Equal functor.
108 * Default std::equal_to<Key>
110 * @tparam _Alloc Allocator type.
112 * @tparam Store_Hash If key type stores extra metadata.
115 * @tparam Comb_Hash_Fn Combining hash functor.
116 * If Hash_Fn is not null_type, then this
117 * is the ranged-hash functor; otherwise,
118 * this is the range-hashing functor.
119 * XXX(See Design::Hash-Based Containers::Hash Policies.)
120 * Default direct_mask_range_hashing.
122 * @tparam Resize_Policy Resizes hash.
123 * Defaults to hash_standard_resize_policy,
124 * using hash_exponential_size_policy and
125 * hash_load_check_resize_trigger.
128 * Bases are: detail::hash_eq_fn, Resize_Policy, detail::ranged_hash_fn,
129 * detail::types_traits. (Optional: detail::debug_map_base.)
131 template<typename Key,
137 typename Comb_Hash_Fn,
138 typename Resize_Policy >
139 class PB_DS_CC_HASH_NAME:
140 #ifdef _GLIBCXX_DEBUG
141 protected PB_DS_DEBUG_MAP_BASE_C_DEC,
143 public PB_DS_HASH_EQ_FN_C_DEC,
144 public Resize_Policy,
145 public PB_DS_RANGED_HASH_FN_C_DEC,
146 public PB_DS_CC_HASH_TRAITS_BASE
149 typedef PB_DS_CC_HASH_TRAITS_BASE traits_base;
150 typedef typename traits_base::comp_hash comp_hash;
151 typedef typename traits_base::value_type value_type_;
152 typedef typename traits_base::pointer pointer_;
153 typedef typename traits_base::const_pointer const_pointer_;
154 typedef typename traits_base::reference reference_;
155 typedef typename traits_base::const_reference const_reference_;
157 struct entry : public traits_base::stored_data_type
159 typename _Alloc::template rebind<entry>::other::pointer m_p_next;
162 typedef cond_dealtor<entry, _Alloc> cond_dealtor_t;
164 typedef typename _Alloc::template rebind<entry>::other entry_allocator;
165 typedef typename entry_allocator::pointer entry_pointer;
166 typedef typename entry_allocator::const_pointer const_entry_pointer;
167 typedef typename entry_allocator::reference entry_reference;
168 typedef typename entry_allocator::const_reference const_entry_reference;
170 typedef typename _Alloc::template rebind<entry_pointer>::other entry_pointer_allocator;
171 typedef typename entry_pointer_allocator::pointer entry_pointer_array;
173 typedef PB_DS_RANGED_HASH_FN_C_DEC ranged_hash_fn_base;
174 typedef PB_DS_HASH_EQ_FN_C_DEC hash_eq_fn_base;
175 typedef Resize_Policy resize_base;
177 #ifdef _GLIBCXX_DEBUG
178 typedef PB_DS_DEBUG_MAP_BASE_C_DEC debug_base;
181 #define PB_DS_GEN_POS std::pair<entry_pointer, typename _Alloc::size_type>
183 #include <ext/pb_ds/detail/unordered_iterator/point_const_iterator.hpp>
184 #include <ext/pb_ds/detail/unordered_iterator/point_iterator.hpp>
185 #include <ext/pb_ds/detail/unordered_iterator/const_iterator.hpp>
186 #include <ext/pb_ds/detail/unordered_iterator/iterator.hpp>
191 typedef _Alloc allocator_type;
192 typedef typename _Alloc::size_type size_type;
193 typedef typename _Alloc::difference_type difference_type;
194 typedef Hash_Fn hash_fn;
196 typedef Comb_Hash_Fn comb_hash_fn;
197 typedef Resize_Policy resize_policy;
199 /// Value stores hash, true or false.
202 store_hash = Store_Hash
205 typedef typename traits_base::key_type key_type;
206 typedef typename traits_base::key_pointer key_pointer;
207 typedef typename traits_base::key_const_pointer key_const_pointer;
208 typedef typename traits_base::key_reference key_reference;
209 typedef typename traits_base::key_const_reference key_const_reference;
210 typedef typename traits_base::mapped_type mapped_type;
211 typedef typename traits_base::mapped_pointer mapped_pointer;
212 typedef typename traits_base::mapped_const_pointer mapped_const_pointer;
213 typedef typename traits_base::mapped_reference mapped_reference;
214 typedef typename traits_base::mapped_const_reference mapped_const_reference;
215 typedef typename traits_base::value_type value_type;
216 typedef typename traits_base::pointer pointer;
217 typedef typename traits_base::const_pointer const_pointer;
218 typedef typename traits_base::reference reference;
219 typedef typename traits_base::const_reference const_reference;
221 #ifdef PB_DS_DATA_TRUE_INDICATOR
222 typedef point_iterator_ point_iterator;
225 #ifdef PB_DS_DATA_FALSE_INDICATOR
226 typedef point_const_iterator_ point_iterator;
229 typedef point_const_iterator_ point_const_iterator;
231 #ifdef PB_DS_DATA_TRUE_INDICATOR
232 typedef iterator_ iterator;
235 #ifdef PB_DS_DATA_FALSE_INDICATOR
236 typedef const_iterator_ iterator;
239 typedef const_iterator_ const_iterator;
241 PB_DS_CC_HASH_NAME();
243 PB_DS_CC_HASH_NAME(const Hash_Fn&);
245 PB_DS_CC_HASH_NAME(const Hash_Fn&, const Eq_Fn&);
247 PB_DS_CC_HASH_NAME(const Hash_Fn&, const Eq_Fn&, const Comb_Hash_Fn&);
249 PB_DS_CC_HASH_NAME(const Hash_Fn&, const Eq_Fn&, const Comb_Hash_Fn&,
250 const Resize_Policy&);
252 PB_DS_CC_HASH_NAME(const PB_DS_CLASS_C_DEC&);
255 ~PB_DS_CC_HASH_NAME();
258 swap(PB_DS_CLASS_C_DEC&);
260 template<typename It>
262 copy_from_range(It, It);
273 /// True if size() == 0.
277 /// Return current hash_fn.
281 /// Return current const hash_fn.
285 /// Return current eq_fn.
289 /// Return current const eq_fn.
293 /// Return current comb_hash_fn.
297 /// Return current const comb_hash_fn.
299 get_comb_hash_fn() const;
301 /// Return current resize_policy.
305 /// Return current const resize_policy.
307 get_resize_policy() const;
309 inline std::pair<point_iterator, bool>
310 insert(const_reference r_val)
311 { return insert_imp(r_val, traits_base::m_store_extra_indicator); }
313 inline mapped_reference
314 operator[](key_const_reference r_key)
316 #ifdef PB_DS_DATA_TRUE_INDICATOR
317 return (subscript_imp(r_key, traits_base::m_store_extra_indicator));
320 return traits_base::s_null_type;
324 inline point_iterator
325 find(key_const_reference);
327 inline point_const_iterator
328 find(key_const_reference) const;
330 inline point_iterator
333 inline point_const_iterator
337 erase(key_const_reference);
339 template<typename Pred>
349 inline const_iterator
355 inline const_iterator
358 #ifdef _GLIBCXX_DEBUG
360 assert_valid(const char*, int) const;
363 #ifdef PB_DS_HT_MAP_TRACE_
373 do_resize_if_needed();
376 do_resize_if_needed_no_throw();
379 resize_imp(size_type);
382 do_resize(size_type);
385 resize_imp_no_exceptions(size_type, entry_pointer_array, size_type);
388 resize_imp_no_exceptions_reassign_pointer(entry_pointer,
393 resize_imp_no_exceptions_reassign_pointer(entry_pointer,
398 deallocate_links_in_list(entry_pointer);
401 get_entry(const_reference, false_type);
404 get_entry(const_reference, true_type);
407 rels_entry(entry_pointer);
409 #ifdef PB_DS_DATA_TRUE_INDICATOR
410 inline mapped_reference
411 subscript_imp(key_const_reference r_key, false_type)
413 _GLIBCXX_DEBUG_ONLY(assert_valid(__FILE__, __LINE__);)
414 const size_type pos = ranged_hash_fn_base::operator()(r_key);
415 entry_pointer p_e = m_entries[pos];
416 resize_base::notify_insert_search_start();
419 && !hash_eq_fn_base::operator()(p_e->m_value.first, r_key))
421 resize_base::notify_insert_search_collision();
425 resize_base::notify_insert_search_end();
428 PB_DS_CHECK_KEY_EXISTS(r_key)
429 return (p_e->m_value.second);
432 PB_DS_CHECK_KEY_DOES_NOT_EXIST(r_key)
433 return insert_new_imp(value_type(r_key, mapped_type()), pos)->second;
436 inline mapped_reference
437 subscript_imp(key_const_reference r_key, true_type)
439 _GLIBCXX_DEBUG_ONLY(assert_valid(__FILE__, __LINE__);)
440 comp_hash pos_hash_pair = ranged_hash_fn_base::operator()(r_key);
441 entry_pointer p_e = m_entries[pos_hash_pair.first];
442 resize_base::notify_insert_search_start();
444 !hash_eq_fn_base::operator()(p_e->m_value.first, p_e->m_hash,
445 r_key, pos_hash_pair.second))
447 resize_base::notify_insert_search_collision();
451 resize_base::notify_insert_search_end();
454 PB_DS_CHECK_KEY_EXISTS(r_key)
455 return p_e->m_value.second;
458 PB_DS_CHECK_KEY_DOES_NOT_EXIST(r_key)
459 return insert_new_imp(value_type(r_key, mapped_type()),
460 pos_hash_pair)->second;
464 inline std::pair<point_iterator, bool>
465 insert_imp(const_reference, false_type);
467 inline std::pair<point_iterator, bool>
468 insert_imp(const_reference, true_type);
471 insert_new_imp(const_reference r_val, size_type pos)
473 if (do_resize_if_needed())
474 pos = ranged_hash_fn_base::operator()(PB_DS_V2F(r_val));
476 // Following lines might throw an exception.
477 entry_pointer p_e = get_entry(r_val,
478 traits_base::m_no_throw_copies_indicator);
480 // At this point no exceptions can be thrown.
481 p_e->m_p_next = m_entries[pos];
482 m_entries[pos] = p_e;
483 resize_base::notify_inserted(++m_num_used_e);
485 _GLIBCXX_DEBUG_ONLY(debug_base::insert_new(PB_DS_V2F(r_val));)
486 _GLIBCXX_DEBUG_ONLY(assert_valid(__FILE__, __LINE__);)
487 return &p_e->m_value;
491 insert_new_imp(const_reference r_val, comp_hash& r_pos_hash_pair)
493 // Following lines might throw an exception.
494 if (do_resize_if_needed())
495 r_pos_hash_pair = ranged_hash_fn_base::operator()(PB_DS_V2F(r_val));
497 entry_pointer p_e = get_entry(r_val,
498 traits_base::m_no_throw_copies_indicator);
500 // At this point no exceptions can be thrown.
501 p_e->m_hash = r_pos_hash_pair.second;
502 p_e->m_p_next = m_entries[r_pos_hash_pair.first];
503 m_entries[r_pos_hash_pair.first] = p_e;
504 resize_base::notify_inserted(++m_num_used_e);
505 _GLIBCXX_DEBUG_ONLY(debug_base::insert_new(PB_DS_V2F(r_val));)
506 _GLIBCXX_DEBUG_ONLY(assert_valid(__FILE__, __LINE__);)
507 return &p_e->m_value;
511 find_key_pointer(key_const_reference r_key, false_type)
513 entry_pointer p_e = m_entries[ranged_hash_fn_base::operator()(r_key)];
514 resize_base::notify_find_search_start();
516 !hash_eq_fn_base::operator()(PB_DS_V2F(p_e->m_value), r_key))
518 resize_base::notify_find_search_collision();
522 resize_base::notify_find_search_end();
524 #ifdef _GLIBCXX_DEBUG
526 PB_DS_CHECK_KEY_DOES_NOT_EXIST(r_key)
528 PB_DS_CHECK_KEY_EXISTS(r_key)
530 return &p_e->m_value;
534 find_key_pointer(key_const_reference r_key, true_type)
536 comp_hash pos_hash_pair = ranged_hash_fn_base::operator()(r_key);
537 entry_pointer p_e = m_entries[pos_hash_pair.first];
538 resize_base::notify_find_search_start();
540 !hash_eq_fn_base::operator()(PB_DS_V2F(p_e->m_value),
542 r_key, pos_hash_pair.second))
544 resize_base::notify_find_search_collision();
548 resize_base::notify_find_search_end();
550 #ifdef _GLIBCXX_DEBUG
552 PB_DS_CHECK_KEY_DOES_NOT_EXIST(r_key)
554 PB_DS_CHECK_KEY_EXISTS(r_key)
556 return &p_e->m_value;
560 erase_in_pos_imp(key_const_reference, size_type);
563 erase_in_pos_imp(key_const_reference, const comp_hash&);
566 erase_entry_pointer(entry_pointer&);
568 #ifdef PB_DS_DATA_TRUE_INDICATOR
570 inc_it_state(pointer& r_p_value,
571 std::pair<entry_pointer, size_type>& r_pos) const
573 inc_it_state((mapped_const_pointer& )r_p_value, r_pos);
578 inc_it_state(const_pointer& r_p_value,
579 std::pair<entry_pointer, size_type>& r_pos) const
581 _GLIBCXX_DEBUG_ASSERT(r_p_value != 0);
582 r_pos.first = r_pos.first->m_p_next;
583 if (r_pos.first != 0)
585 r_p_value = &r_pos.first->m_value;
589 for (++r_pos.second; r_pos.second < m_num_e; ++r_pos.second)
590 if (m_entries[r_pos.second] != 0)
592 r_pos.first = m_entries[r_pos.second];
593 r_p_value = &r_pos.first->m_value;
600 get_start_it_state(pointer& r_p_value,
601 std::pair<entry_pointer, size_type>& r_pos) const
603 for (r_pos.second = 0; r_pos.second < m_num_e; ++r_pos.second)
604 if (m_entries[r_pos.second] != 0)
606 r_pos.first = m_entries[r_pos.second];
607 r_p_value = &r_pos.first->m_value;
613 #ifdef _GLIBCXX_DEBUG
615 assert_entry_pointer_array_valid(const entry_pointer_array,
616 const char*, int) const;
619 assert_entry_pointer_valid(const entry_pointer, true_type,
620 const char*, int) const;
623 assert_entry_pointer_valid(const entry_pointer, false_type,
624 const char*, int) const;
627 #ifdef PB_DS_HT_MAP_TRACE_
629 trace_list(const_entry_pointer) const;
633 #ifdef PB_DS_DATA_TRUE_INDICATOR
634 friend class iterator_;
637 friend class const_iterator_;
639 static entry_allocator s_entry_allocator;
640 static entry_pointer_allocator s_entry_pointer_allocator;
641 static iterator s_end_it;
642 static const_iterator s_const_end_it;
643 static point_iterator s_find_end_it;
644 static point_const_iterator s_const_find_end_it;
647 size_type m_num_used_e;
648 entry_pointer_array m_entries;
652 store_hash_ok = !Store_Hash
653 || !is_same<Hash_Fn, __gnu_pbds::null_type>::value
656 PB_DS_STATIC_ASSERT(sth, store_hash_ok);
659 #include <ext/pb_ds/detail/cc_hash_table_map_/constructor_destructor_fn_imps.hpp>
660 #include <ext/pb_ds/detail/cc_hash_table_map_/entry_list_fn_imps.hpp>
661 #include <ext/pb_ds/detail/cc_hash_table_map_/find_fn_imps.hpp>
662 #include <ext/pb_ds/detail/cc_hash_table_map_/resize_fn_imps.hpp>
663 #include <ext/pb_ds/detail/cc_hash_table_map_/debug_fn_imps.hpp>
664 #include <ext/pb_ds/detail/cc_hash_table_map_/size_fn_imps.hpp>
665 #include <ext/pb_ds/detail/cc_hash_table_map_/policy_access_fn_imps.hpp>
666 #include <ext/pb_ds/detail/cc_hash_table_map_/erase_fn_imps.hpp>
667 #include <ext/pb_ds/detail/cc_hash_table_map_/iterators_fn_imps.hpp>
668 #include <ext/pb_ds/detail/cc_hash_table_map_/insert_fn_imps.hpp>
669 #include <ext/pb_ds/detail/cc_hash_table_map_/trace_fn_imps.hpp>
671 #undef PB_DS_CLASS_T_DEC
672 #undef PB_DS_CLASS_C_DEC
673 #undef PB_DS_HASH_EQ_FN_C_DEC
674 #undef PB_DS_RANGED_HASH_FN_C_DEC
675 #undef PB_DS_CC_HASH_TRAITS_BASE
676 #undef PB_DS_DEBUG_MAP_BASE_C_DEC
677 #undef PB_DS_CC_HASH_NAME
678 } // namespace detail
679 } // namespace __gnu_pbds