00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00024 #ifndef __vtkDijkstraGraphInternals_h
00025 #define __vtkDijkstraGraphInternals_h
00026
00027 #include <vtkstd/vector>
00028 #include <vtkstd/map>
00029
00030
00031 class vtkDijkstraGraphInternals
00032 {
00033 public:
00034
00035 vtkDijkstraGraphInternals()
00036 {
00037 this->HeapSize = 0;
00038 }
00039
00040 ~vtkDijkstraGraphInternals()
00041 {
00042 }
00043
00044
00045 vtkstd::vector<double> CumulativeWeights;
00046
00047
00048 vtkstd::vector<int> Predecessors;
00049
00050
00051
00052
00053 vtkstd::vector<unsigned char> OpenVertices;
00054
00055
00056
00057
00058 vtkstd::vector<unsigned char> ClosedVertices;
00059
00060
00061 vtkstd::vector< vtkstd::map< int,double > > Adjacency;
00062
00063
00064 vtkstd::vector<unsigned char> BlockedVertices;
00065
00066
00067 void Heapify(const int& i)
00068 {
00069
00070 unsigned int l = i * 2;
00071
00072 unsigned int r = i * 2 + 1;
00073 int smallest = -1;
00074
00075
00076
00077 if ( l <= this->HeapSize &&
00078 ( this->CumulativeWeights[ this->Heap[l] ] <
00079 this->CumulativeWeights[ this->Heap[i] ] ) )
00080 {
00081 smallest = l;
00082 }
00083 else
00084 {
00085 smallest = i;
00086 }
00087
00088 if ( r <= this->HeapSize &&
00089 ( this->CumulativeWeights[ this->Heap[ r ] ] <
00090 this->CumulativeWeights[ this->Heap[ smallest ] ] ) )
00091 {
00092 smallest = r;
00093 }
00094
00095 if ( smallest != i )
00096 {
00097 int t = this->Heap[i];
00098
00099 this->Heap[ i ] = this->Heap[ smallest ];
00100
00101
00102 this->HeapIndices[ this->Heap[i] ] = i;
00103
00104
00105 this->Heap[ smallest ] = t;
00106 this->HeapIndices[ t ] = smallest;
00107
00108 this->Heapify( smallest );
00109 }
00110 }
00111
00112 void HeapInsert(const int& v)
00113 {
00114 if ( this->HeapSize >= (this->Heap.size() - 1) )
00115 {
00116 return;
00117 }
00118
00119 this->HeapSize++;
00120 int i = this->HeapSize;
00121
00122 while ( i > 1 &&
00123 this->CumulativeWeights[ this->Heap[i/2] ] >
00124 this->CumulativeWeights[v] )
00125 {
00126 this->Heap[ i ] = this->Heap[i/2];
00127 this->HeapIndices[ this->Heap[i] ] = i;
00128 i /= 2;
00129 }
00130
00131 this->Heap[ i ] = v;
00132 this->HeapIndices[ v ] = i;
00133 }
00134
00135 int HeapExtractMin()
00136 {
00137 if ( this->HeapSize == 0 )
00138 {
00139 return -1;
00140 }
00141
00142 int minv = this->Heap[ 1 ];
00143 this->HeapIndices[ minv ] = -1;
00144
00145 this->Heap[ 1 ] = this->Heap[ this->HeapSize ];
00146 this->HeapIndices[ this->Heap[1] ]= 1;
00147
00148 this->HeapSize--;
00149 this->Heapify( 1 );
00150
00151 return minv;
00152 }
00153
00154 void HeapDecreaseKey(const int& v)
00155 {
00156
00157 int i = this->HeapIndices[ v ];
00158 if ( i < 1 || i > static_cast<int>(this->HeapSize) )
00159 {
00160 return;
00161 }
00162
00163 while ( i > 1 &&
00164 this->CumulativeWeights[ this->Heap[ i/2 ] ] >
00165 this->CumulativeWeights[ v ] )
00166 {
00167 this->Heap[ i ] = this->Heap[i/2];
00168 this->HeapIndices[ this->Heap[i] ] = i;
00169 i /= 2;
00170 }
00171
00172
00173 this->Heap[ i ] = v;
00174 this->HeapIndices[ v ] = i;
00175 }
00176
00177 void ResetHeap()
00178 {
00179 this->HeapSize = 0;
00180 }
00181
00182 void InitializeHeap(const int& size)
00183 {
00184 this->Heap.resize( size + 1 );
00185 this->HeapIndices.resize( size );
00186 }
00187
00188 private:
00189 unsigned int HeapSize;
00190
00191
00192 vtkstd::vector<int> Heap;
00193
00194
00195 vtkstd::vector<int> HeapIndices;
00196
00197 };
00198
00199 #endif