aGrUM 2.3.2
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-2025 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-2025 *
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#pragma once
41
42
49#ifndef DOXYGEN_SHOULD_SKIP_THIS
50
51// to ease parsers in IDE
53
54namespace gum {
55
56 // Returns a discrete variable given it's node id.
57 // @throws NotFound Raised if no nodes matches id.
58 INLINE
60 return *(_nodes2vars_.second(id));
61 }
62
63 // Returns a node id given it's variable.
64 // @throws NotFound Raised if no nodes matches var.
65 INLINE
67 return _nodes2vars_.first(&var);
68 }
69
70 // Return true if id matches a node
71 INLINE
72 bool VariableNodeMap::exists(NodeId id) const { return _nodes2vars_.existsFirst(id); }
73
74 // Return true if var matches a node
75 INLINE
76 bool VariableNodeMap::exists(const DiscreteVariable& var) const {
77 return _nodes2vars_.existsSecond(&var);
78 }
79
80 // Return the size of the map
81 INLINE
82 gum::Size VariableNodeMap::size() const { return _nodes2vars_.size(); }
83
84 // Returns a node id given it's variable.
85 // @throws NotFound Raised if no nodes matches var.
86 INLINE
87 const DiscreteVariable& VariableNodeMap::operator[](NodeId varId) const { return get(varId); }
88
89 // Returns a node id given it's variable.
90 // @throws NotFound Raised if no nodes matches var.
91 INLINE
92 NodeId VariableNodeMap::operator[](const DiscreteVariable& var) const { return get(var); }
93
94 // Maps id with var. Var is added by copy.
95 // @throw DuplicateLabel if the name already exists in the mapping
96 // @throw DuplicateElement if the id already exists in the mapping
97 INLINE
99 if (_names2nodes_.existsFirst(var.name())) {
100 GUM_ERROR(DuplicateLabel, "Unable to insert var with the name '" << var.name() << "'.")
101 }
102
103 if (exists(id)) {
104 GUM_ERROR(DuplicateElement, "Unable to insert a new variable with id " << id << ".")
105 }
106
107 _nodes2vars_.insert(id, var.clone());
108 _names2nodes_.insert(var.name(), id);
109
110 return id;
111 }
112
113 // Removes a var and it's id of this mapping. The pointer is deleted.
114 INLINE
116 const DiscreteVariable* var = _nodes2vars_.second(id);
117 _names2nodes_.eraseFirst(var->name());
118 delete var;
119 _nodes2vars_.eraseFirst(id);
120 }
121
122 // Removes a var and it's id of this mapping. The pointer is deleted.
123 INLINE
125 NodeId id = _nodes2vars_.first(&var);
126 erase(id);
127 }
128
129 INLINE
130 NodeId VariableNodeMap::idFromName(const std::string& name) const {
131 return _names2nodes_.second(name);
132 }
133
134 INLINE
135 const DiscreteVariable& VariableNodeMap::variableFromName(const std::string& name) const {
136 return *_nodes2vars_.second(idFromName(name));
137 }
138
139 // we allow the user to change the name of a variable
140 // @throws DuplicateLabel if this name already exists
141 // @throws NotFound Raised if no nodes matches id.
142 INLINE
143 void VariableNodeMap::changeName(NodeId id, const std::string& new_name) {
144 if (_names2nodes_.existsFirst(new_name)) {
145 GUM_ERROR(DuplicateLabel, "Unable to insert var with the name '" << new_name << "'.")
146 }
147
148 auto var = const_cast< DiscreteVariable* >(_nodes2vars_.second(id));
149
150 _names2nodes_.eraseFirst(var->name());
151 var->setName(new_name);
152 _names2nodes_.insert(new_name, id);
153 }
154
155 INLINE const std::string& VariableNodeMap::name(NodeId id) const {
156 return _names2nodes_.first(id);
157 }
158
159 INLINE const std::string& VariableNodeMap::name(const DiscreteVariable& var) const {
160 return _names2nodes_.first(_nodes2vars_.first(&var));
161 }
162
163} /* namespace gum */
164
165#endif /*DOXYGEN_SHOULD_SKIP_THIS*/
Base class for discrete random variable.
const DiscreteVariable & variableFromName(const std::string &name) const
bool exists(NodeId id) const
Return true if id matches a node.
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.
NodeId idFromName(const std::string &name) const
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
void changeName(NodeId id, const std::string &new_name)
we allow the user to change the name of a variable
NodeId insert(NodeId id, const DiscreteVariable &var)
Maps id with var.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
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.