aGrUM  0.16.0
variableNodeMap_inl.h
Go to the documentation of this file.
1 
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 
31 // to ease parsers in IDE
33 
34 namespace gum {
35 
36  // Returns a discrete variable given it's node id.
37  // @throws NotFound Raised if no nodes matches id.
38  INLINE
39  const DiscreteVariable& VariableNodeMap::get(NodeId id) const {
40  return *(__nodes2vars.second(id));
41  }
42 
43  // Returns a node id given it's variable.
44  // @throws NotFound Raised if no nodes matches var.
45  INLINE
46  NodeId VariableNodeMap::get(const DiscreteVariable& var) const {
47  return __nodes2vars.first(&var);
48  }
49 
50  // Return true if id matches a node
51  INLINE
52  bool VariableNodeMap::exists(NodeId id) const {
53  return __nodes2vars.existsFirst(id);
54  }
55 
56  // Return true if var matches a node
57  INLINE
58  bool VariableNodeMap::exists(const DiscreteVariable& var) const {
59  return __nodes2vars.existsSecond(&var);
60  }
61 
62  // Returns a node id given it's variable.
63  // @throws NotFound Raised if no nodes matches var.
64  INLINE
65  const DiscreteVariable& VariableNodeMap::operator[](NodeId varId) const {
66  return get(varId);
67  }
68 
69  // Returns a node id given it's variable.
70  // @throws NotFound Raised if no nodes matches var.
71  INLINE
72  NodeId VariableNodeMap::operator[](const DiscreteVariable& var) const {
73  return get(var);
74  }
75 
76  // Maps id with var. Var is added by copy.
77  // @throw DuplicateLabel if the name already exists in the mapping
78  // @throw DuplicateElement if the id already exists in the mapping
79  INLINE
80  NodeId VariableNodeMap::insert(NodeId id, const DiscreteVariable& var) {
81  if (__names2nodes.existsFirst(var.name())) {
82  GUM_ERROR(DuplicateLabel, "Unable to insert var with this name.");
83  }
84 
85  if (exists(id)) {
86  GUM_ERROR(DuplicateElement,
87  "Unable to insert a new variable with id " << id << ".");
88  }
89 
90  __nodes2vars.insert(id, var.clone());
91  __names2nodes.insert(var.name(), id);
92 
93  return id;
94  }
95 
96  // Removes a var and it's id of this mapping. The pointer is deleted.
97  INLINE
99  const DiscreteVariable* var = __nodes2vars.second(id);
100  __names2nodes.eraseFirst(var->name());
101  delete (var);
102  __nodes2vars.eraseFirst(id);
103  }
104 
105  // Removes a var and it's id of this mapping. The pointer is deleted.
106  INLINE
107  void VariableNodeMap::erase(const DiscreteVariable& var) {
108  NodeId id = __nodes2vars.first(&var);
109  erase(id);
110  }
111 
112  INLINE
113  NodeId VariableNodeMap::idFromName(const std::string& name) const {
114  return __names2nodes.second(name);
115  }
116 
117  INLINE
118  const DiscreteVariable&
119  VariableNodeMap::variableFromName(const std::string& name) const {
120  return *__nodes2vars.second(idFromName(name));
121  }
122 
123  // we allow the user to change the name of a variable
124  // @throws DuplicateLabel if this name already exists
125  // @throws NotFound Raised if no nodes matches id.
126  INLINE
127  void VariableNodeMap::changeName(NodeId id, const std::string& new_name) {
128  if (__names2nodes.existsFirst(new_name)) {
129  GUM_ERROR(DuplicateLabel, "Unable to insert var with this name.");
130  }
131 
132  DiscreteVariable* var =
133  const_cast< DiscreteVariable* >(__nodes2vars.second(id));
134 
135  __names2nodes.eraseFirst(var->name());
136  var->setName(new_name);
137  __names2nodes.insert(new_name, id);
138  }
139 
140  INLINE const std::string& VariableNodeMap::name(NodeId id) const {
141  return __names2nodes.first(id);
142  }
143 
144  INLINE const std::string&
145  VariableNodeMap::name(const DiscreteVariable& var) const {
146  return __names2nodes.first(__nodes2vars.first(&var));
147  }
148 
149 } /* namespace gum */
150 
151 #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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:98
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
const DiscreteVariable & get(NodeId id) const
Returns a discrete variable given it&#39;s node id.