aGrUM  0.16.0
hashFunc_tpl.h
Go to the documentation of this file.
1 
30 // to help IDE parser
31 #include <agrum/core/hashFunc.h>
32 
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
34 
35 namespace gum {
36 
37  // Update the hash function to take into account a resize of the hash table
38  template < typename Key >
39  INLINE void HashFuncBase< Key >::resize(const Size new_size) {
40  // things work properly only for hashtables with at least 2 elements
41  if (new_size < 2) {
42  GUM_ERROR(SizeError,
43  "the size of the hashtable must be at least 2 but a size of "
44  << new_size << " was provided to the resize function.");
45  }
46 
47  _hash_log2_size = __hashTableLog2(new_size);
48  _hash_size = Size(1) << _hash_log2_size;
49  _hash_mask = _hash_size - 1;
50  _right_shift = HashFuncConst::offset - _hash_log2_size;
51  }
52 
53  // Returns the hash table size as known by the hash function
54  template < typename Key >
55  INLINE Size HashFuncBase< Key >::size() const {
56  return _hash_size;
57  }
58 
59  // ===========================================================================
60 
61  // constructor
62  template < typename Key >
64  static_assert(std::is_integral< Key >::value && sizeof(Key) <= sizeof(Size),
65  "Error: you used HashFuncSmallKey for a key which cannot be "
66  "converted (without narrowing) into a gum::Size");
67  }
68 
69  // Returns the value of a key as a Size
70  template < typename Key >
71  INLINE Size HashFuncSmallKey< Key >::castToSize(const Key& key) {
72  return Size(key);
73  }
74 
75  // Returns the hashed value of a key.
76  template < typename Key >
77  INLINE Size HashFuncSmallKey< Key >::operator()(const Key& key) const {
78  return (castToSize(key) * HashFuncConst::gold) >> this->_right_shift;
79  }
80 
81  // ===========================================================================
82 
83  // constructor
84  template < typename Key >
86  static_assert(sizeof(Key) < sizeof(Size),
87  "Error: you used HashFuncSmallCastKey for a key whose size "
88  "is longer than or equal to that of gum::Size");
89  }
90 
91  // Returns the value of a key as a Size
92  template < typename Key >
93  INLINE Size HashFuncSmallCastKey< Key >::castToSize(const Key& key) {
95  }
96 
97  // Returns the hashed value of a key.
98  template < typename Key >
99  INLINE Size HashFuncSmallCastKey< Key >::operator()(const Key& key) const {
100  return (castToSize(key) * HashFuncConst::gold) >> this->_right_shift;
101  }
102 
103  // ===========================================================================
104 
105  // constructor
106  template < typename Key >
108  static_assert(sizeof(Key) == sizeof(Size),
109  "Error: using HashFuncMediumCastKey for a key whose size "
110  "is different from that of a gum::Size");
111  }
112 
113  // Returns the value of a key as a Size
114  template < typename Key >
115  INLINE Size HashFuncMediumCastKey< Key >::castToSize(const Key& key) {
116  return *((Size*)(&key));
117  }
118 
119  // Returns the hashed value of a key.
120  template < typename Key >
121  INLINE Size HashFuncMediumCastKey< Key >::operator()(const Key& key) const {
122  return (castToSize(key) * HashFuncConst::gold) >> this->_right_shift;
123  }
124 
125  // ===========================================================================
126 
127  // constructor
128  template < typename Key >
130  static_assert(sizeof(Key) == 2 * sizeof(Size),
131  "Error: you used HashFuncLargeCastKey for a key whose size "
132  "is different from twice that of a gum::Size");
133  }
134 
135  // Returns the value of a key as a Size
136  template < typename Key >
137  INLINE Size HashFuncLargeCastKey< Key >::castToSize(const Key& key) {
138  const Size* ptr = reinterpret_cast< const Size* >(&key);
139  return ptr[0] ^ ptr[1];
140  }
141 
142  // Returns the hashed value of a key.
143  template < typename Key >
144  INLINE Size HashFuncLargeCastKey< Key >::operator()(const Key& key) const {
145  return (castToSize(key) * HashFuncConst::gold) >> this->_right_shift;
146  }
147 
148  // ===========================================================================
149 
150  // Returns the value of a key as a Size
151  template < typename Key1, typename Key2 >
152  INLINE Size HashFunc< std::pair< Key1, Key2 > >::castToSize(
153  const std::pair< Key1, Key2 >& key) {
154  return HashFunc< Key1 >::castToSize(key.first) * HashFuncConst::pi
155  + HashFunc< Key2 >::castToSize(key.second);
156  }
157 
158  // Returns the hashed value of a key.
159  template < typename Key1, typename Key2 >
160  INLINE Size HashFunc< std::pair< Key1, Key2 > >::
161  operator()(const std::pair< Key1, Key2 >& key) const {
162  return (castToSize(key) * HashFuncConst::gold) >> this->_right_shift;
163  }
164 
165  // ===========================================================================
166 
167  // Returns the hashed value of a key.
168  template < typename Type >
169  INLINE Size HashFunc< RefPtr< Type > >::castToSize(const RefPtr< Type >& key) {
170  return HashFunc< Type* >::castToSize(key.__refCountPtr());
171  }
172 
173  // Returns the hashed value of a key.
174  template < typename Type >
175  INLINE Size HashFunc< RefPtr< Type > >::
176  operator()(const RefPtr< Type >& key) const {
177  return (castToSize(key) * HashFuncConst::gold) & this->_hash_mask;
178  }
179 
180 } /* namespace gum */
181 
182 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
static constexpr Size pi
Definition: hashFunc.h:78
HashFuncSmallCastKey()
Class constructor.
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 offset
Definition: hashFunc.h:82
HashFuncMediumCastKey()
Class constructor.
static constexpr Size gold
Definition: hashFunc.h:76
HashFuncSmallKey()
Class constructor.
static constexpr Size _small_key_mask
An additional mask to ensure that keys with fewer bits than Size are cast correctly.
Definition: hashFunc.h:300
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
static Size castToSize(const Key &key)
Returns the value of a key as a Size.
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.
virtual Size operator()(const Key &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.
void resize(const Size new_size)
Update the hash function to take into account a resize of the hash table.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
virtual Size operator()(const Key &key) const override final
Computes the hashed value of a key.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
static Size castToSize(const Key &key)
Returns the value of a key as a Size.