aGrUM  0.16.0
hashFunc.h
Go to the documentation of this file.
1 
29 #ifndef GUM_HASH_FUNC_H
30 #define GUM_HASH_FUNC_H
31 
32 // utility provides the std::pair <> template
33 #include <climits>
34 #include <string>
35 #include <type_traits>
36 #include <utility>
37 
38 #include <agrum/agrum.h>
39 #include <agrum/core/refPtr.h>
40 
41 namespace gum {
42 
58  unsigned int __hashTableLog2(const Size nb);
59 
75  struct HashFuncConst {
76  static constexpr Size gold =
77  sizeof(Size) == 4 ? Size(2654435769UL) : Size(11400714819323198486UL);
78  static constexpr Size pi =
79  sizeof(Size) == 4 ? Size(3373259426UL) : Size(14488038916154245684UL);
80  static constexpr Size mask =
81  sizeof(Size) == 4 ? Size(4294967295UL) : Size(18446744073709551615UL);
82  static constexpr Size offset = sizeof(Size) == 4 ? Size(32) : Size(64);
83  };
84 
85 
86  // ===========================================================================
87  // === BASE CLASS SHARED BY ALL THE HASH FUNCTIONS ===
88  // ===========================================================================
148  template < typename Key >
149  class HashFuncBase {
150  public:
166  void resize(const Size new_size);
167 
173  Size size() const;
174 
197  virtual Size operator()(const Key& key) const = 0;
198 
199  protected:
201  Size _hash_size{Size(0)};
202 
204  unsigned int _hash_log2_size{0};
205 
215  Size _hash_mask{Size(0)};
216 
226  unsigned int _right_shift{0};
227  };
228 
229 
230  // ===========================================================================
231  // === GENERIC HASH FUNCTIONS FOR SIMPLE TYPES ===
232  // ===========================================================================
233 
241  template < typename Key >
242  class HashFuncSmallKey : public HashFuncBase< Key > {
243  public:
248 
254  static Size castToSize(const Key& key);
255 
261  virtual Size operator()(const Key& key) const override final;
262  };
263 
264 
273  template < typename Key >
274  class HashFuncSmallCastKey : public HashFuncBase< Key > {
275  public:
280 
286  static Size castToSize(const Key& key);
287 
293  virtual Size operator()(const Key& key) const override final;
294 
295  protected:
300  static constexpr Size _small_key_mask{(Size(1) << (8 * sizeof(Key)))
301  - Size(1)};
302  };
303 
304 
313  template < typename Key >
314  class HashFuncMediumCastKey : public HashFuncBase< Key > {
315  public:
320 
326  static Size castToSize(const Key& key);
327 
333  virtual Size operator()(const Key& key) const override final;
334  };
335 
336 
345  template < typename Key >
346  class HashFuncLargeCastKey : public HashFuncBase< Key > {
347  public:
352 
358  static Size castToSize(const Key& key);
359 
365  virtual Size operator()(const Key& key) const override final;
366  };
367 
368 
381  template < typename Key >
384  using type = typename std::conditional<
385  sizeof(Key) <= sizeof(Size) && std::is_integral< Key >::value,
387  typename std::conditional<
388  sizeof(Key) < sizeof(Size),
390  typename std::conditional<
391  sizeof(Key) == sizeof(Size),
393  typename std::conditional< sizeof(Key) == 2 * sizeof(Size),
396  };
397 
398 
399  // ===========================================================================
400  // === CLASSES FOR NOT DEFINING SEVERAL TIMES THE SAME HASH FUNCTIONS ===
401  // ===========================================================================
402 
403  // a dummy hash type
404  template < typename Key >
405  class dummyHash {};
406 
407  // the general type of the recursive type to select the appropriate hash function
408  template < typename... >
410 
411  // base of the recursive type to select the appropriate hash function
412  template < typename Key >
413  struct HashFuncConditionalType< Key > {
414  using type = Key;
415  };
416 
417  // base of the recursive type to select the appropriate hash function
418  template < typename KEY_TYPE, typename TYPE >
419  struct HashFuncConditionalType< KEY_TYPE, TYPE > {
420  using type = typename std::conditional< std::is_same< KEY_TYPE, TYPE >::value,
422  KEY_TYPE >::type;
423  };
424 
446  template < typename KEY_TYPE, typename FIRST_TYPE, typename... OTHER_TYPES >
447  struct HashFuncConditionalType< KEY_TYPE, FIRST_TYPE, OTHER_TYPES... > {
448  using type = typename std::conditional<
449  std::is_same< KEY_TYPE, FIRST_TYPE >::value,
451  typename HashFuncConditionalType< KEY_TYPE, OTHER_TYPES... >::type >::type;
452  };
453 
454 
455  // ===========================================================================
456  // === HASH FUNCTIONS FOR PAIRS OF KEYS ===
457  // ===========================================================================
458 
470  template < typename key >
471  class HashFunc {};
472 
481  template < typename Key1, typename Key2 >
482  class HashFunc< std::pair< Key1, Key2 > >
483  : public HashFuncBase< std::pair< Key1, Key2 > > {
484  public:
490  static Size castToSize(const std::pair< Key1, Key2 >& key);
491 
497  virtual Size
498  operator()(const std::pair< Key1, Key2 >& key) const override final;
499  };
500 
501 
502  // ===========================================================================
503  // === WIDELY USED HASH FUNCTIONS ===
504  // ===========================================================================
505 
511  template <>
512  class HashFunc< bool > : public HashFuncSmallKey< bool > {};
513 
519  template <>
520  class HashFunc< int > : public HashFuncSmallKey< int > {};
521 
527  template <>
528  class HashFunc< unsigned int > : public HashFuncSmallKey< unsigned int > {};
529 
535  template <>
536  class HashFunc< long > : public HashFuncSmallKey< long > {};
537 
543  template <>
544  class HashFunc< unsigned long > : public HashFuncSmallKey< unsigned long > {};
545 
551  template <>
552  class HashFunc< typename HashFuncConditionalType< std::size_t,
553  unsigned long,
554  unsigned int,
555  long,
556  int >::type >
557  : public HashFuncCastKey< std::size_t >::type {};
558 
564  template <>
565  class HashFunc< float > : public HashFuncCastKey< float >::type {};
566 
572  template <>
573  class HashFunc< double > : public HashFuncCastKey< double >::type {};
574 
580  template < typename Type >
581  class HashFunc< Type* > : public HashFuncCastKey< Type* >::type {};
582 
588  template <>
589  class HashFunc< std::string > : public HashFuncBase< std::string > {
590  public:
596  static Size castToSize(const std::string& key);
597 
603  virtual Size operator()(const std::string& key) const override final;
604  };
605 
606 
612  template <>
613  class HashFunc< std::vector< Idx > >
614  : public HashFuncBase< std::vector< Idx > > {
615  public:
621  static Size castToSize(const std::vector< Idx >& key);
622 
628  virtual Size operator()(const std::vector< Idx >& key) const override final;
629  };
630 
631 
637  template <>
638  class HashFunc< Debug > : public HashFuncBase< Debug > {
639  public:
645  static Size castToSize(const Debug& key);
646 
652  virtual Size operator()(const Debug& key) const override final;
653 
654  template < typename OTHER_KEY >
655  friend class HashFunc;
656  };
657 
658 
665  template < typename Type >
666  class HashFunc< RefPtr< Type > > : public HashFuncBase< RefPtr< Type > > {
667  public:
673  static Size castToSize(const RefPtr< Type >& key);
674 
680  virtual Size operator()(const RefPtr< Type >& key) const override final;
681  };
682 
683 } /* namespace gum */
684 
685 
687 #ifndef GUM_NO_INLINE
688 # include <agrum/core/hashFunc_inl.h>
689 #endif /* GUM_NO_INLINE */
690 
692 #include <agrum/core/hashFunc_tpl.h>
693 
694 #endif /* GUM_HASHFUNC_H */
static constexpr Size pi
Definition: hashFunc.h:78
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...
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
static constexpr Size offset
Definition: hashFunc.h:82
Generic hash functions for keys castable as Size and whose size is precisely twice that of Size...
Definition: hashFunc.h:346
static constexpr Size gold
Definition: hashFunc.h:76
typename std::conditional< std::is_same< KEY_TYPE, FIRST_TYPE >::value, dummyHash< KEY_TYPE >, typename HashFuncConditionalType< KEY_TYPE, OTHER_TYPES... >::type >::type type
Definition: hashFunc.h:451
STL namespace.
static constexpr Size mask
Definition: hashFunc.h:80
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Class template representing hashing function of LpCol.
Definition: hashFunc.h:471
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Generic hash functions for keys castable as Size whose size is either smaller than Size...
Definition: hashFunc.h:382
Generic hash functions for keys castable as Size and whose size is precisely that of Size...
Definition: hashFunc.h:314
Smart pointersaGrUM&#39;s smart pointers keep track of the number of times the value they point to is ref...
Definition: refPtr.h:117
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Useful constants for hash functions.
Definition: hashFunc.h:75
Generic hash functions for numeric keys smaller than or equal to Size.
Definition: hashFunc.h:242
typename std::conditional< std::is_same< KEY_TYPE, TYPE >::value, dummyHash< KEY_TYPE >, KEY_TYPE >::type type
Definition: hashFunc.h:422
All hash functions should inherit from this class.
Definition: hashFunc.h:149
typename std::conditional< sizeof(Key)<=sizeof(Size) &&std::is_integral< Key >::value, HashFuncSmallKey< Key >, typename std::conditional< sizeof(Key)< sizeof(Size), HashFuncSmallCastKey< Key >, typename std::conditional< sizeof(Key)==sizeof(Size), HashFuncMediumCastKey< Key >, typename std::conditional< sizeof(Key)==2 *sizeof(Size), HashFuncLargeCastKey< Key >, void >::type >::type >::type >::type type
The type used by this class.
Definition: hashFunc.h:395
This class enables to safely define hash functions for types that may or may not already has defined ...
Definition: hashFunc.h:409
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
Generic hash functions for keys castable as Size and whose size is strictly smaller than that of Size...
Definition: hashFunc.h:274