aGrUM  0.20.3
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  /// friendly displays the content of the VariableNodeMap
113  std::string toString() const;
114 
115  /**
116  * Returns the name of a variable given its id.
117  * @param id The variable's id.
118  * @return id's name.
119  * @throw NotFound Raised if no variable matches id.
120  */
121  const std::string& name(NodeId id) const;
122 
123  /**
124  * Returns the name of a variable.
125  * @param var The variable.
126  * @return var's name.
127  * @throw NotFound Raised if var is not in this VariableNodeMap.
128  */
129  const std::string& name(const DiscreteVariable& var) const;
130 
131  /// @}
132 
133  // ===========================================================================
134  /// @name Operators.
135  // ===========================================================================
136  /// @{
137 
138  /// Returns a discrete variable given it's node id.
139  /** @throws NotFound Raised if no nodes matches id. */
140  const DiscreteVariable& operator[](NodeId id) const;
141 
142  /// Returns a node id given it's variable.
143  /** @throws NotFound Raised if no nodes matches var. */
144  NodeId operator[](const DiscreteVariable& var) const;
145 
146  /// @}
147 
148  // ===========================================================================
149  /// @name Accessor by name
150  // ===========================================================================
151  /// @{
152 
153  /// @throw NotFound if no such name exists in the graph.
154  NodeId idFromName(const std::string& name) const;
155  const DiscreteVariable& variableFromName(const std::string& name) const;
156 
157  /// @}
158 
159  private:
160  /// effectively do the copy (for copy constructor or operator=)
161  void _copy_(const VariableNodeMap& source);
162 
163  /// Bijection between the node's NodeIds and the variables.
164  Bijection< NodeId, const DiscreteVariable* > _nodes2vars_;
165 
166  /// HashTable for easely find an id from a name
167  Bijection< std::string, NodeId > _names2nodes_;
168  };
169 
170  /// for friendly displaying the content of clique graphs
171 
172  std::ostream& operator<<(std::ostream&, const VariableNodeMap&);
173 
174 } /* namespace gum */
175 
176 #ifndef GUM_NO_INLINE
177 # include <agrum/tools/graphicalModels/variableNodeMap_inl.h>
178 #endif /* GUM_NO_INLINE */
179 
180 #endif /* GUM_VARIABLE_NODE_MAP_H*/