aGrUM  0.14.2
arcGraphPart_inl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 // to ease parsing by IDE
29 
30 namespace gum {
31 
32  INLINE bool ArcGraphPart::emptyArcs() const { return __arcs.empty(); }
33 
34  INLINE Size ArcGraphPart::sizeArcs() const { return __arcs.size(); }
35 
36  INLINE const ArcSet& ArcGraphPart::arcs() const { return __arcs; }
37 
38  INLINE bool ArcGraphPart::existsArc(const Arc& arc) const {
39  return __arcs.contains(arc);
40  }
41 
42  INLINE bool ArcGraphPart::existsArc(const NodeId tail, const NodeId head) const {
43  return __parents.exists(head) && __parents[head]->exists(tail);
44  }
45 
46  INLINE void ArcGraphPart::__checkParents(const NodeId id) const {
47  if (!__parents.exists(id)) { __parents.insert(id, new NodeSet); }
48  }
49 
50  INLINE void ArcGraphPart::__checkChildren(const NodeId id) const {
51  if (!__children.exists(id)) { __children.insert(id, new NodeSet); }
52  }
53 
54  INLINE const NodeSet& ArcGraphPart::parents(const NodeId id) const {
55  __checkParents(id);
56  return *(__parents[id]);
57  }
58 
59  INLINE const NodeSet& ArcGraphPart::children(const NodeId id) const {
60  __checkChildren(id);
61  return *(__children[id]);
62  }
63 
64  INLINE void ArcGraphPart::addArc(const NodeId tail, const NodeId head) {
65  Arc arc(tail, head);
66 
67  __arcs.insert(arc);
68  __checkParents(head);
69  __checkChildren(tail);
70  __parents[head]->insert(tail);
71  __children[tail]->insert(head);
72 
73  GUM_EMIT2(onArcAdded, tail, head);
74  }
75 
76  INLINE void ArcGraphPart::eraseArc(const Arc& arc) {
77  // ASSUMING tail and head exists in __parents anf __children
78  // (if not, it is an error)
79  if (existsArc(arc)) {
80  NodeId tail = arc.tail(), head = arc.head();
81  __parents[head]->erase(tail);
82  __children[tail]->erase(head);
83  __arcs.erase(arc);
84  GUM_EMIT2(onArcDeleted, tail, head);
85  }
86  }
87 
88  INLINE void ArcGraphPart::_eraseSetOfArcs(const ArcSet& set) {
89  for (const auto arc : set)
90  eraseArc(arc);
91  }
92 
93  INLINE void ArcGraphPart::eraseParents(const NodeId id) {
94  if (__parents.exists(id)) {
95  NodeSet& parents = *(__parents[id]);
96 
97  for (auto iter = parents.beginSafe(); // safe iterator needed here
98  iter != parents.endSafe();
99  ++iter) {
100  // warning: use this erase so that you actually use the virtualized
101  // arc removal function
102  eraseArc(Arc(*iter, id));
103  }
104  }
105  }
106 
107  INLINE void ArcGraphPart::eraseChildren(const NodeId id) {
108  if (__children.exists(id)) {
109  NodeSet& children = *(__children[id]);
110 
111  for (auto iter = children.beginSafe(); // safe iterator needed here
112  iter != children.endSafe();
113  ++iter) {
114  // warning: use this erase so that you actually use the vritualized
115  // arc removal function
116  eraseArc(Arc(id, *iter));
117  }
118  }
119  }
120 
122  for (const auto& arc : set)
124  }
125 
127  if (__parents.exists(id)) {
128  NodeSet& parents = *(__parents[id]);
129 
130  for (auto iter = parents.beginSafe(); // safe iterator needed here
131  iter != parents.endSafe();
132  ++iter) {
133  ArcGraphPart::eraseArc(Arc(*iter, id));
134  }
135  }
136  }
137 
139  if (__children.exists(id)) {
140  NodeSet& children = *(__children[id]);
141 
142  for (auto iter = children.beginSafe(); // safe iterator needed here
143  iter != children.endSafe();
144  ++iter) {
145  ArcGraphPart::eraseArc(Arc(id, *iter));
146  }
147  }
148  }
149 
150  INLINE bool ArcGraphPart::operator==(const ArcGraphPart& p) const {
151  return __arcs == p.__arcs;
152  }
153 
154  INLINE bool ArcGraphPart::operator!=(const ArcGraphPart& p) const {
155  return __arcs != p.__arcs;
156  }
157 
158 } /* namespace gum */
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:578
void __checkParents(const NodeId id) const
when the ArcGraphPart contains no arc ingoing into a given node, this function adds an empty set entr...
virtual void addArc(const NodeId tail, const NodeId head)
insert a new arc into the ArcGraphPart
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition: set_tpl.h:704
Classes for directed edge sets.
Definition: arcGraphPart.h:76
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
void erase(const Key &k)
Erases an element from the set.
Definition: set_tpl.h:653
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
NodeId head() const
returns the head of the arc
void __checkChildren(const NodeId id) const
when the ArcGraphPart contains no arc outgoing from a given node, this function adds an empty set ent...
const iterator_safe & endSafe() const noexcept
The usual safe end iterator to parse the set.
Definition: set_tpl.h:499
void _eraseSetOfArcs(const ArcSet &set)
a (virtualized) function to remove a given set of arcs
bool emptyArcs() const
indicates wether the ArcGraphPart contains any arc
bool operator!=(const ArcGraphPart &p) const
tests whether two ArcGraphParts contain different arcs
#define GUM_EMIT2(signal, arg1, arg2)
Definition: signaler2.h:40
Size sizeArcs() const
indicates the number of arcs stored within the ArcGraphPart
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
bool operator==(const ArcGraphPart &p) const
tests whether two ArcGraphParts contain the same arcs
void _unvirtualizedEraseSetOfArcs(const ArcSet &set)
similar to _eraseSetOfArcs except that it is unvirtualized
void eraseChildren(const NodeId id)
removes all the children of a given node
NodeProperty< NodeSet *> __parents
for each arc, the sets of its parents
Definition: arcGraphPart.h:269
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
Signaler2< NodeId, NodeId > onArcAdded
Definition: arcGraphPart.h:80
const ArcSet & arcs() const
returns the set of arcs stored within the ArcGraphPart
NodeProperty< NodeSet *> __children
for each arc, the set of its children
Definition: arcGraphPart.h:272
iterator_safe beginSafe() const
The usual safe begin iterator to parse the set.
Definition: set_tpl.h:485
void unvirtualizedEraseChildren(const NodeId id)
same function as eraseChildren but without any virtual call to an erase
void unvirtualizedEraseParents(const NodeId id)
same function as eraseParents but without any virtual call to an erase
Signaler2< NodeId, NodeId > onArcDeleted
Definition: arcGraphPart.h:81
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:698
Set< Arc > __arcs
the set of all the arcs contained within the ArcGraphPart
Definition: arcGraphPart.h:266
bool existsArc(const Arc &arc) const
indicates whether a given arc exists
Size NodeId
Type for node ids.
Definition: graphElements.h:97
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:610
NodeId tail() const
returns the tail of the arc
void eraseParents(const NodeId id)
erase all the parents of a given node