aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
variableNodeMap_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
/**
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
{
return
_nodes2vars_
.
existsFirst
(
id
); }
52
53
// Return true if var matches a node
54
INLINE
55
bool
VariableNodeMap
::
exists
(
const
DiscreteVariable
&
var
)
const
{
56
return
_nodes2vars_
.
existsSecond
(&
var
);
57
}
58
59
// Returns a node id given it's variable.
60
// @throws NotFound Raised if no nodes matches var.
61
INLINE
62
const
DiscreteVariable
&
VariableNodeMap
::
operator
[](
NodeId
varId
)
const
{
return
get
(
varId
); }
63
64
// Returns a node id given it's variable.
65
// @throws NotFound Raised if no nodes matches var.
66
INLINE
67
NodeId
VariableNodeMap
::
operator
[](
const
DiscreteVariable
&
var
)
const
{
return
get
(
var
); }
68
69
// Maps id with var. Var is added by copy.
70
// @throw DuplicateLabel if the name already exists in the mapping
71
// @throw DuplicateElement if the id already exists in the mapping
72
INLINE
73
NodeId
VariableNodeMap
::
insert
(
NodeId
id
,
const
DiscreteVariable
&
var
) {
74
if
(
_names2nodes_
.
existsFirst
(
var
.
name
())) {
75
GUM_ERROR
(
DuplicateLabel
,
"Unable to insert var with the name '"
<<
var
.
name
() <<
"'."
)
76
}
77
78
if
(
exists
(
id
)) {
79
GUM_ERROR
(
DuplicateElement
,
"Unable to insert a new variable with id "
<<
id
<<
"."
)
80
}
81
82
_nodes2vars_
.
insert
(
id
,
var
.
clone
());
83
_names2nodes_
.
insert
(
var
.
name
(),
id
);
84
85
return
id
;
86
}
87
88
// Removes a var and it's id of this mapping. The pointer is deleted.
89
INLINE
90
void
VariableNodeMap
::
erase
(
NodeId
id
) {
91
const
DiscreteVariable
*
var
=
_nodes2vars_
.
second
(
id
);
92
_names2nodes_
.
eraseFirst
(
var
->
name
());
93
delete
(
var
);
94
_nodes2vars_
.
eraseFirst
(
id
);
95
}
96
97
// Removes a var and it's id of this mapping. The pointer is deleted.
98
INLINE
99
void
VariableNodeMap
::
erase
(
const
DiscreteVariable
&
var
) {
100
NodeId
id
=
_nodes2vars_
.
first
(&
var
);
101
erase
(
id
);
102
}
103
104
INLINE
105
NodeId
VariableNodeMap
::
idFromName
(
const
std
::
string
&
name
)
const
{
106
return
_names2nodes_
.
second
(
name
);
107
}
108
109
INLINE
110
const
DiscreteVariable
&
VariableNodeMap
::
variableFromName
(
const
std
::
string
&
name
)
const
{
111
return
*
_nodes2vars_
.
second
(
idFromName
(
name
));
112
}
113
114
// we allow the user to change the name of a variable
115
// @throws DuplicateLabel if this name already exists
116
// @throws NotFound Raised if no nodes matches id.
117
INLINE
118
void
VariableNodeMap
::
changeName
(
NodeId
id
,
const
std
::
string
&
new_name
) {
119
if
(
_names2nodes_
.
existsFirst
(
new_name
)) {
120
GUM_ERROR
(
DuplicateLabel
,
"Unable to insert var with the name '"
<<
new_name
<<
"'."
)
121
}
122
123
DiscreteVariable
*
var
=
const_cast
<
DiscreteVariable
* >(
_nodes2vars_
.
second
(
id
));
124
125
_names2nodes_
.
eraseFirst
(
var
->
name
());
126
var
->
setName
(
new_name
);
127
_names2nodes_
.
insert
(
new_name
,
id
);
128
}
129
130
INLINE
const
std
::
string
&
VariableNodeMap
::
name
(
NodeId
id
)
const
{
131
return
_names2nodes_
.
first
(
id
);
132
}
133
134
INLINE
const
std
::
string
&
VariableNodeMap
::
name
(
const
DiscreteVariable
&
var
)
const
{
135
return
_names2nodes_
.
first
(
_nodes2vars_
.
first
(&
var
));
136
}
137
138
}
/* namespace gum */
139
140
#
endif
/*DOXYGEN_SHOULD_SKIP_THIS*/
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:643