3 // Copyright (C) 2005, 2006, 2007 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/debug_map_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_DEBUG_MAP_BASE_C_DEC \
93 debug_map_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 template<typename Key
,
112 typename Comb_Probe_Fn
,
114 typename Resize_Policy
>
115 class PB_DS_CLASS_NAME
:
116 #ifdef _GLIBCXX_DEBUG
117 protected PB_DS_DEBUG_MAP_BASE_C_DEC
,
119 public PB_DS_HASH_EQ_FN_C_DEC
,
120 public Resize_Policy
,
121 public PB_DS_RANGED_PROBE_FN_C_DEC
,
122 public PB_DS_TYPES_TRAITS_C_DEC
125 typedef PB_DS_TYPES_TRAITS_C_DEC traits_base
;
126 typedef typename
traits_base::value_type value_type_
;
127 typedef typename
traits_base::pointer pointer_
;
128 typedef typename
traits_base::const_pointer const_pointer_
;
129 typedef typename
traits_base::reference reference_
;
130 typedef typename
traits_base::const_reference const_reference_
;
131 typedef typename
traits_base::comp_hash comp_hash
;
138 } __attribute__ ((packed
));
140 struct entry
: public traits_base::stored_value_type
145 typedef typename
Allocator::template rebind
<entry
>::other entry_allocator
;
146 typedef typename
entry_allocator::pointer entry_pointer
;
147 typedef typename
entry_allocator::const_pointer const_entry_pointer
;
148 typedef typename
entry_allocator::reference entry_reference
;
149 typedef typename
entry_allocator::const_reference const_entry_reference
;
150 typedef typename
entry_allocator::pointer entry_array
;
152 typedef PB_DS_RANGED_PROBE_FN_C_DEC ranged_probe_fn_base
;
154 #ifdef _GLIBCXX_DEBUG
155 typedef PB_DS_DEBUG_MAP_BASE_C_DEC debug_base
;
158 typedef PB_DS_HASH_EQ_FN_C_DEC hash_eq_fn_base
;
159 typedef Resize_Policy resize_base
;
161 #define PB_DS_GEN_POS typename Allocator::size_type
163 #include <ext/pb_ds/detail/unordered_iterator/const_point_iterator.hpp>
164 #include <ext/pb_ds/detail/unordered_iterator/point_iterator.hpp>
165 #include <ext/pb_ds/detail/unordered_iterator/const_iterator.hpp>
166 #include <ext/pb_ds/detail/unordered_iterator/iterator.hpp>
171 typedef Allocator allocator
;
172 typedef typename
Allocator::size_type size_type
;
173 typedef typename
Allocator::difference_type difference_type
;
174 typedef Hash_Fn hash_fn
;
176 typedef Probe_Fn probe_fn
;
177 typedef Comb_Probe_Fn comb_probe_fn
;
178 typedef Resize_Policy resize_policy
;
182 store_hash
= Store_Hash
185 typedef typename
traits_base::key_type key_type
;
186 typedef typename
traits_base::key_pointer key_pointer
;
187 typedef typename
traits_base::const_key_pointer const_key_pointer
;
188 typedef typename
traits_base::key_reference key_reference
;
189 typedef typename
traits_base::const_key_reference const_key_reference
;
190 typedef typename
traits_base::mapped_type mapped_type
;
191 typedef typename
traits_base::mapped_pointer mapped_pointer
;
192 typedef typename
traits_base::const_mapped_pointer const_mapped_pointer
;
193 typedef typename
traits_base::mapped_reference mapped_reference
;
194 typedef typename
traits_base::const_mapped_reference const_mapped_reference
;
195 typedef typename
traits_base::value_type value_type
;
196 typedef typename
traits_base::pointer pointer
;
197 typedef typename
traits_base::const_pointer const_pointer
;
198 typedef typename
traits_base::reference reference
;
199 typedef typename
traits_base::const_reference const_reference
;
201 #ifdef PB_DS_DATA_TRUE_INDICATOR
202 typedef point_iterator_ point_iterator
;
205 #ifdef PB_DS_DATA_FALSE_INDICATOR
206 typedef const_point_iterator_ point_iterator
;
209 typedef const_point_iterator_ const_point_iterator
;
211 #ifdef PB_DS_DATA_TRUE_INDICATOR
212 typedef iterator_ iterator
;
215 #ifdef PB_DS_DATA_FALSE_INDICATOR
216 typedef const_iterator_ iterator
;
219 typedef const_iterator_ const_iterator
;
223 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC
&);
225 PB_DS_CLASS_NAME(const Hash_Fn
&);
227 PB_DS_CLASS_NAME(const Hash_Fn
&, const Eq_Fn
&);
229 PB_DS_CLASS_NAME(const Hash_Fn
&, const Eq_Fn
&, const Comb_Probe_Fn
&);
231 PB_DS_CLASS_NAME(const Hash_Fn
&, const Eq_Fn
&, const Comb_Probe_Fn
&,
234 PB_DS_CLASS_NAME(const Hash_Fn
&, const Eq_Fn
&, const Comb_Probe_Fn
&,
235 const Probe_Fn
&, const Resize_Policy
&);
237 template<typename It
>
239 copy_from_range(It first_it
, It last_it
);
245 swap(PB_DS_CLASS_C_DEC
& other
);
272 get_probe_fn() const;
278 get_comb_probe_fn() const;
284 get_resize_policy() const;
286 inline std::pair
<point_iterator
, bool>
287 insert(const_reference r_val
)
289 _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::assert_valid();)
290 return insert_imp(r_val
, traits_base::m_store_extra_indicator
);
293 inline mapped_reference
294 operator[](const_key_reference r_key
)
296 #ifdef PB_DS_DATA_TRUE_INDICATOR
297 return subscript_imp(r_key
, traits_base::m_store_extra_indicator
);
300 return traits_base::s_null_mapped
;
304 inline point_iterator
305 find(const_key_reference r_key
);
307 inline const_point_iterator
308 find(const_key_reference r_key
) const;
310 inline point_iterator
313 inline const_point_iterator
317 erase(const_key_reference r_key
);
319 template<typename Pred
>
329 inline const_iterator
335 inline const_iterator
338 #ifdef _GLIBCXX_DEBUG
340 assert_valid() const;
343 #ifdef PB_DS_HT_MAP_TRACE_
349 #ifdef PB_DS_DATA_TRUE_INDICATOR
350 friend class iterator_
;
353 friend class const_iterator_
;
362 erase_all_valid_entries(entry_array
, size_type
);
365 do_resize_if_needed();
368 do_resize_if_needed_no_throw();
371 resize_imp(size_type
);
374 do_resize(size_type
);
377 resize_imp(entry_array
, size_type
);
380 resize_imp_reassign(entry_pointer
, entry_array
, false_type
);
383 resize_imp_reassign(entry_pointer
, entry_array
, true_type
);
386 find_ins_pos(const_key_reference
, false_type
);
389 find_ins_pos(const_key_reference
, true_type
);
391 inline std::pair
<point_iterator
, bool>
392 insert_imp(const_reference
, false_type
);
394 inline std::pair
<point_iterator
, bool>
395 insert_imp(const_reference
, true_type
);
398 insert_new_imp(const_reference r_val
, size_type pos
)
400 _GLIBCXX_DEBUG_ASSERT(m_entries
[pos
].m_stat
!= valid_entry_status
);
402 if (do_resize_if_needed())
403 pos
= find_ins_pos(PB_DS_V2F(r_val
),
404 traits_base::m_store_extra_indicator
);
406 _GLIBCXX_DEBUG_ASSERT(m_entries
[pos
].m_stat
!= valid_entry_status
);
408 entry
* const p_e
= m_entries
+ pos
;
409 new (&p_e
->m_value
) value_type(r_val
);
410 p_e
->m_stat
= valid_entry_status
;
411 resize_base::notify_inserted(++m_num_used_e
);
413 _GLIBCXX_DEBUG_ONLY(debug_base::insert_new(PB_DS_V2F(p_e
->m_value
));)
415 _GLIBCXX_DEBUG_ONLY(assert_valid();)
416 return &p_e
->m_value
;
420 insert_new_imp(const_reference r_val
, comp_hash
& r_pos_hash_pair
)
422 _GLIBCXX_DEBUG_ASSERT(m_entries
[r_pos_hash_pair
.first
].m_stat
!=
425 if (do_resize_if_needed())
426 r_pos_hash_pair
= find_ins_pos(PB_DS_V2F(r_val
),
427 traits_base::m_store_extra_indicator
);
429 _GLIBCXX_DEBUG_ASSERT(m_entries
[r_pos_hash_pair
.first
].m_stat
!=
432 entry
* const p_e
= m_entries
+ r_pos_hash_pair
.first
;
433 new (&p_e
->m_value
) value_type(r_val
);
434 p_e
->m_hash
= r_pos_hash_pair
.second
;
435 p_e
->m_stat
= valid_entry_status
;
437 resize_base::notify_inserted(++m_num_used_e
);
439 _GLIBCXX_DEBUG_ONLY(debug_base::insert_new(PB_DS_V2F(p_e
->m_value
));)
441 _GLIBCXX_DEBUG_ONLY(assert_valid();)
442 return &p_e
->m_value
;
445 #ifdef PB_DS_DATA_TRUE_INDICATOR
446 inline mapped_reference
447 subscript_imp(const_key_reference key
, false_type
)
449 _GLIBCXX_DEBUG_ONLY(assert_valid();)
451 const size_type pos
= find_ins_pos(key
,
452 traits_base::m_store_extra_indicator
);
454 entry_pointer p_e
= &m_entries
[pos
];
455 if (p_e
->m_stat
!= valid_entry_status
)
456 return insert_new_imp(value_type(key
, mapped_type()), pos
)->second
;
458 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(key
);)
459 return p_e
->m_value
.second
;
462 inline mapped_reference
463 subscript_imp(const_key_reference key
, true_type
)
465 _GLIBCXX_DEBUG_ONLY(assert_valid();)
467 comp_hash pos_hash_pair
=
468 find_ins_pos(key
, traits_base::m_store_extra_indicator
);
470 if (m_entries
[pos_hash_pair
.first
].m_stat
!= valid_entry_status
)
471 return insert_new_imp(value_type(key
, mapped_type()),
472 pos_hash_pair
)->second
;
474 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(key
));
475 return (m_entries
+ pos_hash_pair
.first
)->m_value
.second
;
480 find_key_pointer(const_key_reference key
, false_type
)
482 const size_type hash
= ranged_probe_fn_base::operator()(key
);
484 resize_base::notify_find_search_start();
486 // Loop until entry is found or until all possible entries accessed.
487 for (i
= 0; i
< m_num_e
; ++i
)
489 const size_type pos
= ranged_probe_fn_base::operator()(key
, hash
, i
);
491 entry
* const p_e
= m_entries
+ pos
;
494 case empty_entry_status
:
496 resize_base::notify_find_search_end();
497 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(key
);)
502 case valid_entry_status
:
503 if (hash_eq_fn_base::operator()(PB_DS_V2F(p_e
->m_value
), key
))
505 resize_base::notify_find_search_end();
506 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(key
);)
508 return pointer(&p_e
->m_value
);
511 case erased_entry_status
:
514 _GLIBCXX_DEBUG_ASSERT(0);
517 resize_base::notify_find_search_collision();
520 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(key
);)
521 resize_base::notify_find_search_end();
526 find_key_pointer(const_key_reference key
, true_type
)
528 comp_hash pos_hash_pair
= ranged_probe_fn_base::operator()(key
);
530 resize_base::notify_find_search_start();
532 // Loop until entry is found or until all possible entries accessed.
533 for (i
= 0; i
< m_num_e
; ++i
)
535 const size_type pos
=
536 ranged_probe_fn_base::operator()(key
, pos_hash_pair
.second
, i
);
538 entry
* const p_e
= m_entries
+ pos
;
542 case empty_entry_status
:
544 resize_base::notify_find_search_end();
545 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(key
);)
550 case valid_entry_status
:
551 if (hash_eq_fn_base::operator()(PB_DS_V2F(p_e
->m_value
),
553 key
, pos_hash_pair
.second
))
555 resize_base::notify_find_search_end();
556 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(key
);)
557 return pointer(&p_e
->m_value
);
560 case erased_entry_status
:
563 _GLIBCXX_DEBUG_ASSERT(0);
566 resize_base::notify_find_search_collision();
569 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(key
);)
570 resize_base::notify_find_search_end();
575 erase_imp(const_key_reference
, true_type
);
578 erase_imp(const_key_reference
, false_type
);
581 erase_entry(entry_pointer p_e
);
583 #ifdef PB_DS_DATA_TRUE_INDICATOR
585 inc_it_state(pointer
& r_p_value
, size_type
& r_pos
) const
586 { inc_it_state((const_mapped_pointer
& )r_p_value
, r_pos
); }
590 inc_it_state(const_pointer
& r_p_value
, size_type
& r_pos
) const
592 _GLIBCXX_DEBUG_ASSERT(r_p_value
!= NULL
);
593 for (++r_pos
; r_pos
< m_num_e
; ++r_pos
)
595 const_entry_pointer p_e
=& m_entries
[r_pos
];
596 if (p_e
->m_stat
== valid_entry_status
)
598 r_p_value
=& p_e
->m_value
;
606 get_start_it_state(const_pointer
& r_p_value
, size_type
& r_pos
) const
608 for (r_pos
= 0; r_pos
< m_num_e
; ++r_pos
)
610 const_entry_pointer p_e
= &m_entries
[r_pos
];
611 if (p_e
->m_stat
== valid_entry_status
)
613 r_p_value
= &p_e
->m_value
;
621 get_start_it_state(pointer
& r_p_value
, size_type
& r_pos
)
623 for (r_pos
= 0; r_pos
< m_num_e
; ++r_pos
)
625 entry_pointer p_e
= &m_entries
[r_pos
];
626 if (p_e
->m_stat
== valid_entry_status
)
628 r_p_value
= &p_e
->m_value
;
635 #ifdef _GLIBCXX_DEBUG
637 assert_entry_array_valid(const entry_array
, false_type
) const;
640 assert_entry_array_valid(const entry_array
, true_type
) const;
643 static entry_allocator s_entry_allocator
;
644 static iterator s_end_it
;
645 static const_iterator s_const_end_it
;
648 size_type m_num_used_e
;
649 entry_pointer m_entries
;
653 store_hash_ok
= !Store_Hash
654 || !is_same
<Hash_Fn
, __gnu_pbds::null_hash_fn
>::value
657 PB_DS_STATIC_ASSERT(sth
, store_hash_ok
);
660 #include <ext/pb_ds/detail/gp_hash_table_map_/constructor_destructor_fn_imps.hpp>
661 #include <ext/pb_ds/detail/gp_hash_table_map_/find_fn_imps.hpp>
662 #include <ext/pb_ds/detail/gp_hash_table_map_/resize_fn_imps.hpp>
663 #include <ext/pb_ds/detail/gp_hash_table_map_/debug_fn_imps.hpp>
664 #include <ext/pb_ds/detail/gp_hash_table_map_/info_fn_imps.hpp>
665 #include <ext/pb_ds/detail/gp_hash_table_map_/policy_access_fn_imps.hpp>
666 #include <ext/pb_ds/detail/gp_hash_table_map_/erase_fn_imps.hpp>
667 #include <ext/pb_ds/detail/gp_hash_table_map_/iterator_fn_imps.hpp>
668 #include <ext/pb_ds/detail/gp_hash_table_map_/insert_fn_imps.hpp>
669 #include <ext/pb_ds/detail/gp_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_PROBE_FN_C_DEC
675 #undef PB_DS_TYPES_TRAITS_C_DEC
676 #undef PB_DS_DEBUG_MAP_BASE_C_DEC
677 #undef PB_DS_CLASS_NAME
681 } // namespace detail
682 } // namespace __gnu_pbds