aGrUM
0.21.0
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
#
include
<
cstring
>
33
34
#
ifndef
DOXYGEN_SHOULD_SKIP_THIS
35
36
namespace
gum
{
37
38
// Update the hash function to take into account a resize of the hash table
39
template
<
typename
Key
>
40
INLINE
void
HashFuncBase
<
Key
>::
resize
(
const
Size
new_size
) {
41
// things work properly only for hashtables with at least 2 elements
42
if
(
new_size
< 2) {
43
GUM_ERROR
(
SizeError
,
44
"the size of the hashtable must be at least 2 but a size of "
45
<<
new_size
<<
" was provided to the resize function."
);
46
}
47
48
hash_log2_size_
=
_hashTableLog2_
(
new_size
);
49
hash_size_
=
Size
(1) <<
hash_log2_size_
;
50
hash_mask_
=
hash_size_
- 1;
51
right_shift_
=
HashFuncConst
::
offset
-
hash_log2_size_
;
52
}
53
54
// Returns the hash table size as known by the hash function
55
template
<
typename
Key
>
56
INLINE
Size
HashFuncBase
<
Key
>::
size
()
const
{
57
return
hash_size_
;
58
}
59
60
// ===========================================================================
61
62
// constructor
63
template
<
typename
Key
>
64
INLINE
HashFuncSmallKey
<
Key
>::
HashFuncSmallKey
() {
65
static_assert
(
std
::
is_integral
<
Key
>::
value
&&
sizeof
(
Key
) <=
sizeof
(
Size
),
66
"Error: you used HashFuncSmallKey for a key which cannot be "
67
"converted (without narrowing) into a gum::Size"
);
68
}
69
70
// Returns the value of a key as a Size
71
template
<
typename
Key
>
72
INLINE
Size
HashFuncSmallKey
<
Key
>::
castToSize
(
const
Key
&
key
) {
73
return
Size
(
key
);
74
}
75
76
// Returns the hashed value of a key.
77
template
<
typename
Key
>
78
INLINE
Size
HashFuncSmallKey
<
Key
>::
operator
()(
const
Key
&
key
)
const
{
79
return
(
castToSize
(
key
) *
HashFuncConst
::
gold
) >>
this
->
right_shift_
;
80
}
81
82
// ===========================================================================
83
84
// constructor
85
template
<
typename
Key
>
86
INLINE
HashFuncSmallCastKey
<
Key
>::
HashFuncSmallCastKey
() {
87
static_assert
(
sizeof
(
Key
) <
sizeof
(
Size
),
88
"Error: you used HashFuncSmallCastKey for a key whose size "
89
"is longer than or equal to that of gum::Size"
);
90
}
91
92
// Returns the value of a key as a Size
93
template
<
typename
Key
>
94
INLINE
Size
HashFuncSmallCastKey
<
Key
>::
castToSize
(
const
Key
&
key
) {
95
// the code for MVSC differs from the code of the other compilers for
96
// speed-up reasons: according to godbolt.org, the first code
97
// should be twice faster than the second one
98
#
ifdef
_MSC_VER
99
return
*((
Size
*)(&
key
)) &
HashFuncSmallCastKey
<
Key
>::
small_key_mask_
;
100
#
else
101
Size
result
= 0;
102
memcpy
(&
result
, &
key
,
sizeof
(
Key
));
103
return
result
;
104
#
endif
/* _MSC_VER */
105
}
106
107
// Returns the hashed value of a key.
108
template
<
typename
Key
>
109
INLINE
Size
HashFuncSmallCastKey
<
Key
>::
operator
()(
const
Key
&
key
)
const
{
110
return
(
castToSize
(
key
) *
HashFuncConst
::
gold
) >>
this
->
right_shift_
;
111
}
112
113
// ===========================================================================
114
115
// constructor
116
template
<
typename
Key
>
117
INLINE
HashFuncMediumCastKey
<
Key
>::
HashFuncMediumCastKey
() {
118
static_assert
(
sizeof
(
Key
) ==
sizeof
(
Size
),
119
"Error: using HashFuncMediumCastKey for a key whose size "
120
"is different from that of a gum::Size"
);
121
}
122
123
// Returns the value of a key as a Size
124
template
<
typename
Key
>
125
INLINE
Size
HashFuncMediumCastKey
<
Key
>::
castToSize
(
const
Key
&
key
) {
126
return
*((
Size
*)(&
key
));
127
}
128
129
// Returns the hashed value of a key.
130
template
<
typename
Key
>
131
INLINE
Size
HashFuncMediumCastKey
<
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
>
139
INLINE
HashFuncLargeCastKey
<
Key
>::
HashFuncLargeCastKey
() {
140
static_assert
(
sizeof
(
Key
) == 2 *
sizeof
(
Size
),
141
"Error: you used HashFuncLargeCastKey for a key whose size "
142
"is different from twice that of a gum::Size"
);
143
}
144
145
// Returns the value of a key as a Size
146
template
<
typename
Key
>
147
INLINE
Size
HashFuncLargeCastKey
<
Key
>::
castToSize
(
const
Key
&
key
) {
148
const
Size
*
ptr
=
reinterpret_cast
<
const
Size
* >(&
key
);
149
return
ptr
[0] ^
ptr
[1];
150
}
151
152
// Returns the hashed value of a key.
153
template
<
typename
Key
>
154
INLINE
Size
HashFuncLargeCastKey
<
Key
>::
operator
()(
const
Key
&
key
)
const
{
155
return
(
castToSize
(
key
) *
HashFuncConst
::
gold
) >>
this
->
right_shift_
;
156
}
157
158
// ===========================================================================
159
160
// Returns the value of a key as a Size
161
template
<
typename
Key1
,
typename
Key2
>
162
INLINE
Size
HashFunc
<
std
::
pair
<
Key1
,
Key2
> >::
castToSize
(
const
std
::
pair
<
Key1
,
Key2
>&
key
) {
163
return
HashFunc
<
Key1
>::
castToSize
(
key
.
first
) *
HashFuncConst
::
pi
164
+
HashFunc
<
Key2
>::
castToSize
(
key
.
second
);
165
}
166
167
// Returns the hashed value of a key.
168
template
<
typename
Key1
,
typename
Key2
>
169
INLINE
Size
170
HashFunc
<
std
::
pair
<
Key1
,
Key2
> >::
operator
()(
const
std
::
pair
<
Key1
,
Key2
>&
key
)
const
{
171
return
(
castToSize
(
key
) *
HashFuncConst
::
gold
) >>
this
->
right_shift_
;
172
}
173
174
// ===========================================================================
175
176
// Returns the hashed value of a key.
177
template
<
typename
Type
>
178
INLINE
Size
HashFunc
<
RefPtr
<
Type
> >::
castToSize
(
const
RefPtr
<
Type
>&
key
) {
179
return
HashFunc
<
Type
* >::
castToSize
(
key
.
_refCountPtr_
());
180
}
181
182
// Returns the hashed value of a key.
183
template
<
typename
Type
>
184
INLINE
Size
HashFunc
<
RefPtr
<
Type
> >::
operator
()(
const
RefPtr
<
Type
>&
key
)
const
{
185
return
(
castToSize
(
key
) *
HashFuncConst
::
gold
) &
this
->
hash_mask_
;
186
}
187
188
}
/* namespace gum */
189
190
#
endif
/* DOXYGEN_SHOULD_SKIP_THIS */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643