1 -- Created on: 1991-04-24
2 -- Created by: Denis PASCAL
3 -- Copyright (c) 1991-1999 Matra Datavision
4 -- Copyright (c) 1999-2014 OPEN CASCADE SAS
6 -- This file is part of Open CASCADE Technology software library.
8 -- This library is free software; you can redistribute it and/or modify it under
9 -- the terms of the GNU Lesser General Public License version 2.1 as published
10 -- by the Free Software Foundation, with special exception defined in the file
11 -- OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 -- distribution for complete text of the license and disclaimer of any warranty.
14 -- Alternatively, this file may be used under the terms of Open CASCADE
15 -- commercial license or contractual agreement.
17 -- Revised: Mireille MERCIEN
20 generic class HDirectedGraph from PCollection (
22 Attribute as Storable)
24 ---Purpose: Definition of a generic directed graph for a type of
25 -- Item associated to a vertex, and a type of Attribute
26 -- associated to an Edge. A Directed Graph is a
27 -- collection that includes a set of Vertices and a set
28 -- of Edges. A vertex, also called a "Node", forms the
29 -- basic structural element of the graph. Each edge,
30 -- also called an "Arc" defines an oriented link between
31 -- two vertices. In the scheme A->B, vertex A is called
32 -- the SOURCE of the link, B its DESTINATION, and B is
33 -- ADJACENT to A. If there is no edge which destinates
34 -- to a vertex, this vertex is a ROOT of the graph. If
35 -- there is no edge which originates from a vertex, this
36 -- vertex is a LEAF of the graph.
38 ---Keywords: SOURCE vertex, DESTINATION Vertex, ROOT vertex, LEAF
39 -- vertex, ADJACENT vertex. Depth-first search, breadth, first Search.
41 ---References: Software Components with ADA (The Benjamin/Cummings
42 -- Company, Inc. 1986).
47 raises NoSuchObject from Standard,
48 NoMoreObject from Standard
51 class Edge inherits Persistent
53 ---Purpose: Defines nested class Edge of a Directed Graph.
54 -- An Edge is an oriented link between two vertices.
56 raises NoMoreObject from Standard ,
57 NoSuchObject from Standard
60 Create (Source,Destination : Vertex; Value : Attribute)
61 ---Purpose: Creates an oriented link between <source> and
62 -- <destination> with an associated attribute. To
63 -- be used only by DirectedGraph methods to
64 -- create and add an edge to a given graph.
69 ---Purpose: Returns attribute associated to <me>.
72 SetAttribute (me : mutable; Value : Attribute)
74 ---Purpose: To associate an attribute to <me>.
79 ---Purpose: Returns the vertex which originates from <me>.
80 returns mutable Vertex;
82 SetSource (me :mutable; V: Vertex)
84 ---Purpose: Sets the vertex which originates from <me>.
89 ---Purpose: Returns the vertex which destinates to <me>.
90 returns mutable Vertex;
92 SetDestination (me :mutable ; V: Vertex)
94 ---Purpose: Sets the vertex which destinates to <me>.
97 Reverse (me : mutable);
99 ---Purpose: Reverse the orientation of <me>.
100 -- The source vertex becomes the destination vertex, and
101 -- the destination becomes the source.
103 IsLoop (me) returns Boolean from Standard;
105 ---Purpose: Returns True if the fields <MyDestination> and
106 -- <Mysource> are equal.
109 MyAttribute : Attribute;
111 MyDestination : Vertex;
113 friends class HDirectedGraph from PCollection,
114 class Vertex from PCollection
118 class SetOfEdge instantiates HSet from PCollection(Edge);
119 ---Purpose: Defines nested class SetOfEdge used as field of
120 -- Vertex and DirectedGraph.
122 class SetOfVertex instantiates HSet from PCollection(Vertex);
123 ---Purpose: Defines nested class SetOfVertex used as field of a
126 class StackOfVertex instantiates HStack from PCollection(Vertex);
127 ---Purpose: Defines nested class StackOfVertex used as field of
128 -- DepthFirstIterator class.
131 class QueueOfVertex instantiates HQueue from PCollection(Vertex);
132 ---Purpose: Defines nested class QueueOfVertex used as field of
133 -- BreadthFirstIterator.
135 class FrontEdgesIterator
136 ---Purpose: Defines nested class FrontEdgesIterator, to visit
137 -- every edge that originates from a given vertex.
139 raises NoMoreObject from Standard ,
140 NoSuchObject from Standard
143 Create (aVertex : Vertex) returns FrontEdgesIterator;
147 ---Purpose: Returns TRUE if there are other edges.
148 returns Boolean from Standard;
150 Next (me : in out) raises NoMoreObject from Standard;
152 ---Purpose: Set the iterator to the next Edge.
156 ---Purpose: Returns the edge value for the current
157 -- position of the iterator.
159 raises NoSuchObject from Standard;
163 ---Purpose: To clear the iterator before having visiting all edges.
166 MyEdgeIterator : SetIteratorOfSetOfEdge;
167 HasMore : Boolean from Standard;
170 class BackEdgesIterator
171 ---Purpose: Defines nested class BackEdgesIterator, to visit
172 -- every edge that destinates to a given vertex.
174 raises NoMoreObject from Standard ,
175 NoSuchObject from Standard
177 Create (aVertex : Vertex) returns BackEdgesIterator;
181 ---Purpose: Returns TRUE if there are other edges.
182 returns Boolean from Standard;
184 Next (me : in out) raises NoMoreObject from Standard;
186 ---Purpose: Sets the iterator to the next edge.
190 ---Purpose: Returns the edge value for the current
191 -- position of the iterator.
193 raises NoSuchObject from Standard;
197 ---Purpose: To clear the iterator before having visiting all edges.
200 MyEdgeIterator : SetIteratorOfSetOfEdge;
201 HasMore : Boolean from Standard;
204 class DepthFirstIterator
205 ---Purpose: Defines nested class DepthFirstIterator, to visit
206 -- every vertex that depends of a given one. Depth
207 -- First Search is functionnally the equivalent of a
208 -- preorder tree traversal. We start at a given
209 -- Vertex V and recursively visit all adjacent
210 -- unvisited Vertices.
212 raises NoMoreObject from Standard ,
213 NoSuchObject from Standard
216 Create (aVertex : Vertex) returns DepthFirstIterator;
220 ---Purpose: Returns TRUE if there are other vertices.
221 returns Boolean from Standard;
223 Next (me : in out) raises NoMoreObject from Standard;
225 ---Purpose: Sets the iterator to the next vertex.
229 ---Purpose: Returns the vertex value for the current
230 -- position of the iterator.
232 raises NoSuchObject from Standard;
236 ---Purpose: To clear the iterator before having visiting
240 Visited : SetOfVertex;
241 Ready : StackOfVertex;
242 HasMore : Boolean from Standard;
245 class BreadthFirstIterator
246 ---Purpose: Defines nested class BreadthFirstIterator, to visit
247 -- every vertex that depends of a given one. We start
248 -- at a given vertex V, visit all its adjacent
249 -- vertices, and then recursively visit the unvisited
250 -- adjacent vertices of these previous ones.
252 raises NoMoreObject from Standard ,
253 NoSuchObject from Standard
256 Create (aVertex : Vertex) returns BreadthFirstIterator;
260 ---Purpose: Returns TRUE if there are other vertices.
261 returns Boolean from Standard;
263 Next (me : in out) raises NoMoreObject from Standard;
265 ---Purpose: Sets the iterator to the next vertex.
269 ---Purpose: Returns the vertex value for the current
270 -- position of the iterator.
272 raises NoSuchObject from Standard;
276 ---Purpose: To clear the iterator before having visiting
280 Visited : SetOfVertex;
281 Ready : QueueOfVertex;
282 HasMore : Boolean from Standard;
285 class AdjacentVerticesIterator
287 ---Purpose: Defines nested class AdjacentVerticesIterator, to
288 -- visit every adjacent vertex of a given one
289 -- (Destination vertices of its front edges).
291 raises NoMoreObject from Standard ,
292 NoSuchObject from Standard
294 Create (aVertex : Vertex) returns AdjacentVerticesIterator;
298 ---Purpose: Returns TRUE if there are other vertices.
299 returns Boolean from Standard;
301 Next (me : in out) raises NoMoreObject from Standard;
303 ---Purpose: Sets the iterator to the next vertex.
307 ---Purpose: Returns the vertex value for the current
308 -- position of the iterator.
310 raises NoSuchObject from Standard;
314 ---Purpose: To clear the iterator before having visiting all vertices.
317 MyEdgeIterator : SetIteratorOfSetOfEdge;
318 HasMore : Boolean from Standard;
321 ----------------------- Class Vertex ----------------------------------
323 class Vertex inherits Persistent
324 ---Purpose: Defines nested class vertex of a DirectedGraph. The
325 -- Vertex is the basic element of a graph.
327 raises NoMoreObject from Standard ,
328 NoSuchObject from Standard
331 Create (Value : Item ; InGraph : HDirectedGraph)
332 ---Purpose: Creates a vertex with an associated item. To
333 -- be used only by DirectedGraph methods to
334 -- create and add a vertex to a given graph.
335 returns mutable Vertex;
339 ---Purpose: Returns item associated to <me>.
342 SetItem (me : mutable; value : Item)
344 ---Purpose: Associates an item to <me>.
349 ---Purpose: Returns the HDirectedGraph which owns <me>.
350 returns HDirectedGraph;
352 AddFrontEdge (me : mutable; anEdge : Edge)
354 ---Purpose: To add an edge that destinates to <me>.
355 -- To be used only by editing methods of a DirectedGraph.
356 -- Returns False if the edge already exists.
357 returns Boolean from Standard
360 RemoveFrontEdge (me : mutable; anEdge : Edge)
362 ---Purpose: To remove an edge that originates from <me>.
363 -- To be used only by editing methods of a DirectedGraph.
364 -- An exception is raised if <anEdge> doesn't belong to
365 -- myFrontEdges field.
366 raises NoSuchObject from Standard
369 NbFrontEdges (me) returns Integer;
371 ---Purpose: Returns the number of edges which originates from <me>.
375 ---Purpose: Returns "myFrontEdges" Field for Iterator.
379 AddBackEdge (me : mutable; anEdge : Edge)
381 ---Purpose: To add an edge that destinates to <me>. To be
382 -- used only by editing methods of a
383 -- DirectedGraph, to update it after
384 -- modifications. Returns False if the edge already exists.
385 returns Boolean from Standard
388 RemoveBackEdge (me : mutable; anEdge : Edge)
390 ---Purpose: To remove an edge that destinates to <me>. To
391 -- be used only by editing methods of a
392 -- DirectedGraph, to update it after
393 -- modifications. An exception is raised if
394 -- <anEdge> doesn't belong to myBackEdges field.
395 raises NoSuchObject from Standard
400 ---Purpose: Returns the number of edge which destinates to <me>.
401 returns Integer from Standard;
405 ---Purpose: Returns "myBackEdges" field for Iterator.
411 ---Purpose: Returns TRUE if NbBackEdges = 0.
412 returns Boolean from Standard;
416 ---Purpose: Returns TRUE if NbFrontEdges = 0.
417 returns Boolean from Standard;
419 IsDestination (me; aVertex : Vertex)
421 ---Purpose: Returns TRUE if it exists an edge which
422 -- originates from <me> and destinates to <aVertex>.
423 returns Boolean from Standard;
425 IsSource (me; aVertex : Vertex)
427 ---Purpose: Returns TRUE if it exists an edge which
428 -- originates from <aVertex> and destinates to <me>.
429 returns Boolean from Standard;
433 MyGraph : HDirectedGraph;
434 MyFrontEdges : SetOfEdge;
435 MyBackEdges : SetOfEdge;
437 friends class HDirectedGraph from PCollection,
438 class Edge from PCollection,
439 class BackEdgesIterator from PCollection,
440 class FrontEdgesIterator from PCollection,
441 class DepthFirstIterator from PCollection,
442 class BreadthFirstIterator from PCollection,
443 class AdjacentVerticesIterator from PCollection
447 class VerticesIterator
448 ---Purpose: Defines nested class VerticesIterator, to visit
449 -- every vertex of a given directed graph.
451 raises NoMoreObject from Standard ,
452 NoSuchObject from Standard
454 Create (aGraph : HDirectedGraph) returns VerticesIterator;
458 ---Purpose: Returns TRUE if there are other vertices.
459 returns Boolean from Standard;
461 Next (me : in out) raises NoMoreObject from Standard;
463 ---Purpose: Sets the iterator to the next vertex.
467 ---Purpose: Returns the vertex value for the current
468 -- position of the iterator.
470 raises NoSuchObject from Standard;
474 ---Purpose: To clear the iterator before having visiting
478 MyVertexIterator : SetIteratorOfSetOfVertex;
479 HasMore : Boolean from Standard;
483 ---Purpose: Defines nested class RootsIterator, to visit every
484 -- "root" vertex of a directed graph.
486 raises NoMoreObject from Standard ,
487 NoSuchObject from Standard
489 Create (aGraph : HDirectedGraph) returns RootsIterator;
493 ---Purpose: Returns TRUE if there are other vertices.
494 returns Boolean from Standard;
496 Next (me : in out) raises NoMoreObject from Standard;
498 ---Purpose: Sets the iterator to the next vertex.
502 ---Purpose: Returns the vertex value for the current
503 -- position of the iterator.
505 raises NoSuchObject from Standard;
509 ---Purpose: To clear the iterator before having visiting
513 MyVertexIterator : SetIteratorOfSetOfVertex;
514 HasMore : Boolean from Standard;
518 ---Purpose: Defines nested class LeavesIterator, to visit every
519 -- "leaf" vertex of a directed graph.
521 raises NoMoreObject from Standard ,
522 NoSuchObject from Standard
524 Create (aGraph : HDirectedGraph) returns LeavesIterator;
528 ---Purpose: Returns TRUE if there are other vertices.
529 returns Boolean from Standard;
531 Next (me : in out) raises NoMoreObject from Standard;
533 ---Purpose: Set the iterator to the next vertex.
537 ---Purpose: Returns the vertex value for the current
538 -- position of the iterator.
540 raises NoSuchObject from Standard;
544 ---Purpose: To clear the iterator before having visiting
548 MyVertexIterator : SetIteratorOfSetOfVertex;
549 HasMore : Boolean from Standard;
553 ---Purpose: Defines nested class EdgesIterator, to visit every
554 -- edge of a directed graph.
556 raises NoMoreObject from Standard ,
557 NoSuchObject from Standard
559 Create (aGraph : HDirectedGraph) returns EdgesIterator;
561 More (me) returns Boolean from Standard;
563 ---Purpose: Returns TRUE if there are other edges.
565 Next (me : in out) raises NoMoreObject from Standard;
567 ---Purpose: Sets the iterator to the next edge.
571 ---Purpose: Returns the edge value for the current
572 -- position of the iterator.
574 raises NoSuchObject from Standard;
578 ---Purpose: To clear the iterator before having visiting all edges.
581 MyEdgeIterator : SetIteratorOfSetOfEdge;
582 HasMore : Boolean from Standard;
585 --------------------- class HDirectedGraph -----------------------------
589 Create returns mutable HDirectedGraph;
590 ---Purpose: Creates an empty Directed Graph.
592 NumberOfVertices (me) returns Integer from Standard;
595 NumberOfEdges (me) returns Integer from Standard;
598 NumberOfLeaves (me) returns Integer from Standard;
600 ---Purpose: Returns the number of "leaf" vertices of <me>.
602 NumberOfRoots (me) returns Integer from Standard;
604 ---Purpose: Returns the number of "root" vertices of <me>.
606 IsEmpty (me) returns Boolean from Standard;
609 Clear (me : mutable);
611 ---Purpose: Removes all edges and vertices of <me>.
613 IsMember (me; aVertex : Vertex) returns Boolean from Standard;
616 IsMember (me; anEdge : Edge) returns Boolean from Standard;
619 Add (me : mutable; Value : Item)
621 ---Purpose: Creates and Adds a vertex, with a given value
622 -- <value>, to <me>. Of course this new Vertex is a
623 -- "root" and "leaf" vertex of <me> because it has no
624 -- connexion with other vertices of the directed graph.
625 returns mutable Vertex;
627 Remove (me : mutable; aVertex : mutable Vertex)
629 ---Purpose: Removes <aVertex> from <me>. Removes also all the
630 -- edges of <me> which reference <aVertex>; Updates
631 -- Source and Destination vertices of each of these
632 -- edges . Raises an exception if <aVertex> is not
634 raises NoSuchObject from Standard;
636 Add (me : mutable; Source : mutable Vertex;
637 Destination : mutable Vertex; Value : Attribute)
639 ---Purpose: Creates and adds an edge from <source> to
640 -- <destination>, with the attribute <value>, to
641 -- <me>. Updates Source and Destination Vertices of
642 -- this new edge. Raises an exception if <source> or
643 -- <destination> are not members of <me>.
645 raises NoSuchObject from Standard;
647 Remove (me : mutable; AnEdge : mutable Edge)
649 ---Purpose: Removes <anEdge> from <me>. And Updates Source
650 -- and Destination Vertices of <anEdge>. Raises an
651 -- exception if <Edge> is not member of <me>.
652 raises NoSuchObject from Standard;
656 ---Purpose: Returns "myVertices" field for Iterator.
662 ---Purpose: Returns "myEdges" field for Iterator.
666 ShallowCopy(me) returns mutable like me
669 ---C++: function call
672 ShallowDump (me; s: in out OStream)
675 ---C++: function call
679 MyVertices : SetOfVertex;
682 friends class VerticesIterator from PCollection,
683 class RootsIterator from PCollection,
684 class LeavesIterator from PCollection,
685 class EdgesIterator from PCollection