aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
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
55
56namespace gum {
57
58 // Returns a discrete variable given it's node id.
59 // @throws NotFound Raised if no nodes matches id.
60 INLINE
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
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
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
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
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*/
Base class for discrete random variable.
void changeName(NodeId id, std::string_view new_name)
we allow the user to change the name of a variable
const DiscreteVariable & variableFromName(std::string_view name) const
bool exists(NodeId id) const
Return true if id matches a node.
NodeId idFromName(std::string_view name) const
void erase(NodeId id)
Removes a var and it's id of this mapping. The pointer is deleted.
Bijection< std::string, NodeId > _names2nodes_
HashTable for easely find an id from a name.
const DiscreteVariable & get(NodeId id) const
Returns a discrete variable given it's node id.
const DiscreteVariable & operator[](NodeId id) const
Returns a discrete variable given it's node id.
const std::string & name(NodeId id) const
Returns the name of a variable given its id.
Bijection< NodeId, const DiscreteVariable * > _nodes2vars_
Bijection between the node's NodeIds and the variables.
Size size() const
give the size
NodeId insert(NodeId id, const DiscreteVariable &var)
Maps id with var.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Header of class VariableNodeMap.