aGrUM  0.14.2
SetTerminalNodePolicy.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_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  ***************************************************************************/
27 #ifndef GUM_MULTI_DIM_FUNCTION_GRAPH_SET_TERMINAL_NODE_POLICY_H
28 #define GUM_MULTI_DIM_FUNCTION_GRAPH_SET_TERMINAL_NODE_POLICY_H
29 
31 
32 namespace gum {
33 
34  // clang-format off
43  // clang-format on
44  template < typename GUM_SCALAR >
46  public:
48 
49  // ============================================================================
51  // ============================================================================
53 
55  void addTerminalNode(const NodeId& n, const GUM_SCALAR& v) {
56  if (__map.exists(n)) *(__map[n]) += v;
57  __map.insert(n, new GUM_SCALAR(v));
58  }
59 
61  void eraseTerminalNode(const NodeId& n) {
62  if (__map.exists(n)) __map.erase(n);
63  }
64 
67  for (auto nodeIter = __map.beginSafe(); nodeIter != __map.endSafe();
68  ++nodeIter)
69  delete nodeIter.val();
70  __map.clear();
71  }
72 
74  // ============================================================================
76  // ============================================================================
78 
80  bool existsTerminalNodeWithId(const NodeId& n) const {
81  return __map.exists(n);
82  }
83 
85  bool existsTerminalNodeWithValue(const GUM_SCALAR& v) const {
86  return terminalNodeId(v) != 0;
87  }
88 
90  // ============================================================================
92  // ============================================================================
94 
96  const GUM_SCALAR& terminalNodeValue(const NodeId& n) const {
97  return *(__map[n]);
98  }
99 
101  const NodeId& terminalNodeId(const GUM_SCALAR& v) const {
102  for (auto nodeIter = __map.beginSafe(); nodeIter != __map.endSafe();
103  ++nodeIter)
104  if (*(nodeIter.val()) == v) return nodeIter.key();
105  return jocker;
106  }
107 
109  // ============================================================================
111  // ============================================================================
113 
115  void beginValues() const { __mappy = __map.cbeginSafe(); }
116 
118  bool hasValue() const { return __mappy != __map.cendSafe(); }
119 
121  void nextValue() const { ++__mappy; }
122 
125  const GUM_SCALAR& value() const { return *(__mappy.val()); }
126 
129  const NodeId& id() const { return __mappy.key(); }
130 
131  private:
135 
136  const NodeId jocker = 0;
137  };
138 
139 } // End of namespace gum
140 
141 #endif /* GUM_MULTI_DIM_FUNCTION_GRAPH_SET_TERMINAL_NODE_POLICY_H */
Base node set class for graphs.
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.
gum is the global namespace for all aGrUM entities
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:97
void clearAllTerminalNodes()
Erase all terminal nodes.