aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
hashFunc_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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 Template implementation of the basic hash functions.
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 // to help IDE parser
30 #include <agrum/tools/core/hashFunc.h>
31 
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33 
34 namespace gum {
35 
36  // Update the hash function to take into account a resize of the hash table
37  template < typename Key >
38  INLINE void HashFuncBase< Key >::resize(const Size new_size) {
39  // things work properly only for hashtables with at least 2 elements
40  if (new_size < 2) {
42  "the size of the hashtable must be at least 2 but a size of "
43  << new_size << " was provided to the resize function.");
44  }
45 
48  hash_mask_ = hash_size_ - 1;
50  }
51 
52  // Returns the hash table size as known by the hash function
53  template < typename Key >
54  INLINE Size HashFuncBase< Key >::size() const {
55  return hash_size_;
56  }
57 
58  // ===========================================================================
59 
60  // constructor
61  template < typename Key >
63  static_assert(std::is_integral< Key >::value && sizeof(Key) <= sizeof(Size),
64  "Error: you used HashFuncSmallKey for a key which cannot be "
65  "converted (without narrowing) into a gum::Size");
66  }
67 
68  // Returns the value of a key as a Size
69  template < typename Key >
71  return Size(key);
72  }
73 
74  // Returns the hashed value of a key.
75  template < typename Key >
76  INLINE Size HashFuncSmallKey< Key >::operator()(const Key& key) const {
77  return (castToSize(key) * HashFuncConst::gold) >> this->right_shift_;
78  }
79 
80  // ===========================================================================
81 
82  // constructor
83  template < typename Key >
85  static_assert(sizeof(Key) < sizeof(Size),
86  "Error: you used HashFuncSmallCastKey for a key whose size "
87  "is longer than or equal to that of gum::Size");
88  }
89 
90  // Returns the value of a key as a Size
91  template < typename Key >
93  return *((Size*)(&key)) & HashFuncSmallCastKey< Key >::small_key_mask_;
94  }
95 
96  // Returns the hashed value of a key.
97  template < typename Key >
98  INLINE Size HashFuncSmallCastKey< Key >::operator()(const Key& key) const {
99  return (castToSize(key) * HashFuncConst::gold) >> this->right_shift_;
100  }
101 
102  // ===========================================================================
103 
104  // constructor
105  template < typename Key >
107  static_assert(sizeof(Key) == sizeof(Size),
108  "Error: using HashFuncMediumCastKey for a key whose size "
109  "is different from that of a gum::Size");
110  }
111 
112  // Returns the value of a key as a Size
113  template < typename Key >
115  return *((Size*)(&key));
116  }
117 
118  // Returns the hashed value of a key.
119  template < typename Key >
120  INLINE Size HashFuncMediumCastKey< Key >::operator()(const Key& key) const {
121  return (castToSize(key) * HashFuncConst::gold) >> this->right_shift_;
122  }
123 
124  // ===========================================================================
125 
126  // constructor
127  template < typename Key >
129  static_assert(sizeof(Key) == 2 * sizeof(Size),
130  "Error: you used HashFuncLargeCastKey for a key whose size "
131  "is different from twice that of a gum::Size");
132  }
133 
134  // Returns the value of a key as a Size
135  template < typename Key >
137  const Size* ptr = reinterpret_cast< const Size* >(&key);
138  return ptr[0] ^ ptr[1];
139  }
140 
141  // Returns the hashed value of a key.
142  template < typename Key >
143  INLINE Size HashFuncLargeCastKey< Key >::operator()(const Key& key) const {
144  return (castToSize(key) * HashFuncConst::gold) >> this->right_shift_;
145  }
146 
147  // ===========================================================================
148 
149  // Returns the value of a key as a Size
150  template < typename Key1, typename Key2 >
151  INLINE Size HashFunc< std::pair< Key1, Key2 > >::castToSize(const std::pair< Key1, Key2 >& key) {
154  }
155 
156  // Returns the hashed value of a key.
157  template < typename Key1, typename Key2 >
158  INLINE Size
159  HashFunc< std::pair< Key1, Key2 > >::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 > >::operator()(const RefPtr< Type >& key) const {
174  return (castToSize(key) * HashFuncConst::gold) & this->hash_mask_;
175  }
176 
177 } /* namespace gum */
178 
179 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643