aGrUM  0.14.2
hashFunc_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
28 // to help IDE parser
29 #include <agrum/core/hashFunc.h>
30 
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 
33 namespace gum {
34 
35  // Update the hash function to take into account a resize of the hash table
36  template < typename Key >
37  INLINE void HashFuncBase< Key >::resize(const Size new_size) {
38  // things work properly only for hashtables with at least 2 elements
39  if (new_size < 2) {
40  GUM_ERROR(SizeError,
41  "the size of the hashtable must be at least 2 but a size of "
42  << new_size << " was provided to the resize function.");
43  }
44 
45  _hash_log2_size = __hashTableLog2(new_size);
46  _hash_size = Size(1) << _hash_log2_size;
47  _hash_mask = _hash_size - 1;
48  _right_shift = HashFuncConst::offset - _hash_log2_size;
49  }
50 
51  // Returns the hash table size as known by the hash function
52  template < typename Key >
53  INLINE Size HashFuncBase< Key >::size() const {
54  return _hash_size;
55  }
56 
57  // ===========================================================================
58 
59  // constructor
60  template < typename Key >
62  static_assert(std::is_integral< Key >::value && sizeof(Key) <= sizeof(Size),
63  "Error: you used HashFuncSmallKey for a key which cannot be "
64  "converted (without narrowing) into a gum::Size");
65  }
66 
67  // Returns the value of a key as a Size
68  template < typename Key >
69  INLINE Size HashFuncSmallKey< Key >::castToSize(const Key& key) {
70  return Size(key);
71  }
72 
73  // Returns the hashed value of a key.
74  template < typename Key >
75  INLINE Size HashFuncSmallKey< Key >::operator()(const Key& key) const {
76  return (castToSize(key) * HashFuncConst::gold) >> this->_right_shift;
77  }
78 
79  // ===========================================================================
80 
81  // constructor
82  template < typename Key >
84  static_assert(sizeof(Key) < sizeof(Size),
85  "Error: you used HashFuncSmallCastKey for a key whose size "
86  "is longer than or equal to that of gum::Size");
87  }
88 
89  // Returns the value of a key as a Size
90  template < typename Key >
91  INLINE Size HashFuncSmallCastKey< Key >::castToSize(const Key& key) {
93  }
94 
95  // Returns the hashed value of a key.
96  template < typename Key >
97  INLINE Size HashFuncSmallCastKey< Key >::operator()(const Key& key) const {
98  return (castToSize(key) * HashFuncConst::gold) >> this->_right_shift;
99  }
100 
101  // ===========================================================================
102 
103  // constructor
104  template < typename Key >
106  static_assert(sizeof(Key) == sizeof(Size),
107  "Error: using HashFuncMediumCastKey for a key whose size "
108  "is different from that of a gum::Size");
109  }
110 
111  // Returns the value of a key as a Size
112  template < typename Key >
113  INLINE Size HashFuncMediumCastKey< Key >::castToSize(const Key& key) {
114  return *((Size*)(&key));
115  }
116 
117  // Returns the hashed value of a key.
118  template < typename Key >
119  INLINE Size HashFuncMediumCastKey< Key >::operator()(const Key& key) const {
120  return (castToSize(key) * HashFuncConst::gold) >> this->_right_shift;
121  }
122 
123  // ===========================================================================
124 
125  // constructor
126  template < typename Key >
128  static_assert(sizeof(Key) == 2 * sizeof(Size),
129  "Error: you used HashFuncLargeCastKey for a key whose size "
130  "is different from twice that of a gum::Size");
131  }
132 
133  // Returns the value of a key as a Size
134  template < typename Key >
135  INLINE Size HashFuncLargeCastKey< Key >::castToSize(const Key& key) {
136  const Size* ptr = reinterpret_cast< const Size* >(&key);
137  return ptr[0] ^ ptr[1];
138  }
139 
140  // Returns the hashed value of a key.
141  template < typename Key >
142  INLINE Size HashFuncLargeCastKey< Key >::operator()(const Key& key) const {
143  return (castToSize(key) * HashFuncConst::gold) >> this->_right_shift;
144  }
145 
146  // ===========================================================================
147 
148  // Returns the value of a key as a Size
149  template < typename Key1, typename Key2 >
150  INLINE Size HashFunc< std::pair< Key1, Key2 > >::castToSize(
151  const std::pair< Key1, Key2 >& key) {
152  return HashFunc< Key1 >::castToSize(key.first) * HashFuncConst::pi
153  + HashFunc< Key2 >::castToSize(key.second);
154  }
155 
156  // Returns the hashed value of a key.
157  template < typename Key1, typename Key2 >
158  INLINE Size HashFunc< std::pair< Key1, Key2 > >::
159  operator()(const std::pair< Key1, Key2 >& key) const {
160  return (castToSize(key) * HashFuncConst::gold) >> this->_right_shift;
161  }
162 
163  // ===========================================================================
164 
165  // Returns the hashed value of a key.
166  template < typename Type >
167  INLINE Size HashFunc< RefPtr< Type > >::castToSize(const RefPtr< Type >& key) {
168  return HashFunc< Type* >::castToSize(key.__refCountPtr());
169  }
170 
171  // Returns the hashed value of a key.
172  template < typename Type >
173  INLINE Size HashFunc< RefPtr< Type > >::
174  operator()(const RefPtr< Type >& key) const {
175  return (castToSize(key) * HashFuncConst::gold) & this->_hash_mask;
176  }
177 
178 } /* namespace gum */
179 
180 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
static constexpr Size pi
Definition: hashFunc.h:76
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:80
HashFuncMediumCastKey()
Class constructor.
static constexpr Size gold
Definition: hashFunc.h:74
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:298
Classes providing basic hash functions for hash tables.
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.
gum is the global namespace for all aGrUM entities
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:45
virtual Size operator()(const Key &key) const override final
Computes the hashed value of a key.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
static Size castToSize(const Key &key)
Returns the value of a key as a Size.