aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
SetTerminalNodePolicy.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Headers of the ITerminalNodePolicy
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
27  * GONZALES(@AMU)
28  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
29  */
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 
33 #include <agrum/tools/graphs/parts/nodeGraphPart.h>
34 
35 namespace gum {
36 
37  // clang-format off
38  /**
39  * @class SetTerminalNodePolicy
40  * @headerfile SetTerminalNodePolicy.h <agrum/tools/multidim/FunctionGraphUtilities/terminalNodePolicies/SetTerminalNodePolicy.h>
41  * @ingroup multidim_group
42  *
43  * @brief Implementation of a Terminal Node Policy that maps nodeid to a set
44  * of value
45  */
46  // clang-format on
47  template < typename GUM_SCALAR >
49  public:
51 
52  // ============================================================================
53  /// @name Terminal Node Creation and Destruction
54  // ============================================================================
55  /// @{
56 
57  /// Insert a new terminal node with given value
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 
63  /// Remove node matching given id
64  void eraseTerminalNode(const NodeId& n) {
65  if (_map_.exists(n)) _map_.erase(n);
66  }
67 
68  /// Erase all terminal nodes
70  for (auto nodeIter = _map_.beginSafe(); nodeIter != _map_.endSafe(); ++nodeIter)
71  delete nodeIter.val();
72  _map_.clear();
73  }
74 
75  /// @}
76  // ============================================================================
77  /// @name Terminal Nodes Existence
78  // ============================================================================
79  /// @{
80 
81  /// Returns true if a terminal node matching this id exists
82  bool existsTerminalNodeWithId(const NodeId& n) const { return _map_.exists(n); }
83 
84  /// Returns true if a terminal node matching this value exists
85  bool existsTerminalNodeWithValue(const GUM_SCALAR& v) const { return terminalNodeId(v) != 0; }
86 
87  /// @}
88  // ============================================================================
89  /// @name Terminal Nodes value and id access
90  // ============================================================================
91  /// @{
92 
93  /// Returns the value of the terminal node that has the given id
94  const GUM_SCALAR& terminalNodeValue(const NodeId& n) const { return *(_map_[n]); }
95 
96  /// Returns the id of the terminal node that has the given value
97  const NodeId& terminalNodeId(const GUM_SCALAR& v) const {
98  for (auto nodeIter = _map_.beginSafe(); nodeIter != _map_.endSafe(); ++nodeIter)
99  if (*(nodeIter.val()) == v) return nodeIter.key();
100  return jocker;
101  }
102 
103  /// @}
104  // ============================================================================
105  /// @name Iterator on Terminal Nodes
106  // ============================================================================
107  /// @{
108 
109  /// Initializes the constant safe iterator on terminal nodes
110  void beginValues() const { _mappy_ = _map_.cbeginSafe(); }
111 
112  /// Indicates if constant safe iterator has reach end of terminal nodes list
113  bool hasValue() const { return _mappy_ != _map_.cendSafe(); }
114 
115  /// Increments the constant safe iterator
116  void nextValue() const { ++_mappy_; }
117 
118  /// Returns the value of the current terminal nodes pointed by the constant
119  /// safe iterator
120  const GUM_SCALAR& value() const { return *(_mappy_.val()); }
121 
122  /// Returns the id of the current terminal nodes pointed by the constant
123  /// safe iterator
124  const NodeId& id() const { return _mappy_.key(); }
125 
126  private:
127  /// The mapping between NodeIds and Value Sets
130 
131  const NodeId jocker = 0;
132  };
133 
134 } // End of namespace gum
135 
136 #endif /* GUM_MULTI_DIM_FUNCTION_GRAPH_SET_TERMINAL_NODE_POLICY_H */
const GUM_SCALAR & value() const
Returns the value of the current terminal nodes pointed by the constant safe iterator.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
const GUM_SCALAR & terminalNodeValue(const NodeId &n) const
Returns the value of the terminal node that has the given id.
HashTable< NodeId, GUM_SCALAR *> _map_
The mapping between NodeIds and Value Sets.
const NodeId & id() const
Returns the id of the current terminal nodes pointed by the constant safe iterator.
void eraseTerminalNode(const NodeId &n)
Remove node matching given id.
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.
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.
HashTableConstIteratorSafe< NodeId, GUM_SCALAR *> _mappy_
The mapping between NodeIds and Value Sets.
void clearAllTerminalNodes()
Erase all terminal nodes.