aGrUM  0.21.0
a C++ library for (probabilistic) graphical models
variableNodeMap.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 Header of class VariableNodeMap.
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 #ifndef GUM_VARIABLE_NODE_MAP_H
29 #define GUM_VARIABLE_NODE_MAP_H
30 
31 #include <iostream>
32 #include <string>
33 
34 #include <agrum/agrum.h>
35 
36 #include <agrum/tools/core/bijection.h>
37 #include <agrum/tools/graphs/graphElements.h>
38 #include <agrum/tools/variables/discreteVariable.h>
39 
40 namespace gum {
41 
42  /**
43  * @class VariableNodeMap
44  * @brief Container used to map discrete variables with nodes.
45  * @warning VariableNodeMap ensures that every name of variable is unique in
46  * the container.
47  */
48  class VariableNodeMap {
49  public:
50  /// @name Constructors / Destructors
51 
52  /// @{
53 
54  /// Default constructor
55  VariableNodeMap();
56 
57  /// Copy constructor
58  /// Proceed a deep copy: all variables are copied but keep the same node id.
59  VariableNodeMap(const VariableNodeMap& source);
60 
61  /// Destructor
62  ~VariableNodeMap();
63 
64  /// Copy operator.
65  VariableNodeMap& operator=(const VariableNodeMap& source);
66 
67  /// @}
68 
69  // ===========================================================================
70  /// @name Getters and setters.
71  // ===========================================================================
72  /// @{
73 
74  /// Returns a discrete variable given it's node id.
75  /** @throws NotFound Raised if no nodes matches id. */
76  const DiscreteVariable& get(NodeId id) const;
77 
78  /// Returns a node id given it's variable.
79  /** @throws NotFound Raised if no nodes matches var. */
80  NodeId get(const DiscreteVariable& var) const;
81 
82  /// Return true if id matches a node.
83  bool exists(NodeId id) const;
84 
85  /// Return true if var matches a node.
86  bool exists(const DiscreteVariable& var) const;
87 
88  /// Maps id with var.
89  /**
90  * @warning Var is added by copy.
91  * @throws DuplicateLabel if this name already exists
92  * @throws DuplicateElement if this id already exists
93  * @return Returns id (useful in some case); */
94  NodeId insert(NodeId id, const DiscreteVariable& var);
95 
96  /// Removes a var and it's id of this mapping. The pointer is deleted.
97  /** @throws NotFound Raised if no nodes matches id. */
98  void erase(NodeId id);
99 
100  /// Removes a var and it's id of this mapping. The pointer is deleted.
101  /** @throws NotFound Raised if no nodes matches id. */
102  void erase(const DiscreteVariable& var);
103 
104  /// we allow the user to change the name of a variable
105  /** @throws DuplicateLabel if this name already exists
106  * @throws NotFound Raised if no nodes matches id. */
107  void changeName(NodeId id, const std::string& new_name);
108 
109  /// removes all the associations
110  void clear();
111 
112  /// give the size
113  Size size() const;
114 
115  /// friendly displays the content of the VariableNodeMap
116  std::string toString() const;
117 
118  /**
119  * Returns the name of a variable given its id.
120  * @param id The variable's id.
121  * @return id's name.
122  * @throw NotFound Raised if no variable matches id.
123  */
124  const std::string& name(NodeId id) const;
125 
126  /**
127  * Returns the name of a variable.
128  * @param var The variable.
129  * @return var's name.
130  * @throw NotFound Raised if var is not in this VariableNodeMap.
131  */
132  const std::string& name(const DiscreteVariable& var) const;
133 
134  /// @}
135 
136  // ===========================================================================
137  /// @name Operators.
138  // ===========================================================================
139  /// @{
140 
141  /// Returns a discrete variable given it's node id.
142  /** @throws NotFound Raised if no nodes matches id. */
143  const DiscreteVariable& operator[](NodeId id) const;
144 
145  /// Returns a node id given it's variable.
146  /** @throws NotFound Raised if no nodes matches var. */
147  NodeId operator[](const DiscreteVariable& var) const;
148 
149  /// @}
150 
151  // ===========================================================================
152  /// @name Accessor by name
153  // ===========================================================================
154  /// @{
155 
156  /// @throw NotFound if no such name exists in the graph.
157  NodeId idFromName(const std::string& name) const;
158  const DiscreteVariable& variableFromName(const std::string& name) const;
159 
160  /// @}
161 
162  private:
163  /// effectively do the copy (for copy constructor or operator=)
164  void _copy_(const VariableNodeMap& source);
165 
166  /// Bijection between the node's NodeIds and the variables.
167  Bijection< NodeId, const DiscreteVariable* > _nodes2vars_;
168 
169  /// HashTable for easely find an id from a name
170  Bijection< std::string, NodeId > _names2nodes_;
171  };
172 
173  /// for friendly displaying the content of clique graphs
174 
175  std::ostream& operator<<(std::ostream&, const VariableNodeMap&);
176 
177 } /* namespace gum */
178 
179 #ifndef GUM_NO_INLINE
180 # include <agrum/tools/graphicalModels/variableNodeMap_inl.h>
181 #endif /* GUM_NO_INLINE */
182 
183 #endif /* GUM_VARIABLE_NODE_MAP_H*/