STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hash_policy.hpp
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 // Copyright (C) 2005-2013 Free Software Foundation, Inc.
4 //
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 3, or (at your option) any later
9 // version.
10 
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.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
26 
27 // Permission to use, copy, modify, sell, and distribute this software
28 // is hereby granted without fee, provided that the above copyright
29 // notice appears in all copies, and that both that copyright notice
30 // and this permission notice appear in supporting documentation. None
31 // of the above authors, nor IBM Haifa Research Laboratories, make any
32 // representation about the suitability of this software for any
33 // purpose. It is provided "as is" without express or implied
34 // warranty.
35 
41 #ifndef PB_DS_HASH_POLICY_HPP
42 #define PB_DS_HASH_POLICY_HPP
43 
44 #include <bits/c++config.h>
45 #include <algorithm>
46 #include <vector>
47 #include <cmath>
48 #include <ext/pb_ds/exception.hpp>
53 
54 namespace __gnu_pbds
55 {
56 #define PB_DS_CLASS_T_DEC template<typename Size_Type>
57 #define PB_DS_CLASS_C_DEC linear_probe_fn<Size_Type>
58 
60  template<typename Size_Type = std::size_t>
62  {
63  public:
64  typedef Size_Type size_type;
65 
66  void
67  swap(PB_DS_CLASS_C_DEC& other);
68 
69  protected:
71  inline size_type
72  operator()(size_type i) const;
73  };
74 
76 
77 #undef PB_DS_CLASS_T_DEC
78 #undef PB_DS_CLASS_C_DEC
79 
80 #define PB_DS_CLASS_T_DEC template<typename Size_Type>
81 #define PB_DS_CLASS_C_DEC quadratic_probe_fn<Size_Type>
82 
84  template<typename Size_Type = std::size_t>
86  {
87  public:
88  typedef Size_Type size_type;
89 
90  void
91  swap(PB_DS_CLASS_C_DEC& other);
92 
93  protected:
95  inline size_type
96  operator()(size_type i) const;
97  };
98 
100 
101 #undef PB_DS_CLASS_T_DEC
102 #undef PB_DS_CLASS_C_DEC
103 
104 #define PB_DS_CLASS_T_DEC template<typename Size_Type>
105 #define PB_DS_CLASS_C_DEC direct_mask_range_hashing<Size_Type>
106 
108  template<typename Size_Type = std::size_t>
110  : public detail::mask_based_range_hashing<Size_Type>
111  {
112  private:
114 
115  public:
116  typedef Size_Type size_type;
117 
118  void
119  swap(PB_DS_CLASS_C_DEC& other);
120 
121  protected:
122  void
124 
127  inline size_type
128  operator()(size_type hash) const;
129  };
130 
132 
133 #undef PB_DS_CLASS_T_DEC
134 #undef PB_DS_CLASS_C_DEC
135 
136 #define PB_DS_CLASS_T_DEC template<typename Size_Type>
137 #define PB_DS_CLASS_C_DEC direct_mod_range_hashing<Size_Type>
138 
140  template<typename Size_Type = std::size_t>
142  : public detail::mod_based_range_hashing<Size_Type>
143  {
144  public:
145  typedef Size_Type size_type;
146 
147  void
148  swap(PB_DS_CLASS_C_DEC& other);
149 
150  protected:
151  void
153 
156  inline size_type
157  operator()(size_type hash) const;
158 
159  private:
161  };
162 
164 
165 #undef PB_DS_CLASS_T_DEC
166 #undef PB_DS_CLASS_C_DEC
167 
168 #define PB_DS_CLASS_T_DEC template<bool External_Load_Access, typename Size_Type>
169 #define PB_DS_CLASS_C_DEC hash_load_check_resize_trigger<External_Load_Access, Size_Type>
170 #define PB_DS_SIZE_BASE_C_DEC detail::hash_load_check_resize_trigger_size_base<Size_Type, External_Load_Access>
171 
174  template<bool External_Load_Access = false, typename Size_Type = std::size_t>
176  {
177  public:
178  typedef Size_Type size_type;
179 
180  enum
181  {
185  external_load_access = External_Load_Access
186  };
187 
191  hash_load_check_resize_trigger(float load_min = 0.125,
192  float load_max = 0.5);
193 
194  void
196 
197  virtual
199 
201  inline std::pair<float, float>
202  get_loads() const;
203 
206  void
207  set_loads(std::pair<float, float> load_pair);
208 
209  protected:
210  inline void
212 
213  inline void
215 
216  inline void
218 
219  inline void
221 
222  inline void
224 
225  inline void
227 
228  inline void
230 
231  inline void
233 
234  inline void
236 
239  inline void
240  notify_inserted(size_type num_entries);
241 
242  inline void
243  notify_erased(size_type num_entries);
244 
246  void
247  notify_cleared();
248 
251  void
252  notify_resized(size_type new_size);
253 
254  void
256 
257  inline bool
258  is_resize_needed() const;
259 
260  inline bool
261  is_grow_needed(size_type size, size_type num_entries) const;
262 
263  private:
264  virtual void
265  do_resize(size_type new_size);
266 
268 
269 #ifdef _GLIBCXX_DEBUG
270  void
271  assert_valid(const char* file, int line) const;
272 #endif
273 
274  float m_load_min;
275  float m_load_max;
279  };
280 
282 
283 #undef PB_DS_CLASS_T_DEC
284 #undef PB_DS_CLASS_C_DEC
285 #undef PB_DS_SIZE_BASE_C_DEC
286 
287 #define PB_DS_CLASS_T_DEC template<bool External_Load_Access, typename Size_Type>
288 #define PB_DS_CLASS_C_DEC cc_hash_max_collision_check_resize_trigger<External_Load_Access, Size_Type>
289 
292  template<bool External_Load_Access = false, typename Size_Type = std::size_t>
294  {
295  public:
296  typedef Size_Type size_type;
297 
298  enum
299  {
303  external_load_access = External_Load_Access
304  };
305 
309 
310  void
311  swap(PB_DS_CLASS_C_DEC& other);
312 
314  inline float
315  get_load() const;
316 
318  void
319  set_load(float load);
320 
321  protected:
323  inline void
325 
327  inline void
329 
331  inline void
333 
335  inline void
337 
339  inline void
341 
343  inline void
345 
347  inline void
349 
351  inline void
353 
355  inline void
357 
359  inline void
360  notify_inserted(size_type num_entries);
361 
363  inline void
364  notify_erased(size_type num_entries);
365 
367  void
368  notify_cleared();
369 
372  void
373  notify_resized(size_type new_size);
374 
376  void
378 
380  inline bool
381  is_resize_needed() const;
382 
385  inline bool
386  is_grow_needed(size_type size, size_type num_entries) const;
387 
388  private:
389  void
391 
392  inline void
394 
395  float m_load;
400  };
401 
403 
404 #undef PB_DS_CLASS_T_DEC
405 #undef PB_DS_CLASS_C_DEC
406 
407 #define PB_DS_CLASS_T_DEC template<typename Size_Type>
408 #define PB_DS_CLASS_C_DEC hash_exponential_size_policy<Size_Type>
409 
412  template<typename Size_Type = std::size_t>
414  {
415  public:
416  typedef Size_Type size_type;
417 
423  size_type grow_factor = 2);
424 
425  void
426  swap(PB_DS_CLASS_C_DEC& other);
427 
428  protected:
429  size_type
431 
432  size_type
434 
435  private:
438  };
439 
441 
442 #undef PB_DS_CLASS_T_DEC
443 #undef PB_DS_CLASS_C_DEC
444 
445 #define PB_DS_CLASS_T_DEC
446 #define PB_DS_CLASS_C_DEC hash_prime_size_policy
447 
451  {
452  public:
455 
459  hash_prime_size_policy(size_type start_size = 8);
460 
461  inline void
462  swap(PB_DS_CLASS_C_DEC& other);
463 
464  protected:
465  size_type
467 
468  size_type
470 
471  private:
473  };
474 
476 
477 #undef PB_DS_CLASS_T_DEC
478 #undef PB_DS_CLASS_C_DEC
479 
480 #define PB_DS_CLASS_T_DEC template<typename Size_Policy, typename Trigger_Policy, bool External_Size_Access, typename Size_Type>
481 
482 #define PB_DS_CLASS_C_DEC hash_standard_resize_policy<Size_Policy, Trigger_Policy, External_Size_Access, Size_Type>
483 
485  template<typename Size_Policy = hash_exponential_size_policy<>,
486  typename Trigger_Policy = hash_load_check_resize_trigger<>,
487  bool External_Size_Access = false,
488  typename Size_Type = std::size_t>
490  : public Size_Policy, public Trigger_Policy
491  {
492  public:
493  typedef Size_Type size_type;
494  typedef Trigger_Policy trigger_policy;
495  typedef Size_Policy size_policy;
496 
497  enum
498  {
499  external_size_access = External_Size_Access
500  };
501 
504 
507  hash_standard_resize_policy(const Size_Policy& r_size_policy);
508 
513  hash_standard_resize_policy(const Size_Policy& r_size_policy,
514  const Trigger_Policy& r_trigger_policy);
515 
516  virtual
518 
519  inline void
520  swap(PB_DS_CLASS_C_DEC& other);
521 
523  Size_Policy&
524  get_size_policy();
525 
527  const Size_Policy&
528  get_size_policy() const;
529 
531  Trigger_Policy&
533 
535  const Trigger_Policy&
536  get_trigger_policy() const;
537 
539  inline size_type
540  get_actual_size() const;
541 
545  void
546  resize(size_type suggested_new_size);
547 
548  protected:
549  inline void
551 
552  inline void
554 
555  inline void
557 
558  inline void
560 
561  inline void
563 
564  inline void
566 
567  inline void
569 
570  inline void
572 
573  inline void
575 
576  inline void
577  notify_inserted(size_type num_e);
578 
579  inline void
580  notify_erased(size_type num_e);
581 
582  void
583  notify_cleared();
584 
585  void
586  notify_resized(size_type new_size);
587 
588  inline bool
589  is_resize_needed() const;
590 
595  size_type
596  get_new_size(size_type size, size_type num_used_e) const;
597 
598  private:
600  virtual void
601  do_resize(size_type new_size);
602 
603  typedef Trigger_Policy trigger_policy_base;
604 
605  typedef Size_Policy size_policy_base;
606 
608  };
609 
611 
612 #undef PB_DS_CLASS_T_DEC
613 #undef PB_DS_CLASS_C_DEC
614 
615 } // namespace __gnu_pbds
616 
617 #endif
void notify_erase_search_start()
Notifies an erase search started.
void swap(PB_DS_CLASS_C_DEC &other)
Size_Type size_type
Definition: hash_policy.hpp:296
void notify_insert_search_end()
Notifies a search ended.
void swap(PB_DS_CLASS_C_DEC &other)
Definition: hash_policy.hpp:413
void notify_insert_search_start()
Notifies an insert search started.
virtual void do_resize(size_type new_size)
Resizes to new_size.
void notify_erase_search_collision()
Notifies a search encountered a collision.
void swap(PB_DS_CLASS_C_DEC &other)
void notify_externally_resized(size_type new_size)
Notifies the table was resized externally.
size_type m_size
Definition: hash_policy.hpp:607
void notify_resized(size_type new_size)
Size_Type size_type
Definition: mask_based_range_hashing.hpp:53
size_type m_size
Definition: hash_policy.hpp:396
detail::mod_based_range_hashing< size_type > mod_based_base
Definition: hash_policy.hpp:160
Definition: hash_policy.hpp:175
bool is_grow_needed(size_type size, size_type num_entries) const
Trigger_Policy & get_trigger_policy()
Access to the Trigger_Policy object used.
void notify_cleared()
Notifies the table was cleared.
A resize policy which delegates operations to size and trigger policies.
Definition: hash_policy.hpp:489
void resize(size_type suggested_new_size)
Size_Type size_type
Definition: hash_policy.hpp:88
hash_standard_resize_policy()
Default constructor.
size_type m_next_grow_size
Definition: hash_policy.hpp:277
void notify_find_search_start()
Notifies a find search started.
void set_loads(std::pair< float, float > load_pair)
void notify_erased(size_type num_entries)
Notifies an element was erased.
void notify_find_search_collision()
Notifies a search encountered a collision.
Size_Policy & get_size_policy()
Access to the Size_Policy object used.
float m_load_min
Definition: hash_policy.hpp:274
A mask range-hashing class (uses a bitmask).
Definition: hash_policy.hpp:109
void swap(PB_DS_CLASS_C_DEC &other)
size_type get_nearest_smaller_size(size_type size) const
void notify_inserted(size_type num_entries)
void notify_externally_resized(size_type new_size)
void notify_resized(size_type new_size)
float get_load() const
Returns the current load.
detail::mask_based_range_hashing< Size_Type > mask_based_base
Definition: hash_policy.hpp:113
void notify_find_search_end()
Notifies a search ended.
Size_Type size_type
Definition: mod_based_range_hashing.hpp:53
void set_load(float load)
Sets the load; does not resize the container.
size_type m_num_col
Definition: hash_policy.hpp:397
void swap(PB_DS_CLASS_C_DEC &other)
virtual void do_resize(size_type new_size)
void swap(PB_DS_CLASS_C_DEC &other)
bool is_grow_needed(size_type size, size_type num_entries) const
Size_Policy size_policy_base
Definition: hash_policy.hpp:605
size_type m_next_shrink_size
Definition: hash_policy.hpp:276
Size_Type size_type
Definition: hash_policy.hpp:178
Trigger_Policy trigger_policy_base
Definition: hash_policy.hpp:603
Size_Type size_type
Definition: hash_policy.hpp:116
size_type m_start_size
Definition: hash_policy.hpp:436
Mod based range hashing.
Definition: mod_based_range_hashing.hpp:50
void notify_cleared()
Notifies the table was cleared.
Size_Type size_type
Definition: hash_policy.hpp:416
hash_load_check_resize_trigger(float load_min=0.125, float load_max=0.5)
hash_prime_size_policy(size_type start_size=8)
Size_Type size_type
Definition: hash_policy.hpp:493
bool m_resize_needed
Definition: hash_policy.hpp:399
__SIZE_TYPE__ size_t
Definition: stddef.h:212
Range hashing policy.
Definition: mask_based_range_hashing.hpp:50
void swap(hash_load_check_resize_trigger &other)
void notify_insert_search_collision()
Notifies a search encountered a collision.
float m_load_max
Definition: hash_policy.hpp:275
Size_Policy size_policy
Definition: hash_policy.hpp:495
A probe sequence policy using square increments.
Definition: hash_policy.hpp:85
size_type get_nearest_larger_size(size_type size) const
Definition: hash_policy.hpp:450
Size_Type size_type
Definition: hash_policy.hpp:64
void swap(PB_DS_CLASS_C_DEC &other)
std::pair< float, float > get_loads() const
Returns a pair of the minimal and maximal loads, respectively.
void notify_resized(size_type size)
size_type get_new_size(size_type size, size_type num_used_e) const
A mod range-hashing class (uses the modulo function).
Definition: hash_policy.hpp:141
size_type operator()(size_type hash) const
hash_exponential_size_policy(size_type start_size=8, size_type grow_factor=2)
size_type operator()(size_type i) const
Returns the i-th offset from the hash value.
Size_Type size_type
Definition: hash_policy.hpp:145
size_type m_start_size
Definition: hash_policy.hpp:472
float m_load
Definition: hash_policy.hpp:395
#define PB_DS_SIZE_BASE_C_DEC
Definition: hash_policy.hpp:170
size_type operator()(size_type i) const
Returns the i-th offset from the hash value.
void notify_inserted(size_type num_entries)
Notifies an element was inserted.
size_type m_grow_factor
Definition: hash_policy.hpp:437
Trigger_Policy trigger_policy
Definition: hash_policy.hpp:494
size_type m_max_col
Definition: hash_policy.hpp:398
#define PB_DS_CLASS_C_DEC
Definition: hash_policy.hpp:482
bool m_resize_needed
Definition: hash_policy.hpp:278
PB_DS_SIZE_BASE_C_DEC size_base
Definition: hash_policy.hpp:267
size_type operator()(size_type hash) const
A probe sequence policy using fixed increments.
Definition: hash_policy.hpp:61
size_type get_nearest_smaller_size(size_type size) const
void notify_erase_search_end()
Notifies a search ended.
size_type get_nearest_larger_size(size_type size) const
bool is_resize_needed() const
Queries whether a resize is needed.
std::size_t size_type
Size type.
Definition: hash_policy.hpp:454
void notify_erased(size_type num_entries)
size_type get_actual_size() const
Returns the actual size of the container.