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