3 // Copyright (C) 2005, 2006, 2007, 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
37 * @file gp_ht_map_.hpp
38 * Contains an implementation class for gp_ht_map_.
41 #include <ext/pb_ds/tag_and_trait.hpp>
42 #include <ext/pb_ds/detail/hash_fn/ranged_probe_fn.hpp>
43 #include <ext/pb_ds/detail/types_traits.hpp>
44 #include <ext/pb_ds/exception.hpp>
45 #include <ext/pb_ds/detail/eq_fn/hash_eq_fn.hpp>
47 #ifdef PB_DS_HT_MAP_TRACE_
51 #include <ext/pb_ds/detail/debug_map_base.hpp>
53 #include <debug/debug.h>
59 #define PB_DS_CLASS_T_DEC \
60 template<typename Key, typename Mapped, typename Hash_Fn, typename Eq_Fn, \
61 typename Allocator, bool Store_Hash, typename Comb_Probe_Fn, \
62 typename Probe_Fn, typename Resize_Policy>
64 #ifdef PB_DS_DATA_TRUE_INDICATOR
65 #define PB_DS_CLASS_NAME gp_ht_map_data_
68 #ifdef PB_DS_DATA_FALSE_INDICATOR
69 #define PB_DS_CLASS_NAME gp_ht_map_no_data_
72 #define PB_DS_CLASS_C_DEC \
73 PB_DS_CLASS_NAME<Key, Mapped, Hash_Fn, Eq_Fn, Allocator, \
74 Store_Hash, Comb_Probe_Fn, Probe_Fn, Resize_Policy>
76 #define PB_DS_HASH_EQ_FN_C_DEC \
77 hash_eq_fn<Key, Eq_Fn, Allocator, Store_Hash>
79 #define PB_DS_RANGED_PROBE_FN_C_DEC \
80 ranged_probe_fn<Key, Hash_Fn, Allocator, Comb_Probe_Fn, Probe_Fn, Store_Hash>
82 #define PB_DS_TYPES_TRAITS_C_DEC \
83 types_traits<Key, Mapped, Allocator, Store_Hash>
86 #define PB_DS_DEBUG_MAP_BASE_C_DEC \
87 debug_map_base<Key, Eq_Fn, typename Allocator::template rebind<Key>::other::const_reference>
90 #ifdef PB_DS_DATA_TRUE_INDICATOR
91 #define PB_DS_V2F(X) (X).first
92 #define PB_DS_V2S(X) (X).second
95 #ifdef PB_DS_DATA_FALSE_INDICATOR
96 #define PB_DS_V2F(X) (X)
97 #define PB_DS_V2S(X) Mapped()
100 template<typename Key,
106 typename Comb_Probe_Fn,
108 typename Resize_Policy>
109 class PB_DS_CLASS_NAME :
110 #ifdef _GLIBCXX_DEBUG
111 protected PB_DS_DEBUG_MAP_BASE_C_DEC,
113 public PB_DS_HASH_EQ_FN_C_DEC,
114 public Resize_Policy,
115 public PB_DS_RANGED_PROBE_FN_C_DEC,
116 public PB_DS_TYPES_TRAITS_C_DEC
119 typedef PB_DS_TYPES_TRAITS_C_DEC traits_base;
120 typedef typename traits_base::value_type value_type_;
121 typedef typename traits_base::pointer pointer_;
122 typedef typename traits_base::const_pointer const_pointer_;
123 typedef typename traits_base::reference reference_;
124 typedef typename traits_base::const_reference const_reference_;
125 typedef typename traits_base::comp_hash comp_hash;
132 } __attribute__ ((packed));
134 struct entry : public traits_base::stored_value_type
139 typedef typename Allocator::template rebind<entry>::other entry_allocator;
140 typedef typename entry_allocator::pointer entry_pointer;
141 typedef typename entry_allocator::const_pointer const_entry_pointer;
142 typedef typename entry_allocator::reference entry_reference;
143 typedef typename entry_allocator::const_reference const_entry_reference;
144 typedef typename entry_allocator::pointer entry_array;
146 typedef PB_DS_RANGED_PROBE_FN_C_DEC ranged_probe_fn_base;
148 #ifdef _GLIBCXX_DEBUG
149 typedef PB_DS_DEBUG_MAP_BASE_C_DEC debug_base;
152 typedef PB_DS_HASH_EQ_FN_C_DEC hash_eq_fn_base;
153 typedef Resize_Policy resize_base;
155 #define PB_DS_GEN_POS typename Allocator::size_type
157 #include <ext/pb_ds/detail/unordered_iterator/const_point_iterator.hpp>
158 #include <ext/pb_ds/detail/unordered_iterator/point_iterator.hpp>
159 #include <ext/pb_ds/detail/unordered_iterator/const_iterator.hpp>
160 #include <ext/pb_ds/detail/unordered_iterator/iterator.hpp>
165 typedef Allocator allocator_type;
166 typedef typename Allocator::size_type size_type;
167 typedef typename Allocator::difference_type difference_type;
168 typedef Hash_Fn hash_fn;
170 typedef Probe_Fn probe_fn;
171 typedef Comb_Probe_Fn comb_probe_fn;
172 typedef Resize_Policy resize_policy;
176 store_hash = Store_Hash
179 typedef typename traits_base::key_type key_type;
180 typedef typename traits_base::key_pointer key_pointer;
181 typedef typename traits_base::const_key_pointer const_key_pointer;
182 typedef typename traits_base::key_reference key_reference;
183 typedef typename traits_base::const_key_reference const_key_reference;
184 typedef typename traits_base::mapped_type mapped_type;
185 typedef typename traits_base::mapped_pointer mapped_pointer;
186 typedef typename traits_base::const_mapped_pointer const_mapped_pointer;
187 typedef typename traits_base::mapped_reference mapped_reference;
188 typedef typename traits_base::const_mapped_reference const_mapped_reference;
189 typedef typename traits_base::value_type value_type;
190 typedef typename traits_base::pointer pointer;
191 typedef typename traits_base::const_pointer const_pointer;
192 typedef typename traits_base::reference reference;
193 typedef typename traits_base::const_reference const_reference;
195 #ifdef PB_DS_DATA_TRUE_INDICATOR
196 typedef point_iterator_ point_iterator;
199 #ifdef PB_DS_DATA_FALSE_INDICATOR
200 typedef const_point_iterator_ point_iterator;
203 typedef const_point_iterator_ const_point_iterator;
205 #ifdef PB_DS_DATA_TRUE_INDICATOR
206 typedef iterator_ iterator;
209 #ifdef PB_DS_DATA_FALSE_INDICATOR
210 typedef const_iterator_ iterator;
213 typedef const_iterator_ const_iterator;
217 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC&);
219 PB_DS_CLASS_NAME(const Hash_Fn&);
221 PB_DS_CLASS_NAME(const Hash_Fn&, const Eq_Fn&);
223 PB_DS_CLASS_NAME(const Hash_Fn&, const Eq_Fn&, const Comb_Probe_Fn&);
225 PB_DS_CLASS_NAME(const Hash_Fn&, const Eq_Fn&, const Comb_Probe_Fn&,
228 PB_DS_CLASS_NAME(const Hash_Fn&, const Eq_Fn&, const Comb_Probe_Fn&,
229 const Probe_Fn&, const Resize_Policy&);
231 template<typename It>
233 copy_from_range(It first_it, It last_it);
239 swap(PB_DS_CLASS_C_DEC& other);
266 get_probe_fn() const;
272 get_comb_probe_fn() const;
278 get_resize_policy() const;
280 inline std::pair<point_iterator, bool>
281 insert(const_reference r_val)
283 _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
284 return insert_imp(r_val, traits_base::m_store_extra_indicator);
287 inline mapped_reference
288 operator[](const_key_reference r_key)
290 #ifdef PB_DS_DATA_TRUE_INDICATOR
291 return subscript_imp(r_key, traits_base::m_store_extra_indicator);
294 return traits_base::s_null_mapped;
298 inline point_iterator
299 find(const_key_reference r_key);
301 inline const_point_iterator
302 find(const_key_reference r_key) const;
304 inline point_iterator
307 inline const_point_iterator
311 erase(const_key_reference r_key);
313 template<typename Pred>
323 inline const_iterator
329 inline const_iterator
332 #ifdef _GLIBCXX_DEBUG
334 assert_valid() const;
337 #ifdef PB_DS_HT_MAP_TRACE_
343 #ifdef PB_DS_DATA_TRUE_INDICATOR
344 friend class iterator_;
347 friend class const_iterator_;
356 erase_all_valid_entries(entry_array, size_type);
359 do_resize_if_needed();
362 do_resize_if_needed_no_throw();
365 resize_imp(size_type);
368 do_resize(size_type);
371 resize_imp(entry_array, size_type);
374 resize_imp_reassign(entry_pointer, entry_array, false_type);
377 resize_imp_reassign(entry_pointer, entry_array, true_type);
380 find_ins_pos(const_key_reference, false_type);
383 find_ins_pos(const_key_reference, true_type);
385 inline std::pair<point_iterator, bool>
386 insert_imp(const_reference, false_type);
388 inline std::pair<point_iterator, bool>
389 insert_imp(const_reference, true_type);
392 insert_new_imp(const_reference r_val, size_type pos)
394 _GLIBCXX_DEBUG_ASSERT(m_entries[pos].m_stat != valid_entry_status);
396 if (do_resize_if_needed())
397 pos = find_ins_pos(PB_DS_V2F(r_val),
398 traits_base::m_store_extra_indicator);
400 _GLIBCXX_DEBUG_ASSERT(m_entries[pos].m_stat != valid_entry_status);
402 entry* const p_e = m_entries + pos;
403 new (&p_e->m_value) value_type(r_val);
404 p_e->m_stat = valid_entry_status;
405 resize_base::notify_inserted(++m_num_used_e);
407 _GLIBCXX_DEBUG_ONLY(debug_base::insert_new(PB_DS_V2F(p_e->m_value));)
409 _GLIBCXX_DEBUG_ONLY(assert_valid();)
410 return &p_e->m_value;
414 insert_new_imp(const_reference r_val, comp_hash& r_pos_hash_pair)
416 _GLIBCXX_DEBUG_ASSERT(m_entries[r_pos_hash_pair.first].m_stat !=
419 if (do_resize_if_needed())
420 r_pos_hash_pair = find_ins_pos(PB_DS_V2F(r_val),
421 traits_base::m_store_extra_indicator);
423 _GLIBCXX_DEBUG_ASSERT(m_entries[r_pos_hash_pair.first].m_stat !=
426 entry* const p_e = m_entries + r_pos_hash_pair.first;
427 new (&p_e->m_value) value_type(r_val);
428 p_e->m_hash = r_pos_hash_pair.second;
429 p_e->m_stat = valid_entry_status;
431 resize_base::notify_inserted(++m_num_used_e);
433 _GLIBCXX_DEBUG_ONLY(debug_base::insert_new(PB_DS_V2F(p_e->m_value));)
435 _GLIBCXX_DEBUG_ONLY(assert_valid();)
436 return &p_e->m_value;
439 #ifdef PB_DS_DATA_TRUE_INDICATOR
440 inline mapped_reference
441 subscript_imp(const_key_reference key, false_type)
443 _GLIBCXX_DEBUG_ONLY(assert_valid();)
445 const size_type pos = find_ins_pos(key,
446 traits_base::m_store_extra_indicator);
448 entry_pointer p_e = &m_entries[pos];
449 if (p_e->m_stat != valid_entry_status)
450 return insert_new_imp(value_type(key, mapped_type()), pos)->second;
452 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(key);)
453 return p_e->m_value.second;
456 inline mapped_reference
457 subscript_imp(const_key_reference key, true_type)
459 _GLIBCXX_DEBUG_ONLY(assert_valid();)
461 comp_hash pos_hash_pair =
462 find_ins_pos(key, traits_base::m_store_extra_indicator);
464 if (m_entries[pos_hash_pair.first].m_stat != valid_entry_status)
465 return insert_new_imp(value_type(key, mapped_type()),
466 pos_hash_pair)->second;
468 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(key));
469 return (m_entries + pos_hash_pair.first)->m_value.second;
474 find_key_pointer(const_key_reference key, false_type)
476 const size_type hash = ranged_probe_fn_base::operator()(key);
478 resize_base::notify_find_search_start();
480 // Loop until entry is found or until all possible entries accessed.
481 for (i = 0; i < m_num_e; ++i)
483 const size_type pos = ranged_probe_fn_base::operator()(key, hash, i);
485 entry* const p_e = m_entries + pos;
488 case empty_entry_status:
490 resize_base::notify_find_search_end();
491 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(key);)
496 case valid_entry_status:
497 if (hash_eq_fn_base::operator()(PB_DS_V2F(p_e->m_value), key))
499 resize_base::notify_find_search_end();
500 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(key);)
502 return pointer(&p_e->m_value);
505 case erased_entry_status:
508 _GLIBCXX_DEBUG_ASSERT(0);
511 resize_base::notify_find_search_collision();
514 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(key);)
515 resize_base::notify_find_search_end();
520 find_key_pointer(const_key_reference key, true_type)
522 comp_hash pos_hash_pair = ranged_probe_fn_base::operator()(key);
524 resize_base::notify_find_search_start();
526 // Loop until entry is found or until all possible entries accessed.
527 for (i = 0; i < m_num_e; ++i)
529 const size_type pos =
530 ranged_probe_fn_base::operator()(key, pos_hash_pair.second, i);
532 entry* const p_e = m_entries + pos;
536 case empty_entry_status:
538 resize_base::notify_find_search_end();
539 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(key);)
544 case valid_entry_status:
545 if (hash_eq_fn_base::operator()(PB_DS_V2F(p_e->m_value),
547 key, pos_hash_pair.second))
549 resize_base::notify_find_search_end();
550 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(key);)
551 return pointer(&p_e->m_value);
554 case erased_entry_status:
557 _GLIBCXX_DEBUG_ASSERT(0);
560 resize_base::notify_find_search_collision();
563 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(key);)
564 resize_base::notify_find_search_end();
569 erase_imp(const_key_reference, true_type);
572 erase_imp(const_key_reference, false_type);
575 erase_entry(entry_pointer p_e);
577 #ifdef PB_DS_DATA_TRUE_INDICATOR
579 inc_it_state(pointer& r_p_value, size_type& r_pos) const
580 { inc_it_state((const_mapped_pointer& )r_p_value, r_pos); }
584 inc_it_state(const_pointer& r_p_value, size_type& r_pos) const
586 _GLIBCXX_DEBUG_ASSERT(r_p_value != NULL);
587 for (++r_pos; r_pos < m_num_e; ++r_pos)
589 const_entry_pointer p_e =& m_entries[r_pos];
590 if (p_e->m_stat == valid_entry_status)
592 r_p_value =& p_e->m_value;
600 get_start_it_state(const_pointer& r_p_value, size_type& r_pos) const
602 for (r_pos = 0; r_pos < m_num_e; ++r_pos)
604 const_entry_pointer p_e = &m_entries[r_pos];
605 if (p_e->m_stat == valid_entry_status)
607 r_p_value = &p_e->m_value;
615 get_start_it_state(pointer& r_p_value, size_type& r_pos)
617 for (r_pos = 0; r_pos < m_num_e; ++r_pos)
619 entry_pointer p_e = &m_entries[r_pos];
620 if (p_e->m_stat == valid_entry_status)
622 r_p_value = &p_e->m_value;
629 #ifdef _GLIBCXX_DEBUG
631 assert_entry_array_valid(const entry_array, false_type) const;
634 assert_entry_array_valid(const entry_array, true_type) const;
637 static entry_allocator s_entry_allocator;
638 static iterator s_end_it;
639 static const_iterator s_const_end_it;
642 size_type m_num_used_e;
643 entry_pointer m_entries;
647 store_hash_ok = !Store_Hash
648 || !is_same<Hash_Fn, __gnu_pbds::null_hash_fn>::value
651 PB_DS_STATIC_ASSERT(sth, store_hash_ok);
654 #include <ext/pb_ds/detail/gp_hash_table_map_/constructor_destructor_fn_imps.hpp>
655 #include <ext/pb_ds/detail/gp_hash_table_map_/find_fn_imps.hpp>
656 #include <ext/pb_ds/detail/gp_hash_table_map_/resize_fn_imps.hpp>
657 #include <ext/pb_ds/detail/gp_hash_table_map_/debug_fn_imps.hpp>
658 #include <ext/pb_ds/detail/gp_hash_table_map_/info_fn_imps.hpp>
659 #include <ext/pb_ds/detail/gp_hash_table_map_/policy_access_fn_imps.hpp>
660 #include <ext/pb_ds/detail/gp_hash_table_map_/erase_fn_imps.hpp>
661 #include <ext/pb_ds/detail/gp_hash_table_map_/iterator_fn_imps.hpp>
662 #include <ext/pb_ds/detail/gp_hash_table_map_/insert_fn_imps.hpp>
663 #include <ext/pb_ds/detail/gp_hash_table_map_/trace_fn_imps.hpp>
665 #undef PB_DS_CLASS_T_DEC
666 #undef PB_DS_CLASS_C_DEC
667 #undef PB_DS_HASH_EQ_FN_C_DEC
668 #undef PB_DS_RANGED_PROBE_FN_C_DEC
669 #undef PB_DS_TYPES_TRAITS_C_DEC
670 #undef PB_DS_DEBUG_MAP_BASE_C_DEC
671 #undef PB_DS_CLASS_NAME
675 } // namespace detail
676 } // namespace __gnu_pbds