aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
DAGCycleDetector_inl.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 /** @file
23  * @brief A class for detecting directed cycles in DAGs when trying to apply
24  * many changes to the graph
25  *
26  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 namespace gum {
30 
31  /* ===========================================================================
32  */
33  // CHANGES
34  /* ===========================================================================
35  */
36 
37  // default constructor
41  }
42 
43  // copy constructor
47  }
48 
49  // move constructor
53  }
54 
55  // destructor
57 
58  // copy operator
61  _type_ = from._type_;
62  _tail_ = from._tail_;
63  _head_ = from._head_;
64  return *this;
65  }
66 
67  // move operator
70  _type_ = from._type_;
71  _tail_ = from._tail_;
72  _head_ = from._head_;
73  return *this;
74  }
75 
76  /// returns the type of the operation
78  return _type_;
79  }
80 
81  /// indicates the tail of the arc involved in the modification
82  INLINE NodeId DAGCycleDetector::Change::tail() const noexcept { return _tail_; }
83 
84  /// indicates the head of the arc involved in the modification
85  INLINE NodeId DAGCycleDetector::Change::head() const noexcept { return _head_; }
86 
87  /* ===========================================================================
88  */
89  // ArcAdd
90  /* ===========================================================================
91  */
92 
93  /// default constructor
97  }
98 
99  /// copy constructor
103  }
104 
105  /// move constructor
108  std::move(from.tail()),
109  std::move(from.head())) {
111  }
112 
113  /// destructor
115 
116  /// copy operator
120  return *this;
121  }
122 
123  /// move operator
127  return *this;
128  }
129 
130  /* ===========================================================================
131  */
132  // ArcDel
133  /* ===========================================================================
134  */
135 
136  /// default constructor
140  }
141 
142  /// copy constructor
146  }
147 
148  /// move constructor
151  std::move(from.tail()),
152  std::move(from.head())) {
154  }
155 
156  /// destructor
158 
159  /// copy operator
163  return *this;
164  }
165 
166  /// move operator
170  return *this;
171  }
172 
173  /* ===========================================================================
174  */
175  // ArcReverse
176  /* ===========================================================================
177  */
178 
179  /// default constructor
183  }
184 
185  /// copy constructor
187  :
190  }
191 
192  /// move constructor
195  std::move(from.tail()),
196  std::move(from.head())) {
198  }
199 
200  /// destructor
203  }
204 
205  /// copy operator
209  return *this;
210  }
211 
212  /// move operator
216  return *this;
217  }
218 
219  /* ===========================================================================
220  */
221  // DAGCycleDetector
222  /* ===========================================================================
223  */
224 
225  /// default constructor
227 
228  /// copy constructor
232  }
233 
234  /// move constructor
239  }
240 
241  /// destructor
243 
244  /// copy operator
245  INLINE
247  if (this != &from) {
248  _dag_ = from._dag_;
251  }
252 
253  return *this;
254  }
255 
256  /// move operator
258  if (this != &from) {
259  _dag_ = std::move(from._dag_);
262  }
263 
264  return *this;
265  }
266 
267  /// indicates whether an arc addition would create a cycle
269  return _descendants_[y].exists(x);
270  }
271 
272  /// indicates wether an arc reversal would create a cycle
274  return (_ancestors_[y][x] > 1);
275  }
276 
277  /// indicates whether an arc deletion would create a cycle
279  return false;
280  }
281 
282  /// adds a nodeset to another (nodes are weighted, so weights are added)
283  INLINE
285  const NodeProperty< Size >& set_to_add,
286  Size multiplier) const {
287  for (auto iter = set_to_add.cbegin(); iter != set_to_add.cend(); ++iter) {
288  if (nodeset.exists(iter.key())) {
289  nodeset[iter.key()] += iter.val() * multiplier;
290  } else {
292  }
293  }
294  }
295 
296  /// removes a weighted nodeset from another (weights are subtracted)
297  INLINE
299  const NodeProperty< Size >& set_to_del,
300  Size multiplier) const {
301  for (auto iter = set_to_del.cbegin(); iter != set_to_del.cend(); ++iter) {
302  if (nodeset.exists(iter.key())) {
303  Size& weight = nodeset[iter.key()];
304  weight -= iter.val() * multiplier;
305 
306  if (!weight) { nodeset.erase(iter.key()); }
307  }
308  }
309  }
310 
311  /** @brief put into a weighted nodeset the nodes of another weighted set that
312  * belong to a set of arc extremities */
313  INLINE
316  const NodeSet& extremities) const {
317  for (auto iter = set_to_restrict.cbegin(); iter != set_to_restrict.cend(); ++iter) {
319  }
320  }
321 
322  /// reverses an arc from the DAG
325  GUM_ERROR(InvalidDirectedCycle, "the arc would create a directed into a DAG")
326  }
327 
328  eraseArc(tail, head);
329  addArc(head, tail);
330  }
331 
332  /// check the equality between two DAGCycleDetectors
334  return ( //( _dagmodel_ == from. _dagmodel_ ) &&
336  }
337 
338  /// check the inequality between two DAGCycleDetectors
340  return !operator==(from);
341  }
342 
343 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643