3 // Copyright (C) 2005 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
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32 // Permission to use, copy, modify, sell, and distribute this software
33 // is hereby granted without fee, provided that the above copyright
34 // notice appears in all copies, and that both that copyright notice and
35 // this permission notice appear in supporting documentation. None of
36 // the above authors, nor IBM Haifa Research Laboratories, make any
37 // representation about the suitability of this software for any
38 // purpose. It is provided "as is" without express or implied warranty.
41 * @file hash_policy.hpp
42 * Contains hash-related policies.
45 #ifndef HASH_POLICY_HPP
46 #define HASH_POLICY_HPP
51 #include <ext/pb_assoc/exception.hpp>
52 #include <ext/pb_assoc/detail/hash_fn/mask_based_range_hashing.hpp>
53 #include <ext/pb_assoc/detail/hash_fn/mod_based_range_hashing.hpp>
54 #include <ext/pb_assoc/detail/resize_policy/size_base.hpp>
64 #define PB_ASSOC_CLASS_T_DEC \
65 template<typename Const_Key_Ref, typename Size_Type>
67 #define PB_ASSOC_CLASS_C_DEC \
72 template<typename Const_Key_Ref
, typename Size_Type
= size_t>
76 typedef Size_Type size_type
;
79 swap(PB_ASSOC_CLASS_C_DEC
& r_other
);
83 operator()(Const_Key_Ref r_key
, size_type i
) const;
86 #include <ext/pb_assoc/detail/hash_fn/linear_probe_fn_imp.hpp>
88 #undef PB_ASSOC_CLASS_T_DEC
89 #undef PB_ASSOC_CLASS_C_DEC
91 #define PB_ASSOC_CLASS_T_DEC \
92 template<class Const_Key_Ref, typename Size_Type>
94 #define PB_ASSOC_CLASS_C_DEC \
95 quadratic_probe_fn<Const_Key_Ref, Size_Type>
97 template<typename Const_Key_Ref
, typename Size_Type
= size_t>
98 class quadratic_probe_fn
101 typedef Size_Type size_type
;
104 swap(PB_ASSOC_CLASS_C_DEC
& r_other
);
108 operator()(Const_Key_Ref r_key
, size_type i
) const;
111 #include <ext/pb_assoc/detail/hash_fn/quadratic_probe_fn_imp.hpp>
113 #undef PB_ASSOC_CLASS_T_DEC
114 #undef PB_ASSOC_CLASS_C_DEC
116 #define PB_ASSOC_CLASS_T_DEC \
117 template<typename Size_Type>
119 #define PB_ASSOC_CLASS_C_DEC \
120 direct_mask_range_hashing<Size_Type>
122 template<typename Size_Type
= size_t>
123 class direct_mask_range_hashing
124 : public pb_assoc::detail::mask_based_range_hashing
<Size_Type
>
127 typedef Size_Type size_type
;
130 swap(PB_ASSOC_CLASS_C_DEC
& r_other
);
134 notify_resized(size_type size
);
137 operator()(size_type hash
) const;
140 typedef pb_assoc::detail::mask_based_range_hashing
<Size_Type
>
144 #define PB_ASSOC_MASK_BASED_C_DEC \
145 pb_assoc::detail::mask_based_range_hashing< \
148 #include <ext/pb_assoc/detail/hash_fn/direct_mask_range_hashing_imp.hpp>
150 #undef PB_ASSOC_CLASS_T_DEC
151 #undef PB_ASSOC_CLASS_C_DEC
153 #undef PB_ASSOC_MASK_BASED_C_DEC
155 #define PB_ASSOC_CLASS_T_DEC \
156 template<typename Size_Type>
158 #define PB_ASSOC_CLASS_C_DEC \
159 direct_mod_range_hashing<Size_Type>
161 #define PB_ASSOC_MOD_BASED_C_DEC \
162 pb_assoc::detail::mod_based_range_hashing<Size_Type>
164 template<typename Size_Type
= size_t>
165 class direct_mod_range_hashing
: public PB_ASSOC_MOD_BASED_C_DEC
168 typedef Size_Type size_type
;
171 swap(PB_ASSOC_CLASS_C_DEC
& r_other
);
175 * description = "Notifies the policy object that the container's
176 * __size has changed to size.">
179 notify_resized(size_type size
);
182 operator()(size_type hash
) const;
185 typedef PB_ASSOC_MOD_BASED_C_DEC my_mod_based_base
;
188 #include <ext/pb_assoc/detail/hash_fn/direct_mod_range_hashing_imp.hpp>
190 #undef PB_ASSOC_CLASS_T_DEC
191 #undef PB_ASSOC_CLASS_C_DEC
193 #undef PB_ASSOC_MOD_BASED_C_DEC
195 #ifdef PB_ASSOC_HT_LOAD_CHECK_RESIZE_TRIGGER_DEBUG
196 #define PB_ASSOC_DBG_ASSERT(X) assert(X)
197 #define PB_ASSOC_DBG_VERIFY(X) assert(X)
198 #define PB_ASSOC_DBG_ONLY(X) X
199 #else // #ifdef PB_ASSOC_HT_LOAD_CHECK_RESIZE_TRIGGER_DEBUG
200 #define PB_ASSOC_DBG_ASSERT(X)
201 #define PB_ASSOC_DBG_VERIFY(X) {if((X)==0);}
202 #define PB_ASSOC_DBG_ONLY(X) ;
203 #endif // #ifdef PB_ASSOC_HT_LOAD_CHECK_RESIZE_TRIGGER_DEBUG
205 #define PB_ASSOC_CLASS_T_DEC \
206 template<bool External_Load_Access, typename Size_Type>
208 #define PB_ASSOC_CLASS_C_DEC \
209 hash_load_check_resize_trigger<External_Load_Access, Size_Type>
211 #define PB_ASSOC_SIZE_BASE_C_DEC \
212 pb_assoc::detail::size_base<Size_Type, External_Load_Access>
214 template<bool External_Load_Access
= false, typename Size_Type
= size_t>
215 class hash_load_check_resize_trigger
: private PB_ASSOC_SIZE_BASE_C_DEC
218 typedef Size_Type size_type
;
222 external_load_access
= External_Load_Access
225 hash_load_check_resize_trigger(float load_min
= 0.125,
226 float load_max
= 0.5);
229 swap(PB_ASSOC_CLASS_C_DEC
& r_other
);
232 ~hash_load_check_resize_trigger();
234 inline std::pair
<float, float>
238 set_loads(std::pair
<float, float> load_pair
);
242 notify_insert_search_start();
245 notify_insert_search_collision();
248 notify_insert_search_end();
251 notify_find_search_start();
254 notify_find_search_collision();
257 notify_find_search_end();
260 notify_erase_search_start();
263 notify_erase_search_collision();
266 notify_erase_search_end();
269 notify_inserted(size_type num_entries
);
272 notify_erased(size_type num_entries
);
278 notify_resized(size_type new_size
);
281 notify_externally_resized(size_type new_size
);
284 is_resize_needed() const;
287 is_grow_needed(size_type size
, size_type num_entries
) const;
290 is_shrink_needed(size_type size
, size_type num_entries
) const;
292 typedef PB_ASSOC_SIZE_BASE_C_DEC my_size_base
;
295 inline std::pair
<float, float>
296 get_loads_imp(pb_assoc::detail::int_to_type
<true>) const;
299 set_loads_imp(std::pair
<float, float>,
300 pb_assoc::detail::int_to_type
<true>);
303 do_resize(size_type new_size
);
305 #ifdef PB_ASSOC_HT_LOAD_CHECK_RESIZE_TRIGGER_DEBUG
307 assert_valid() const;
308 #endif // #ifdef PB_ASSOC_HT_LOAD_CHECK_RESIZE_TRIGGER_DEBUG
310 float m_load_min
, m_load_max
;
312 size_type m_next_shrink_size
;
314 size_type m_next_grow_size
;
316 bool m_resize_needed
;
318 static pb_assoc::detail::int_to_type
<External_Load_Access
>
319 s_external_load_access_ind
;
322 #include <ext/pb_assoc/detail/resize_policy/hash_load_check_resize_trigger_imp.hpp>
324 #undef PB_ASSOC_CLASS_T_DEC
325 #undef PB_ASSOC_CLASS_C_DEC
327 #undef PB_ASSOC_SIZE_BASE_C_DEC
329 #undef PB_ASSOC_DBG_ASSERT
330 #undef PB_ASSOC_DBG_VERIFY
331 #undef PB_ASSOC_DBG_ONLY
333 #ifdef PB_ASSOC_HT_MAX_COLLISION_CHECK_RESIZE_TRIGGER_POLICY_DEBUG
334 #define PB_ASSOC_DBG_ASSERT(X) assert(X)
335 #define PB_ASSOC_DBG_VERIFY(X) assert(X)
336 #define PB_ASSOC_DBG_ONLY(X) X
337 #else // #ifdef PB_ASSOC_HT_MAX_COLLISION_CHECK_RESIZE_TRIGGER_POLICY_DEBUG
338 #define PB_ASSOC_DBG_ASSERT(X)
339 #define PB_ASSOC_DBG_VERIFY(X) {if((X)==0);}
340 #define PB_ASSOC_DBG_ONLY(X) ;
341 #endif // #ifdef PB_ASSOC_HT_MAX_COLLISION_CHECK_RESIZE_TRIGGER_POLICY_DEBUG
343 #define PB_ASSOC_CLASS_T_DEC \
344 template<bool External_Load_Access, typename Size_Type>
346 #define PB_ASSOC_CLASS_C_DEC \
347 cc_hash_max_collision_check_resize_trigger< \
348 External_Load_Access, \
351 template<bool External_Load_Access
= false, typename Size_Type
= size_t>
352 class cc_hash_max_collision_check_resize_trigger
355 typedef Size_Type size_type
;
359 external_load_access
= External_Load_Access
362 cc_hash_max_collision_check_resize_trigger(float load
= 0.5);
365 swap(PB_ASSOC_CLASS_C_DEC
& r_other
);
372 notify_insert_search_start();
375 notify_insert_search_collision();
378 notify_insert_search_end();
381 notify_find_search_start();
384 notify_find_search_collision();
387 notify_find_search_end();
390 notify_erase_search_start();
393 notify_erase_search_collision();
396 notify_erase_search_end();
399 notify_inserted(size_type num_entries
);
402 notify_erased(size_type num_entries
);
408 notify_resized(size_type new_size
);
411 notify_externally_resized(size_type new_size
);
414 is_resize_needed() const;
417 is_grow_needed(size_type size
, size_type num_entries
) const;
420 is_shrink_needed(size_type size
, size_type num_entries
) const;
423 template<typename Key
>
424 class max_col_checker
427 max_col_checker(size_type size
, size_type
* p_max_col
)
428 : m_p_max_col(p_max_col
), m_a_col(size
, 0)
432 operator()(const std::pair
<const Key
, size_type
>& r_key_pos_pair
)
433 { ++m_a_col
[r_key_pos_pair
.second
]; }
436 std::vector
<size_type
> m_a_col
;
438 size_type
* const m_p_max_col
;
443 get_load_imp(pb_assoc::detail::int_to_type
<true>) const;
453 bool m_resize_needed
;
455 static pb_assoc::detail::int_to_type
<External_Load_Access
>
456 s_external_load_access_ind
;
459 #include <ext/pb_assoc/detail/resize_policy/cc_hash_max_collision_resize_trigger_imp.hpp>
461 #undef PB_ASSOC_CLASS_T_DEC
462 #undef PB_ASSOC_CLASS_C_DEC
464 #undef PB_ASSOC_DBG_ASSERT
465 #undef PB_ASSOC_DBG_VERIFY
466 #undef PB_ASSOC_DBG_ONLY
468 #define PB_ASSOC_CLASS_T_DEC \
469 template<typename Size_Type>
471 #define PB_ASSOC_CLASS_C_DEC \
472 hash_exponential_size_policy< \
475 template<typename Size_Type
= size_t>
476 class hash_exponential_size_policy
479 typedef Size_Type size_type
;
481 hash_exponential_size_policy(size_type start_size
= 8,
482 size_type grow_factor
= 2);
485 swap(PB_ASSOC_CLASS_C_DEC
& r_other
);
489 get_init_size(size_type suggested_size
) const;
492 get_nearest_larger_size(size_type cur_size
) const;
495 get_nearest_smaller_size(size_type cur_size
) const;
497 #ifdef PB_ASSOC_HT_EXPONENTIAL_SIZE_POLICY_DEBUG
499 assert_is_one_of_my_sizes(size_type size
) const;
500 #endif // #ifdef PB_ASSOC_HT_EXPONENTIAL_SIZE_POLICY_DEBUG
503 size_type m_start_size
;
504 size_type m_grow_factor
;
507 #include <ext/pb_assoc/detail/resize_policy/hash_exponential_size_policy_imp.hpp>
509 #undef PB_ASSOC_CLASS_T_DEC
510 #undef PB_ASSOC_CLASS_C_DEC
512 #undef PB_ASSOC_DBG_ASSERT
513 #undef PB_ASSOC_DBG_VERIFY
514 #undef PB_ASSOC_DBG_ONLY
516 #define PB_ASSOC_CLASS_T_DEC
518 #define PB_ASSOC_CLASS_C_DEC \
519 hash_prime_size_policy
521 #ifdef PB_ASSOC_HT_PRIME_SIZE_POLICY_DEBUG
522 #define PB_ASSOC_DBG_ASSERT(X) assert(X)
523 #define PB_ASSOC_DBG_VERIFY(X) assert(X)
524 #define PB_ASSOC_DBG_ONLY(X) X
525 #else // #ifdef PB_ASSOC_HT_PRIME_SIZE_POLICY_DEBUG
526 #define PB_ASSOC_DBG_ASSERT(X)
527 #define PB_ASSOC_DBG_VERIFY(X) {if((X)==0);}
528 #define PB_ASSOC_DBG_ONLY(X) ;
529 #endif // #ifdef PB_ASSOC_HT_PRIME_SIZE_POLICY_DEBUG
531 struct hash_prime_size_policy
533 typedef size_t size_type
;
536 swap(PB_ASSOC_CLASS_C_DEC
& r_other
);
540 get_init_size(size_type suggested_size
) const;
543 get_nearest_larger_size(size_type cur_size
) const;
546 get_nearest_smaller_size(size_type cur_size
) const;
549 get_nearest_larger_size_imp(size_type size
) const;
551 #ifdef PB_ASSOC_HT_PRIME_SIZE_POLICY_DEBUG
553 assert_is_one_of_my_sizes(size_type size
) const;
554 #endif // #ifdef PB_ASSOC_HT_PRIME_SIZE_POLICY_DEBUG
557 #include <ext/pb_assoc/detail/resize_policy/hash_prime_size_policy_imp.hpp>
559 #undef PB_ASSOC_CLASS_T_DEC
560 #undef PB_ASSOC_CLASS_C_DEC
562 #undef PB_ASSOC_DBG_ASSERT
563 #undef PB_ASSOC_DBG_VERIFY
564 #undef PB_ASSOC_DBG_ONLY
566 #define PB_ASSOC_CLASS_T_DEC \
569 class Trigger_Policy, \
570 bool External_Size_Access, \
573 #define PB_ASSOC_CLASS_C_DEC \
574 hash_standard_resize_policy< \
577 External_Size_Access, \
580 template<class Size_Policy
= pb_assoc::hash_exponential_size_policy
<>,
581 class Trigger_Policy
= pb_assoc::hash_load_check_resize_trigger
<>,
582 bool External_Size_Access
= false,
583 typename Size_Type
= size_t>
584 class hash_standard_resize_policy
: public Size_Policy
, public Trigger_Policy
587 typedef Size_Type size_type
;
588 typedef Trigger_Policy trigger_policy
;
589 typedef Size_Policy size_policy
;
593 external_size_access
= External_Size_Access
596 hash_standard_resize_policy(size_type suggested_size
= 8);
598 hash_standard_resize_policy(const Size_Policy
&,
599 size_type suggested_size
= 8);
601 hash_standard_resize_policy(const Size_Policy
&, const Trigger_Policy
&,
602 size_type suggested_size
= 8);
605 ~hash_standard_resize_policy();
608 swap(PB_ASSOC_CLASS_C_DEC
& r_other
);
614 get_size_policy() const;
617 get_trigger_policy();
619 const Trigger_Policy
&
620 get_trigger_policy() const;
623 get_actual_size() const;
626 resize(size_type suggested_new_size
);
631 notify_insert_search_start();
634 notify_insert_search_collision();
637 notify_insert_search_end();
640 notify_find_search_start();
643 notify_find_search_collision();
646 notify_find_search_end();
649 notify_erase_search_start();
652 notify_erase_search_collision();
655 notify_erase_search_end();
658 notify_inserted(size_type num_e
);
661 notify_erased(size_type num_e
);
667 notify_resized(size_type new_size
);
670 get_init_size() const;
673 is_resize_needed() const;
676 get_new_size(size_type size
, size_type num_used_e
) const;
679 typedef Trigger_Policy my_trigger_policy_base
;
681 typedef Size_Policy my_size_policy_base
;
684 pb_assoc::detail::int_to_type
<false>
685 external_resize_false_indicator
;
688 pb_assoc::detail::int_to_type
<true>
689 external_resize_true_indicator
;
692 get_actual_size(external_resize_true_indicator
) const;
695 resize(size_type new_size
, external_resize_true_indicator
);
698 do_resize(size_type new_size
);
700 static pb_assoc::detail::int_to_type
<External_Size_Access
>
701 s_external_size_access_indicator
;
706 #include <ext/pb_assoc/detail/resize_policy/hash_standard_resize_policy_imp.hpp>
708 #undef PB_ASSOC_CLASS_T_DEC
709 #undef PB_ASSOC_CLASS_C_DEC
711 #undef PB_ASSOC_DBG_ASSERT
712 #undef PB_ASSOC_DBG_VERIFY
713 #undef PB_ASSOC_DBG_ONLY
715 } // namespace pb_assoc
717 #endif // #ifndef HASH_POLICY_HPP