aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
hashFunc.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
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 Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Classes providing basic hash functions for hash tables.
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 #ifndef GUM_HASH_FUNC_H
29 #define GUM_HASH_FUNC_H
30 
31 // utility provides the std::pair <> template
32 #include <climits>
33 #include <string>
34 #include <type_traits>
35 #include <utility>
36 
37 #include <agrum/agrum.h>
38 #include <agrum/tools/core/refPtr.h>
39 
40 namespace gum {
41 
42  /**
43  * @ingroup hashfunctions_group
44  * @brief Returns the size in bits - 1 necessary to store the smallest power
45  * of 2 greater than or equal to nb.
46  *
47  * In aGrUM, the sizes of hash tables (number of slots) are powers of 2. This
48  * is not actually compulsory for the hash function we use. However, as it
49  * speeds up the computations of hashed values, we chose to impose this
50  * restriction.
51  *
52  * @param nb The greatest number for which this function will return a power
53  * of 2.
54  * @return Returns the size in bits - 1 necessary to store the smallest power
55  * of 2 greater than or equal to nb.
56  */
57  unsigned int _hashTableLog2_(const Size nb);
58 
59  /**
60  * @class HashFuncConst
61  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
62  * @ingroup hashfunctions_group
63  * @brief Useful constants for hash functions.
64  *
65  * Hash functions are of the form [M * ((k * A) mod 1)], where [] stands for
66  * the integer part, M is equal to the number of slots in the hashtable, k is
67  * the key to be hashed, and mod 1 retrieves the decimal part of (k * A). A
68  * is an irrational number (currently either the gold number or pi/4). To
69  * speed up computations, the hash function is implemented using only
70  * Size (a.k.a. std::size_t). Therefore pi/4 and the gold number are encoded
71  * as X * 2^{-n} where n is the number of bits in a Size. Consequently, we
72  * should adapt X's definition to 32 and 64 bits architectures.
73  */
74  struct HashFuncConst {
75  static constexpr Size gold
76  = sizeof(Size) == 4 ? Size(2654435769UL) : Size(11400714819323198486UL);
77  static constexpr Size pi
78  = sizeof(Size) == 4 ? Size(3373259426UL) : Size(14488038916154245684UL);
79  static constexpr Size mask
80  = sizeof(Size) == 4 ? Size(4294967295UL) : Size(18446744073709551615UL);
81  static constexpr Size offset = sizeof(Size) == 4 ? Size(32) : Size(64);
82  };
83 
84 
85  // ===========================================================================
86  // === BASE CLASS SHARED BY ALL THE HASH FUNCTIONS ===
87  // ===========================================================================
88  /**
89  * @class HashFuncBase
90  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
91  * @brief All hash functions should inherit from this class.
92  * @ingroup hashfunctions_group
93  *
94  * Whenever you create a new hash function you must inherit from this class.
95  * Otherwise, your hash function will not compile because gum::HashTable will
96  * refer directly to this class.
97  *
98  * The way gum::HashTable work, you do not need to define constructors,
99  * destructors and assignment operators: the defaults created by C++ will
100  * work correctly. However, if your hash function contains new attributes,
101  * you must override the resize function to properly set these attributes.
102  * You may even have to redefine the default constructor and/or the
103  * assignment operator, but this should not occur very often.
104  *
105  * In fact, usually, when you create a new hash function, you will only need
106  * to write something like:
107  *
108  * @code
109  * template <> class HashFunc<MyObject> : public HashFuncBase<MyObject> {
110  * public:
111  * Size operator() (const MyObject& key) const {
112  * // here write the code using
113  * // HashFuncBase<MyObject>::hash_size_ and/or
114  * // HashFuncBase<MyObject>::hash_log2_size_ and/or
115  * // HashFuncBase<MyObject>::hash_mask_
116  * }
117  * };
118  * @endcode
119  *
120  * For instance, here is how HashFunc<string> is implemented:
121  *
122  * @code
123  * template <> class HashFunc<std::string>: public HashFuncBase<std::string> {
124  * public:
125  * Size operator() (const std::string& key) const {
126  * Size h = 0;
127  * for (size_t i = 0, j = key.size(); i < j; ++i) {
128  * h = 19 * h + key[i];
129  * }
130  * return ((h * GUM_HASHTABLE_INT_GOLD) & hash_mask_);
131  * }
132  * };
133  * @endcode
134  *
135  * The gum::HashFunc::hash_size_ attribute corresponds to the number of slots
136  * in the gum::HashTable. Your function should always return a number in
137  * [0,_hash_size). As the number of slots in a gum::HashTable is always a
138  * power of 2, it may be convenient to know the number of bits used by the
139  * hashed keys, this is precisely the information contained in
140  * gum::HashFunc::hash_log2_size_. Finally, gum::HashFunc::hash_mask_ is a
141  * mask that can be used to ensure that the hashed key actually belongs to
142  * [0,_hash_size). This is used in particular in the hash function for
143  * hashing strings.
144  *
145  * @tparam Key The type hashed by this hash function.
146  */
147  template < typename Key >
148  class HashFuncBase {
149  public:
150  /**
151  * @brief Update the hash function to take into account a resize of the
152  * hash table.
153  *
154  * When the user wishes to resize the gum::HashTable so that the array is
155  * of size s, the gum::HashTable resizes itself to the smallest power of 2
156  * greater than or equal to s. This new size is computed by function
157  * gum::HashFuncBase::resize(gum::Size). Hence, s should be the size of the
158  * array of lists, not the number of elements stored into the
159  * gum::HashTable.
160  *
161  * @param new_size The hashtable's size wished by the user. Actually, a
162  * hashtable of size n is an array of n lists.
163  * @throw SizeError Raised if s is too small.
164  */
165  void resize(const Size new_size);
166 
167  /**
168  * @brief Returns the hash table size as known by the hash function.
169  * @return Returns the size of the hash table, i.e., the number of slots
170  * (lists) it contains.
171  */
172  Size size() const;
173 
174  /**
175  * @brief Computes the hashed value of a key.
176  *
177  * The classes inheriting from HashFuncBase should always declare
178  * Operator() as follows:
179  * @code
180  * Size operator()(const Key& key) const override final;
181  * @endcode
182  * and its implementation should be something like:
183  * @code
184  * INLINE Size HashFunc< Key >::operator()(const Key& key) const {
185  * return (castToSize(key) * HashFuncConst::gold) >> this->right_shift_;
186  * }
187  * @endcode
188  * By doing this, compilers optimize the code so that it is significantly
189  * speeded-up because no virtual table will be used and everything is
190  * most certainly inlined. Of course, you need to define a static method
191  * castToSize which should take as argument a const Key& and return a Size
192  *
193  * @param key The key to compute the hashed value.
194  * @return Returns the hashed value of a key.
195  */
196  virtual Size operator()(const Key& key) const = 0;
197 
198  protected:
199  /// The size of the hash table.
201 
202  /// Log of the number of slots of the hash table in base 2.
203  unsigned int hash_log2_size_{0};
204 
205  /**
206  * @brief performing y = x & hash_mask_ guarantees that y is a slot index
207  * of the hash table
208  *
209  * To transform a Size x into a slot index of the hash table, you can either
210  * use x & hash_mask_ or x >> right_shift_ depending on whether you want
211  * to exploit the least significant bits of x (&) or the most significant
212  * one (>>).
213  */
215 
216  /**
217  * @brief performing y = x >> right_shift_ guarantees that y is a slot
218  * index of the hash table
219  *
220  * To transform a Size x into a slot index of the hash table, you can either
221  * use x & hash_mask_ or x >> right_shift_ depending on whether you want
222  * to exploit the least significant bits of x (&) or the most significant
223  * one (>>).
224  */
225  unsigned int right_shift_{0};
226  };
227 
228 
229  // ===========================================================================
230  // === GENERIC HASH FUNCTIONS FOR SIMPLE TYPES ===
231  // ===========================================================================
232 
233  /**
234  * @class HashFuncSmallKey
235  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
236  * @brief Generic hash functions for numeric keys smaller than or equal to Size
237  * @ingroup hashfunctions_group
238  * @tparam Key The type hashed by this hash function.
239  */
240  template < typename Key >
241  class HashFuncSmallKey: public HashFuncBase< Key > {
242  public:
243  /**
244  * @brief Class constructor.
245  */
247 
248  /**
249  * @brief Returns the value of a key as a Size.
250  * @param key The value to return as a Size.
251  * @return Returns the value of a key as a Size.
252  */
253  static Size castToSize(const Key& key);
254 
255  /**
256  * @brief Computes the hashed value of a key.
257  * @param key The key to compute the hashed value.
258  * @return Returns the hashed value of a key.
259  */
260  virtual Size operator()(const Key& key) const override final;
261  };
262 
263 
264  /**
265  * @class HashFuncSmallCastKey
266  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
267  * @brief Generic hash functions for keys castable as Size and
268  * whose size is strictly smaller than that of Size.
269  * @ingroup hashfunctions_group
270  * @tparam Key The type hashed by this hash function.
271  */
272  template < typename Key >
273  class HashFuncSmallCastKey: public HashFuncBase< Key > {
274  public:
275  /**
276  * @brief Class constructor.
277  */
279 
280  /**
281  * @brief Returns the value of a key as a Size.
282  * @param key The value to return as a Size.
283  * @return Returns the value of a key as a Size.
284  */
285  static Size castToSize(const Key& key);
286 
287  /**
288  * @brief Computes the hashed value of a key.
289  * @param key The key to compute the hashed value.
290  * @return Returns the hashed value of a key.
291  */
292  virtual Size operator()(const Key& key) const override final;
293 
294  protected:
295  /**
296  * An additional mask to ensure that keys with fewer bits than Size
297  * are cast correctly.
298  */
299  static constexpr Size small_key_mask_{(Size(1) << (8 * sizeof(Key))) - Size(1)};
300  };
301 
302 
303  /**
304  * @class HashFuncMediumCastKey
305  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
306  * @brief Generic hash functions for keys castable as Size and
307  * whose size is precisely that of Size.
308  * @ingroup hashfunctions_group
309  * @tparam Key The type hashed by this hash function.
310  */
311  template < typename Key >
312  class HashFuncMediumCastKey: public HashFuncBase< Key > {
313  public:
314  /**
315  * @brief Class constructor.
316  */
318 
319  /**
320  * @brief Returns the value of a key as a Size.
321  * @param key The value to return as a Size.
322  * @return Returns the value of a key as a Size.
323  */
324  static Size castToSize(const Key& key);
325 
326  /**
327  * @brief Computes the hashed value of a key.
328  * @param key The key to compute the hashed value.
329  * @return Returns the hashed value of a key.
330  */
331  virtual Size operator()(const Key& key) const override final;
332  };
333 
334 
335  /**
336  * @class HashFuncLargeCastKey
337  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
338  * @brief Generic hash functions for keys castable as Size and
339  * whose size is precisely twice that of Size.
340  * @ingroup hashfunctions_group
341  * @tparam Key The type hashed by this hash function.
342  */
343  template < typename Key >
344  class HashFuncLargeCastKey: public HashFuncBase< Key > {
345  public:
346  /**
347  * @brief Class constructor.
348  */
350 
351  /**
352  * @brief Cast key to the expected type.
353  * @param key The key to cast.
354  * @return Returns the cast key to the expected type.
355  */
356  static Size castToSize(const Key& key);
357 
358  /**
359  * @brief Computes the hashed value of a key.
360  * @param key The key to compute the hashed value.
361  * @return Returns the hashed value of a key.
362  */
363  virtual Size operator()(const Key& key) const override final;
364  };
365 
366 
367  /**
368  * @class HashFuncCastKey
369  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
370  * @brief Generic hash functions for keys castable as Size whose
371  * size is either smaller than Size, or equal to that of one or two Size
372  *
373  * This class uses metaprogramming to select automatically which of classes
374  * HashFuncSmallCastKey, HashFuncMediumCastKey or HashFuncLargeCastKey
375  * you should inherit from.
376  * @ingroup hashfunctions_group
377  * @tparam Key The type hashed by this hash function.
378  */
379  template < typename Key >
381  /// The type used by this class.
382  using type = typename std::conditional<
383  sizeof(Key) <= sizeof(Size) && std::is_integral< Key >::value,
385  typename std::conditional<
386  sizeof(Key) < sizeof(Size),
388  typename std::conditional< sizeof(Key) == sizeof(Size),
390  typename std::conditional< sizeof(Key) == 2 * sizeof(Size),
392  void >::type >::type >::type >::
394  };
395 
396 
397  // ===========================================================================
398  // === CLASSES FOR NOT DEFINING SEVERAL TIMES THE SAME HASH FUNCTIONS ===
399  // ===========================================================================
400 
401  // a dummy hash type
402  template < typename Key >
403  class dummyHash {};
404 
405  // the general type of the recursive type to select the appropriate hash function
406  template < typename... >
407  struct HashFuncConditionalType;
408 
409  // base of the recursive type to select the appropriate hash function
410  template < typename Key >
411  struct HashFuncConditionalType< Key > {
412  using type = Key;
413  };
414 
415  // base of the recursive type to select the appropriate hash function
416  template < typename KEY_TYPE, typename TYPE >
417  struct HashFuncConditionalType< KEY_TYPE, TYPE > {
418  using type = typename std::
419  conditional< std::is_same< KEY_TYPE, TYPE >::value, dummyHash< KEY_TYPE >, KEY_TYPE >::type;
420  };
421 
422  /**
423  * @class HashFuncConditionalType
424  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
425  * @brief This class enables to safely define hash functions for types
426  * that may or may not already has defined hash functions
427  * @ingroup hashfunctions_group
428  *
429  * There are types that are defined differently depending on the architecture
430  * or the compiler you use. This is the case, for instance, of std::size_t
431  * which is defined as an unsigned long by gcc and clang on 64 bits
432  * architectures, but is defined as an unsigned int in 32 bits architectures by
433  * theses compilers, and it is defined neither as an unsigned long nor as
434  * an unsigned int by Visual Studio 15 MVSC on 64 bits architectures. To
435  * enable to define the hash function of std::size_t appropriately in all these
436  * cases, instead of defining directly a HasHunc of <std::size_t>, it is
437  * sufficient to define a HashFunc of
438  * <HashFuncConditionalType<std::size_t,unsigned int,unsigned long>::type>.
439  * The latter will actually define a HasHunc of <std::size_t> if size_t
440  * corresponds neither to an unsigned int nor to an unsigned long, else it
441  * will not define the HasHunc of <std::size_t> (which would redefine an
442  * already defined HashFunc, hence resulting in a compilation failure). */
443  template < typename KEY_TYPE, typename FIRST_TYPE, typename... OTHER_TYPES >
444  struct HashFuncConditionalType< KEY_TYPE, FIRST_TYPE, OTHER_TYPES... > {
445  using type = typename std::conditional<
446  std::is_same< KEY_TYPE, FIRST_TYPE >::value,
447  dummyHash< KEY_TYPE >,
448  typename HashFuncConditionalType< KEY_TYPE, OTHER_TYPES... >::type >::type;
449  };
450 
451 
452  // ===========================================================================
453  // === HASH FUNCTIONS FOR PAIRS OF KEYS ===
454  // ===========================================================================
455 
456  /**
457  * @class HashFunc
458  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
459  * @brief This class should be useless as only its specializations should be
460  * used.
461  *
462  * However it prevents to create hash functions on key types that are not yet
463  * supported.
464  *
465  * @ingroup hashfunctions_group
466  */
467  template < typename key >
468  class HashFunc {};
469 
470  /**
471  * @class HashFunc
472  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
473  * @brief Generic hash functions for pairs of keys.
474  * @ingroup hashfunctions_group
475  * @tparam Key1 The type hashed of the first element in the pair.
476  * @tparam Key2 The type hashed of the second element in the pair.
477  */
478  template < typename Key1, typename Key2 >
479  class HashFunc< std::pair< Key1, Key2 > >: public HashFuncBase< std::pair< Key1, Key2 > > {
480  public:
481  /**
482  * @brief Returns the value of a key as a Size.
483  * @param key The value to return as a Size.
484  * @return Returns the value of a key as a Size.
485  */
486  static Size castToSize(const std::pair< Key1, Key2 >& key);
487 
488  /**
489  * @brief Computes the hashed value of a key.
490  * @param key The key to compute the hashed value.
491  * @return Returns the hashed value of a key.
492  */
493  virtual Size operator()(const std::pair< Key1, Key2 >& key) const override final;
494  };
495 
496 
497  // ===========================================================================
498  // === WIDELY USED HASH FUNCTIONS ===
499  // ===========================================================================
500 
501  /**
502  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
503  * @brief Hash function for booleans.
504  * @ingroup hashfunctions_group
505  */
506  template <>
507  class HashFunc< bool >: public HashFuncSmallKey< bool > {};
508 
509  /**
510  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
511  * @brief Hash function for integers.
512  * @ingroup hashfunctions_group
513  */
514  template <>
515  class HashFunc< int >: public HashFuncSmallKey< int > {};
516 
517  /**
518  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
519  * @brief Hash function for unsigned integers.
520  * @ingroup hashfunctions_group
521  */
522  template <>
523  class HashFunc< unsigned int >: public HashFuncSmallKey< unsigned int > {};
524 
525  /**
526  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
527  * @brief Hash function for long integers.
528  * @ingroup hashfunctions_group
529  */
530  template <>
531  class HashFunc< long >: public HashFuncSmallKey< long > {};
532 
533  /**
534  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
535  * @brief Hash function for unsigned long integers.
536  * @ingroup hashfunctions_group
537  */
538  template <>
539  class HashFunc< unsigned long >: public HashFuncSmallKey< unsigned long > {};
540 
541  /**
542  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
543  * @brief Hash function for std::size_t.
544  * @ingroup hashfunctions_group
545  */
546  template <>
547  class HashFunc<
548  typename HashFuncConditionalType< std::size_t, unsigned long, unsigned int, long, int >::
549  type >: public HashFuncCastKey< std::size_t >::type {};
550 
551  /**
552  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
553  * @brief Hash function for floats.
554  * @ingroup hashfunctions_group
555  */
556  template <>
557  class HashFunc< float >: public HashFuncCastKey< float >::type {};
558 
559  /**
560  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
561  * @brief Hash function for doubles.
562  * @ingroup hashfunctions_group
563  */
564  template <>
565  class HashFunc< double >: public HashFuncCastKey< double >::type {};
566 
567  /**
568  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
569  * @brief Hash function for pointers.
570  * @tparam The type for which the pointer is used to compute a hash.
571  */
572  template < typename Type >
573  class HashFunc< Type* >: public HashFuncCastKey< Type* >::type {};
574 
575  /**
576  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
577  * @brief Hash function for strings.
578  * @ingroup hashfunctions_group
579  */
580  template <>
581  class HashFunc< std::string >: public HashFuncBase< std::string > {
582  public:
583  /**
584  * @brief Returns the value of a key as a Size.
585  * @param key The value to return as a Size.
586  * @return Returns the value of a key as a Size.
587  */
588  static Size castToSize(const std::string& key);
589 
590  /**
591  * @brief Computes the hashed value of a key.
592  * @param key The key to compute the hashed value.
593  * @return Returns the hashed value of a key.
594  */
595  virtual Size operator()(const std::string& key) const override final;
596  };
597 
598 
599  /**
600  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
601  * @brief Hash function for vectors of gum::Idx.
602  * @ingroup hashfunctions_group
603  */
604  template <>
605  class HashFunc< std::vector< Idx > >: public HashFuncBase< std::vector< Idx > > {
606  public:
607  /**
608  * @brief Returns the value of a key as a Size.
609  * @param key The value to return as a Size.
610  * @return Returns the value of a key as a Size.
611  */
612  static Size castToSize(const std::vector< Idx >& key);
613 
614  /**
615  * @brief Computes the hashed value of a key.
616  * @param key The key to compute the hashed value.
617  * @return Returns the hashed value of a key.
618  */
619  virtual Size operator()(const std::vector< Idx >& key) const override final;
620  };
621 
622 
623  /**
624  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
625  * @brief Hash function for gum::Debug.
626  * @ingroup hashfunctions_group
627  */
628  template <>
629  class HashFunc< Debug >: public HashFuncBase< Debug > {
630  public:
631  /**
632  * @brief Returns the value of a key as a Size.
633  * @param key The value to return as a Size.
634  * @return Returns the value of a key as a Size.
635  */
636  static Size castToSize(const Debug& key);
637 
638  /**
639  * @brief Computes the hashed value of a key.
640  * @param key The key to compute the hashed value.
641  * @return Returns the hashed value of a key.
642  */
643  virtual Size operator()(const Debug& key) const override final;
644 
645  template < typename OTHER_KEY >
646  friend class HashFunc;
647  };
648 
649 
650  /**
651  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
652  * @brief Hash function for RefPtr.
653  * @ingroup hashfunctions_group
654  * @tparam Type The type of the RefPtr.
655  */
656  template < typename Type >
657  class HashFunc< RefPtr< Type > >: public HashFuncBase< RefPtr< Type > > {
658  public:
659  /**
660  * @brief Returns the value of a key as a Size.
661  * @param key The value to return as a Size.
662  * @return Returns the value of a key as a Size.
663  */
664  static Size castToSize(const RefPtr< Type >& key);
665 
666  /**
667  * @brief Computes the hashed value of a key.
668  * @param key The key to compute the hashed value.
669  * @return Returns the hashed value of a key.
670  */
671  virtual Size operator()(const RefPtr< Type >& key) const override final;
672  };
673 
674 } /* namespace gum */
675 
676 
677 /// include the inlined functions if necessary
678 #ifndef GUM_NO_INLINE
679 # include <agrum/tools/core/hashFunc_inl.h>
680 #endif /* GUM_NO_INLINE */
681 
682 /// always include the implementation of the templates
683 #include <agrum/tools/core/hashFunc_tpl.h>
684 
685 #endif /* GUM_HASHFUNC_H */
static constexpr Size pi
Definition: hashFunc.h:78
HashFuncSmallCastKey()
Class constructor.
static constexpr Size offset
Definition: hashFunc.h:81
HashFuncMediumCastKey()
Class constructor.
Generic hash functions for keys castable as Size and whose size is precisely twice that of Size...
Definition: hashFunc.h:344
static constexpr Size gold
Definition: hashFunc.h:76
static constexpr Size small_key_mask_
An additional mask to ensure that keys with fewer bits than Size are cast correctly.
Definition: hashFunc.h:299
Size hash_size_
The size of the hash table.
Definition: hashFunc.h:200
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
HashFuncSmallKey()
Class constructor.
static constexpr Size mask
Definition: hashFunc.h:80
Size size() const
Returns the hash table size as known by the hash function.
virtual Size operator()(const Key &key) const override final
Computes the hashed value of a key.
friend class HashFunc
A friend for hashing quickly ref pointers.
Definition: refPtr.h:334
static Size castToSize(const Key &key)
Returns the value of a key as a Size.
Generic hash functions for keys castable as Size whose size is either smaller than Size...
Definition: hashFunc.h:380
unsigned int right_shift_
performing y = x >> right_shift_ guarantees that y is a slot index of the hash table ...
Definition: hashFunc.h:225
Generic hash functions for keys castable as Size and whose size is precisely that of Size...
Definition: hashFunc.h:312
unsigned int _hashTableLog2_(const Size nb)
Returns the size in bits - 1 necessary to store the smallest power of 2 greater than or equal to nb...
Size hash_mask_
performing y = x & hash_mask_ guarantees that y is a slot index of the hash table ...
Definition: hashFunc.h:214
static Size castToSize(const Key &key)
Returns the value of a key as a Size.
static Size castToSize(const Key &key)
Cast key to the expected type.
HashFuncLargeCastKey()
Class constructor.
Useful constants for hash functions.
Definition: hashFunc.h:74
Generic hash functions for numeric keys smaller than or equal to Size.
Definition: hashFunc.h:241
virtual Size operator()(const Key &key) const override final
Computes the hashed value of a key.
virtual Size operator()(const std::pair< Key1, Key2 > &key) const override final
Computes the hashed value of a key.
virtual Size operator()(const Key &key) const override final
Computes the hashed value of a key.
static Size castToSize(const std::pair< Key1, Key2 > &key)
Returns the value of a key as a Size.
All hash functions should inherit from this class.
Definition: hashFunc.h:148
void resize(const Size new_size)
Update the hash function to take into account a resize of the hash table.
unsigned int hash_log2_size_
Log of the number of slots of the hash table in base 2.
Definition: hashFunc.h:203
Generic hash functions for keys castable as Size and whose size is strictly smaller than that of Size...
Definition: hashFunc.h:273
virtual Size operator()(const Key &key) const =0
Computes the hashed value of a key.
virtual Size operator()(const Key &key) const override final
Computes the hashed value of a key.
static Size castToSize(const Key &key)
Returns the value of a key as a Size.