1 // Created on: 1991-05-29
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.
19 #include <Standard_NoMoreObject.hxx>
20 #include <Standard_NoSuchObject.hxx>
21 #include <Standard_DomainError.hxx>
22 #include <TColStd_ListOfInteger.hxx>
23 #include <GraphTools_TSNode.hxx>
25 //=======================================================================
26 //function : GraphTools_TopologicalSortFromIterator
28 //=======================================================================
30 GraphTools_TopologicalSortFromIterator::GraphTools_TopologicalSortFromIterator () {}
34 //=======================================================================
35 //function : FromVertex
37 //=======================================================================
39 void GraphTools_TopologicalSortFromIterator::FromVertex (const Vertex& V)
41 GraphTools_TSNode newnode;
42 myVertices.Add(V,newnode);
46 //=======================================================================
49 //=======================================================================
51 void GraphTools_TopologicalSortFromIterator::Perform
53 const Standard_Boolean ignoreSelfLoop,
54 const Standard_Boolean processCycle)
56 myIgnoreSelfLoop = ignoreSelfLoop;
57 myProcessCycle = processCycle;
61 Standard_Integer i, indexcurrent, indexadjacent, nbadjacent;
63 while (indexcurrent <= myVertices.Extent()) {
64 VIterator itV (G,myVertices.FindKey(indexcurrent));
65 for ( ; itV.More(); itV.Next()) {
66 indexadjacent = myVertices.FindIndex(itV.Value());
67 if (indexadjacent == 0) {
68 GraphTools_TSNode newnode;
69 indexadjacent = myVertices.Add(itV.Value(),newnode);
71 if (! (indexcurrent == indexadjacent && myIgnoreSelfLoop)) {
72 myVertices(indexcurrent).AddSuccessor(indexadjacent);
73 myVertices(indexadjacent).IncreaseRef();
78 // current root vertices queue
79 TColStd_ListOfInteger processQueue;
80 Standard_Integer nbVertices = myVertices.Extent();
81 for (i = 1 ; i <= nbVertices; i++) {
82 if (myVertices(i).NbRef() == 0) processQueue.Append(i);
85 while (!processQueue.IsEmpty()) {
86 indexcurrent = processQueue.First();
87 mySort.Append(indexcurrent);
88 nbadjacent = myVertices(indexcurrent).NbSuccessors();
89 for (i = 1; i <= nbadjacent; i++) {
90 indexadjacent = myVertices(indexcurrent).GetSuccessor(i);
91 myVertices(indexadjacent).DecreaseRef();
92 if (myVertices(indexadjacent).NbRef() == 0) {
93 processQueue.Append(indexadjacent);
96 processQueue.RemoveFirst();
99 myCycles = mySort.Length() + 1;
100 if (myProcessCycle) {
101 for (i = 1 ; i <= nbVertices; i++) {
102 if (myVertices(i).NbRef() != 0) mySort.Append(i);
108 //=======================================================================
111 //=======================================================================
113 void GraphTools_TopologicalSortFromIterator::Reset ()
116 // myIgnoreSelfLoop : Boolean from Standard;
117 // myProcessCycle : Boolean from Standard;
119 // myCycles : Integer from Standard;
124 //=======================================================================
127 //=======================================================================
129 Standard_Boolean GraphTools_TopologicalSortFromIterator::More () const
131 return myCurrent <= mySort.Length();
135 //=======================================================================
138 //=======================================================================
140 void GraphTools_TopologicalSortFromIterator::Next ()
142 if (!More()) Standard_NoMoreObject::Raise();
147 //=======================================================================
150 //=======================================================================
152 const Vertex& GraphTools_TopologicalSortFromIterator::Value () const {
153 if (!More()) Standard_NoSuchObject::Raise();
154 return myVertices.FindKey (mySort(myCurrent));
158 //=======================================================================
159 //function : IsInCycle
161 //=======================================================================
163 Standard_Boolean GraphTools_TopologicalSortFromIterator::IsInCycle () const
165 if (!More()) Standard_NoSuchObject::Raise();
166 return myCurrent >= myCycles;