aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
graphChange.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
51#ifndef GUM_LEARNING_GRAPH_CHANGE_H
52#define GUM_LEARNING_GRAPH_CHANGE_H
53
54#include <cstdint>
55#include <cstring>
56#include <iostream>
57#include <string>
58
59#include <agrum/agrum.h>
60
62
63namespace gum {
64
65 namespace learning {
66
68 // it is important that the size of this type be a multiple of 32 bits.
69 // This is exploited to both reduce the memory consumption and speed-up
70 // copying the fields of GraphChange
71 using LearnNodeId = uint32_t;
72
83
84 // forward the declarations of the Graph changes classes. This will be
85 // useful for specifying HashFunc friendships in Class GraphChange
86 class ArcAddition;
87 class ArcDeletion;
88 class ArcReversal;
89 class ArcTriangleDeletion1;
90 class ArcTriangleDeletion2;
91 class EdgeAddition;
92 class EdgeDeletion;
93
94 /* ========================================================================= */
95 /* === GRAPH CHANGE CLASS === */
96 /* ========================================================================= */
102 // ##########################################################################
104 // ##########################################################################
106
107 protected:
110
111 public:
113 GraphChange(const GraphChange& from) noexcept;
114
116 GraphChange(GraphChange&& from) noexcept;
117
119 ~GraphChange() noexcept;
120
122
123 // ##########################################################################
125 // ##########################################################################
127
129 GraphChange& operator=(const GraphChange& from) noexcept;
130
132 GraphChange& operator=(GraphChange&& from) noexcept;
133
135 bool operator==(const GraphChange& from) const noexcept;
136
138 bool operator!=(const GraphChange& from) const noexcept;
139
141
142 // ##########################################################################
144 // ##########################################################################
146
148 GraphChangeType type() const noexcept;
149
151 std::string typeAsString() const;
152
154 NodeId node1() const noexcept;
155
157 NodeId node2() const noexcept;
158
160
162 NodeId node3() const;
163
165 // This method has not been declared as virtual in order to reduce the
166 // memory consumption. The drawback is that it is slower than what we
167 // would get with a virtual method. But this method should essentially
168 // be used for debugging purposes, hence this is not really an issue.
169 std::string toString() const;
170
172
173 protected:
176
178 // @warning this field must follow _nodes_. This fact is exploited to
179 // speed up copying Class GraphChange fields: as LearnNodeId is 32-bit
180 // long, _type is aligned as 32 bits, hence, it directly follows _nodes
181 // without any padding. Overall, _nodes + _type should be 16-byte long.
183
184
185 // friendships allow accessing _nodes. This will speed up computations
186 // when sizeof(Size) == 2 * sizeof(LearnNodeId)
187 friend class HashFunc< GraphChange >;
188 friend class HashFunc< ArcAddition >;
189 friend class HashFunc< ArcDeletion >;
190 friend class HashFunc< ArcReversal >;
195 };
196
197 /* ========================================================================= */
198 /* === ARC ADDITION CLASS === */
199 /* ========================================================================= */
209 class ArcAddition: public GraphChange {
210 public:
211 // ##########################################################################
213 // ##########################################################################
215
218
220 ArcAddition(const ArcAddition& from) noexcept;
221
223 ArcAddition(ArcAddition&& from) noexcept;
224
226 ~ArcAddition() noexcept;
227
229
230 // ##########################################################################
232 // ##########################################################################
234
236 ArcAddition& operator=(const ArcAddition& from) noexcept;
237
239 ArcAddition& operator=(ArcAddition&& from) noexcept;
240
242 bool operator==(const ArcAddition& from) const noexcept;
243
245 bool operator!=(const ArcAddition& from) const noexcept;
246
248 };
249
250 /* =========================================================================*/
251 /* === ARC DELETION CLASS ===*/
252 /* =========================================================================*/
262 class ArcDeletion: public GraphChange {
263 public:
264 // ##########################################################################
266 // ##########################################################################
268
271
273 ArcDeletion(const ArcDeletion& from) noexcept;
274
276 ArcDeletion(ArcDeletion&& from) noexcept;
277
279 ~ArcDeletion() noexcept;
280
282
283 // ##########################################################################
285 // ##########################################################################
287
289 ArcDeletion& operator=(const ArcDeletion& from) noexcept;
290
292 ArcDeletion& operator=(ArcDeletion&& from) noexcept;
293
295 bool operator==(const ArcDeletion& from) const noexcept;
296
298 bool operator!=(const ArcDeletion& from) const noexcept;
299
301 };
302
303 /* ========================================================================= */
304 /* === ARC REVERSAL CLASS === */
305 /* ========================================================================= */
315 class ArcReversal: public GraphChange {
316 public:
317 // ##########################################################################
319 // ##########################################################################
321
324
326 ArcReversal(const ArcReversal& from) noexcept;
327
329 ArcReversal(ArcReversal&& from) noexcept;
330
332 ~ArcReversal() noexcept;
333
335
336 // ##########################################################################
338 // ##########################################################################
340
342 ArcReversal& operator=(const ArcReversal& from) noexcept;
343
345 ArcReversal& operator=(ArcReversal&& from) noexcept;
346
348 bool operator==(const ArcReversal& from) const noexcept;
349
351 bool operator!=(const ArcReversal& from) const noexcept;
352
354 };
355
356 /* ========================================================================= */
357 /* === ARC TRIANGLE DELETION 1ST CLASS === */
358 /* ========================================================================= */
370 public:
371 // ##########################################################################
373 // ##########################################################################
375
377
381
384
387
390
392
393 // ##########################################################################
395 // ##########################################################################
397
399 ArcTriangleDeletion1& operator=(const ArcTriangleDeletion1& from) noexcept;
400
402 ArcTriangleDeletion1& operator=(ArcTriangleDeletion1&& from) noexcept;
403
405
408 bool operator==(const ArcTriangleDeletion1& from) const noexcept;
409
411
414 bool operator!=(const ArcTriangleDeletion1& from) const noexcept;
415
417
418 // ##########################################################################
420 // ##########################################################################
422
424
426 NodeId node3() const;
427
429 };
430
431 /* ========================================================================= */
432 /* === ARC TRIANGLE DELETION 2ND CLASS === */
433 /* ========================================================================= */
445 public:
446 // ##########################################################################
448 // ##########################################################################
450
452
456
459
462
465
467
468 // ##########################################################################
470 // ##########################################################################
472
474 ArcTriangleDeletion2& operator=(const ArcTriangleDeletion2& from) noexcept;
475
477 ArcTriangleDeletion2& operator=(ArcTriangleDeletion2&& from) noexcept;
478
480
483 bool operator==(const ArcTriangleDeletion2& from) const noexcept;
484
486
489 bool operator!=(const ArcTriangleDeletion2& from) const noexcept;
490
492
493 // ##########################################################################
495 // ##########################################################################
497
499
501 NodeId node3() const;
502
504 };
505
506 /* =========================================================================*/
507 /* === EDGE ADDITION CLASS ===*/
508 /* =========================================================================*/
518 class EdgeAddition: public GraphChange {
519 public:
520 // ##########################################################################
522 // ##########################################################################
524
527
529 EdgeAddition(const EdgeAddition& from) noexcept;
530
532 EdgeAddition(EdgeAddition&& from) noexcept;
533
535 ~EdgeAddition() noexcept;
536
538
539 // ##########################################################################
541 // ##########################################################################
543
545 EdgeAddition& operator=(const EdgeAddition& from) noexcept;
546
548 EdgeAddition& operator=(EdgeAddition&& from) noexcept;
549
551 bool operator==(const EdgeAddition& from) const noexcept;
552
554 bool operator!=(const EdgeAddition& from) const noexcept;
555
557 };
558
559 /* ========================================================================= */
560 /* === EDGE DELETION CLASS === */
561 /* ========================================================================= */
571 class EdgeDeletion: public GraphChange {
572 public:
573 // ##########################################################################
575 // ##########################################################################
577
580
582 EdgeDeletion(const EdgeDeletion& from) noexcept;
583
585 EdgeDeletion(EdgeDeletion&& from) noexcept;
586
588 ~EdgeDeletion() noexcept;
589
591
592 // ##########################################################################
594 // ##########################################################################
596
598 EdgeDeletion& operator=(const EdgeDeletion& from) noexcept;
599
601 EdgeDeletion& operator=(EdgeDeletion&& from) noexcept;
602
604 bool operator==(const EdgeDeletion& from) const noexcept;
605
607 bool operator!=(const EdgeDeletion& from) const noexcept;
608
610 };
611
613 std::ostream& operator<<(std::ostream& stream, const GraphChange& change);
614
615 } /* namespace learning */
616
618 template <>
620 public:
627
629 Size operator()(const learning::GraphChange& key) const final;
630 };
631
633 template <>
634 class HashFunc< learning::ArcAddition >: public HashFuncBase< learning::ArcAddition > {
635 public:
642
644 Size operator()(const learning::ArcAddition& key) const final;
645 };
646
648 template <>
649 class HashFunc< learning::ArcDeletion >: public HashFuncBase< learning::ArcDeletion > {
650 public:
657
659 Size operator()(const learning::ArcDeletion& key) const final;
660 };
661
663 template <>
664 class HashFunc< learning::ArcReversal >: public HashFuncBase< learning::ArcReversal > {
665 public:
672
674 Size operator()(const learning::ArcReversal& key) const final;
675 };
676
678 template <>
679 class HashFunc< learning::ArcTriangleDeletion1 >:
680 public HashFuncBase< learning::ArcTriangleDeletion1 > {
681 public:
688
691 };
692
694 template <>
695 class HashFunc< learning::ArcTriangleDeletion2 >:
696 public HashFuncBase< learning::ArcTriangleDeletion2 > {
697 public:
704
707 };
708
710 template <>
711 class HashFunc< learning::EdgeAddition >: public HashFuncBase< learning::EdgeAddition > {
718
720 Size operator()(const learning::EdgeAddition& key) const final;
721 };
722
724 template <>
725 class HashFunc< learning::EdgeDeletion >: public HashFuncBase< learning::EdgeDeletion > {
726 public:
733
735 Size operator()(const learning::EdgeDeletion& key) const final;
736 };
737
738} /* namespace gum */
739
740#ifndef GUM_NO_INLINE
742#endif // GUM_NOINLINE
743
744#endif /* GUM_LEARNING_GRAPH_CHANGE_H */
All hash functions should inherit from this class.
Definition hashFunc.h:174
static Size castToSize(const learning::ArcAddition &key)
Returns the value of a key as a Size.
Size operator()(const learning::ArcAddition &key) const final
computes the hashed value of a key
Size operator()(const learning::ArcDeletion &key) const final
computes the hashed value of a key
static Size castToSize(const learning::ArcDeletion &key)
Returns the value of a key as a Size.
Size operator()(const learning::ArcReversal &key) const final
computes the hashed value of a key
static Size castToSize(const learning::ArcReversal &key)
Returns the value of a key as a Size.
static Size castToSize(const learning::ArcTriangleDeletion1 &key)
Returns the value of a key as a Size.
Size operator()(const learning::ArcTriangleDeletion1 &key) const final
computes the hashed value of a key
static Size castToSize(const learning::ArcTriangleDeletion2 &key)
Returns the value of a key as a Size.
Size operator()(const learning::ArcTriangleDeletion2 &key) const final
computes the hashed value of a key
Size operator()(const learning::EdgeAddition &key) const final
computes the hashed value of a key
static Size castToSize(const learning::EdgeAddition &key)
Returns the value of a key as a Size.
static Size castToSize(const learning::EdgeDeletion &key)
Returns the value of a key as a Size.
Size operator()(const learning::EdgeDeletion &key) const final
computes the hashed value of a key
static Size castToSize(const learning::GraphChange &key)
Returns the value of a key as a Size.
Size operator()(const learning::GraphChange &key) const final
computes the hashed value of a key
This class should be useless as only its specializations should be used.
Definition hashFunc.h:492
The class for notifying learning algorithms of new arc additions.
ArcAddition(NodeId node1, NodeId node2) noexcept
default constructor
ArcAddition(const ArcAddition &from) noexcept
copy constructor
~ArcAddition() noexcept
destructor
ArcAddition(ArcAddition &&from) noexcept
move constructor
The class for notifying learning algorithms of arc removals.
ArcDeletion(const ArcDeletion &from) noexcept
copy constructor
ArcDeletion(ArcDeletion &&from) noexcept
move constructor
ArcDeletion(NodeId node1, NodeId node2) noexcept
default constructor
~ArcDeletion() noexcept
destructor
The class for notifying learning algorithms of arc reversals.
ArcReversal(const ArcReversal &from) noexcept
copy constructor
ArcReversal(NodeId node1, NodeId node2) noexcept
default constructor
~ArcReversal() noexcept
destructor
ArcReversal(ArcReversal &&from) noexcept
move constructor
The graph change substituting a triangle node1->node2->node3 + node1->node3 into v-structure node2->n...
ArcTriangleDeletion1(NodeId node1, NodeId node2, NodeId node3) noexcept
default constructor
ArcTriangleDeletion1(const ArcTriangleDeletion1 &from) noexcept
copy constructor
NodeId node3() const
returns the third node involved in the modification (if any)
ArcTriangleDeletion1(ArcTriangleDeletion1 &&from) noexcept
move constructor
~ArcTriangleDeletion1() noexcept
destructor
The graph change substituting a triangle node1->node2->node3 + node1->node3 into v-structure node1->n...
~ArcTriangleDeletion2() noexcept
destructor
NodeId node3() const
returns the third node involved in the modification (if any)
ArcTriangleDeletion2(ArcTriangleDeletion2 &&from) noexcept
move constructor
ArcTriangleDeletion2(NodeId node1, NodeId node2, NodeId node3) noexcept
default constructor
ArcTriangleDeletion2(const ArcTriangleDeletion2 &from) noexcept
copy constructor
The class for notifying learning algorithms of new edge additions.
EdgeAddition(EdgeAddition &&from) noexcept
move constructor
EdgeAddition(NodeId node1, NodeId node2) noexcept
default constructor
EdgeAddition(const EdgeAddition &from) noexcept
copy constructor
~EdgeAddition() noexcept
destructor
The class for notifying learning algorithms of edge removals.
EdgeDeletion(NodeId node1, NodeId node2) noexcept
default constructor
~EdgeDeletion() noexcept
destructor
EdgeDeletion(EdgeDeletion &&from) noexcept
move constructor
EdgeDeletion(const EdgeDeletion &from) noexcept
copy constructor
GraphChange(GraphChangeType type, NodeId node1, NodeId node2, NodeId node3=0) noexcept
default constructor
std::string typeAsString() const
returns a string corresponding to the type of the change
GraphChangeType type_
the type of modification
NodeId node1() const noexcept
returns the first node involved in the modification
GraphChangeType type() const noexcept
returns the type of the operation
LearnNodeId nodes_[3]
the nodes involved in the edge or arc to be modified
NodeId node2() const noexcept
returns the second node involved in the modification
NodeId node3() const
returns the third node involved in the modification (if any)
std::string toString() const
put the content of the graph change into a string
A class to account for changes in a graph.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size NodeId
Type for node ids.
include the inlined functions if necessary
Definition CSVParser.h:55
GraphChangeType
the type of modification that can be applied to the graph
Definition graphChange.h:74
uint32_t LearnNodeId
the internal type of the nodes involved in the arc/edge modifications
Definition graphChange.h:71
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.
Base node set class for graphs.