aGrUM  0.17.2
a C++ library for (probabilistic) graphical models
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,
83  "Unable to insert var with the name '" << var.name() << "'.");
84  }
85 
86  if (exists(id)) {
87  GUM_ERROR(DuplicateElement,
88  "Unable to insert a new variable with id " << id << ".");
89  }
90 
91  __nodes2vars.insert(id, var.clone());
92  __names2nodes.insert(var.name(), id);
93 
94  return id;
95  }
96 
97  // Removes a var and it's id of this mapping. The pointer is deleted.
98  INLINE
100  const DiscreteVariable* var = __nodes2vars.second(id);
101  __names2nodes.eraseFirst(var->name());
102  delete (var);
103  __nodes2vars.eraseFirst(id);
104  }
105 
106  // Removes a var and it's id of this mapping. The pointer is deleted.
107  INLINE
108  void VariableNodeMap::erase(const DiscreteVariable& var) {
109  NodeId id = __nodes2vars.first(&var);
110  erase(id);
111  }
112 
113  INLINE
114  NodeId VariableNodeMap::idFromName(const std::string& name) const {
115  return __names2nodes.second(name);
116  }
117 
118  INLINE
119  const DiscreteVariable&
120  VariableNodeMap::variableFromName(const std::string& name) const {
121  return *__nodes2vars.second(idFromName(name));
122  }
123 
124  // we allow the user to change the name of a variable
125  // @throws DuplicateLabel if this name already exists
126  // @throws NotFound Raised if no nodes matches id.
127  INLINE
128  void VariableNodeMap::changeName(NodeId id, const std::string& new_name) {
129  if (__names2nodes.existsFirst(new_name)) {
130  GUM_ERROR(DuplicateLabel,
131  "Unable to insert var with the name '" << new_name << "'.");
132  }
133 
134  DiscreteVariable* var =
135  const_cast< DiscreteVariable* >(__nodes2vars.second(id));
136 
137  __names2nodes.eraseFirst(var->name());
138  var->setName(new_name);
139  __names2nodes.insert(new_name, id);
140  }
141 
142  INLINE const std::string& VariableNodeMap::name(NodeId id) const {
143  return __names2nodes.first(id);
144  }
145 
146  INLINE const std::string&
147  VariableNodeMap::name(const DiscreteVariable& var) const {
148  return __names2nodes.first(__nodes2vars.first(&var));
149  }
150 
151 } /* namespace gum */
152 
153 #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-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Definition: agrum.h:25
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
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.