summaryrefslogtreecommitdiff
path: root/inc/GraphTools_SortedStrgCmptsFromIterator.gxx
blob: 154d08598297d68bc3a5dbb4bb39bb30fcee2980 (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
// Copyright: 	Matra-Datavision 1991
// File:	GraphTools_SortedStrgCmptsFromIterator.gxx
// Created:	Wed Oct 23 16:28:16 1991
// Author:	Denis PASCAL
//		<dp>

#include <Standard_DomainError.hxx>
#include <Standard_NoMoreObject.hxx>
#include <Standard_NoSuchObject.hxx>
#include <TColStd_SequenceOfInteger.hxx>



//=======================================================================
//function : GraphTools_SortedStrgCmptsFromIterator
//purpose  : 
//=======================================================================

GraphTools_SortedStrgCmptsFromIterator::
  GraphTools_SortedStrgCmptsFromIterator ()
{ 
   myNowIndex = 0;
}


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

void GraphTools_SortedStrgCmptsFromIterator::FromVertex (const Vertex& V) 
{
#ifdef DEB
  Standard_Integer index = 
#endif
    myVertices.Add (V,0);
}



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

void GraphTools_SortedStrgCmptsFromIterator::Perform (const Graph& G)
{
  myNowIndex = 0;
  mySort.Clear();
  Standard_Integer visited;
  Standard_Integer temp;
  Standard_Integer index = 1;
  while (index <= myVertices.Extent()) {
    visited = myVertices.FindFromIndex(index);
    if (visited == 0) temp = Visit(index,G);
    index ++;
  }  
  myIterator.Initialize(mySort);
}


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

void GraphTools_SortedStrgCmptsFromIterator::Reset ()
{
  myVertices.Clear();
  myNowIndex = 0;
  myStack.Clear();
  mySort.Clear();
}


//=======================================================================
//function : More
//purpose  : declenche l'algorithme de recherche des Strong Components
//=======================================================================

Standard_Boolean GraphTools_SortedStrgCmptsFromIterator::More() const
{
  return myIterator.More();
} 


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

void GraphTools_SortedStrgCmptsFromIterator::Next()
{ 
  myIterator.Next();
}


//=======================================================================
//function : NbVertices
//purpose  : 
//=======================================================================

Standard_Integer GraphTools_SortedStrgCmptsFromIterator::NbVertices() const
{  
  return myIterator.Value().Length();
}


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

const Vertex& GraphTools_SortedStrgCmptsFromIterator::Value
  (const Standard_Integer I) const
{  
  Standard_Integer indexvertex = myIterator.Value().Value(I);
  return myVertices.FindKey (indexvertex);
}

//=======================================================================
//function : Visit
//purpose  : private
//=======================================================================

Standard_Integer GraphTools_SortedStrgCmptsFromIterator::Visit
  (const Standard_Integer k, const Graph& G) 
{
  Standard_Integer MIN;
  Standard_Integer M;
  myNowIndex++; 
  myVertices(k) = myNowIndex;
  MIN = myNowIndex;
  myStack.Push(k);
  Standard_Integer currentVisited;
  currentVisited = myVertices.FindFromIndex (k);
  Standard_Integer adjacentIndex;
  Standard_Integer adjacentVisited;
  for (VIterator itV (G,myVertices.FindKey(k)); itV.More(); itV.Next()) {
    adjacentIndex = myVertices.FindIndex(itV.Value());
    if (adjacentIndex == 0) {
      adjacentIndex   = myVertices.Add (itV.Value(),0);
      adjacentVisited = 0;
    }
    else  adjacentVisited = myVertices.FindFromIndex (adjacentIndex);
    if (adjacentVisited == 0) M = Visit(adjacentIndex,G);
    else M = adjacentVisited;
    if (M < MIN) MIN = M;
  }
  if (MIN == currentVisited) {
    TColStd_SequenceOfInteger theSequence;
    mySort.Prepend(theSequence);
    TColStd_SequenceOfInteger& newSC = mySort.First();
    Standard_Boolean more;
    do {
      newSC.Append(myStack.Top());
      myVertices(myStack.Top()) = IntegerLast();
      more = myStack.Top() != k;
      myStack.Pop() ; 
    }
    while (more);
  }
  return MIN;
}