summaryrefslogtreecommitdiff
path: root/inc/GraphTools_TopologicalSortFromIterator.gxx
blob: 1f581539c3292ea1545f6f2b146110d419725913 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright: 	Matra-Datavision 1991
// File:	GraphTools_TopologicalSortFromIterator.gxx
// Created:	Wed May 29 17:42:50 1991
// Author:	Denis PASCAL
//		<dp>

#include <Standard_NoMoreObject.hxx>
#include <Standard_NoSuchObject.hxx>
#include <Standard_DomainError.hxx>
#include <TColStd_QueueOfInteger.hxx>
#include <GraphTools_TSNode.hxx>

//=======================================================================
//function : GraphTools_TopologicalSortFromIterator
//purpose  : 
//=======================================================================

GraphTools_TopologicalSortFromIterator::GraphTools_TopologicalSortFromIterator () {}



//=======================================================================
//function : FromVertex
//purpose  : 
//=======================================================================

void GraphTools_TopologicalSortFromIterator::FromVertex (const Vertex& V) 
{
  GraphTools_TSNode newnode;
  myVertices.Add(V,newnode);    
}


//=======================================================================
//function : Perform
//purpose  : 
//=======================================================================

void GraphTools_TopologicalSortFromIterator::Perform 
  (const Graph& G,
   const Standard_Boolean ignoreSelfLoop,
   const Standard_Boolean processCycle)
{
  myIgnoreSelfLoop = ignoreSelfLoop;
  myProcessCycle   = processCycle;
  myCurrent        = 1;
  mySort.Clear();
  // algorithm DS
  Standard_Integer i, indexcurrent, indexadjacent, nbadjacent;
  indexcurrent = 1;
  while (indexcurrent <= myVertices.Extent()) {
    VIterator itV (G,myVertices.FindKey(indexcurrent));
    for ( ; itV.More(); itV.Next()) {
      indexadjacent = myVertices.FindIndex(itV.Value());
      if (indexadjacent == 0) {
        GraphTools_TSNode newnode;
        indexadjacent = myVertices.Add(itV.Value(),newnode);
      }
      if (! (indexcurrent == indexadjacent && myIgnoreSelfLoop)) {
	myVertices(indexcurrent).AddSuccessor(indexadjacent);
	myVertices(indexadjacent).IncreaseRef();
      }
    }
    indexcurrent++;
  }
  // current root vertices queue
  TColStd_QueueOfInteger processQueue;
  Standard_Integer nbVertices = myVertices.Extent();
  for (i = 1 ; i <= nbVertices; i++) {
    if (myVertices(i).NbRef() == 0) processQueue.Push(i);
  }
  // acyclic processing
  while (!processQueue.IsEmpty()) {
    indexcurrent = processQueue.Front();
    mySort.Append(indexcurrent);
    nbadjacent = myVertices(indexcurrent).NbSuccessors();
    for (i = 1; i <= nbadjacent; i++) { 
      indexadjacent = myVertices(indexcurrent).GetSuccessor(i);
      myVertices(indexadjacent).DecreaseRef();
      if (myVertices(indexadjacent).NbRef() == 0) {
	processQueue.Push (indexadjacent);
      }
    }
    processQueue.Pop();
  }
  // cyclic processing
  myCycles = mySort.Length() + 1;
  if (myProcessCycle) {
    for (i = 1 ; i <= nbVertices; i++) {
      if (myVertices(i).NbRef() != 0) mySort.Append(i);
    }
  }
}


//=======================================================================
//function : Reset
//purpose  : 
//=======================================================================

void GraphTools_TopologicalSortFromIterator::Reset ()
{
   myVertices.Clear();
//   myIgnoreSelfLoop : Boolean from Standard;
//   myProcessCycle   : Boolean from Standard;
   mySort.Clear();
//   myCycles         : Integer from Standard;
   myCurrent = 1;
}


//=======================================================================
//function : More
//purpose  : 
//=======================================================================

Standard_Boolean GraphTools_TopologicalSortFromIterator::More () const 
{
  return myCurrent <= mySort.Length();
}


//=======================================================================
//function : Next
//purpose  : 
//=======================================================================

void GraphTools_TopologicalSortFromIterator::Next () 
{
  if (!More()) Standard_NoMoreObject::Raise();
  myCurrent ++;
}


//=======================================================================
//function : Value
//purpose  : 
//=======================================================================

const Vertex& GraphTools_TopologicalSortFromIterator::Value () const {
  if (!More()) Standard_NoSuchObject::Raise();
  return myVertices.FindKey (mySort(myCurrent));
}


//=======================================================================
//function : IsInCycle
//purpose  : 
//=======================================================================

Standard_Boolean GraphTools_TopologicalSortFromIterator::IsInCycle () const 
{
  if (!More()) Standard_NoSuchObject::Raise();
  return myCurrent >= myCycles;
}