aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
hashFunc_inl.h
Go to the documentation of this file.
1
/**
2
*
3
* Copyright 2005-2020 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
83
HashFunc
<
std
::
vector
<
Idx
> >::
castToSize
(
const
std
::
vector
<
Idx
>&
key
) {
84
Size
h
=
Size
(0);
85
Size
size
=
Size
(
key
.
size
());
86
for
(
Size
i
=
Size
(0);
i
<
size
; ++
i
)
87
h
+=
i
*
Size
(
key
[
i
]);
88
89
return
h
;
90
}
91
92
// Returns the hashed value of a key.
93
INLINE
Size
HashFunc
<
std
::
vector
<
Idx
> >::
operator
()(
94
const
std
::
vector
<
Idx
>&
key
)
const
{
95
return
(
castToSize
(
key
) *
HashFuncConst
::
gold
) &
this
->
hash_mask_
;
96
}
97
98
// ===========================================================================
99
100
/// returns a hashed key for hash tables whose keys are Debugs
101
INLINE
Size
HashFunc
<
Debug
>::
castToSize
(
const
Debug
&
key
) {
102
Size
h
=
Size
(0);
103
104
for
(
Size
i
=
Size
(0),
j
=
Size
(
key
.
size
());
i
<
j
; ++
i
)
105
h
=
Size
(19) *
h
+
Size
(
key
[
i
]);
106
107
return
h
;
108
}
109
110
// Returns the hashed value of a key.
111
INLINE
Size
HashFunc
<
Debug
>::
operator
()(
const
Debug
&
key
)
const
{
112
return
(
castToSize
(
key
) *
HashFuncConst
::
gold
) &
this
->
hash_mask_
;
113
}
114
115
116
}
/* namespace gum */
117
118
#
endif
/* DOXYGEN_SHOULD_SKIP_THIS */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669