aGrUM  0.16.0
hashFunc_inl.h
Go to the documentation of this file.
1 
28 #include <string>
29 #include <utility>
30 
31 #include <agrum/agrum.h>
32 
33 // to ease parsing in IDE
34 #include <agrum/core/hashFunc.h>
35 
36 #ifndef DOXYGEN_SHOULD_SKIP_THIS
37 
38 namespace gum {
39 
40  /* in aGrUM, the sizes of hash tables (number of slots) are powers of 2. This
41  * is
42  * not actually compulsory for the hash function we use. However, as it
43  * speeds up the computations of hashed values, we chose to impose
44  * this restriction. Function __hashTableLog2 thus returns the size in
45  * bits - 1 necessary to store the smallest power of 2 greater than or
46  * equal nb. */
47  INLINE unsigned int __hashTableLog2(const Size nb) {
48  unsigned int i = 0;
49 
50  for (Size nbb = nb; nbb > Size(1); ++i, nbb >>= 1) {};
51 
52  return ((Size(1) << i) < nb ? i + Size(1) : i);
53  }
54 
55  // ===========================================================================
56 
58  INLINE Size HashFunc< std::string >::castToSize(const std::string& key) {
59  Size h = 0;
60  Size size = Size(key.size());
61  const char* char_ptr = key.c_str();
62  const Size* int_ptr = (const Size*)char_ptr;
63 
64  for (; size >= sizeof(Size); size -= sizeof(Size), ++int_ptr) {
65  h = h * HashFuncConst::gold + *int_ptr;
66  }
67 
68  for (char_ptr = (const char*)int_ptr; size != Size(0); --size, ++char_ptr) {
69  h = Size(19) * h + Size(*char_ptr);
70  }
71 
72  return h;
73  }
74 
75  // Returns the hashed value of a key.
76  INLINE Size HashFunc< std::string >::operator()(const std::string& key) const {
77  return castToSize(key) & this->_hash_mask;
78  }
79 
80  // ===========================================================================
81 
83  INLINE Size
84  HashFunc< std::vector< Idx > >::castToSize(const std::vector< Idx >& key) {
85  Size h = Size(0);
86  Size size = Size(key.size());
87  for (Size i = Size(0); i < size; ++i)
88  h += i * Size(key[i]);
89 
90  return h;
91  }
92 
93  // Returns the hashed value of a key.
94  INLINE Size HashFunc< std::vector< Idx > >::
95  operator()(const std::vector< Idx >& key) const {
96  return (castToSize(key) * HashFuncConst::gold) & this->_hash_mask;
97  }
98 
99  // ===========================================================================
100 
102  INLINE Size HashFunc< Debug >::castToSize(const Debug& key) {
103  Size h = Size(0);
104 
105  for (Size i = Size(0), j = Size(key.size()); i < j; ++i)
106  h = Size(19) * h + Size(key[i]);
107 
108  return h;
109  }
110 
111  // Returns the hashed value of a key.
112  INLINE Size HashFunc< Debug >::operator()(const Debug& key) const {
113  return (castToSize(key) * HashFuncConst::gold) & this->_hash_mask;
114  }
115 
116 
117 } /* namespace gum */
118 
119 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
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...
static constexpr Size gold
Definition: hashFunc.h:76
static Size castToSize(const std::string &key)
Returns the value of a key as a Size.
virtual Size operator()(const std::string &key) const override final
Computes the hashed value of a key.
virtual Size operator()(const Debug &key) const override final
Computes the hashed value of a key.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
static Size castToSize(const Debug &key)
Returns the value of a key as a Size.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48