aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
hashFunc_inl.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
49#include <string>
50#include <utility>
51
52#include <agrum/agrum.h>
53
54#include <string_view>
55
56// to ease parsing in IDE
58
59#ifndef DOXYGEN_SHOULD_SKIP_THIS
60
61namespace gum {
62
63 /* in aGrUM, the sizes of hash tables (number of slots) are powers of 2. This
64 * is not actually compulsory for the hash function we use. However, as it
65 * speeds up the computations of hashed values, we chose to impose
66 * this restriction. Function _hashTableLog2_ thus returns the size in
67 * bits - 1 necessary to store the smallest power of 2 greater than or
68 * equal to nb. */
69 INLINE unsigned int _hashTableLog2_(const Size nb) {
70 unsigned int i = 0;
71
72 for (Size nbb = nb; nbb > Size(1); ++i, nbb >>= 1) {}
73
74 // check that the smallest power of 2 greater than or equal to nb does not
75 // require more bits than what Size can provide
76 const Size power_two = Size(1) << i;
77 if ((i == sizeof(Size) * 8 - 1) && (power_two < nb)) {
78 GUM_ERROR(OutOfBounds, "HashTable size " << nb << " is too large");
79 }
80
81 return (power_two < nb ? i + Size(1) : i);
82 }
83
84 // ===========================================================================
85
87 INLINE Size HashFunc< std::string >::castToSize(const std::string& key) {
88 Size h = 0;
89 Size size = Size(key.size());
90 const char* char_ptr = key.c_str();
91 const Size* int_ptr = (const Size*)char_ptr;
92
93 for (; size >= sizeof(Size); size -= sizeof(Size), ++int_ptr) {
94 h = h * HashFuncConst::gold + *int_ptr;
95 }
96
97 for (char_ptr = (const char*)int_ptr; size != Size(0); --size, ++char_ptr) {
98 h = Size(19) * h + Size(*char_ptr);
99 }
100
101 return h;
102 }
103
104 // Returns the hashed value of a key.
105 INLINE Size HashFunc< std::string >::operator()(const std::string& key) const {
106 return castToSize(key) & this->hash_mask_;
107 }
108
110 INLINE Size HashFunc< std::string >::castToSize(std::string_view key) {
111 Size h = 0;
112 Size size = Size(key.size());
113 const char* char_ptr = key.data();
114 const Size* int_ptr = (const Size*)char_ptr;
115
116 for (; size >= sizeof(Size); size -= sizeof(Size), ++int_ptr) {
117 h = h * HashFuncConst::gold + *int_ptr;
118 }
119
120 for (char_ptr = (const char*)int_ptr; size != Size(0); --size, ++char_ptr) {
121 h = Size(19) * h + Size(*char_ptr);
122 }
123
124 return h;
125 }
126
127 // Non-virtual overload for heterogeneous lookup with string_view.
128 INLINE Size HashFunc< std::string >::operator()(std::string_view key) const {
129 return castToSize(key) & this->hash_mask_;
130 }
131
132 // ===========================================================================
133
135 INLINE Size HashFunc< std::vector< Idx > >::castToSize(const std::vector< Idx >& key) {
136 Size h = Size(0);
137 Size size = Size(key.size());
138 for (Size i = Size(0); i < size; ++i)
139 h += i * Size(key[i]);
140
141 return h;
142 }
143
144 // Returns the hashed value of a key.
145 INLINE Size HashFunc< std::vector< Idx > >::operator()(const std::vector< Idx >& key) const {
146 return (castToSize(key) * HashFuncConst::gold) & this->hash_mask_;
147 }
148
149 // ===========================================================================
150
152 INLINE Size HashFunc< Debug >::castToSize(const Debug& key) {
153 Size h = Size(0);
154
155 for (Size i = Size(0), j = Size(key.size()); i < j; ++i)
156 h = Size(19) * h + Size(key[i]);
157
158 return h;
159 }
160
161 // Returns the hashed value of a key.
162 INLINE Size HashFunc< Debug >::operator()(const Debug& key) const {
163 return (castToSize(key) * HashFuncConst::gold) & this->hash_mask_;
164 }
165
166
167} /* namespace gum */
168
169#endif /* DOXYGEN_SHOULD_SKIP_THIS */
Size operator()(const Debug &key) const final
Computes the hashed value of a key.
static Size castToSize(const Debug &key)
Returns the value of a key as a Size.
static Size castToSize(const std::string &key)
Returns the value of a key as a Size.
Size operator()(const std::string &key) const final
Computes the hashed value of a key.
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 gold
Definition hashFunc.h:101