23#include "vtkDoubleArray.h"
25#include "vtkObjectFactory.h"
34 this->
Heap = vtkIdList::New();
56 vtkErrorMacro(<<
"No HeapScalars.");
60 this->
Heap->Initialize();
62 vtkIdType numberOfScalars;
65 for (i=0; i<numberOfScalars; i++)
75 vtkErrorMacro(<<
"No MinHeapScalars.");
79 vtkIdType numberOfScalars;
82 if ((
id<0)||(
id>=numberOfScalars))
84 vtkErrorMacro(<<
"Id inserted exceeds MinHeapScalars dimension.");
90 currentLoc = this->
Heap->InsertNextId(
id);
95 while (this->
BackPointers->GetNumberOfIds() < numberOfScalars)
100 if (this->
BackPointers->GetNumberOfIds() < numberOfScalars)
108 return this->
Heap->GetNumberOfIds();
115 vtkErrorMacro(<<
"No MinHeapScalars.");
121 vtkErrorMacro(<<
"Id inserted exceeds MinHeapScalars dimension.");
132 heapSize = this->
Heap->GetNumberOfIds();
134 if ((loc<0)||(loc>=heapSize))
136 vtkErrorMacro(<<
"Heap smaller than requested location.");
140 if ((loc >= heapSize/2)&&(loc < heapSize))
149 heapSize = this->
Heap->GetNumberOfIds();
151 if ((loc<0)||(loc>=heapSize))
153 vtkErrorMacro(<<
"Heap smaller than requested location.");
157 if (loc >= heapSize/2)
166 heapSize = this->
Heap->GetNumberOfIds();
168 if ((loc<0)||(loc>=heapSize))
170 vtkErrorMacro(<<
"Heap smaller than requested location.");
174 if (loc >= (heapSize-1)/2)
184 vtkErrorMacro(<<
"Negative location requested.");
198 vtkErrorMacro(<<
"No MinHeapScalars.");
203 heapSize = this->
Heap->GetNumberOfIds();
205 if ((loc<0)||(loc>=heapSize))
207 vtkErrorMacro(<<
"Heap smaller than requested location.");
222 this->
Swap(loc,parentLoc);
231 vtkErrorMacro(<<
"No MinHeapScalars.");
236 heapSize = this->
Heap->GetNumberOfIds();
238 if ((loc<0)||(loc>=heapSize))
240 vtkErrorMacro(<<
"Heap smaller than requested location.");
244 vtkIdType minChildLoc, leftChildLoc, rightChildLoc;
246 while (!this->
IsLeaf(loc))
249 rightChildLoc = leftChildLoc + 1;
251 minChildLoc = leftChildLoc;
252 if ((leftChildLoc<this->
Heap->GetNumberOfIds()-1) &&
253 (this->MinHeapScalars->GetValue(this->Heap->GetId(leftChildLoc)) - this->MinHeapScalars->GetValue(this->Heap->GetId(rightChildLoc))) >
VTK_VMTK_DOUBLE_TOL)
255 minChildLoc = rightChildLoc;
263 this->
Swap(loc,minChildLoc);
271 vtkIdType minId, lastElementLoc;
273 minId = this->
Heap->GetId(0);
274 lastElementLoc = this->
Heap->GetNumberOfIds()-1;
275 this->
Swap(0,lastElementLoc);
276 this->
Heap->SetNumberOfIds(lastElementLoc);
279 if (this->
Heap->GetNumberOfIds()>0)
291 minId = this->
Heap->GetId(0);
299 heapSize = this->
Heap->GetNumberOfIds();
301 if ((loc<0)||(loc>=heapSize))
303 vtkErrorMacro(<<
"Heap smaller than requested location.");
307 vtkIdType locId, lastElementLoc;
309 locId = this->
Heap->GetId(loc);
310 lastElementLoc = heapSize-1;
311 this->
Swap(locId,lastElementLoc);
312 this->
Heap->SetNumberOfIds(lastElementLoc);
315 if (this->
Heap->GetNumberOfIds()>0)
326 heapSize = this->
Heap->GetNumberOfIds();
328 if ((heapSize<loc0)||(heapSize<loc1))
330 vtkErrorMacro(<<
"Heap smaller than requested location.");
334 temp = this->
Heap->GetId(loc0);
335 this->
Heap->SetId(loc0,this->
Heap->GetId(loc1));
336 this->
Heap->SetId(loc1,temp);
344 this->Superclass::PrintSelf(os,indent);
Implements the min-heap data structure.
void Swap(vtkIdType loc0, vtkIdType loc1)
void SiftDown(vtkIdType loc)
void PrintSelf(std::ostream &os, vtkIndent indent) override
int IsLeaf(vtkIdType loc)
vtkDoubleArray * MinHeapScalars
vtkIdType GetRightChild(vtkIdType loc)
vtkIdType GetLeftChild(vtkIdType loc)
vtkIdType RemoveAt(vtkIdType loc)
void SiftUp(vtkIdType loc)
vtkIdType GetParent(vtkIdType loc)
void UpdateId(vtkIdType id)
void InsertNextId(vtkIdType id)
#define VTK_VMTK_DOUBLE_TOL
vtkStandardNewMacro(vtkvmtkMinHeap)