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 hash_policy.hpp
44 * Contains hash-related policies.
47 #ifndef PB_DS_HASH_POLICY_HPP
48 #define PB_DS_HASH_POLICY_HPP
53 #include <ext/pb_ds/exception.hpp>
54 #include <ext/pb_ds/detail/hash_fn/mask_based_range_hashing.hpp>
55 #include <ext/pb_ds/detail/hash_fn/mod_based_range_hashing.hpp>
56 #include <ext/pb_ds/detail/resize_policy/hash_load_check_resize_trigger_size_base.hpp>
60 // A null hash function, indicating that the combining hash function
61 // is actually a ranged hash function.
65 // A null probe function, indicating that the combining probe
66 // function is actually a ranged probe function.
70 #define PB_DS_CLASS_T_DEC template<typename Size_Type>
71 #define PB_DS_CLASS_C_DEC linear_probe_fn<Size_Type>
73 // A probe sequence policy using fixed increments.
74 template<typename Size_Type = size_t>
78 typedef Size_Type size_type;
81 swap(PB_DS_CLASS_C_DEC& other);
84 // Returns the i-th offset from the hash value.
86 operator()(size_type i) const;
89 #include <ext/pb_ds/detail/hash_fn/linear_probe_fn_imp.hpp>
91 #undef PB_DS_CLASS_T_DEC
92 #undef PB_DS_CLASS_C_DEC
94 #define PB_DS_CLASS_T_DEC template<typename Size_Type>
95 #define PB_DS_CLASS_C_DEC quadratic_probe_fn<Size_Type>
97 // A probe sequence policy using square increments.
98 template<typename Size_Type = size_t>
99 class quadratic_probe_fn
102 typedef Size_Type size_type;
105 swap(PB_DS_CLASS_C_DEC& other);
108 // Returns the i-th offset from the hash value.
110 operator()(size_type i) const;
113 #include <ext/pb_ds/detail/hash_fn/quadratic_probe_fn_imp.hpp>
115 #undef PB_DS_CLASS_T_DEC
116 #undef PB_DS_CLASS_C_DEC
118 #define PB_DS_CLASS_T_DEC template<typename Size_Type>
119 #define PB_DS_CLASS_C_DEC direct_mask_range_hashing<Size_Type>
121 // A mask range-hashing class (uses a bit-mask).
122 template<typename Size_Type = size_t>
123 class direct_mask_range_hashing
124 : public detail::mask_based_range_hashing<Size_Type>
127 typedef detail::mask_based_range_hashing<Size_Type> mask_based_base;
130 typedef Size_Type size_type;
133 swap(PB_DS_CLASS_C_DEC& other);
137 notify_resized(size_type size);
139 // Transforms the __hash value hash into a ranged-hash value
140 // (using a bit-mask).
142 operator()(size_type hash) const;
145 #include <ext/pb_ds/detail/hash_fn/direct_mask_range_hashing_imp.hpp>
147 #undef PB_DS_CLASS_T_DEC
148 #undef PB_DS_CLASS_C_DEC
150 #define PB_DS_CLASS_T_DEC template<typename Size_Type>
151 #define PB_DS_CLASS_C_DEC direct_mod_range_hashing<Size_Type>
153 // A mod range-hashing class (uses the modulo function).
154 template<typename Size_Type = size_t>
155 class direct_mod_range_hashing
156 : public detail::mod_based_range_hashing<Size_Type>
159 typedef Size_Type size_type;
162 swap(PB_DS_CLASS_C_DEC& other);
166 notify_resized(size_type size);
168 // Transforms the __hash value hash into a ranged-hash value
169 // (using a modulo operation).
171 operator()(size_type hash) const;
174 typedef detail::mod_based_range_hashing<size_type> mod_based_base;
177 #include <ext/pb_ds/detail/hash_fn/direct_mod_range_hashing_imp.hpp>
179 #undef PB_DS_CLASS_T_DEC
180 #undef PB_DS_CLASS_C_DEC
182 #define PB_DS_CLASS_T_DEC template<bool External_Load_Access, typename Size_Type>
183 #define PB_DS_CLASS_C_DEC hash_load_check_resize_trigger<External_Load_Access, Size_Type>
184 #define PB_DS_SIZE_BASE_C_DEC detail::hash_load_check_resize_trigger_size_base<Size_Type, External_Load_Access>
186 // A resize trigger policy based on a load check. It keeps the
187 // load factor between some load factors load_min and load_max.
188 template<bool External_Load_Access = false, typename Size_Type = size_t>
189 class hash_load_check_resize_trigger : private PB_DS_SIZE_BASE_C_DEC
192 typedef Size_Type size_type;
196 external_load_access = External_Load_Access
199 // Default constructor, or constructor taking load_min and
200 // load_max load factors between which this policy will keep the
202 hash_load_check_resize_trigger(float load_min = 0.125,
203 float load_max = 0.5);
206 swap(hash_load_check_resize_trigger& other);
209 ~hash_load_check_resize_trigger();
211 // Returns a pair of the minimal and maximal loads, respectively.
212 inline std::pair<float, float>
215 // Sets the loads through a pair of the minimal and maximal
216 // loads, respectively.
218 set_loads(std::pair<float, float> load_pair);
222 notify_insert_search_start();
225 notify_insert_search_collision();
228 notify_insert_search_end();
231 notify_find_search_start();
234 notify_find_search_collision();
237 notify_find_search_end();
240 notify_erase_search_start();
243 notify_erase_search_collision();
246 notify_erase_search_end();
248 // Notifies an element was inserted. The total number of entries
249 // in the table is num_entries.
251 notify_inserted(size_type num_entries);
254 notify_erased(size_type num_entries);
256 // Notifies the table was cleared.
260 // Notifies the table was resized as a result of this object's
261 // signifying that a resize is needed.
263 notify_resized(size_type new_size);
266 notify_externally_resized(size_type new_size);
269 is_resize_needed() const;
272 is_grow_needed(size_type size, size_type num_entries) const;
276 do_resize(size_type new_size);
278 typedef PB_DS_SIZE_BASE_C_DEC size_base;
280 #ifdef _GLIBCXX_DEBUG
282 assert_valid() const;
287 size_type m_next_shrink_size;
288 size_type m_next_grow_size;
289 bool m_resize_needed;
292 #include <ext/pb_ds/detail/resize_policy/hash_load_check_resize_trigger_imp.hpp>
294 #undef PB_DS_CLASS_T_DEC
295 #undef PB_DS_CLASS_C_DEC
296 #undef PB_DS_SIZE_BASE_C_DEC
298 #define PB_DS_CLASS_T_DEC template<bool External_Load_Access, typename Size_Type>
299 #define PB_DS_CLASS_C_DEC cc_hash_max_collision_check_resize_trigger<External_Load_Access, Size_Type>
301 // A resize trigger policy based on collision checks. It keeps the
302 // simulated load factor lower than some given load factor.
303 template<bool External_Load_Access = false, typename Size_Type = size_t>
304 class cc_hash_max_collision_check_resize_trigger
307 typedef Size_Type size_type;
311 external_load_access = External_Load_Access
314 // Default constructor, or constructor taking load, a __load
315 // factor which it will attempt to maintain.
316 cc_hash_max_collision_check_resize_trigger(float load = 0.5);
319 swap(PB_DS_CLASS_C_DEC& other);
321 // Returns the current load.
325 // Sets the load; does not resize the container.
327 set_load(float load);
331 notify_insert_search_start();
334 notify_insert_search_collision();
337 notify_insert_search_end();
340 notify_find_search_start();
343 notify_find_search_collision();
346 notify_find_search_end();
349 notify_erase_search_start();
352 notify_erase_search_collision();
355 notify_erase_search_end();
358 notify_inserted(size_type num_entries);
361 notify_erased(size_type num_entries);
366 // Notifies the table was resized as a result of this object's
367 // signifying that a resize is needed.
369 notify_resized(size_type new_size);
372 notify_externally_resized(size_type new_size);
375 is_resize_needed() const;
378 is_grow_needed(size_type size, size_type num_entries) const;
385 calc_resize_needed();
391 bool m_resize_needed;
394 #include <ext/pb_ds/detail/resize_policy/cc_hash_max_collision_check_resize_trigger_imp.hpp>
396 #undef PB_DS_CLASS_T_DEC
397 #undef PB_DS_CLASS_C_DEC
399 #define PB_DS_CLASS_T_DEC template<typename Size_Type>
400 #define PB_DS_CLASS_C_DEC hash_exponential_size_policy<Size_Type>
402 // A size policy whose sequence of sizes form an exponential
403 // sequence (typically powers of 2.
404 template<typename Size_Type = size_t>
405 class hash_exponential_size_policy
408 typedef Size_Type size_type;
410 // Default constructor, or onstructor taking a start_size, or
411 // constructor taking a start size and grow_factor. The policy
412 // will use the sequence of sizes start_size, start_size*
413 // grow_factor, start_size* grow_factor^2, ...
414 hash_exponential_size_policy(size_type start_size = 8,
415 size_type grow_factor = 2);
418 swap(PB_DS_CLASS_C_DEC& other);
422 get_nearest_larger_size(size_type size) const;
425 get_nearest_smaller_size(size_type size) const;
428 size_type m_start_size;
429 size_type m_grow_factor;
432 #include <ext/pb_ds/detail/resize_policy/hash_exponential_size_policy_imp.hpp>
434 #undef PB_DS_CLASS_T_DEC
435 #undef PB_DS_CLASS_C_DEC
437 #define PB_DS_CLASS_T_DEC
438 #define PB_DS_CLASS_C_DEC hash_prime_size_policy
440 // A size policy whose sequence of sizes form a nearly-exponential
441 // sequence of primes.
442 class hash_prime_size_policy
446 typedef size_t size_type;
448 // Default constructor, or onstructor taking a start_size The
449 // policy will use the sequence of sizes approximately
450 // start_size, start_size* 2, start_size* 2^2, ...
451 hash_prime_size_policy(size_type start_size = 8);
454 swap(PB_DS_CLASS_C_DEC& other);
458 get_nearest_larger_size(size_type size) const;
461 get_nearest_smaller_size(size_type size) const;
464 size_type m_start_size;
467 #include <ext/pb_ds/detail/resize_policy/hash_prime_size_policy_imp.hpp>
469 #undef PB_DS_CLASS_T_DEC
470 #undef PB_DS_CLASS_C_DEC
472 #define PB_DS_CLASS_T_DEC template<typename Size_Policy, typename Trigger_Policy, bool External_Size_Access, typename Size_Type>
474 #define PB_DS_CLASS_C_DEC hash_standard_resize_policy<Size_Policy, Trigger_Policy, External_Size_Access, Size_Type>
476 // A resize policy which delegates operations to size and trigger policies.
477 template<typename Size_Policy = hash_exponential_size_policy<>,
478 typename Trigger_Policy = hash_load_check_resize_trigger<>,
479 bool External_Size_Access = false,
480 typename Size_Type = size_t>
481 class hash_standard_resize_policy
482 : public Size_Policy, public Trigger_Policy
485 typedef Size_Type size_type;
486 typedef Trigger_Policy trigger_policy;
487 typedef Size_Policy size_policy;
491 external_size_access = External_Size_Access
494 // Default constructor.
495 hash_standard_resize_policy();
497 // constructor taking some policies r_size_policy will be copied
498 // by the Size_Policy object of this object.
499 hash_standard_resize_policy(const Size_Policy& r_size_policy);
501 // constructor taking some policies. r_size_policy will be
502 // copied by the Size_Policy object of this
503 // object. r_trigger_policy will be copied by the Trigger_Policy
504 // object of this object.
505 hash_standard_resize_policy(const Size_Policy& r_size_policy,
506 const Trigger_Policy& r_trigger_policy);
509 ~hash_standard_resize_policy();
512 swap(PB_DS_CLASS_C_DEC& other);
514 // Access to the Size_Policy object used.
518 // Const access to the Size_Policy object used.
520 get_size_policy() const;
522 // Access to the Trigger_Policy object used.
524 get_trigger_policy();
526 // Access to the Trigger_Policy object used.
527 const Trigger_Policy&
528 get_trigger_policy() const;
530 // Returns the actual size of the container.
532 get_actual_size() const;
534 // Resizes the container to suggested_new_size, a suggested size
535 // (the actual size will be determined by the Size_Policy
538 resize(size_type suggested_new_size);
542 notify_insert_search_start();
545 notify_insert_search_collision();
548 notify_insert_search_end();
551 notify_find_search_start();
554 notify_find_search_collision();
557 notify_find_search_end();
560 notify_erase_search_start();
563 notify_erase_search_collision();
566 notify_erase_search_end();
569 notify_inserted(size_type num_e);
572 notify_erased(size_type num_e);
578 notify_resized(size_type new_size);
581 is_resize_needed() const;
583 // Queries what the new size should be, when the container is
584 // resized naturally. The current __size of the container is
585 // size, and the number of used entries within the container is
588 get_new_size(size_type size, size_type num_used_e) const;
591 // Resizes to new_size.
593 do_resize(size_type new_size);
595 typedef Trigger_Policy trigger_policy_base;
597 typedef Size_Policy size_policy_base;
602 #include <ext/pb_ds/detail/resize_policy/hash_standard_resize_policy_imp.hpp>
604 #undef PB_DS_CLASS_T_DEC
605 #undef PB_DS_CLASS_C_DEC