aGrUM 2.3.2
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-2025 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-2025 *
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#pragma once
41
42
48#include <string>
49#include <utility>
50
51#include <agrum/agrum.h>
52
53// to ease parsing in IDE
55
56#ifndef DOXYGEN_SHOULD_SKIP_THIS
57
58namespace gum {
59
60 /* in aGrUM, the sizes of hash tables (number of slots) are powers of 2. This
61 * is
62 * not actually compulsory for the hash function we use. However, as it
63 * speeds up the computations of hashed values, we chose to impose
64 * this restriction. Function _hashTableLog2_ thus returns the size in
65 * bits - 1 necessary to store the smallest power of 2 greater than or
66 * equal nb. */
67 INLINE unsigned int _hashTableLog2_(const Size nb) {
68 unsigned int i = 0;
69
70 for (Size nbb = nb; nbb > Size(1); ++i, nbb >>= 1) {};
71
72 return ((Size(1) << i) < nb ? i + Size(1) : i);
73 }
74
75 // ===========================================================================
76
78 INLINE Size HashFunc< std::string >::castToSize(const std::string& key) {
79 Size h = 0;
80 Size size = Size(key.size());
81 const char* char_ptr = key.c_str();
82 const Size* int_ptr = (const Size*)char_ptr;
83
84 for (; size >= sizeof(Size); size -= sizeof(Size), ++int_ptr) {
85 h = h * HashFuncConst::gold + *int_ptr;
86 }
87
88 for (char_ptr = (const char*)int_ptr; size != Size(0); --size, ++char_ptr) {
89 h = Size(19) * h + Size(*char_ptr);
90 }
91
92 return h;
93 }
94
95 // Returns the hashed value of a key.
96 INLINE Size HashFunc< std::string >::operator()(const std::string& key) const {
97 return castToSize(key) & this->hash_mask_;
98 }
99
100 // ===========================================================================
101
103 INLINE Size HashFunc< std::vector< Idx > >::castToSize(const std::vector< Idx >& key) {
104 Size h = Size(0);
105 Size size = Size(key.size());
106 for (Size i = Size(0); i < size; ++i)
107 h += i * Size(key[i]);
108
109 return h;
110 }
111
112 // Returns the hashed value of a key.
113 INLINE Size HashFunc< std::vector< Idx > >::operator()(const std::vector< Idx >& key) const {
114 return (castToSize(key) * HashFuncConst::gold) & this->hash_mask_;
115 }
116
117 // ===========================================================================
118
120 INLINE Size HashFunc< Debug >::castToSize(const Debug& key) {
121 Size h = Size(0);
122
123 for (Size i = Size(0), j = Size(key.size()); i < j; ++i)
124 h = Size(19) * h + Size(key[i]);
125
126 return h;
127 }
128
129 // Returns the hashed value of a key.
130 INLINE Size HashFunc< Debug >::operator()(const Debug& key) const {
131 return (castToSize(key) * HashFuncConst::gold) & this->hash_mask_;
132 }
133
134
135} /* namespace gum */
136
137#endif /* DOXYGEN_SHOULD_SKIP_THIS */
static Size castToSize(const Debug &key)
Returns the value of a key as a Size.
virtual Size operator()(const Debug &key) const override final
Computes the hashed value of a key.
virtual Size operator()(const std::string &key) const override final
Computes the hashed value of a key.
static Size castToSize(const std::string &key)
Returns the value of a key as a Size.
This class should be useless as only its specializations should be used.
Definition hashFunc.h:487
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:98