aGrUM  0.14.2
variableNodeMap_inl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}@lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
26 #ifndef DOXYGEN_SHOULD_SKIP_THIS
27 
28 // to ease parsers in IDE
30 
31 namespace gum {
32 
33  // Returns a discrete variable given it's node id.
34  // @throws NotFound Raised if no nodes matches id.
35  INLINE
36  const DiscreteVariable& VariableNodeMap::get(NodeId id) const {
37  return *(__nodes2vars.second(id));
38  }
39 
40  // Returns a node id given it's variable.
41  // @throws NotFound Raised if no nodes matches var.
42  INLINE
43  NodeId VariableNodeMap::get(const DiscreteVariable& var) const {
44  return __nodes2vars.first(&var);
45  }
46 
47  // Return true if id matches a node
48  INLINE
49  bool VariableNodeMap::exists(NodeId id) const {
50  return __nodes2vars.existsFirst(id);
51  }
52 
53  // Return true if var matches a node
54  INLINE
55  bool VariableNodeMap::exists(const DiscreteVariable& var) const {
56  return __nodes2vars.existsSecond(&var);
57  }
58 
59  // Returns a node id given it's variable.
60  // @throws NotFound Raised if no nodes matches var.
61  INLINE
62  const DiscreteVariable& VariableNodeMap::operator[](NodeId varId) const {
63  return get(varId);
64  }
65 
66  // Returns a node id given it's variable.
67  // @throws NotFound Raised if no nodes matches var.
68  INLINE
69  NodeId VariableNodeMap::operator[](const DiscreteVariable& var) const {
70  return get(var);
71  }
72 
73  // Maps id with var. Var is added by copy.
74  // @throw DuplicateLabel if the name already exists in the mapping
75  // @throw DuplicateElement if the id already exists in the mapping
76  INLINE
77  NodeId VariableNodeMap::insert(NodeId id, const DiscreteVariable& var) {
78  if (__names2nodes.existsFirst(var.name())) {
79  GUM_ERROR(DuplicateLabel, "Unable to insert var with this name.");
80  }
81 
82  if (exists(id)) {
83  GUM_ERROR(DuplicateElement,
84  "Unable to insert a new variable with id " << id << ".");
85  }
86 
87  __nodes2vars.insert(id, var.clone());
88  __names2nodes.insert(var.name(), id);
89 
90  return id;
91  }
92 
93  // Removes a var and it's id of this mapping. The pointer is deleted.
94  INLINE
96  const DiscreteVariable* var = __nodes2vars.second(id);
97  __names2nodes.eraseFirst(var->name());
98  delete (var);
99  __nodes2vars.eraseFirst(id);
100  }
101 
102  // Removes a var and it's id of this mapping. The pointer is deleted.
103  INLINE
104  void VariableNodeMap::erase(const DiscreteVariable& var) {
105  NodeId id = __nodes2vars.first(&var);
106  erase(id);
107  }
108 
109  INLINE
110  NodeId VariableNodeMap::idFromName(const std::string& name) const {
111  return __names2nodes.second(name);
112  }
113 
114  INLINE
115  const DiscreteVariable&
116  VariableNodeMap::variableFromName(const std::string& name) const {
117  return *__nodes2vars.second(idFromName(name));
118  }
119 
120  // we allow the user to change the name of a variable
121  // @throws DuplicateLabel if this name already exists
122  // @throws NotFound Raised if no nodes matches id.
123  INLINE
124  void VariableNodeMap::changeName(NodeId id, const std::string& new_name) {
125  if (__names2nodes.existsFirst(new_name)) {
126  GUM_ERROR(DuplicateLabel, "Unable to insert var with this name.");
127  }
128 
129  DiscreteVariable* var =
130  const_cast< DiscreteVariable* >(__nodes2vars.second(id));
131 
132  __names2nodes.eraseFirst(var->name());
133  var->setName(new_name);
134  __names2nodes.insert(new_name, id);
135  }
136 
137  INLINE const std::string& VariableNodeMap::name(NodeId id) const {
138  return __names2nodes.first(id);
139  }
140 
141  INLINE const std::string&
142  VariableNodeMap::name(const DiscreteVariable& var) const {
143  return __names2nodes.first(__nodes2vars.first(&var));
144  }
145 
146 } /* namespace gum */
147 
148 #endif /*DOXYGEN_SHOULD_SKIP_THIS*/
void insert(const T1 &first, const T2 &second)
Inserts a new association in the gum::Bijection.
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
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&#39;s NodeIds and the variables.
const T1 & first(const T2 &second) const
Returns the first value of a pair given its second value.
void eraseFirst(const T1 &first)
Erases an association containing the given first element.
NodeId insert(NodeId id, const DiscreteVariable &var)
Maps id with var.
bool exists(NodeId id) const
Return true if id matches a node.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Header of class VariableNodeMap.
void erase(NodeId id)
Removes a var and it&#39;s id of this mapping. The pointer is deleted.
bool existsFirst(const T1 &first) const
Returns true if first is the first element in a pair in the gum::Bijection.
const DiscreteVariable & operator[](NodeId id) const
Returns a discrete variable given it&#39;s node id.
NodeId idFromName(const std::string &name) const
void changeName(NodeId id, const std::string &new_name)
we allow the user to change the name of a variable
Bijection< std::string, NodeId > __names2nodes
HashTable for easely find an id from a name.
const DiscreteVariable & variableFromName(const std::string &name) const
Size NodeId
Type for node ids.
Definition: graphElements.h:97
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
const DiscreteVariable & get(NodeId id) const
Returns a discrete variable given it&#39;s node id.