aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
ExactTerminalNodePolicy.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 ExactTerminalNodePolicy
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_EXACT_TERMINAL_NODE_POLICY_H
31 #define GUM_MULTI_DIM_FUNCTION_GRAPH_EXACT_TERMINAL_NODE_POLICY_H
32 
33 #include <agrum/tools/core/bijection.h>
34 #include <agrum/tools/graphs/parts/nodeGraphPart.h>
35 #include <agrum/tools/multidim/utils/FunctionGraphUtilities/terminalNodePolicies/ITerminalNodePolicy.h>
36 
37 namespace gum {
38 
39  // clang-format off
40  /**
41  * @class ExactTerminalNodePolicy
42  * @headerfile ExactTerminalNodePolicy.h <agrum/tools/multidim/FunctionGraphUtilities/terminalNodePolicies/ExactTerminalNodePolicy.h>
43  * @ingroup multidim_group
44  *
45  * @brief Implementation of a Terminal Node Policy that maps nodeid directly
46  * to value.
47  */
48  // clang-format on
49  template < typename GUM_SCALAR >
51  public:
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  map__.insert(n, v);
60  }
61 
62  /// Remove node matching given id
63  void eraseTerminalNode(const NodeId& n) { map__.eraseFirst(n); }
64 
65  /// Erase all terminal nodes
66  void clearAllTerminalNodes() { map__.clear(); }
67 
68  /// @}
69  // ============================================================================
70  /// @name Terminal Nodes Existence
71  // ============================================================================
72  /// @{
73 
74  /// Returns true if a terminal node matching this id exists
75  bool existsTerminalNodeWithId(const NodeId& n) const {
76  return map__.existsFirst(n);
77  }
78 
79  /// Returns true if a terminal node matching this value exists
80  bool existsTerminalNodeWithValue(const GUM_SCALAR& v) const {
81  return map__.existsSecond(v);
82  }
83 
84  /// @}
85  // ============================================================================
86  /// @name Terminal Nodes value and id access
87  // ============================================================================
88  /// @{
89 
90  /// Returns the value of the terminal node that has the given id
91  const GUM_SCALAR& terminalNodeValue(const NodeId& n) const {
92  return map__.second(n);
93  }
94 
95  /// Returns the id of the terminal node that has the given value
96  const NodeId& terminalNodeId(const GUM_SCALAR& v) const {
97  return map__.first(v);
98  }
99 
100  /// @}
101  // ============================================================================
102  /// @name Iterator on Terminal Nodes
103  // ============================================================================
104  /// @{
105 
106  /// Initializes the constant safe iterator on terminal nodes
107  void beginValues() const { mappy__ = map__.beginSafe(); }
108 
109  /// Indicates if constant safe iterator has reach end of terminal nodes list
110  bool hasValue() const { return mappy__ != map__.endSafe(); }
111 
112  /// Increments the constant safe iterator
113  void nextValue() const { ++mappy__; }
114 
115  /// Returns the value of the current terminal nodes pointed by the constant
116  /// safe iterator
117  const GUM_SCALAR& value() const { return mappy__.second(); }
118 
119  /// Returns the id of the current terminal nodes pointed by the constant
120  /// safe iterator
121  const NodeId& id() const { return mappy__.first(); }
122 
123  /// @}
124 
125  private:
128  };
129 
130 } // End of namespace gum
131 
132 #endif /* GUM_MULTI_DIM_FUNCTION_GRAPH_EXACT_TERMINAL_NODE_POLICY_H */
void nextValue() const
Increments the constant safe iterator.
void beginValues() const
Initializes the constant safe iterator on terminal nodes.
void clearAllTerminalNodes()
Erase all terminal nodes.
Bijection< NodeId, GUM_SCALAR > map__
const NodeId & id() const
Returns the id of the current terminal nodes pointed by the constant safe iterator.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
void eraseTerminalNode(const NodeId &n)
Remove node matching given id.
BijectionIteratorSafe< NodeId, GUM_SCALAR > mappy__
const NodeId & terminalNodeId(const GUM_SCALAR &v) const
Returns the id of the terminal node that has the given value.
bool existsTerminalNodeWithValue(const GUM_SCALAR &v) const
Returns true if a terminal node matching this value exists.
void addTerminalNode(const NodeId &n, const GUM_SCALAR &v)
Insert a new terminal node with given value.
bool hasValue() const
Indicates if constant safe iterator has reach end of terminal nodes list.
const GUM_SCALAR & terminalNodeValue(const NodeId &n) const
Returns the value of the terminal node that has the given id.
bool existsTerminalNodeWithId(const NodeId &n) const
Returns true if a terminal node matching this id exists.
Implementation of a Terminal Node Policy that maps nodeid directly to value.
const GUM_SCALAR & value() const
Returns the value of the current terminal nodes pointed by the constant safe iterator.