aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
operatorRegister4MultiDim_tpl.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
/**
23
* @file
24
* @brief A container for registering binary functions on
25
* multiDimImplementations
26
*
27
* @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
28
*/
29
30
#
ifndef
DOXYGEN_SHOULD_SKIP_THIS
31
32
#
include
<
agrum
/
agrum
.
h
>
33
34
#
include
<
agrum
/
tools
/
multidim
/
utils
/
operators
/
operatorRegister4MultiDim
.
h
>
35
36
namespace
gum
{
37
38
// adds a new entry into the register
39
template
<
typename
GUM_SCALAR >
40
void
OperatorRegister4MultiDim<
GUM_SCALAR
>::
insert
(
41
const
std
::
string
&
operation_name
,
42
const
std
::
string
&
type1
,
43
const
std
::
string
&
type2
,
44
typename
OperatorRegister4MultiDim
<
GUM_SCALAR
>::
OperatorPtr
newFunction
) {
45
// insert the new entry
46
OperatorSet
*
theset
;
47
48
if
(!
set__
.
exists
(
operation_name
)) {
49
theset
=
set__
.
insert
(
operation_name
,
new
OperatorSet
).
second
;
50
#
ifdef
GUM_DEBUG_MODE
51
// for debugging purposes, we should inform the aGrUM's debugger that
52
// the hashtable contained within the OperatorRegister4MultiDim will be
53
// removed at the end of the program's execution.
54
__debug__
::
inc_deletion__
(
"HashTable"
,
55
__FILE__
,
56
__LINE__
,
57
"destructor of"
,
58
(
void
*)
theset
);
59
#
endif
/* GUM_DEBUG_MODE */
60
}
else
{
61
theset
=
set__
[
operation_name
];
62
}
63
64
std
::
pair
<
std
::
string
,
std
::
string
>
thepair
(
type1
,
type2
);
65
66
theset
->
insert
(
thepair
,
newFunction
);
67
}
68
69
// removes a given entry from the register
70
template
<
typename
GUM_SCALAR
>
71
void
OperatorRegister4MultiDim
<
GUM_SCALAR
>::
erase
(
72
const
std
::
string
&
operation_name
,
73
const
std
::
string
&
type1
,
74
const
std
::
string
&
type2
) {
75
if
(!
set__
.
exists
(
operation_name
))
return
;
76
77
OperatorSet
*
theset
=
set__
[
operation_name
];
78
79
theset
->
erase
(
std
::
pair
<
std
::
string
,
std
::
string
>(
type1
,
type2
));
80
}
81
82
// indicates whether a given entry exists in the register
83
template
<
typename
GUM_SCALAR
>
84
INLINE
bool
OperatorRegister4MultiDim
<
GUM_SCALAR
>::
exists
(
85
const
std
::
string
&
operation_name
,
86
const
std
::
string
&
type1
,
87
const
std
::
string
&
type2
)
const
{
88
if
(!
set__
.
exists
(
operation_name
))
return
false
;
89
OperatorSet
*
theset
=
set__
[
operation_name
];
90
return
theset
->
exists
(
std
::
pair
<
std
::
string
,
std
::
string
>(
type1
,
type2
));
91
}
92
93
/** @brief returns the specialized operator assigned to a given pair of
94
* MultiDimImplementations */
95
template
<
typename
GUM_SCALAR
>
96
INLINE
typename
OperatorRegister4MultiDim
<
GUM_SCALAR
>::
OperatorPtr
97
OperatorRegister4MultiDim
<
GUM_SCALAR
>::
get
(
98
const
std
::
string
&
operation_name
,
99
const
std
::
string
&
type1
,
100
const
std
::
string
&
type2
)
const
{
101
OperatorSet
*
theset
=
set__
[
operation_name
];
102
return
(*
theset
)[
std
::
pair
<
std
::
string
,
std
::
string
>(
type1
,
type2
)];
103
}
104
105
// a named constructor that constructs one and only one Register per data
106
// type
107
template
<
typename
GUM_SCALAR
>
108
OperatorRegister4MultiDim
<
GUM_SCALAR
>&
109
OperatorRegister4MultiDim
<
GUM_SCALAR
>::
Register
() {
110
static
OperatorRegister4MultiDim
<
GUM_SCALAR
>*
container
=
nullptr
;
111
static
bool
first
=
true
;
112
113
if
(
first
) {
114
first
=
false
;
115
container
=
new
OperatorRegister4MultiDim
<
GUM_SCALAR
>;
116
117
#
ifdef
GUM_DEBUG_MODE
118
// for debugging purposes, we should inform the aGrUM's debugger that
119
// the hashtable contained within the OperatorRegister4MultiDim will be
120
// removed at the end of the program's execution.
121
__debug__
::
inc_deletion__
(
"HashTable"
,
122
__FILE__
,
123
__LINE__
,
124
"destructor of"
,
125
(
void
*)&
container
->
set__
);
126
#
endif
/* GUM_DEBUG_MODE */
127
}
128
129
return
*
container
;
130
}
131
132
133
// Default constructor: creates an empty register
134
template
<
typename
GUM_SCALAR
>
135
OperatorRegister4MultiDim
<
GUM_SCALAR
>::
OperatorRegister4MultiDim
() {}
136
137
// destructor
138
template
<
typename
GUM_SCALAR
>
139
OperatorRegister4MultiDim
<
GUM_SCALAR
>::~
OperatorRegister4MultiDim
() {
140
// remove all the sets
141
for
(
typename
HashTable
<
std
::
string
,
OperatorSet
* >::
iterator_safe
iter
142
=
set__
.
beginSafe
();
143
iter
!=
set__
.
endSafe
();
144
++
iter
)
145
delete
iter
.
val
();
146
}
147
148
// a function to more easily register new operators in MultiDims
149
template
<
typename
GUM_SCALAR
>
150
void
registerOperator
(
151
const
std
::
string
&
operation_name
,
152
const
std
::
string
&
type1
,
153
const
std
::
string
&
type2
,
154
typename
OperatorRegister4MultiDim
<
GUM_SCALAR
>::
OperatorPtr
function
) {
155
OperatorRegister4MultiDim
<
GUM_SCALAR
>::
Register
().
insert
(
operation_name
,
156
type1
,
157
type2
,
158
function
);
159
}
160
161
}
/* namespace gum */
162
163
#
endif
/* DOXYGEN_SHOULD_SKIP_THIS */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669