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>
66 #define PB_DS_CLASS_T_DEC \
74 class Comb_Probe_Fn, \
78 #ifdef PB_DS_DATA_TRUE_INDICATOR
79 #define PB_DS_CLASS_NAME gp_ht_map_data_
82 #ifdef PB_DS_DATA_FALSE_INDICATOR
83 #define PB_DS_CLASS_NAME gp_ht_map_no_data_
86 #define PB_DS_CLASS_C_DEC \
98 #define PB_DS_HASH_EQ_FN_C_DEC \
105 #define PB_DS_RANGED_PROBE_FN_C_DEC \
114 #define PB_DS_TYPES_TRAITS_C_DEC \
121 #ifdef _GLIBCXX_DEBUG
122 #define PB_DS_MAP_DEBUG_BASE_C_DEC \
123 map_debug_base<Key, Eq_Fn, typename Allocator::template rebind<Key>::other::const_reference>
126 #ifdef PB_DS_DATA_TRUE_INDICATOR
127 #define PB_DS_V2F(X) (X).first
128 #define PB_DS_V2S(X) (X).second
131 #ifdef PB_DS_DATA_FALSE_INDICATOR
132 #define PB_DS_V2F(X) (X)
133 #define PB_DS_V2S(X) Mapped()
136 #define PB_DS_STATIC_ASSERT(UNIQUE, E) \
138 static_assert_dumclass< \
139 sizeof(static_assert<(bool)(E)>)> \
140 UNIQUE##static_assert_type
142 template<typename Key,
151 class PB_DS_CLASS_NAME :
152 #ifdef _GLIBCXX_DEBUG
153 protected PB_DS_MAP_DEBUG_BASE_C_DEC,
155 public PB_DS_HASH_EQ_FN_C_DEC,
156 public Resize_Policy,
157 public PB_DS_RANGED_PROBE_FN_C_DEC,
158 public PB_DS_TYPES_TRAITS_C_DEC
164 typename PB_DS_TYPES_TRAITS_C_DEC::store_extra_false_type
165 store_hash_false_type;
168 typename PB_DS_TYPES_TRAITS_C_DEC::store_extra_true_type
169 store_hash_true_type;
171 typedef typename PB_DS_TYPES_TRAITS_C_DEC::value_type value_type_;
173 typedef typename PB_DS_TYPES_TRAITS_C_DEC::pointer pointer_;
176 typename PB_DS_TYPES_TRAITS_C_DEC::const_pointer
179 typedef typename PB_DS_TYPES_TRAITS_C_DEC::reference reference_;
182 typename PB_DS_TYPES_TRAITS_C_DEC::const_reference
185 #define PB_DS_GEN_POS \
186 typename Allocator::size_type
188 #include <ext/pb_ds/detail/unordered_iterator/const_point_iterator.hpp>
189 #include <ext/pb_ds/detail/unordered_iterator/point_iterator.hpp>
190 #include <ext/pb_ds/detail/unordered_iterator/const_iterator.hpp>
191 #include <ext/pb_ds/detail/unordered_iterator/iterator.hpp>
197 typedef typename Allocator::size_type size_type;
199 typedef typename Allocator::difference_type difference_type;
201 typedef Hash_Fn hash_fn;
205 typedef Allocator allocator;
207 typedef Probe_Fn probe_fn;
209 typedef Comb_Probe_Fn comb_probe_fn;
211 typedef Resize_Policy resize_policy;
215 store_hash = Store_Hash
218 typedef typename PB_DS_TYPES_TRAITS_C_DEC::key_type key_type;
220 typedef typename PB_DS_TYPES_TRAITS_C_DEC::key_pointer key_pointer;
223 typename PB_DS_TYPES_TRAITS_C_DEC::const_key_pointer
226 typedef typename PB_DS_TYPES_TRAITS_C_DEC::key_reference key_reference;
229 typename PB_DS_TYPES_TRAITS_C_DEC::const_key_reference
232 typedef typename PB_DS_TYPES_TRAITS_C_DEC::mapped_type mapped_type;
235 typename PB_DS_TYPES_TRAITS_C_DEC::mapped_pointer
239 typename PB_DS_TYPES_TRAITS_C_DEC::const_mapped_pointer
240 const_mapped_pointer;
243 typename PB_DS_TYPES_TRAITS_C_DEC::mapped_reference
247 typename PB_DS_TYPES_TRAITS_C_DEC::const_mapped_reference
248 const_mapped_reference;
250 typedef typename PB_DS_TYPES_TRAITS_C_DEC::value_type value_type;
252 typedef typename PB_DS_TYPES_TRAITS_C_DEC::pointer pointer;
254 typedef typename PB_DS_TYPES_TRAITS_C_DEC::const_pointer const_pointer;
256 typedef typename PB_DS_TYPES_TRAITS_C_DEC::reference reference;
259 typename PB_DS_TYPES_TRAITS_C_DEC::const_reference
262 #ifdef PB_DS_DATA_TRUE_INDICATOR
263 typedef point_iterator_ point_iterator;
264 #endif // #ifdef PB_DS_DATA_TRUE_INDICATOR
266 #ifdef PB_DS_DATA_FALSE_INDICATOR
267 typedef const_point_iterator_ point_iterator;
268 #endif // #ifdef PB_DS_DATA_FALSE_INDICATOR
270 typedef const_point_iterator_ const_point_iterator;
272 #ifdef PB_DS_DATA_TRUE_INDICATOR
273 typedef iterator_ iterator;
274 #endif // #ifdef PB_DS_DATA_TRUE_INDICATOR
276 #ifdef PB_DS_DATA_FALSE_INDICATOR
277 typedef const_iterator_ iterator;
278 #endif // #ifdef PB_DS_DATA_FALSE_INDICATOR
280 typedef const_iterator_ const_iterator;
286 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC& other);
288 PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn);
290 PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn);
292 PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn, const Comb_Probe_Fn& r_comb_probe_fn);
294 PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn, const Comb_Probe_Fn& r_comb_probe_fn, const Probe_Fn& r_probe_fn);
296 PB_DS_CLASS_NAME(const Hash_Fn& r_hash_fn, const Eq_Fn& r_eq_fn, const Comb_Probe_Fn& r_comb_probe_fn, const Probe_Fn& r_probe_fn, const Resize_Policy& r_resize_policy);
298 template<typename It>
300 copy_from_range(It first_it, It last_it);
306 swap(PB_DS_CLASS_C_DEC& other);
333 get_probe_fn() const;
339 get_comb_probe_fn() const;
345 get_resize_policy() const;
347 inline std::pair<point_iterator, bool>
348 insert(const_reference r_val)
350 _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
352 return (insert_imp(r_val, traits_base::m_store_extra_indicator));
355 inline mapped_reference
356 operator[](const_key_reference r_key)
358 #ifdef PB_DS_DATA_TRUE_INDICATOR
359 return (subscript_imp(r_key, traits_base::m_store_extra_indicator));
360 #else // #ifdef PB_DS_DATA_TRUE_INDICATOR
363 return (traits_base::s_null_mapped);
364 #endif // #ifdef PB_DS_DATA_TRUE_INDICATOR
367 inline point_iterator
368 find(const_key_reference r_key);
370 inline const_point_iterator
371 find(const_key_reference r_key) const;
373 inline point_iterator
376 inline const_point_iterator
380 erase(const_key_reference r_key);
382 template<typename Pred>
392 inline const_iterator
398 inline const_iterator
401 #ifdef _GLIBCXX_DEBUG
403 assert_valid() const;
406 #ifdef PB_DS_HT_MAP_TRACE_
412 typedef PB_DS_TYPES_TRAITS_C_DEC traits_base;
421 typedef char entry_status;
423 struct entry : public PB_DS_TYPES_TRAITS_C_DEC::stored_value_type
429 typename Allocator::template rebind<entry>::other
432 typedef typename entry_allocator::pointer entry_pointer;
434 typedef typename entry_allocator::const_pointer const_entry_pointer;
436 typedef typename entry_allocator::reference entry_reference;
439 typename entry_allocator::const_reference
440 const_entry_reference;
442 typedef typename entry_allocator::pointer entry_array;
444 typedef PB_DS_RANGED_PROBE_FN_C_DEC ranged_probe_fn_base;
446 #ifdef _GLIBCXX_DEBUG
447 typedef PB_DS_MAP_DEBUG_BASE_C_DEC map_debug_base;
450 typedef PB_DS_HASH_EQ_FN_C_DEC hash_eq_fn_base;
452 typedef Resize_Policy resize_base;
454 #ifdef PB_DS_DATA_TRUE_INDICATOR
455 friend class iterator_;
456 #endif // #ifdef PB_DS_DATA_TRUE_INDICATOR
458 friend class const_iterator_;
460 typedef typename PB_DS_TYPES_TRAITS_C_DEC::comp_hash comp_hash;
471 erase_all_valid_entries(entry_array a_entries_resized, size_type size);
474 do_resize_if_needed();
477 do_resize_if_needed_no_throw();
480 resize_imp(size_type new_size);
483 do_resize(size_type new_size);
486 resize_imp(entry_array a_entries_resized, size_type old_size);
489 resize_imp_reassign(entry_pointer p_e, entry_array a_entries_resized, store_hash_false_type);
492 resize_imp_reassign(entry_pointer p_e, entry_array a_entries_resized, store_hash_true_type);
495 find_ins_pos(const_key_reference r_key, store_hash_false_type);
498 find_ins_pos(const_key_reference r_key, store_hash_true_type);
500 inline std::pair<point_iterator, bool>
501 insert_imp(const_reference r_val, store_hash_false_type);
503 inline std::pair<point_iterator, bool>
504 insert_imp(const_reference r_val, store_hash_true_type);
507 insert_new_imp(const_reference r_val, size_type pos)
509 _GLIBCXX_DEBUG_ASSERT(m_a_entries[pos].m_stat != valid_entry_status);
511 if (do_resize_if_needed())
512 pos = find_ins_pos(PB_DS_V2F(r_val),
513 traits_base::m_store_extra_indicator);
515 _GLIBCXX_DEBUG_ASSERT(m_a_entries[pos].m_stat != valid_entry_status);
517 entry* const p_e = m_a_entries + pos;
519 new (&p_e->m_value) value_type(r_val);
521 p_e->m_stat = valid_entry_status;
523 resize_base::notify_inserted(++m_num_used_e);
525 _GLIBCXX_DEBUG_ONLY(map_debug_base::
526 insert_new(PB_DS_V2F(p_e->m_value));)
528 _GLIBCXX_DEBUG_ONLY(assert_valid();)
530 return (&p_e->m_value);
534 insert_new_imp(const_reference r_val, comp_hash& r_pos_hash_pair)
536 _GLIBCXX_DEBUG_ASSERT(m_a_entries[r_pos_hash_pair.first].m_stat !=
539 if (do_resize_if_needed())
540 r_pos_hash_pair = find_ins_pos(
542 traits_base::m_store_extra_indicator);
544 _GLIBCXX_DEBUG_ASSERT(m_a_entries[r_pos_hash_pair.first].m_stat !=
547 entry* const p_e = m_a_entries + r_pos_hash_pair.first;
549 new (&p_e->m_value) value_type(r_val);
551 p_e->m_hash = r_pos_hash_pair.second;
553 p_e->m_stat = valid_entry_status;
555 resize_base::notify_inserted(++m_num_used_e);
557 _GLIBCXX_DEBUG_ONLY(map_debug_base::insert_new(
558 PB_DS_V2F(p_e->m_value));)
560 _GLIBCXX_DEBUG_ONLY(assert_valid();)
562 return (&p_e->m_value);
565 #ifdef PB_DS_DATA_TRUE_INDICATOR
566 inline mapped_reference
567 subscript_imp(const_key_reference r_key, store_hash_false_type)
569 _GLIBCXX_DEBUG_ONLY(assert_valid();)
571 const size_type pos =
572 find_ins_pos(r_key, traits_base::m_store_extra_indicator);
574 entry_pointer p_e =& m_a_entries[pos];
576 if (p_e->m_stat != valid_entry_status)
577 return (insert_new_imp(
583 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_exists(r_key);)
585 return (p_e->m_value.second);
588 inline mapped_reference
589 subscript_imp(const_key_reference r_key, store_hash_true_type)
591 _GLIBCXX_DEBUG_ONLY(assert_valid();)
593 comp_hash pos_hash_pair =
594 find_ins_pos(r_key, traits_base::m_store_extra_indicator);
596 if (m_a_entries[pos_hash_pair.first].m_stat != valid_entry_status)
597 return (insert_new_imp(
601 pos_hash_pair)->second);
603 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_exists(r_key));
605 return ((m_a_entries + pos_hash_pair.first)->m_value.second);
607 #endif // #ifdef PB_DS_DATA_TRUE_INDICATOR
610 find_key_pointer(const_key_reference r_key, store_hash_false_type)
612 const size_type hash = ranged_probe_fn_base::operator()(r_key);
616 resize_base::notify_find_search_start();
618 // Loop until entry is found or until all possible entries accessed.
620 for (i = 0; i < m_num_e; ++i)
622 const size_type pos =
623 ranged_probe_fn_base::operator()( r_key, hash, i);
625 entry* const p_e = m_a_entries + pos;
629 case empty_entry_status:
631 resize_base::notify_find_search_end();
633 _GLIBCXX_DEBUG_ONLY(map_debug_base::
634 check_key_does_not_exist(r_key);)
639 case valid_entry_status:
640 if (hash_eq_fn_base::operator()(
641 PB_DS_V2F(p_e->m_value),
644 resize_base::notify_find_search_end();
646 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_exists(r_key);)
648 return ((pointer)&p_e->m_value);
651 case erased_entry_status:
654 _GLIBCXX_DEBUG_ASSERT(0);
657 resize_base::notify_find_search_collision();
660 _GLIBCXX_DEBUG_ONLY(map_debug_base::
661 check_key_does_not_exist(r_key);)
663 resize_base::notify_find_search_end();
669 find_key_pointer(const_key_reference r_key, store_hash_true_type)
671 comp_hash pos_hash_pair = ranged_probe_fn_base::operator()(r_key);
675 resize_base::notify_find_search_start();
677 // Loop until entry is found or until all possible entries accessed.
679 for (i = 0; i < m_num_e; ++i)
681 const size_type pos =
682 ranged_probe_fn_base::operator()( r_key, pos_hash_pair.second, i);
684 entry* const p_e = m_a_entries + pos;
688 case empty_entry_status:
690 resize_base::notify_find_search_end();
692 _GLIBCXX_DEBUG_ONLY(map_debug_base::
693 check_key_does_not_exist(r_key);)
698 case valid_entry_status:
699 if (hash_eq_fn_base::operator()(
700 PB_DS_V2F(p_e->m_value),
702 r_key, pos_hash_pair.second))
704 resize_base::notify_find_search_end();
706 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_exists(r_key);)
708 return ((pointer)&p_e->m_value);
711 case erased_entry_status:
714 _GLIBCXX_DEBUG_ASSERT(0);
717 resize_base::notify_find_search_collision();
720 _GLIBCXX_DEBUG_ONLY(map_debug_base::
721 check_key_does_not_exist(r_key);)
723 resize_base::notify_find_search_end();
729 erase_imp(const_key_reference r_key, true_type);
732 erase_imp(const_key_reference r_key, false_type);
735 erase_entry(entry_pointer p_e);
737 #ifdef PB_DS_DATA_TRUE_INDICATOR
739 inc_it_state(pointer& r_p_value, size_type& r_pos) const
741 inc_it_state((const_mapped_pointer& )r_p_value, r_pos);
743 #endif // #ifdef PB_DS_DATA_TRUE_INDICATOR
746 inc_it_state(const_pointer& r_p_value, size_type& r_pos) const
748 _GLIBCXX_DEBUG_ASSERT(r_p_value != NULL);
750 for (++r_pos; r_pos < m_num_e; ++r_pos)
752 const_entry_pointer p_e =& m_a_entries[r_pos];
754 if (p_e->m_stat == valid_entry_status)
756 r_p_value =& p_e->m_value;
766 get_start_it_state(const_pointer& r_p_value, size_type& r_pos) const
768 for (r_pos = 0; r_pos < m_num_e; ++r_pos)
770 const_entry_pointer p_e =& m_a_entries[r_pos];
772 if (p_e->m_stat == valid_entry_status)
774 r_p_value =& p_e->m_value;
784 get_start_it_state(pointer& r_p_value, size_type& r_pos)
786 for (r_pos = 0; r_pos < m_num_e; ++r_pos)
788 entry_pointer p_e =& m_a_entries[r_pos];
790 if (p_e->m_stat == valid_entry_status)
792 r_p_value =& p_e->m_value;
801 #ifdef _GLIBCXX_DEBUG
803 assert_entry_array_valid(const entry_array a_entries,
804 store_hash_false_type) const;
807 assert_entry_array_valid(const entry_array a_entries,
808 store_hash_true_type) const;
812 static entry_allocator s_entry_allocator;
814 entry_pointer m_a_entries;
818 size_type m_num_used_e;
820 static iterator s_end_it;
822 static const_iterator s_const_end_it;
830 pb_ds::null_hash_fn>::value
833 PB_DS_STATIC_ASSERT(sth, store_hash_ok);
836 #include <ext/pb_ds/detail/gp_hash_table_map_/constructor_destructor_fn_imps.hpp>
837 #include <ext/pb_ds/detail/gp_hash_table_map_/find_fn_imps.hpp>
838 #include <ext/pb_ds/detail/gp_hash_table_map_/resize_fn_imps.hpp>
839 #include <ext/pb_ds/detail/gp_hash_table_map_/debug_fn_imps.hpp>
840 #include <ext/pb_ds/detail/gp_hash_table_map_/info_fn_imps.hpp>
841 #include <ext/pb_ds/detail/gp_hash_table_map_/policy_access_fn_imps.hpp>
842 #include <ext/pb_ds/detail/gp_hash_table_map_/erase_fn_imps.hpp>
843 #include <ext/pb_ds/detail/gp_hash_table_map_/iterator_fn_imps.hpp>
844 #include <ext/pb_ds/detail/gp_hash_table_map_/insert_fn_imps.hpp>
845 #include <ext/pb_ds/detail/gp_hash_table_map_/trace_fn_imps.hpp>
847 #undef PB_DS_CLASS_T_DEC
849 #undef PB_DS_CLASS_C_DEC
851 #undef PB_DS_HASH_EQ_FN_C_DEC
853 #undef PB_DS_RANGED_PROBE_FN_C_DEC
855 #undef PB_DS_TYPES_TRAITS_C_DEC
857 #undef PB_DS_MAP_DEBUG_BASE_C_DEC
859 #undef PB_DS_CLASS_NAME
864 #undef PB_DS_STATIC_ASSERT
866 } // namespace detail