aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
arcGraphPart.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 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 #ifndef GUM_ARC_GRAPH_PART_H
23 #define GUM_ARC_GRAPH_PART_H
24 
25 #include <algorithm>
26 #include <utility>
27 
28 #include <agrum/agrum.h>
29 
30 #include <agrum/tools/core/signal/signaler.h>
31 #include <agrum/tools/graphs/graphElements.h>
32 
33 namespace gum {
34 
35  /** @class ArcGraphPart
36  * @brief Classes for directed edge sets
37  *
38  * \ingroup graph_group
39  *
40  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
41  *
42  * @par Usage example:
43  * @code
44  * ArcGraphPart arcs1,arcs2,arcs3;
45  *
46  * // insert elements into arcs1
47  * arcs1.addArc( 2,3 );
48  * arcs1.addArc( 5,3 );
49  *
50  * // copy arcs1 into arcs2
51  * arcs2=arcs1;
52  *
53  * // remove some elements from arcs1
54  * arcs1.eraseArc( Arc( 5,3 ) );
55  * arcs1.eraseArc( arc );
56  *
57  * if ( arcs1.empty() ) std::cerr<<" arcs1 is empty"<<std::endl;
58  *
59  * // checks whether a given arc exists
60  * if ( arcs2.existArc( arc ) )
61  * cerr << "set contains " << arc << endl;
62  *
63  * if ( arcs2.existArc( 5,3 ) )
64  * cerr << "set contains " << arc << endl;
65  *
66  * std::cerr<<arcs2.toString()<<std::endl;
67  *
68  * std::cerr<<arcs2.parents( 3 )<<std::endl;
69  *
70  * std::cerr<<arcs2.children( 2 )<<std::endl;
71  *
72  * std::cerr<<std::endl<<std::endl;
73  *
74  * std::cerr<<std::endl<<std::endl;
75  * @endcode
76  */
77 
78  class ArcGraphPart {
79  public:
81 
82  Signaler2< NodeId, NodeId > onArcAdded; // onArcAdded(tail,head)
83  Signaler2< NodeId, NodeId > onArcDeleted; // onArcDeleted(tail,head)
84 
85  // ############################################################################
86  /// @name Constructors / Destructors
87  // ############################################################################
88  /// @{
89 
90  /// default constructor
91  /** @param arcs_size the size of the hash table used to store all the arcs
92  * @param arcs_resize_policy the resizing policy of this hash table*/
93  explicit ArcGraphPart(Size arcs_size = HashTableConst::default_size,
94  bool arcs_resize_policy = true);
95 
96  /// copy constructor
97  /** @param s the ArcGraphPart to copy */
98  ArcGraphPart(const ArcGraphPart& s);
99 
100  /// destructor
101  virtual ~ArcGraphPart();
102 
103  /// @}
104 
105  // ############################################################################
106  /// @name Operators
107  // ############################################################################
108  /// @{
109 
110  /// copy operator
111  /** @param s the ArcGraphPart to copy */
112  ArcGraphPart& operator=(const ArcGraphPart& s);
113 
114  /// tests whether two ArcGraphParts contain the same arcs
115  /** @param p the ArcGraphPart that we compare with this */
116  bool operator==(const ArcGraphPart& p) const;
117 
118  /// tests whether two ArcGraphParts contain different arcs
119  /** @param p the ArcGraphPart that we compare with this */
120  bool operator!=(const ArcGraphPart& p) const;
121 
122  /// @}
123 
124  // ############################################################################
125  /// @name Accessors/Modifiers
126  // ############################################################################
127  /// @{
128 
129  /// insert a new arc into the ArcGraphPart
130  /** @param tail the id of the tail of the new arc to be inserted
131  * @param head the id of the head of the new arc to be inserted
132  * @warning if the arc already exists, nothing is done. In particular, no
133  * exception is raised. */
134  virtual void addArc(NodeId tail, NodeId head);
135 
136  /// removes an arc from the ArcGraphPart
137  /** @param arc the arc to be removed
138  * @warning if the arc does not exist, nothing is done. In particular, no
139  * exception is thrown. However, the signal onArcDeleted is fired
140  * only if a node is effectively removed. */
141  virtual void eraseArc(const Arc& arc);
142 
143  /// indicates whether a given arc exists
144  /** @param arc the arc we test whether or not it belongs to the ArcGraphPart
145  */
146  bool existsArc(const Arc& arc) const;
147 
148  /// indicates whether a given arc exists
149  /** @param tail the tail of the arc we test the existence in the
150  * ArcGraphPart
151  * @param head the head of the arc we test the existence in the ArcGraphPart
152  */
153  bool existsArc(NodeId tail, NodeId head) const;
154 
155  /// indicates wether the ArcGraphPart contains any arc
156  bool emptyArcs() const;
157 
158  /// removes all the arcs from the ArcGraphPart
159  void clearArcs();
160 
161  /// indicates the number of arcs stored within the ArcGraphPart
162  Size sizeArcs() const;
163 
164  /// returns the set of arcs stored within the ArcGraphPart
165  const ArcSet& arcs() const;
166 
167  /// returns the set of nodes with arc ingoing to a given node
168  /** Note that the set of arcs returned may be empty if no arc within the
169  * ArcGraphPart is ingoing into the given node.
170  * @param id the node toward which the arcs returned are pointing */
171  const NodeSet& parents(NodeId id) const;
172 
173  /// returns the set of nodes which consists in the node and its parents
174  /** Note that the set of nodes returned may be empty if no path within the
175  * ArcGraphPart is outgoing from the given node.
176  * @param id the node which is the tail of a directed path with the returned
177  * nodes
178  **/
179  NodeSet family(NodeId id) const;
180 
181  /// returns the set of nodes with directed path outgoing from a given node
182  /** Note that the set of nodes returned may be empty if no path within the
183  * ArcGraphPart is outgoing from the given node.
184  * @param id the node which is the tail of a directed path with the returned
185  * nodes
186  **/
187  NodeSet descendants(NodeId id) const;
188 
189 
190  /// returns the set of nodes with directed path ingoing to a given node
191  /** Note that the set of nodes returned may be empty if no path within the
192  * ArcGraphPart is ingoing to the given node.
193  * @param id the node which is the head of a directed path with the returned
194  * nodes
195  **/
196  NodeSet ancestors(NodeId id) const;
197 
198  /// returns the set of children of a set of nodes
199  NodeSet children(const NodeSet& ids) const;
200 
201  /// returns the set of parents of a set of nodes
202  NodeSet parents(const NodeSet& ids) const;
203 
204  /// returns the set of family nodes of a set of nodes
205  NodeSet family(const NodeSet& ids) const;
206 
207 
208  /// returns the set of nodes with arc outgoing from a given node
209  /** Note that the set of arcs returned may be empty if no arc within the
210  * ArcGraphPart is outgoing from the given node.
211  * @param id the node which is the tail of the arcs returned */
212  const NodeSet& children(NodeId id) const;
213 
214  /// erase all the parents of a given node
215  /** @param id the node all the parents of which will be removed
216  * @warning although this method is not virtual, it calls method
217  * eraseArc( const Arc& arc ) and, as such, has a "virtual" behaviour. If
218  * you do not wish it to have this "virtual" behaviour, call instead
219  * method @ref unvirtualizedEraseParents
220  * @warning if no arc is a parent of id, nothing is done. In particular, no
221  * exception is thrown. */
222  void eraseParents(NodeId id);
223 
224  /// same function as eraseParents but without any virtual call to an erase
225  /** @param id the node whose ingoing arcs will be removed */
226  void unvirtualizedEraseParents(NodeId id);
227 
228  /// removes all the children of a given node
229  /** @param id the node all the children of which will be removed
230  * @warning although this method is not virtual, it calls method
231  * eraseArc( const Arc& arc ) and, as such, has a "virtual" behaviour. If
232  * you do not wish it to have this "virtual" behaviour, call instead
233  * method @ref unvirtualizedEraseChildren
234  * @warning if no arc is a parent of id, nothing is done. In particular, no
235  * exception is thrown. */
236  void eraseChildren(NodeId id);
237 
238  /// same function as eraseChildren but without any virtual call to an erase
239  /** @param id the node whose outgoing arcs will be removed */
240  void unvirtualizedEraseChildren(NodeId id);
241 
242  /// to friendly display the content of the ArcGraphPart
243  std::string toString() const;
244 
245  /** @brief a method to create a hashMap of VAL from a set of arcs
246  * (using for every arc, say x, the VAL f(x))
247  * @param f a function assigning a VAL to any arc
248  * @param size an optional parameter enabling to fine-tune the returned
249  * Property. Roughly speaking, it is a good practice to have a size equal to
250  * half the number of arcs. If you do not specify this parameter, the method
251  * will assign it for you. */
252  template < typename VAL >
253  ArcProperty< VAL > arcsProperty(VAL (*f)(const Arc&), Size size = 0) const;
254 
255  /** @brief a method to create a hashMap of VAL from a set of arcs
256  * (using for every arc, say x, the VAL a)
257  * @param a the default value assigned to each arc in the returned Property
258  * @param size an optional parameter enabling to fine-tune the returned
259  * Property. Roughly speaking, it is a good practice to have a size equal to
260  * half the number of arcs. If you do not specify this parameter, the method
261  * will assign it for you. */
262  template < typename VAL >
263  ArcProperty< VAL > arcsProperty(const VAL& a, Size size = 0) const;
264 
265  /** @brief a method to create a list of VAL from a set of arcs
266  * (using for every arc, say x, the VAL f(x))
267  * @param f a function assigning a VAL to any arc */
268  template < typename VAL >
269  List< VAL > listMapArcs(VAL (*f)(const Arc&)) const;
270 
271  /// returns a directed path from node1 to node2 belonging to the set of arcs
272  /** @param node1 the id from which the path begins
273  * @param node2 the id to which the path ends
274  * @throw NotFound exception is raised if no path can be found between the
275  * two nodes */
277 
278  /// returns an unoriented (directed) path from node1 to node2 in the arc set
279  /** @param node1 the id from which the path begins
280  * @param node2 the id to which the path ends
281  * @throw NotFound exception is raised if no path can be found between the
282  * two nodes */
284 
285  /// @}
286 
287  protected:
288  /// a (virtualized) function to remove a given set of arcs
289  /** @warning this function uses eraseArc, which is a virtual function. Hence
290  * the behaviour of this function is that of a virtual function */
291  void eraseSetOfArcs_(const ArcSet& set);
292 
293  /// similar to @ref eraseSetOfArcs_ except that it is unvirtualized
294  /** @warning this function uses ArcGraphPart::eraseArc, hence, as compared
295  * with eraseSetOfArcs_, it removes the arcs without calling a virtual
296  * eraseArc */
297  void unvirtualizedEraseSetOfArcs_(const ArcSet& set);
298 
299  private:
300  /// the set of all the arcs contained within the ArcGraphPart
302 
303  /// for each arc, the sets of its parents
305 
306  /// for each arc, the set of its children
308 
309  /** @brief when the ArcGraphPart contains no arc ingoing into a given node,
310  * this function adds an empty set entry to parents__[id]
311  * @param id the node whose parents__[id] is checked */
312  void checkParents__(NodeId id) const;
313 
314  /** @brief when the ArcGraphPart contains no arc outgoing from a given node,
315  * this function adds an empty set entry to children__[id]
316  * @param id the node whose children__[id] is checked */
317  void checkChildren__(NodeId id) const;
318  };
319 
320  /// for friendly displaying the content of arc set
321  /** @param s the stream to which we display the content of a
322  * @param a the ArcGraphPart to be displayed */
323  std::ostream& operator<<(std::ostream& s, const ArcGraphPart& a);
324 
325 } /* namespace gum */
326 
327 #ifndef GUM_NO_INLINE
328 # include <agrum/tools/graphs/parts/arcGraphPart_inl.h>
329 #endif // GUM_NOINLINE
330 
331 #include <agrum/tools/graphs/parts/arcGraphPart_tpl.h>
332 
333 #endif // GUM_ARC_GRAPH_PART_H
std::vector< NodeId > directedUnorientedPath(NodeId node1, NodeId node2) const
returns an unoriented (directed) path from node1 to node2 in the arc set
std::string toString() const
to friendly display the content of the ArcGraphPart
void eraseChildren(NodeId id)
removes all the children of a given node
ArcGraphPart & operator=(const ArcGraphPart &s)
copy operator
NodeSet family(NodeId id) const
returns the set of nodes which consists in the node and its parents
void clearArcs()
removes all the arcs from the ArcGraphPart
NodeProperty< NodeSet *> children__
for each arc, the set of its children
Definition: arcGraphPart.h:307
Classes for directed edge sets.
Definition: arcGraphPart.h:78
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
NodeSet family(const NodeSet &ids) const
returns the set of family nodes of a set of nodes
virtual void eraseArc(const Arc &arc)
removes an arc from the ArcGraphPart
void unvirtualizedEraseChildren(NodeId id)
same function as eraseChildren but without any virtual call to an erase
NodeSet children(const NodeSet &ids) const
returns the set of children of a set of nodes
ArcProperty< VAL > arcsProperty(VAL(*f)(const Arc &), Size size=0) const
a method to create a hashMap of VAL from a set of arcs (using for every arc, say x, the VAL f(x))
virtual void addArc(NodeId tail, NodeId head)
insert a new arc into the ArcGraphPart
Set< Arc > arcs__
the set of all the arcs contained within the ArcGraphPart
Definition: arcGraphPart.h:301
bool emptyArcs() const
indicates wether the ArcGraphPart contains any arc
bool operator!=(const ArcGraphPart &p) const
tests whether two ArcGraphParts contain different arcs
ArcGraphPart(const ArcGraphPart &s)
copy constructor
Size sizeArcs() const
indicates the number of arcs stored within the ArcGraphPart
void unvirtualizedEraseSetOfArcs_(const ArcSet &set)
similar to eraseSetOfArcs_ except that it is unvirtualized
bool operator==(const ArcGraphPart &p) const
tests whether two ArcGraphParts contain the same arcs
ArcSetIterator ArcIterator
Definition: arcGraphPart.h:80
Signaler2< NodeId, NodeId > onArcAdded
Definition: arcGraphPart.h:82
bool existsArc(NodeId tail, NodeId head) const
indicates whether a given arc exists
virtual ~ArcGraphPart()
destructor
void checkParents__(NodeId id) const
when the ArcGraphPart contains no arc ingoing into a given node, this function adds an empty set entr...
const ArcSet & arcs() const
returns the set of arcs stored within the ArcGraphPart
std::vector< NodeId > directedPath(NodeId node1, NodeId node2) const
returns a directed path from node1 to node2 belonging to the set of arcs
NodeSet parents(const NodeSet &ids) const
returns the set of parents of a set of nodes
const NodeSet & children(NodeId id) const
returns the set of nodes with arc outgoing from a given node
void eraseParents(NodeId id)
erase all the parents of a given node
ArcProperty< VAL > arcsProperty(const VAL &a, Size size=0) const
a method to create a hashMap of VAL from a set of arcs (using for every arc, say x, the VAL a)
NodeProperty< NodeSet *> parents__
for each arc, the sets of its parents
Definition: arcGraphPart.h:304
Signaler2< NodeId, NodeId > onArcDeleted
Definition: arcGraphPart.h:83
NodeSet descendants(NodeId id) const
returns the set of nodes with directed path outgoing from a given node
NodeSet ancestors(NodeId id) const
returns the set of nodes with directed path ingoing to a given node
void checkChildren__(NodeId id) const
when the ArcGraphPart contains no arc outgoing from a given node, this function adds an empty set ent...
List< VAL > listMapArcs(VAL(*f)(const Arc &)) const
a method to create a list of VAL from a set of arcs (using for every arc, say x, the VAL f(x)) ...
bool existsArc(const Arc &arc) const
indicates whether a given arc exists
void eraseSetOfArcs_(const ArcSet &set)
a (virtualized) function to remove a given set of arcs
ArcGraphPart(Size arcs_size=HashTableConst::default_size, bool arcs_resize_policy=true)
default constructor
void unvirtualizedEraseParents(NodeId id)
same function as eraseParents but without any virtual call to an erase