aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
hashFunc.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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)))
300  - Size(1)};
301  };
302 
303 
304  /**
305  * @class HashFuncMediumCastKey
306  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
307  * @brief Generic hash functions for keys castable as Size and
308  * whose size is precisely that of Size.
309  * @ingroup hashfunctions_group
310  * @tparam Key The type hashed by this hash function.
311  */
312  template < typename Key >
313  class HashFuncMediumCastKey: public HashFuncBase< Key > {
314  public:
315  /**
316  * @brief Class constructor.
317  */
319 
320  /**
321  * @brief Returns the value of a key as a Size.
322  * @param key The value to return as a Size.
323  * @return Returns the value of a key as a Size.
324  */
325  static Size castToSize(const Key& key);
326 
327  /**
328  * @brief Computes the hashed value of a key.
329  * @param key The key to compute the hashed value.
330  * @return Returns the hashed value of a key.
331  */
332  virtual Size operator()(const Key& key) const override final;
333  };
334 
335 
336  /**
337  * @class HashFuncLargeCastKey
338  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
339  * @brief Generic hash functions for keys castable as Size and
340  * whose size is precisely twice that of Size.
341  * @ingroup hashfunctions_group
342  * @tparam Key The type hashed by this hash function.
343  */
344  template < typename Key >
345  class HashFuncLargeCastKey: public HashFuncBase< Key > {
346  public:
347  /**
348  * @brief Class constructor.
349  */
351 
352  /**
353  * @brief Cast key to the expected type.
354  * @param key The key to cast.
355  * @return Returns the cast key to the expected type.
356  */
357  static Size castToSize(const Key& key);
358 
359  /**
360  * @brief Computes the hashed value of a key.
361  * @param key The key to compute the hashed value.
362  * @return Returns the hashed value of a key.
363  */
364  virtual Size operator()(const Key& key) const override final;
365  };
366 
367 
368  /**
369  * @class HashFuncCastKey
370  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
371  * @brief Generic hash functions for keys castable as Size whose
372  * size is either smaller than Size, or equal to that of one or two Size
373  *
374  * This class uses metaprogramming to select automatically which of classes
375  * HashFuncSmallCastKey, HashFuncMediumCastKey or HashFuncLargeCastKey
376  * you should inherit from.
377  * @ingroup hashfunctions_group
378  * @tparam Key The type hashed by this hash function.
379  */
380  template < typename Key >
382  /// The type used by this class.
383  using type = typename std::conditional<
384  sizeof(Key) <= sizeof(Size) && std::is_integral< Key >::value,
386  typename std::conditional<
387  sizeof(Key) < sizeof(Size),
389  typename std::conditional<
390  sizeof(Key) == sizeof(Size),
392  typename std::conditional< sizeof(Key) == 2 * sizeof(Size),
394  void >::type >::type >::type >::type;
395  };
396 
397 
398  // ===========================================================================
399  // === CLASSES FOR NOT DEFINING SEVERAL TIMES THE SAME HASH FUNCTIONS ===
400  // ===========================================================================
401 
402  // a dummy hash type
403  template < typename Key >
404  class dummyHash {};
405 
406  // the general type of the recursive type to select the appropriate hash function
407  template < typename... >
408  struct HashFuncConditionalType;
409 
410  // base of the recursive type to select the appropriate hash function
411  template < typename Key >
412  struct HashFuncConditionalType< Key > {
413  using type = Key;
414  };
415 
416  // base of the recursive type to select the appropriate hash function
417  template < typename KEY_TYPE, typename TYPE >
418  struct HashFuncConditionalType< KEY_TYPE, TYPE > {
419  using type = typename std::conditional< std::is_same< KEY_TYPE, TYPE >::value,
420  dummyHash< KEY_TYPE >,
421  KEY_TYPE >::type;
422  };
423 
424  /**
425  * @class HashFuncConditionalType
426  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
427  * @brief This class enables to safely define hash functions for types
428  * that may or may not already has defined hash functions
429  * @ingroup hashfunctions_group
430  *
431  * There are types that are defined differently depending on the architecture
432  * or the compiler you use. This is the case, for instance, of std::size_t
433  * which is defined as an unsigned long by gcc and clang on 64 bits
434  * architectures, but is defined as an unsigned int in 32 bits architectures by
435  * theses compilers, and it is defined neither as an unsigned long nor as
436  * an unsigned int by Visual Studio 15 MVSC on 64 bits architectures. To
437  * enable to define the hash function of std::size_t appropriately in all these
438  * cases, instead of defining directly a HasHunc of <std::size_t>, it is
439  * sufficient to define a HashFunc of
440  * <HashFuncConditionalType<std::size_t,unsigned int,unsigned long>::type>.
441  * The latter will actually define a HasHunc of <std::size_t> if size_t
442  * corresponds neither to an unsigned int nor to an unsigned long, else it
443  * will not define the HasHunc of <std::size_t> (which would redefine an
444  * already defined HashFunc, hence resulting in a compilation failure). */
445  template < typename KEY_TYPE, typename FIRST_TYPE, typename... OTHER_TYPES >
446  struct HashFuncConditionalType< KEY_TYPE, FIRST_TYPE, OTHER_TYPES... > {
447  using type = typename std::conditional<
448  std::is_same< KEY_TYPE, FIRST_TYPE >::value,
449  dummyHash< KEY_TYPE >,
450  typename HashFuncConditionalType< KEY_TYPE, OTHER_TYPES... >::type >::type;
451  };
452 
453 
454  // ===========================================================================
455  // === HASH FUNCTIONS FOR PAIRS OF KEYS ===
456  // ===========================================================================
457 
458  /**
459  * @class HashFunc
460  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
461  * @brief This class should be useless as only its specializations should be
462  * used.
463  *
464  * However it prevents to create hash functions on key types that are not yet
465  * supported.
466  *
467  * @ingroup hashfunctions_group
468  */
469  template < typename key >
470  class HashFunc {};
471 
472  /**
473  * @class HashFunc
474  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
475  * @brief Generic hash functions for pairs of keys.
476  * @ingroup hashfunctions_group
477  * @tparam Key1 The type hashed of the first element in the pair.
478  * @tparam Key2 The type hashed of the second element in the pair.
479  */
480  template < typename Key1, typename Key2 >
481  class HashFunc< std::pair< Key1, Key2 > >:
482  public HashFuncBase< std::pair< Key1, Key2 > > {
483  public:
484  /**
485  * @brief Returns the value of a key as a Size.
486  * @param key The value to return as a Size.
487  * @return Returns the value of a key as a Size.
488  */
489  static Size castToSize(const std::pair< Key1, Key2 >& key);
490 
491  /**
492  * @brief Computes the hashed value of a key.
493  * @param key The key to compute the hashed value.
494  * @return Returns the hashed value of a key.
495  */
496  virtual Size
497  operator()(const std::pair< Key1, Key2 >& key) const override final;
498  };
499 
500 
501  // ===========================================================================
502  // === WIDELY USED HASH FUNCTIONS ===
503  // ===========================================================================
504 
505  /**
506  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
507  * @brief Hash function for booleans.
508  * @ingroup hashfunctions_group
509  */
510  template <>
511  class HashFunc< bool >: public HashFuncSmallKey< bool > {};
512 
513  /**
514  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
515  * @brief Hash function for integers.
516  * @ingroup hashfunctions_group
517  */
518  template <>
519  class HashFunc< int >: public HashFuncSmallKey< int > {};
520 
521  /**
522  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
523  * @brief Hash function for unsigned integers.
524  * @ingroup hashfunctions_group
525  */
526  template <>
527  class HashFunc< unsigned int >: public HashFuncSmallKey< unsigned int > {};
528 
529  /**
530  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
531  * @brief Hash function for long integers.
532  * @ingroup hashfunctions_group
533  */
534  template <>
535  class HashFunc< long >: public HashFuncSmallKey< long > {};
536 
537  /**
538  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
539  * @brief Hash function for unsigned long integers.
540  * @ingroup hashfunctions_group
541  */
542  template <>
543  class HashFunc< unsigned long >: public HashFuncSmallKey< unsigned long > {};
544 
545  /**
546  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
547  * @brief Hash function for std::size_t.
548  * @ingroup hashfunctions_group
549  */
550  template <>
552  unsigned long,
553  unsigned int,
554  long,
555  int >::type >:
556  public HashFuncCastKey< std::size_t >::type {};
557 
558  /**
559  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
560  * @brief Hash function for floats.
561  * @ingroup hashfunctions_group
562  */
563  template <>
564  class HashFunc< float >: public HashFuncCastKey< float >::type {};
565 
566  /**
567  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
568  * @brief Hash function for doubles.
569  * @ingroup hashfunctions_group
570  */
571  template <>
572  class HashFunc< double >: public HashFuncCastKey< double >::type {};
573 
574  /**
575  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
576  * @brief Hash function for pointers.
577  * @tparam The type for which the pointer is used to compute a hash.
578  */
579  template < typename Type >
580  class HashFunc< Type* >: public HashFuncCastKey< Type* >::type {};
581 
582  /**
583  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
584  * @brief Hash function for strings.
585  * @ingroup hashfunctions_group
586  */
587  template <>
588  class HashFunc< std::string >: public HashFuncBase< std::string > {
589  public:
590  /**
591  * @brief Returns the value of a key as a Size.
592  * @param key The value to return as a Size.
593  * @return Returns the value of a key as a Size.
594  */
595  static Size castToSize(const std::string& key);
596 
597  /**
598  * @brief Computes the hashed value of a key.
599  * @param key The key to compute the hashed value.
600  * @return Returns the hashed value of a key.
601  */
602  virtual Size operator()(const std::string& key) const override final;
603  };
604 
605 
606  /**
607  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
608  * @brief Hash function for vectors of gum::Idx.
609  * @ingroup hashfunctions_group
610  */
611  template <>
612  class HashFunc< std::vector< Idx > >: public HashFuncBase< std::vector< Idx > > {
613  public:
614  /**
615  * @brief Returns the value of a key as a Size.
616  * @param key The value to return as a Size.
617  * @return Returns the value of a key as a Size.
618  */
619  static Size castToSize(const std::vector< Idx >& key);
620 
621  /**
622  * @brief Computes the hashed value of a key.
623  * @param key The key to compute the hashed value.
624  * @return Returns the hashed value of a key.
625  */
626  virtual Size operator()(const std::vector< Idx >& key) const override final;
627  };
628 
629 
630  /**
631  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
632  * @brief Hash function for gum::Debug.
633  * @ingroup hashfunctions_group
634  */
635  template <>
636  class HashFunc< Debug >: public HashFuncBase< Debug > {
637  public:
638  /**
639  * @brief Returns the value of a key as a Size.
640  * @param key The value to return as a Size.
641  * @return Returns the value of a key as a Size.
642  */
643  static Size castToSize(const Debug& key);
644 
645  /**
646  * @brief Computes the hashed value of a key.
647  * @param key The key to compute the hashed value.
648  * @return Returns the hashed value of a key.
649  */
650  virtual Size operator()(const Debug& key) const override final;
651 
652  template < typename OTHER_KEY >
653  friend class HashFunc;
654  };
655 
656 
657  /**
658  * @headerfile hashFunc.h <agrum/tools/core/hashFunc.h>
659  * @brief Hash function for RefPtr.
660  * @ingroup hashfunctions_group
661  * @tparam Type The type of the RefPtr.
662  */
663  template < typename Type >
664  class HashFunc< RefPtr< Type > >: public HashFuncBase< RefPtr< Type > > {
665  public:
666  /**
667  * @brief Returns the value of a key as a Size.
668  * @param key The value to return as a Size.
669  * @return Returns the value of a key as a Size.
670  */
671  static Size castToSize(const RefPtr< Type >& key);
672 
673  /**
674  * @brief Computes the hashed value of a key.
675  * @param key The key to compute the hashed value.
676  * @return Returns the hashed value of a key.
677  */
678  virtual Size operator()(const RefPtr< Type >& key) const override final;
679  };
680 
681 } /* namespace gum */
682 
683 
684 /// include the inlined functions if necessary
685 #ifndef GUM_NO_INLINE
686 # include <agrum/tools/core/hashFunc_inl.h>
687 #endif /* GUM_NO_INLINE */
688 
689 /// always include the implementation of the templates
690 #include <agrum/tools/core/hashFunc_tpl.h>
691 
692 #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:345
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:669
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:381
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:313
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.
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...
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.