aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
hashFunc_inl.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
/** @file
23
* @brief Inlined implementation of the basic hash functions
24
*
25
* @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26
*/
27
#
include
<
string
>
28
#
include
<
utility
>
29
30
#
include
<
agrum
/
agrum
.
h
>
31
32
// to ease parsing in IDE
33
#
include
<
agrum
/
tools
/
core
/
hashFunc
.
h
>
34
35
#
ifndef
DOXYGEN_SHOULD_SKIP_THIS
36
37
namespace
gum
{
38
39
/* in aGrUM, the sizes of hash tables (number of slots) are powers of 2. This
40
* is
41
* not actually compulsory for the hash function we use. However, as it
42
* speeds up the computations of hashed values, we chose to impose
43
* this restriction. Function _hashTableLog2_ thus returns the size in
44
* bits - 1 necessary to store the smallest power of 2 greater than or
45
* equal nb. */
46
INLINE
unsigned
int
_hashTableLog2_
(
const
Size
nb
) {
47
unsigned
int
i
= 0;
48
49
for
(
Size
nbb
=
nb
;
nbb
>
Size
(1); ++
i
,
nbb
>>= 1) {};
50
51
return
((
Size
(1) <<
i
) <
nb
?
i
+
Size
(1) :
i
);
52
}
53
54
// ===========================================================================
55
56
/// returns an unnormalized hashed key for hash tables whose keys are strings
57
INLINE Size
HashFunc
<
std
::
string
>::
castToSize
(
const
std
::
string
&
key
) {
58
Size
h
= 0;
59
Size
size
=
Size
(
key
.
size
());
60
const
char
*
char_ptr
=
key
.
c_str
();
61
const
Size
*
int_ptr
= (
const
Size
*)
char_ptr
;
62
63
for
(;
size
>=
sizeof
(
Size
);
size
-=
sizeof
(
Size
), ++
int_ptr
) {
64
h
=
h
*
HashFuncConst
::
gold
+ *
int_ptr
;
65
}
66
67
for
(
char_ptr
= (
const
char
*)
int_ptr
;
size
!=
Size
(0); --
size
, ++
char_ptr
) {
68
h
=
Size
(19) *
h
+
Size
(*
char_ptr
);
69
}
70
71
return
h
;
72
}
73
74
// Returns the hashed value of a key.
75
INLINE
Size
HashFunc
<
std
::
string
>::
operator
()(
const
std
::
string
&
key
)
const
{
76
return
castToSize
(
key
) &
this
->
hash_mask_
;
77
}
78
79
// ===========================================================================
80
81
/// returns a hashed key for hash tables whose keys are vectors of Idx
82
INLINE
Size
HashFunc
<
std
::
vector
<
Idx
> >::
castToSize
(
const
std
::
vector
<
Idx
>&
key
) {
83
Size
h
=
Size
(0);
84
Size
size
=
Size
(
key
.
size
());
85
for
(
Size
i
=
Size
(0);
i
<
size
; ++
i
)
86
h
+=
i
*
Size
(
key
[
i
]);
87
88
return
h
;
89
}
90
91
// Returns the hashed value of a key.
92
INLINE
Size
HashFunc
<
std
::
vector
<
Idx
> >::
operator
()(
const
std
::
vector
<
Idx
>&
key
)
const
{
93
return
(
castToSize
(
key
) *
HashFuncConst
::
gold
) &
this
->
hash_mask_
;
94
}
95
96
// ===========================================================================
97
98
/// returns a hashed key for hash tables whose keys are Debugs
99
INLINE
Size
HashFunc
<
Debug
>::
castToSize
(
const
Debug
&
key
) {
100
Size
h
=
Size
(0);
101
102
for
(
Size
i
=
Size
(0),
j
=
Size
(
key
.
size
());
i
<
j
; ++
i
)
103
h
=
Size
(19) *
h
+
Size
(
key
[
i
]);
104
105
return
h
;
106
}
107
108
// Returns the hashed value of a key.
109
INLINE
Size
HashFunc
<
Debug
>::
operator
()(
const
Debug
&
key
)
const
{
110
return
(
castToSize
(
key
) *
HashFuncConst
::
gold
) &
this
->
hash_mask_
;
111
}
112
113
114
}
/* namespace gum */
115
116
#
endif
/* DOXYGEN_SHOULD_SKIP_THIS */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643