aGrUM
3.2.0
a C++ library for (probabilistic) graphical models
Toggle main menu visibility
variableNodeMap_inl.h
Go to the documentation of this file.
1
/****************************************************************************
2
* This file is part of the aGrUM/pyAgrum library. *
3
* *
4
* Copyright (c) 2005-2026 by *
5
* - Pierre-Henri WUILLEMIN(_at_LIP6) *
6
* - Christophe GONZALES(_at_AMU) *
7
* *
8
* The aGrUM/pyAgrum library is free software; you can redistribute it *
9
* and/or modify it under the terms of either : *
10
* *
11
* - the GNU Lesser General Public License as published by *
12
* the Free Software Foundation, either version 3 of the License, *
13
* or (at your option) any later version, *
14
* - the MIT license (MIT), *
15
* - or both in dual license, as here. *
16
* *
17
* (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18
* *
19
* This aGrUM/pyAgrum library is distributed in the hope that it will be *
20
* useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26
* OTHER DEALINGS IN THE SOFTWARE. *
27
* *
28
* See LICENCES for more details. *
29
* *
30
* SPDX-FileCopyrightText: Copyright 2005-2026 *
31
* - Pierre-Henri WUILLEMIN(_at_LIP6) *
32
* - Christophe GONZALES(_at_AMU) *
33
* SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34
* *
35
* Contact : info_at_agrum_dot_org *
36
* homepage : http://agrum.gitlab.io *
37
* gitlab : https://gitlab.com/agrumery/agrum *
38
* *
39
****************************************************************************/
40
41
#pragma once
42
43
50
#include <
agrum/base/graphicalModels/variableNodeMap.h
>
// to ease IDE parser
51
#ifndef DOXYGEN_SHOULD_SKIP_THIS
52
53
// to ease parsers in IDE
54
# include <
agrum/base/graphicalModels/variableNodeMap.h
>
55
56
namespace
gum
{
57
58
// Returns a discrete variable given it's node id.
59
// @throws NotFound Raised if no nodes matches id.
60
INLINE
61
const
DiscreteVariable
&
VariableNodeMap::get
(
NodeId
id
)
const
{
62
return
*(
_nodes2vars_
.second(
id
));
63
}
64
65
// Returns a node id given it's variable.
66
// @throws NotFound Raised if no nodes matches var.
67
INLINE
68
NodeId
VariableNodeMap::get
(
const
DiscreteVariable
& var)
const
{
69
return
_nodes2vars_
.first(&var);
70
}
71
72
// Return true if id matches a node
73
INLINE
74
bool
VariableNodeMap::exists
(
NodeId
id
)
const
{
return
_nodes2vars_
.existsFirst(
id
); }
75
76
// Return true if var matches a node
77
INLINE
78
bool
VariableNodeMap::exists
(
const
DiscreteVariable
& var)
const
{
79
return
_nodes2vars_
.existsSecond(&var);
80
}
81
82
// Return true if name matches a node
83
INLINE
84
bool
VariableNodeMap::exists
(std::string_view name)
const
{
85
return
_names2nodes_
.
existsFirst
(
name
);
86
}
87
88
// Return the size of the map
89
INLINE
90
gum::Size
VariableNodeMap::size
()
const
{
return
_nodes2vars_
.size(); }
91
92
// Returns a node id given it's variable.
93
// @throws NotFound Raised if no nodes matches var.
94
INLINE
95
const
DiscreteVariable
&
VariableNodeMap::operator[]
(
NodeId
varId)
const
{
return
get
(varId); }
96
97
// Returns a node id given it's variable.
98
// @throws NotFound Raised if no nodes matches var.
99
INLINE
100
NodeId
VariableNodeMap::operator[]
(
const
DiscreteVariable
& var)
const
{
return
get
(var); }
101
102
// Maps id with var. Var is added by copy.
103
// @throw DuplicateLabel if the name already exists in the mapping
104
// @throw DuplicateElement if the id already exists in the mapping
105
INLINE
106
NodeId
VariableNodeMap::insert
(
NodeId
id
,
const
DiscreteVariable
& var) {
107
if
(
_names2nodes_
.
existsFirst
(var.name())) {
108
GUM_ERROR
(DuplicateLabel,
"Unable to insert var with the name '"
<< var.name() <<
"'."
)
109
}
110
111
if
(
exists
(
id
)) {
112
GUM_ERROR
(DuplicateElement,
"Unable to insert a new variable with id "
<<
id
<<
"."
)
113
}
114
115
_nodes2vars_
.insert(
id
, var.clone());
116
_names2nodes_
.
insert
(var.name(),
id
);
117
118
return
id;
119
}
120
121
// Removes a var and it's id of this mapping. The pointer is deleted.
122
INLINE
123
void
VariableNodeMap::erase
(
NodeId
id
) {
124
const
DiscreteVariable* var =
_nodes2vars_
.second(
id
);
125
_names2nodes_
.
eraseFirst
(var->name());
126
delete
var;
127
_nodes2vars_
.eraseFirst(
id
);
128
}
129
130
// Removes a var and it's id of this mapping. The pointer is deleted.
131
INLINE
132
void
VariableNodeMap::erase
(
const
DiscreteVariable
& var) {
133
NodeId
id
=
_nodes2vars_
.first(&var);
134
erase
(
id
);
135
}
136
137
INLINE
138
NodeId
VariableNodeMap::idFromName
(std::string_view name)
const
{
139
return
_names2nodes_
.
second
(
name
);
140
}
141
142
INLINE
143
const
DiscreteVariable
&
VariableNodeMap::variableFromName
(std::string_view name)
const
{
144
return
*
_nodes2vars_
.second(
idFromName
(
name
));
145
}
146
147
// we allow the user to change the name of a variable
148
// @throws DuplicateLabel if this name already exists
149
// @throws NotFound Raised if no nodes matches id.
150
INLINE
151
void
VariableNodeMap::changeName
(
NodeId
id
, std::string_view new_name) {
152
if
(
_names2nodes_
.
existsFirst
(new_name)) {
153
GUM_ERROR
(DuplicateLabel,
"Unable to insert var with the name '"
<< new_name <<
"'."
)
154
}
155
156
auto
var =
const_cast<
DiscreteVariable*
>
(
_nodes2vars_
.second(
id
));
157
158
_names2nodes_
.
eraseFirst
(var->name());
159
var->setName(new_name);
160
_names2nodes_
.
insert
(std::string(new_name),
id
);
161
}
162
163
INLINE
const
std::string&
VariableNodeMap::name
(
NodeId
id
)
const
{
164
return
_names2nodes_
.
first
(
id
);
165
}
166
167
INLINE
const
std::string&
VariableNodeMap::name
(
const
DiscreteVariable
& var)
const
{
168
return
_names2nodes_
.
first
(
_nodes2vars_
.first(&var));
169
}
170
171
}
/* namespace gum */
172
173
#endif
/*DOXYGEN_SHOULD_SKIP_THIS*/
gum::BijectionImplementation< T1, T2, std::is_scalar< T1 >::value &&std::is_scalar< T2 >::value >::second
const T2 & second(const T1 &first) const
gum::BijectionImplementation< T1, T2, std::is_scalar< T1 >::value &&std::is_scalar< T2 >::value >::insert
void insert(const T1 &first, const T2 &second)
gum::BijectionImplementation< T1, T2, std::is_scalar< T1 >::value &&std::is_scalar< T2 >::value >::first
const T1 & first(const T2 &second) const
gum::BijectionImplementation< T1, T2, std::is_scalar< T1 >::value &&std::is_scalar< T2 >::value >::eraseFirst
void eraseFirst(const T1 &first)
gum::BijectionImplementation< T1, T2, std::is_scalar< T1 >::value &&std::is_scalar< T2 >::value >::existsFirst
bool existsFirst(const T1 &first) const
gum::DiscreteVariable
Base class for discrete random variable.
Definition
discreteVariable.h:80
gum::VariableNodeMap::changeName
void changeName(NodeId id, std::string_view new_name)
we allow the user to change the name of a variable
gum::VariableNodeMap::variableFromName
const DiscreteVariable & variableFromName(std::string_view name) const
gum::VariableNodeMap::exists
bool exists(NodeId id) const
Return true if id matches a node.
gum::VariableNodeMap::idFromName
NodeId idFromName(std::string_view name) const
gum::VariableNodeMap::erase
void erase(NodeId id)
Removes a var and it's id of this mapping. The pointer is deleted.
gum::VariableNodeMap::_names2nodes_
Bijection< std::string, NodeId > _names2nodes_
HashTable for easely find an id from a name.
Definition
variableNodeMap.h:199
gum::VariableNodeMap::get
const DiscreteVariable & get(NodeId id) const
Returns a discrete variable given it's node id.
gum::VariableNodeMap::operator[]
const DiscreteVariable & operator[](NodeId id) const
Returns a discrete variable given it's node id.
gum::VariableNodeMap::name
const std::string & name(NodeId id) const
Returns the name of a variable given its id.
gum::VariableNodeMap::_nodes2vars_
Bijection< NodeId, const DiscreteVariable * > _nodes2vars_
Bijection between the node's NodeIds and the variables.
Definition
variableNodeMap.h:196
gum::VariableNodeMap::size
Size size() const
give the size
gum::VariableNodeMap::insert
NodeId insert(NodeId id, const DiscreteVariable &var)
Maps id with var.
GUM_ERROR
#define GUM_ERROR(type, msg)
Definition
exceptions.h:76
gum::Size
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition
types.h:74
gum::NodeId
Size NodeId
Type for node ids.
Definition
graphElements.h:117
gum
gum is the global namespace for all aGrUM entities
Definition
agrum.h:46
variableNodeMap.h
Header of class VariableNodeMap.
aGrUM
3.2.0
© PHW&CG&others - 2022
DoXyGeN 1.18.0