aGrUM  0.16.0
SetTerminalNodePolicy.h
Go to the documentation of this file.
1 
30 #ifndef GUM_MULTI_DIM_FUNCTION_GRAPH_SET_TERMINAL_NODE_POLICY_H
31 #define GUM_MULTI_DIM_FUNCTION_GRAPH_SET_TERMINAL_NODE_POLICY_H
32 
34 
35 namespace gum {
36 
37  // clang-format off
46  // clang-format on
47  template < typename GUM_SCALAR >
49  public:
51 
52  // ============================================================================
54  // ============================================================================
56 
58  void addTerminalNode(const NodeId& n, const GUM_SCALAR& v) {
59  if (__map.exists(n)) *(__map[n]) += v;
60  __map.insert(n, new GUM_SCALAR(v));
61  }
62 
64  void eraseTerminalNode(const NodeId& n) {
65  if (__map.exists(n)) __map.erase(n);
66  }
67 
70  for (auto nodeIter = __map.beginSafe(); nodeIter != __map.endSafe();
71  ++nodeIter)
72  delete nodeIter.val();
73  __map.clear();
74  }
75 
77  // ============================================================================
79  // ============================================================================
81 
83  bool existsTerminalNodeWithId(const NodeId& n) const {
84  return __map.exists(n);
85  }
86 
88  bool existsTerminalNodeWithValue(const GUM_SCALAR& v) const {
89  return terminalNodeId(v) != 0;
90  }
91 
93  // ============================================================================
95  // ============================================================================
97 
99  const GUM_SCALAR& terminalNodeValue(const NodeId& n) const {
100  return *(__map[n]);
101  }
102 
104  const NodeId& terminalNodeId(const GUM_SCALAR& v) const {
105  for (auto nodeIter = __map.beginSafe(); nodeIter != __map.endSafe();
106  ++nodeIter)
107  if (*(nodeIter.val()) == v) return nodeIter.key();
108  return jocker;
109  }
110 
112  // ============================================================================
114  // ============================================================================
116 
118  void beginValues() const { __mappy = __map.cbeginSafe(); }
119 
121  bool hasValue() const { return __mappy != __map.cendSafe(); }
122 
124  void nextValue() const { ++__mappy; }
125 
128  const GUM_SCALAR& value() const { return *(__mappy.val()); }
129 
132  const NodeId& id() const { return __mappy.key(); }
133 
134  private:
138 
139  const NodeId jocker = 0;
140  };
141 
142 } // End of namespace gum
143 
144 #endif /* GUM_MULTI_DIM_FUNCTION_GRAPH_SET_TERMINAL_NODE_POLICY_H */
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
const mapped_type & val() const
Returns the mapped value pointed to by the iterator.
const iterator_safe & endSafe() noexcept
Returns the safe iterator pointing to the end of the hashtable.
const GUM_SCALAR & value() const
Returns the value of the current terminal nodes pointed by the constant safe iterator.
const key_type & key() const
Returns the key pointed to by the iterator.
void erase(const Key &key)
Removes a given element from the hash table.
HashTable< NodeId, GUM_SCALAR *> __map
The mapping between NodeIds and Value Sets.
const_iterator_safe cbeginSafe() const
Returns the safe const_iterator pointing to the beginning of the hashtable.
const GUM_SCALAR & terminalNodeValue(const NodeId &n) const
Returns the value of the terminal node that has the given id.
HashTableConstIteratorSafe< NodeId, GUM_SCALAR *> __mappy
The mapping between NodeIds and Value Sets.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
const NodeId & id() const
Returns the id of the current terminal nodes pointed by the constant safe iterator.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
void eraseTerminalNode(const NodeId &n)
Remove node matching given id.
const const_iterator_safe & cendSafe() const noexcept
Returns the safe const_iterator pointing to the end of the hashtable.
const NodeId jocker
The mapping between NodeIds and Value Sets.
const NodeId & terminalNodeId(const GUM_SCALAR &v) const
Returns the id of the terminal node that has the given value.
Implementation of a Terminal Node Policy that maps nodeid to a set of value.
bool hasValue() const
Indicates if constant safe iterator has reach end of terminal nodes list.
void beginValues() const
Initializes the constant safe iterator on terminal nodes.
void clear()
Removes all the elements in the hash table.
iterator_safe beginSafe()
Returns the safe iterator pointing to the beginning of the hashtable.
bool existsTerminalNodeWithId(const NodeId &n) const
Returns true if a terminal node matching this id exists.
void nextValue() const
Increments the constant safe iterator.
void addTerminalNode(const NodeId &n, const GUM_SCALAR &v)
Insert a new terminal node with given value.
bool existsTerminalNodeWithValue(const GUM_SCALAR &v) const
Returns true if a terminal node matching this value exists.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Size NodeId
Type for node ids.
Definition: graphElements.h:98
void clearAllTerminalNodes()
Erase all terminal nodes.