aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
internalNode.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 InternalNode class.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
27  * @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
28  * GONZALES(@AMU)
29  */
30 #ifndef GUM_MULTI_DIM_FUNCTION_GRAPH_INTERNAL_NODE_H
31 #define GUM_MULTI_DIM_FUNCTION_GRAPH_INTERNAL_NODE_H
32 
33 #include <agrum/agrum.h>
34 #include <agrum/tools/core/smallobjectallocator/smallObjectAllocator.h>
35 #include <agrum/tools/graphs/graphElements.h>
36 #include <agrum/tools/multidim/utils/FunctionGraphUtilities/link.h>
37 #include <agrum/tools/variables/discreteVariable.h>
38 
39 namespace gum {
40 
41  // clang-format off
42  /**
43  * @class Parent
44  * @headerfile internalNode.h <agrum/tools/multidim/FunctionGraphUtilities/internalNode.h>
45  * @ingroup multidim_group
46  *
47  * @brief Represent a node's parent.
48  */
49  // clang-format on
50  struct Parent {
53 
54  /**
55  * @brief Class constructor.
56  */
57  Parent(NodeId pi, Idx m);
58 
59  /**
60  * @brief Copy constructor.
61  */
62  Parent(const Parent& src);
63 
64  // ============================================================================
65  /// @name Allocators and Deallocators redefinition
66  // ============================================================================
67  /// @{
68 
69  /**
70  * @brief New operator.
71  */
72  void* operator new(size_t s);
73 
74  /**
75  * @brief Delete operator.
76  */
77  void operator delete(void* p);
78 
79  /// @}
80  // ============================================================================
81  /// @name Operators
82  // ============================================================================
83  /// @{
84 
85  bool operator==(const Parent& comp) const;
86 
87  bool operator!=(const Parent& comp) const;
88 
89  Parent& operator=(const Parent& src);
90  /// @}
91  };
92 
93  // clang-format off
94  /**
95  * @class InternalNode
96  * @headerfile internalNode.h <agrum/tools/multidim/FunctionGraphUtilities/internalNode.h>
97  * @ingroup multidim_group
98  *
99  * @brief Structure used to represent a node internal structure
100  */
101  // clang-format on
102  class InternalNode {
103  private:
104  /// Variable associated to such node
106 
107  /**
108  * @brief Table of sons of the node.
109  *
110  * Each son is bound to a modality of the variable.
111  * So those modalities are used has indexes for that table.
112  *
113  * @code
114  * _____________________________
115  * | | | | |
116  * | son1 | son2 | son3 | son4 |
117  * | ______| ______| ______| ______|
118  * x1 x2 x3 x4
119  *
120  * @endcode
121  */
123 
124  /// The list of the node's parent
126 
127  /// @brief Set the node variable.
128  void _setNodeVar_(const DiscreteVariable* v);
129 
130  public:
131  // ============================================================================
132  /// @name Constructors and Destructors
133  // ============================================================================
134  /// @{
135 
136  /**
137  * @brief Default Constructor
138  * Creates an empty node with no variable attached.
139  */
140  InternalNode();
141 
142  /**
143  * @brief Class constructor
144  *
145  * Creates a node and attached the specified variable.
146  * Initializes the sons.
147  */
148  explicit InternalNode(const DiscreteVariable* v);
149 
150  /**
151  * @brief Constructor
152  *
153  * Creates a node and attached the specified variable.
154  * Also attached the given on map (and will handle it by itself especially
155  * deallocate it)
156  *
157  * @warning You'd better known what you're doing if you're using this one.
158  * sons must have the size of domainSize of v or the program will fail!
159  */
160  InternalNode(const DiscreteVariable* v, NodeId* sons);
161 
162  /**
163  * @brief Class destructors.
164  */
165  ~InternalNode();
166  /// @}
167  // ============================================================================
168  /// @name Allocators and Deallocators redefinition
169  // ============================================================================
170  /// @{
171 
172  /**
173  * @brief New operator.
174  */
175  void* operator new(size_t s);
176 
177  /**
178  * @brief Delete operator.
179  */
180  void operator delete(void* p);
181 
182  /// @}
183  // ============================================================================
184  /// @name Node handlers
185  // ============================================================================
186  /// @{
187 
188  /**
189  * @brief Allows you to respecify the node, changing its attached variable
190  * as well as its son map
191  *
192  * @warning You'd better known what you're doing if you're using this one.
193  * sons must have the size of domainSize of v or the program will fail!
194  */
195  void setNode(const DiscreteVariable* v, NodeId* sons);
196 
197  /// @}
198  // ============================================================================
199  /// @name Var handlers
200  // ============================================================================
201  /// @{
202 
203  /**
204  * @brief Set the node variable.
205  */
206  void setNodeVar(const DiscreteVariable* v);
207 
208  /**
209  * @brief Returns the node variable.
210  */
211  const DiscreteVariable* nodeVar() const;
212 
213  /// @}
214  // ============================================================================
215  /// @name Sons handlers
216  // ============================================================================
217  /// @{
218 
219  /**
220  * @brief Sets the node's son.
221  */
222  void setSon(Idx modality, NodeId son);
223 
224  /**
225  * @brief Returns the son at a given index.
226  */
227  NodeId son(Idx modality) const;
228 
229  /**
230  * @brief Returns the number of sons.
231  */
232  Idx nbSons() const;
233 
234  /// @}
235  // ============================================================================
236  /// @name Parent handlers
237  // ============================================================================
238  /// @{
239 
240  /**
241  * @brief Adds a parent.
242  */
243  void addParent(NodeId parent, Idx modality);
244 
245  /**
246  * @brief Removes a parent.
247  */
248  void removeParent(NodeId parent, Idx modality);
249 
250  /**
251  * @brief Returns the list of parents.
252  */
253  Link< Parent >* parents();
254 
255  /**
256  * @brief Returns the list of parents.
257  */
258  const Link< Parent >* parents() const;
259 
260  /// @}
261  // ============================================================================
262  /// @name Allocator/deallocator
263  // ============================================================================
264  /// @{
265 
266  /**
267  * @brief Allocates a table of nodeid of the size given in parameter.
268  */
269  static NodeId* allocateNodeSons(const DiscreteVariable* v);
270 
271  /**
272  * @brief Deallocates a NodeSons table.
273  */
274  static void deallocateNodeSons(const DiscreteVariable* v, NodeId* s);
275 
276  /// @}
277  };
278 
279 } // End of namespace gum
280 
281 #ifndef GUM_NO_INLINE
282 # include <agrum/tools/multidim/utils/FunctionGraphUtilities/internalNode_inl.h>
283 #endif /* GUM_NO_INLINE */
284 
285 #endif /* GUM_MULTI_DIM_FUNCTION_GRAPH_INTERNAL_NODE_H */
void addParent(NodeId parent, Idx modality)
Adds a parent.
void setNode(const DiscreteVariable *v, NodeId *sons)
Allows you to respecify the node, changing its attached variable as well as its son map...
bool operator==(const Parent &comp) const
LinkedList< Parent > _nodeParents_
The list of the node&#39;s parent.
Definition: internalNode.h:125
NodeId parentId
Definition: internalNode.h:51
void removeParent(NodeId parent, Idx modality)
Removes a parent.
const DiscreteVariable * nodeVar() const
Returns the node variable.
Idx nbSons() const
Returns the number of sons.
void * operator new(size_t s)
New operator.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
static NodeId * allocateNodeSons(const DiscreteVariable *v)
Allocates a table of nodeid of the size given in parameter.
const DiscreteVariable * _nodeVar_
Variable associated to such node.
Definition: internalNode.h:105
void setSon(Idx modality, NodeId son)
Sets the node&#39;s son.
NodeId son(Idx modality) const
Returns the son at a given index.
~InternalNode()
Class destructors.
Link< Parent > * parents()
Returns the list of parents.
static void deallocateNodeSons(const DiscreteVariable *v, NodeId *s)
Deallocates a NodeSons table.
void setNodeVar(const DiscreteVariable *v)
Set the node variable.
NodeId * _nodeSons_
Table of sons of the node.
Definition: internalNode.h:122
bool operator!=(const Parent &comp) const
const Link< Parent > * parents() const
Returns the list of parents.
Parent & operator=(const Parent &src)
InternalNode(const DiscreteVariable *v, NodeId *sons)
Constructor.
void * operator new(size_t s)
New operator.
Parent(const Parent &src)
Copy constructor.
Structure used to represent a node internal structure.
Definition: internalNode.h:102
void operator delete(void *p)
Delete operator.
Parent(NodeId pi, Idx m)
Class constructor.
void operator delete(void *p)
Delete operator.
Represent a node&#39;s parent.
Definition: internalNode.h:50
void _setNodeVar_(const DiscreteVariable *v)
Set the node variable.
InternalNode(const DiscreteVariable *v)
Class constructor.
InternalNode()
Default Constructor Creates an empty node with no variable attached.