aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
hashFunc_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41#pragma once
42
43
50
51// to help IDE parser
52#include <cstring>
53
55
56#ifndef DOXYGEN_SHOULD_SKIP_THIS
57
58namespace gum {
59
60 // Update the hash function to take into account a resize of the hash table
61 template < typename Key >
62 void HashFuncBase< Key >::resize(const Size new_size) {
63 // things work properly only for hashtables with at least 2 elements
64 if (new_size < 2) {
65 GUM_ERROR(SizeError,
66 "the size of the hashtable must be at least 2 but a size of "
67 << new_size << " was provided to the resize function.");
68 }
69
70 hash_log2_size_ = _hashTableLog2_(new_size);
71 hash_size_ = Size(1) << hash_log2_size_;
72 hash_mask_ = hash_size_ - 1;
73 right_shift_ = HashFuncConst::offset - hash_log2_size_;
74 }
75
76 // Returns the hash table size as known by the hash function
77 template < typename Key >
79 return hash_size_;
80 }
81
82 // ===========================================================================
83
84 // constructor
85 template < typename Key >
87 static_assert(std::is_integral_v< Key > && sizeof(Key) <= sizeof(Size),
88 "Error: you used HashFuncSmallKey for a key which cannot be "
89 "converted (without narrowing) into a gum::Size");
90 }
91
92 // Returns the value of a key as a Size
93 template < typename Key >
95 return Size(key);
96 }
97
98 // Returns the hashed value of a key.
99 template < typename Key >
100 Size HashFuncSmallKey< Key >::operator()(const Key& key) const {
101 return (castToSize(key) * HashFuncConst::gold) >> this->right_shift_;
102 }
103
104 // ===========================================================================
105
106 // constructor
107 template < typename Key >
109 static_assert(sizeof(Key) < sizeof(Size),
110 "Error: you used HashFuncSmallCastKey for a key whose size "
111 "is longer than or equal to that of gum::Size");
112 }
113
114 // Returns the value of a key as a Size
115 template < typename Key >
117 // the code for MVSC differs from the code of the other compilers for
118 // speed-up reasons: according to godbolt.org, the first code
119 // should be twice faster than the second one
120# ifdef _MSC_VER
122# else
123 Size result = 0;
124 memcpy(&result, &key, sizeof(Key));
125 return result;
126# endif /* _MSC_VER */
127 }
128
129 // Returns the hashed value of a key.
130 template < typename Key >
131 Size HashFuncSmallCastKey< Key >::operator()(const Key& key) const {
132 return (castToSize(key) * HashFuncConst::gold) >> this->right_shift_;
133 }
134
135 // ===========================================================================
136
137 // constructor
138 template < typename Key >
140 static_assert(sizeof(Key) == sizeof(Size),
141 "Error: using HashFuncMediumCastKey for a key whose size "
142 "is different from that of a gum::Size");
143 }
144
145 // Returns the value of a key as a Size
146 template < typename Key >
148 return *((Size*)(&key));
149 }
150
151 // Returns the hashed value of a key.
152 template < typename Key >
153 Size HashFuncMediumCastKey< Key >::operator()(const Key& key) const {
154 return (castToSize(key) * HashFuncConst::gold) >> this->right_shift_;
155 }
156
157 // ===========================================================================
158
159 // constructor
160 template < typename Key >
162 static_assert(sizeof(Key) == 2 * sizeof(Size),
163 "Error: you used HashFuncLargeCastKey for a key whose size "
164 "is different from twice that of a gum::Size");
165 }
166
167 // Returns the value of a key as a Size
168 template < typename Key >
170 const Size* ptr = reinterpret_cast< const Size* >(&key);
171 return ptr[0] ^ ptr[1];
172 }
173
174 // Returns the hashed value of a key.
175 template < typename Key >
176 Size HashFuncLargeCastKey< Key >::operator()(const Key& key) const {
177 return (castToSize(key) * HashFuncConst::gold) >> this->right_shift_;
178 }
179
180 // ===========================================================================
181
182 // Returns the value of a key as a Size
183 template < typename Key1, typename Key2 >
184 Size HashFunc< std::pair< Key1, Key2 > >::castToSize(const std::pair< Key1, Key2 >& key) {
186 + HashFunc< Key2 >::castToSize(key.second);
187 }
188
189 // Returns the hashed value of a key.
190 template < typename Key1, typename Key2 >
191 Size HashFunc< std::pair< Key1, Key2 > >::operator()(const std::pair< Key1, Key2 >& key) const {
192 return (castToSize(key) * HashFuncConst::gold) >> this->right_shift_;
193 }
194
195 // ===========================================================================
196
197 // Returns the hashed value of a key.
198 template < typename Type >
199 Size HashFunc< std::shared_ptr< Type > >::castToSize(const std::shared_ptr< Type >& key) {
200 return HashFunc< Type* >::castToSize(key.get());
201 }
202
203 // Returns the hashed value of a key.
204 template < typename Type >
205 Size HashFunc< std::shared_ptr< Type > >::operator()(const std::shared_ptr< Type >& key) const {
206 return (castToSize(key) * HashFuncConst::gold) & this->hash_mask_;
207 }
208
209} /* namespace gum */
210
211#endif /* DOXYGEN_SHOULD_SKIP_THIS */
Size size() const
Returns the hash table size as known by the hash function.
void resize(const Size new_size)
Update the hash function to take into account a resize of the hash table.
virtual Size operator()(const Key &key) const final
Computes the hashed value of a key.
HashFuncLargeCastKey()
Class constructor.
static Size castToSize(const Key &key)
Cast key to the expected type.
static Size castToSize(const Key &key)
Returns the value of a key as a Size.
Size operator()(const Key &key) const final
Computes the hashed value of a key.
HashFuncMediumCastKey()
Class constructor.
HashFuncSmallCastKey()
Class constructor.
static Size castToSize(const Key &key)
Returns the value of a key as a Size.
Size operator()(const Key &key) const final
Computes the hashed value of a key.
static constexpr Size small_key_mask_
An additional mask to ensure that keys with fewer bits than Size are cast correctly.
Definition hashFunc.h:328
static Size castToSize(const Key &key)
Returns the value of a key as a Size.
Size operator()(const Key &key) const final
Computes the hashed value of a key.
HashFuncSmallKey()
Class constructor.
This class should be useless as only its specializations should be used.
Definition hashFunc.h:492
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
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.
Classes providing basic hash functions for hash tables.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
static constexpr Size pi
Definition hashFunc.h:103
static constexpr Size offset
Definition hashFunc.h:108
static constexpr Size gold
Definition hashFunc.h:101