aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
variableNodeMap_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
/**
23
* @file
24
* @brief Inlined implementation of VariableNodeMap
25
*
26
* @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27
*/
28
#
ifndef
DOXYGEN_SHOULD_SKIP_THIS
29
30
// to ease parsers in IDE
31
#
include
<
agrum
/
tools
/
graphicalModels
/
variableNodeMap
.
h
>
32
33
namespace
gum
{
34
35
// Returns a discrete variable given it's node id.
36
// @throws NotFound Raised if no nodes matches id.
37
INLINE
38
const
DiscreteVariable
&
VariableNodeMap
::
get
(
NodeId
id
)
const
{
39
return
*(
nodes2vars__
.
second
(
id
));
40
}
41
42
// Returns a node id given it's variable.
43
// @throws NotFound Raised if no nodes matches var.
44
INLINE
45
NodeId
VariableNodeMap
::
get
(
const
DiscreteVariable
&
var
)
const
{
46
return
nodes2vars__
.
first
(&
var
);
47
}
48
49
// Return true if id matches a node
50
INLINE
51
bool
VariableNodeMap
::
exists
(
NodeId
id
)
const
{
52
return
nodes2vars__
.
existsFirst
(
id
);
53
}
54
55
// Return true if var matches a node
56
INLINE
57
bool
VariableNodeMap
::
exists
(
const
DiscreteVariable
&
var
)
const
{
58
return
nodes2vars__
.
existsSecond
(&
var
);
59
}
60
61
// Returns a node id given it's variable.
62
// @throws NotFound Raised if no nodes matches var.
63
INLINE
64
const
DiscreteVariable
&
VariableNodeMap
::
operator
[](
NodeId
varId
)
const
{
65
return
get
(
varId
);
66
}
67
68
// Returns a node id given it's variable.
69
// @throws NotFound Raised if no nodes matches var.
70
INLINE
71
NodeId
VariableNodeMap
::
operator
[](
const
DiscreteVariable
&
var
)
const
{
72
return
get
(
var
);
73
}
74
75
// Maps id with var. Var is added by copy.
76
// @throw DuplicateLabel if the name already exists in the mapping
77
// @throw DuplicateElement if the id already exists in the mapping
78
INLINE
79
NodeId
VariableNodeMap
::
insert
(
NodeId
id
,
const
DiscreteVariable
&
var
) {
80
if
(
names2nodes__
.
existsFirst
(
var
.
name
())) {
81
GUM_ERROR
(
DuplicateLabel
,
82
"Unable to insert var with the name '"
<<
var
.
name
() <<
"'."
);
83
}
84
85
if
(
exists
(
id
)) {
86
GUM_ERROR
(
DuplicateElement
,
87
"Unable to insert a new variable with id "
<<
id
<<
"."
);
88
}
89
90
nodes2vars__
.
insert
(
id
,
var
.
clone
());
91
names2nodes__
.
insert
(
var
.
name
(),
id
);
92
93
return
id
;
94
}
95
96
// Removes a var and it's id of this mapping. The pointer is deleted.
97
INLINE
98
void
VariableNodeMap
::
erase
(
NodeId
id
) {
99
const
DiscreteVariable
*
var
=
nodes2vars__
.
second
(
id
);
100
names2nodes__
.
eraseFirst
(
var
->
name
());
101
delete
(
var
);
102
nodes2vars__
.
eraseFirst
(
id
);
103
}
104
105
// Removes a var and it's id of this mapping. The pointer is deleted.
106
INLINE
107
void
VariableNodeMap
::
erase
(
const
DiscreteVariable
&
var
) {
108
NodeId
id
=
nodes2vars__
.
first
(&
var
);
109
erase
(
id
);
110
}
111
112
INLINE
113
NodeId
VariableNodeMap
::
idFromName
(
const
std
::
string
&
name
)
const
{
114
return
names2nodes__
.
second
(
name
);
115
}
116
117
INLINE
118
const
DiscreteVariable
&
119
VariableNodeMap
::
variableFromName
(
const
std
::
string
&
name
)
const
{
120
return
*
nodes2vars__
.
second
(
idFromName
(
name
));
121
}
122
123
// we allow the user to change the name of a variable
124
// @throws DuplicateLabel if this name already exists
125
// @throws NotFound Raised if no nodes matches id.
126
INLINE
127
void
VariableNodeMap
::
changeName
(
NodeId
id
,
const
std
::
string
&
new_name
) {
128
if
(
names2nodes__
.
existsFirst
(
new_name
)) {
129
GUM_ERROR
(
DuplicateLabel
,
130
"Unable to insert var with the name '"
<<
new_name
<<
"'."
);
131
}
132
133
DiscreteVariable
*
var
134
=
const_cast
<
DiscreteVariable
* >(
nodes2vars__
.
second
(
id
));
135
136
names2nodes__
.
eraseFirst
(
var
->
name
());
137
var
->
setName
(
new_name
);
138
names2nodes__
.
insert
(
new_name
,
id
);
139
}
140
141
INLINE
const
std
::
string
&
VariableNodeMap
::
name
(
NodeId
id
)
const
{
142
return
names2nodes__
.
first
(
id
);
143
}
144
145
INLINE
const
std
::
string
&
146
VariableNodeMap
::
name
(
const
DiscreteVariable
&
var
)
const
{
147
return
names2nodes__
.
first
(
nodes2vars__
.
first
(&
var
));
148
}
149
150
}
/* namespace gum */
151
152
#
endif
/*DOXYGEN_SHOULD_SKIP_THIS*/
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669