aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
SetTerminalNodePolicy.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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();
71  ++nodeIter)
72  delete nodeIter.val();
73  map__.clear();
74  }
75 
76  /// @}
77  // ============================================================================
78  /// @name Terminal Nodes Existence
79  // ============================================================================
80  /// @{
81 
82  /// Returns true if a terminal node matching this id exists
83  bool existsTerminalNodeWithId(const NodeId& n) const {
84  return map__.exists(n);
85  }
86 
87  /// Returns true if a terminal node matching this value exists
88  bool existsTerminalNodeWithValue(const GUM_SCALAR& v) const {
89  return terminalNodeId(v) != 0;
90  }
91 
92  /// @}
93  // ============================================================================
94  /// @name Terminal Nodes value and id access
95  // ============================================================================
96  /// @{
97 
98  /// Returns the value of the terminal node that has the given id
99  const GUM_SCALAR& terminalNodeValue(const NodeId& n) const {
100  return *(map__[n]);
101  }
102 
103  /// Returns the id of the terminal node that has the given value
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 
111  /// @}
112  // ============================================================================
113  /// @name Iterator on Terminal Nodes
114  // ============================================================================
115  /// @{
116 
117  /// Initializes the constant safe iterator on terminal nodes
118  void beginValues() const { mappy__ = map__.cbeginSafe(); }
119 
120  /// Indicates if constant safe iterator has reach end of terminal nodes list
121  bool hasValue() const { return mappy__ != map__.cendSafe(); }
122 
123  /// Increments the constant safe iterator
124  void nextValue() const { ++mappy__; }
125 
126  /// Returns the value of the current terminal nodes pointed by the constant
127  /// safe iterator
128  const GUM_SCALAR& value() const { return *(mappy__.val()); }
129 
130  /// Returns the id of the current terminal nodes pointed by the constant
131  /// safe iterator
132  const NodeId& id() const { return mappy__.key(); }
133 
134  private:
135  /// The mapping between NodeIds and Value Sets
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 */
HashTable< NodeId, GUM_SCALAR *> map__
The mapping between NodeIds and Value Sets.
HashTableConstIteratorSafe< NodeId, GUM_SCALAR *> mappy__
The mapping between NodeIds and Value Sets.
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:669
const GUM_SCALAR & terminalNodeValue(const NodeId &n) const
Returns the value of the terminal node that has the given id.
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.
void clearAllTerminalNodes()
Erase all terminal nodes.