aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
ExactTerminalNodePolicy.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 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) { _map_.insert(n, v); }
59 
60  /// Remove node matching given id
61  void eraseTerminalNode(const NodeId& n) { _map_.eraseFirst(n); }
62 
63  /// Erase all terminal nodes
64  void clearAllTerminalNodes() { _map_.clear(); }
65 
66  /// @}
67  // ============================================================================
68  /// @name Terminal Nodes Existence
69  // ============================================================================
70  /// @{
71 
72  /// Returns true if a terminal node matching this id exists
73  bool existsTerminalNodeWithId(const NodeId& n) const { return _map_.existsFirst(n); }
74 
75  /// Returns true if a terminal node matching this value exists
76  bool existsTerminalNodeWithValue(const GUM_SCALAR& v) const { return _map_.existsSecond(v); }
77 
78  /// @}
79  // ============================================================================
80  /// @name Terminal Nodes value and id access
81  // ============================================================================
82  /// @{
83 
84  /// Returns the value of the terminal node that has the given id
85  const GUM_SCALAR& terminalNodeValue(const NodeId& n) const { return _map_.second(n); }
86 
87  /// Returns the id of the terminal node that has the given value
88  const NodeId& terminalNodeId(const GUM_SCALAR& v) const { return _map_.first(v); }
89 
90  /// @}
91  // ============================================================================
92  /// @name Iterator on Terminal Nodes
93  // ============================================================================
94  /// @{
95 
96  /// Initializes the constant safe iterator on terminal nodes
97  void beginValues() const { _mappy_ = _map_.beginSafe(); }
98 
99  /// Indicates if constant safe iterator has reach end of terminal nodes list
100  bool hasValue() const { return _mappy_ != _map_.endSafe(); }
101 
102  /// Increments the constant safe iterator
103  void nextValue() const { ++_mappy_; }
104 
105  /// Returns the value of the current terminal nodes pointed by the constant
106  /// safe iterator
107  const GUM_SCALAR& value() const { return _mappy_.second(); }
108 
109  /// Returns the id of the current terminal nodes pointed by the constant
110  /// safe iterator
111  const NodeId& id() const { return _mappy_.first(); }
112 
113  /// @}
114 
115  private:
118  };
119 
120 } // End of namespace gum
121 
122 #endif /* GUM_MULTI_DIM_FUNCTION_GRAPH_EXACT_TERMINAL_NODE_POLICY_H */
Bijection< NodeId, GUM_SCALAR > _map_
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.
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:643
void eraseTerminalNode(const NodeId &n)
Remove node matching given id.
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.
BijectionIteratorSafe< NodeId, GUM_SCALAR > _mappy_
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.