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 gp_ht_map_.hpp
44 * Contains an implementation class for gp_ht_map_.
47 #include <ext/pb_ds/tag_and_trait.hpp>
48 #include <ext/pb_ds/detail/hash_fn/ranged_probe_fn.hpp>
49 #include <ext/pb_ds/detail/types_traits.hpp>
50 #include <ext/pb_ds/exception.hpp>
51 #include <ext/pb_ds/detail/eq_fn/hash_eq_fn.hpp>
53 #ifdef PB_DS_HT_MAP_TRACE_
57 #include <ext/pb_ds/detail/map_debug_base.hpp>
59 #include <debug/debug.h>
65 #define PB_DS_CLASS_T_DEC \
66 template<typename Key, typename Mapped, typename Hash_Fn, typename Eq_Fn, \
67 typename Allocator, bool Store_Hash, typename Comb_Probe_Fn, \
68 typename Probe_Fn, typename Resize_Policy>
70 #ifdef PB_DS_DATA_TRUE_INDICATOR
71 #define PB_DS_CLASS_NAME gp_ht_map_data_
74 #ifdef PB_DS_DATA_FALSE_INDICATOR
75 #define PB_DS_CLASS_NAME gp_ht_map_no_data_
78 #define PB_DS_CLASS_C_DEC \
79 PB_DS_CLASS_NAME<Key, Mapped, Hash_Fn, Eq_Fn, Allocator, \
80 Store_Hash, Comb_Probe_Fn, Probe_Fn, Resize_Policy>
82 #define PB_DS_HASH_EQ_FN_C_DEC \
83 hash_eq_fn<Key, Eq_Fn, Allocator, Store_Hash>
85 #define PB_DS_RANGED_PROBE_FN_C_DEC \
86 ranged_probe_fn<Key, Hash_Fn, Allocator, Comb_Probe_Fn, Probe_Fn, Store_Hash>
88 #define PB_DS_TYPES_TRAITS_C_DEC \
89 types_traits<Key, Mapped, Allocator, Store_Hash>
92 #define PB_DS_MAP_DEBUG_BASE_C_DEC \
93 map_debug_base<Key, Eq_Fn, typename Allocator::template rebind<Key>::other::const_reference>
96 #ifdef PB_DS_DATA_TRUE_INDICATOR
97 #define PB_DS_V2F(X) (X).first
98 #define PB_DS_V2S(X) (X).second
101 #ifdef PB_DS_DATA_FALSE_INDICATOR
102 #define PB_DS_V2F(X) (X)
103 #define PB_DS_V2S(X) Mapped()
106 #define PB_DS_STATIC_ASSERT(UNIQUE, E) \
107 typedef static_assert_dumclass<sizeof(static_assert<(bool)(E)>)> \
108 UNIQUE##static_assert_type
110 template<typename Key,
116 typename Comb_Probe_Fn,
118 typename Resize_Policy>
119 class PB_DS_CLASS_NAME :
120 #ifdef _GLIBCXX_DEBUG
121 protected PB_DS_MAP_DEBUG_BASE_C_DEC,
123 public PB_DS_HASH_EQ_FN_C_DEC,
124 public Resize_Policy,
125 public PB_DS_RANGED_PROBE_FN_C_DEC,
126 public PB_DS_TYPES_TRAITS_C_DEC
129 typedef PB_DS_TYPES_TRAITS_C_DEC traits_base;
130 typedef typename traits_base::store_extra_false_type store_hash_false_type;
131 typedef typename traits_base::store_extra_true_type store_hash_true_type;
132 typedef typename traits_base::value_type value_type_;
133 typedef typename traits_base::pointer pointer_;
134 typedef typename traits_base::const_pointer const_pointer_;
135 typedef typename traits_base::reference reference_;
136 typedef typename traits_base::const_reference const_reference_;
137 typedef typename traits_base::comp_hash comp_hash;
144 } __attribute__ ((packed));
146 struct entry : public traits_base::stored_value_type
151 typedef typename Allocator::template rebind<entry>::other entry_allocator;
152 typedef typename entry_allocator::pointer entry_pointer;
153 typedef typename entry_allocator::const_pointer const_entry_pointer;
154 typedef typename entry_allocator::reference entry_reference;
155 typedef typename entry_allocator::const_reference const_entry_reference;
156 typedef typename entry_allocator::pointer entry_array;
158 typedef PB_DS_RANGED_PROBE_FN_C_DEC ranged_probe_fn_base;
160 #ifdef _GLIBCXX_DEBUG
161 typedef PB_DS_MAP_DEBUG_BASE_C_DEC map_debug_base;
164 typedef PB_DS_HASH_EQ_FN_C_DEC hash_eq_fn_base;
165 typedef Resize_Policy resize_base;
167 #define PB_DS_GEN_POS typename Allocator::size_type
169 #include <ext/pb_ds/detail/unordered_iterator/const_point_iterator.hpp>
170 #include <ext/pb_ds/detail/unordered_iterator/point_iterator.hpp>
171 #include <ext/pb_ds/detail/unordered_iterator/const_iterator.hpp>
172 #include <ext/pb_ds/detail/unordered_iterator/iterator.hpp>
177 typedef Allocator allocator;
178 typedef typename Allocator::size_type size_type;
179 typedef typename Allocator::difference_type difference_type;
180 typedef Hash_Fn hash_fn;
182 typedef Probe_Fn probe_fn;
183 typedef Comb_Probe_Fn comb_probe_fn;
184 typedef Resize_Policy resize_policy;
188 store_hash = Store_Hash
191 typedef typename traits_base::key_type key_type;
192 typedef typename traits_base::key_pointer key_pointer;
193 typedef typename traits_base::const_key_pointer const_key_pointer;
194 typedef typename traits_base::key_reference key_reference;
195 typedef typename traits_base::const_key_reference const_key_reference;
196 typedef typename traits_base::mapped_type mapped_type;
197 typedef typename traits_base::mapped_pointer mapped_pointer;
198 typedef typename traits_base::const_mapped_pointer const_mapped_pointer;
199 typedef typename traits_base::mapped_reference mapped_reference;
200 typedef typename traits_base::const_mapped_reference const_mapped_reference;
201 typedef typename traits_base::value_type value_type;
202 typedef typename traits_base::pointer pointer;
203 typedef typename traits_base::const_pointer const_pointer;
204 typedef typename traits_base::reference reference;
205 typedef typename traits_base::const_reference const_reference;
207 #ifdef PB_DS_DATA_TRUE_INDICATOR
208 typedef point_iterator_ point_iterator;
211 #ifdef PB_DS_DATA_FALSE_INDICATOR
212 typedef const_point_iterator_ point_iterator;
215 typedef const_point_iterator_ const_point_iterator;
217 #ifdef PB_DS_DATA_TRUE_INDICATOR
218 typedef iterator_ iterator;
221 #ifdef PB_DS_DATA_FALSE_INDICATOR
222 typedef const_iterator_ iterator;
225 typedef const_iterator_ const_iterator;
229 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC&);
231 PB_DS_CLASS_NAME(const Hash_Fn&);
233 PB_DS_CLASS_NAME(const Hash_Fn&, const Eq_Fn&);
235 PB_DS_CLASS_NAME(const Hash_Fn&, const Eq_Fn&, const Comb_Probe_Fn&);
237 PB_DS_CLASS_NAME(const Hash_Fn&, const Eq_Fn&, const Comb_Probe_Fn&,
240 PB_DS_CLASS_NAME(const Hash_Fn&, const Eq_Fn&, const Comb_Probe_Fn&,
241 const Probe_Fn&, const Resize_Policy&);
243 template<typename It>
245 copy_from_range(It first_it, It last_it);
251 swap(PB_DS_CLASS_C_DEC& other);
278 get_probe_fn() const;
284 get_comb_probe_fn() const;
290 get_resize_policy() const;
292 inline std::pair<point_iterator, bool>
293 insert(const_reference r_val)
295 _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
296 return insert_imp(r_val, traits_base::m_store_extra_indicator);
299 inline mapped_reference
300 operator[](const_key_reference r_key)
302 #ifdef PB_DS_DATA_TRUE_INDICATOR
303 return subscript_imp(r_key, traits_base::m_store_extra_indicator);
306 return traits_base::s_null_mapped;
310 inline point_iterator
311 find(const_key_reference r_key);
313 inline const_point_iterator
314 find(const_key_reference r_key) const;
316 inline point_iterator
319 inline const_point_iterator
323 erase(const_key_reference r_key);
325 template<typename Pred>
335 inline const_iterator
341 inline const_iterator
344 #ifdef _GLIBCXX_DEBUG
346 assert_valid() const;
349 #ifdef PB_DS_HT_MAP_TRACE_
355 #ifdef PB_DS_DATA_TRUE_INDICATOR
356 friend class iterator_;
359 friend class const_iterator_;
368 erase_all_valid_entries(entry_array, size_type);
371 do_resize_if_needed();
374 do_resize_if_needed_no_throw();
377 resize_imp(size_type);
380 do_resize(size_type);
383 resize_imp(entry_array, size_type);
386 resize_imp_reassign(entry_pointer, entry_array, store_hash_false_type);
389 resize_imp_reassign(entry_pointer, entry_array, store_hash_true_type);
392 find_ins_pos(const_key_reference, store_hash_false_type);
395 find_ins_pos(const_key_reference, store_hash_true_type);
397 inline std::pair<point_iterator, bool>
398 insert_imp(const_reference, store_hash_false_type);
400 inline std::pair<point_iterator, bool>
401 insert_imp(const_reference, store_hash_true_type);
404 insert_new_imp(const_reference r_val, size_type pos)
406 _GLIBCXX_DEBUG_ASSERT(m_entries[pos].m_stat != valid_entry_status);
408 if (do_resize_if_needed())
409 pos = find_ins_pos(PB_DS_V2F(r_val),
410 traits_base::m_store_extra_indicator);
412 _GLIBCXX_DEBUG_ASSERT(m_entries[pos].m_stat != valid_entry_status);
414 entry* const p_e = m_entries + pos;
415 new (&p_e->m_value) value_type(r_val);
416 p_e->m_stat = valid_entry_status;
417 resize_base::notify_inserted(++m_num_used_e);
419 _GLIBCXX_DEBUG_ONLY(map_debug_base::insert_new(PB_DS_V2F(p_e->m_value));)
421 _GLIBCXX_DEBUG_ONLY(assert_valid();)
422 return &p_e->m_value;
426 insert_new_imp(const_reference r_val, comp_hash& r_pos_hash_pair)
428 _GLIBCXX_DEBUG_ASSERT(m_entries[r_pos_hash_pair.first].m_stat !=
431 if (do_resize_if_needed())
432 r_pos_hash_pair = find_ins_pos(PB_DS_V2F(r_val),
433 traits_base::m_store_extra_indicator);
435 _GLIBCXX_DEBUG_ASSERT(m_entries[r_pos_hash_pair.first].m_stat !=
438 entry* const p_e = m_entries + r_pos_hash_pair.first;
439 new (&p_e->m_value) value_type(r_val);
440 p_e->m_hash = r_pos_hash_pair.second;
441 p_e->m_stat = valid_entry_status;
443 resize_base::notify_inserted(++m_num_used_e);
445 _GLIBCXX_DEBUG_ONLY(map_debug_base::insert_new(PB_DS_V2F(p_e->m_value));)
447 _GLIBCXX_DEBUG_ONLY(assert_valid();)
448 return &p_e->m_value;
451 #ifdef PB_DS_DATA_TRUE_INDICATOR
452 inline mapped_reference
453 subscript_imp(const_key_reference key, store_hash_false_type)
455 _GLIBCXX_DEBUG_ONLY(assert_valid();)
457 const size_type pos = find_ins_pos(key,
458 traits_base::m_store_extra_indicator);
460 entry_pointer p_e =& m_entries[pos];
462 if (p_e->m_stat != valid_entry_status)
463 return insert_new_imp(value_type(key, mapped_type()), pos)->second;
465 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_exists(key);)
466 return p_e->m_value.second;
469 inline mapped_reference
470 subscript_imp(const_key_reference key, store_hash_true_type)
472 _GLIBCXX_DEBUG_ONLY(assert_valid();)
474 comp_hash pos_hash_pair =
475 find_ins_pos(key, traits_base::m_store_extra_indicator);
477 if (m_entries[pos_hash_pair.first].m_stat != valid_entry_status)
478 return insert_new_imp(value_type(key, mapped_type()),
479 pos_hash_pair)->second;
481 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_exists(key));
482 return (m_entries + pos_hash_pair.first)->m_value.second;
487 find_key_pointer(const_key_reference key, store_hash_false_type)
489 const size_type hash = ranged_probe_fn_base::operator()(key);
491 resize_base::notify_find_search_start();
493 // Loop until entry is found or until all possible entries accessed.
494 for (i = 0; i < m_num_e; ++i)
496 const size_type pos = ranged_probe_fn_base::operator()(key, hash, i);
498 entry* const p_e = m_entries + pos;
501 case empty_entry_status:
503 resize_base::notify_find_search_end();
504 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_does_not_exist(key);)
509 case valid_entry_status:
510 if (hash_eq_fn_base::operator()(PB_DS_V2F(p_e->m_value), key))
512 resize_base::notify_find_search_end();
513 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_exists(key);)
515 return pointer(&p_e->m_value);
518 case erased_entry_status:
521 _GLIBCXX_DEBUG_ASSERT(0);
524 resize_base::notify_find_search_collision();
527 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_does_not_exist(key);)
528 resize_base::notify_find_search_end();
533 find_key_pointer(const_key_reference key, store_hash_true_type)
535 comp_hash pos_hash_pair = ranged_probe_fn_base::operator()(key);
537 resize_base::notify_find_search_start();
539 // Loop until entry is found or until all possible entries accessed.
540 for (i = 0; i < m_num_e; ++i)
542 const size_type pos =
543 ranged_probe_fn_base::operator()(key, pos_hash_pair.second, i);
545 entry* const p_e = m_entries + pos;
549 case empty_entry_status:
551 resize_base::notify_find_search_end();
552 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_does_not_exist(key);)
557 case valid_entry_status:
558 if (hash_eq_fn_base::operator()(PB_DS_V2F(p_e->m_value),
560 key, pos_hash_pair.second))
562 resize_base::notify_find_search_end();
563 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_exists(key);)
564 return pointer(&p_e->m_value);
567 case erased_entry_status:
570 _GLIBCXX_DEBUG_ASSERT(0);
573 resize_base::notify_find_search_collision();
576 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_does_not_exist(key);)
577 resize_base::notify_find_search_end();
582 erase_imp(const_key_reference, true_type);
585 erase_imp(const_key_reference, false_type);
588 erase_entry(entry_pointer p_e);
590 #ifdef PB_DS_DATA_TRUE_INDICATOR
592 inc_it_state(pointer& r_p_value, size_type& r_pos) const
593 { inc_it_state((const_mapped_pointer& )r_p_value, r_pos); }
597 inc_it_state(const_pointer& r_p_value, size_type& r_pos) const
599 _GLIBCXX_DEBUG_ASSERT(r_p_value != NULL);
600 for (++r_pos; r_pos < m_num_e; ++r_pos)
602 const_entry_pointer p_e =& m_entries[r_pos];
603 if (p_e->m_stat == valid_entry_status)
605 r_p_value =& p_e->m_value;
613 get_start_it_state(const_pointer& r_p_value, size_type& r_pos) const
615 for (r_pos = 0; r_pos < m_num_e; ++r_pos)
617 const_entry_pointer p_e = &m_entries[r_pos];
618 if (p_e->m_stat == valid_entry_status)
620 r_p_value = &p_e->m_value;
628 get_start_it_state(pointer& r_p_value, size_type& r_pos)
630 for (r_pos = 0; r_pos < m_num_e; ++r_pos)
632 entry_pointer p_e = &m_entries[r_pos];
633 if (p_e->m_stat == valid_entry_status)
635 r_p_value = &p_e->m_value;
642 #ifdef _GLIBCXX_DEBUG
644 assert_entry_array_valid(const entry_array, store_hash_false_type) const;
647 assert_entry_array_valid(const entry_array, store_hash_true_type) const;
650 static entry_allocator s_entry_allocator;
651 static iterator s_end_it;
652 static const_iterator s_const_end_it;
655 size_type m_num_used_e;
656 entry_pointer m_entries;
660 store_hash_ok = !Store_Hash
661 || !is_same<Hash_Fn, pb_ds::null_hash_fn>::value
664 PB_DS_STATIC_ASSERT(sth, store_hash_ok);
667 #include <ext/pb_ds/detail/gp_hash_table_map_/constructor_destructor_fn_imps.hpp>
668 #include <ext/pb_ds/detail/gp_hash_table_map_/find_fn_imps.hpp>
669 #include <ext/pb_ds/detail/gp_hash_table_map_/resize_fn_imps.hpp>
670 #include <ext/pb_ds/detail/gp_hash_table_map_/debug_fn_imps.hpp>
671 #include <ext/pb_ds/detail/gp_hash_table_map_/info_fn_imps.hpp>
672 #include <ext/pb_ds/detail/gp_hash_table_map_/policy_access_fn_imps.hpp>
673 #include <ext/pb_ds/detail/gp_hash_table_map_/erase_fn_imps.hpp>
674 #include <ext/pb_ds/detail/gp_hash_table_map_/iterator_fn_imps.hpp>
675 #include <ext/pb_ds/detail/gp_hash_table_map_/insert_fn_imps.hpp>
676 #include <ext/pb_ds/detail/gp_hash_table_map_/trace_fn_imps.hpp>
678 #undef PB_DS_CLASS_T_DEC
679 #undef PB_DS_CLASS_C_DEC
680 #undef PB_DS_HASH_EQ_FN_C_DEC
681 #undef PB_DS_RANGED_PROBE_FN_C_DEC
682 #undef PB_DS_TYPES_TRAITS_C_DEC
683 #undef PB_DS_MAP_DEBUG_BASE_C_DEC
684 #undef PB_DS_CLASS_NAME
687 #undef PB_DS_STATIC_ASSERT
689 } // namespace detail