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
|
/*=========================================================================
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.kitware.com/VolViewCopyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// Include blockers needed since vtkQueue.h includes this file
// when VTK_NO_EXPLICIT_TEMPLATE_INSTANTIATION is defined.
#ifndef __vtkQueue_txx
#define __vtkQueue_txx
#include "vtkQueue.h"
#include "vtkQueueIterator.txx"
#include "vtkVector.txx"
#include "vtkDebugLeaks.h"
template <class DType>
vtkQueue<DType> *vtkQueue<DType>::New()
{
#ifdef VTK_DEBUG_LEAKS
vtkDebugLeaks::ConstructClass("vtkQueue");
#endif
return new vtkQueue<DType>();
}
template <class DType>
vtkQueueIterator<DType>* vtkQueue<DType>::NewQueueIterator()
{
vtkQueueIterator<DType>* it = vtkQueueIterator<DType>::New();
it->SetContainer(this);
it->InitTraversal();
return it;
}
template <class DType>
vtkQueue<DType>::vtkQueue()
{
const vtkIdType initial_size = this->GetSize();
this->Start = initial_size - 1;
this->End = 0;
//this->SetSize(initial_size);
this->NumberOfItems = 0;
}
template <class DType>
vtkQueue<DType>::~vtkQueue()
{
this->MakeEmpty();
}
template <class DType>
int vtkQueue<DType>::EnqueueItem(DType a)
{
if ( this->GetSize() == 0 || this->End == (this->Start + 1) % this->GetSize() &&
this->NumberOfItems > 0 )
{
vtkIdType newsize = (this->Size+1) * 2;
vtkIdType oldsize = this->Size;
//cout << "Queue is full, resizing from: " << oldsize << " to " << newsize<< endl;
DType* newarray = new DType[newsize];
vtkIdType num = this->NumberOfItems;
vtkIdType nidx = 0;
if ( oldsize > 0 )
{
vtkIdType cc;
for ( cc = this->End; nidx < num; cc = (cc + 1) % oldsize)
{
//cout << "Copy " << cc << " to " << nidx << endl;
newarray[nidx] = this->Array[cc];
nidx ++;
}
}
this->End = 0;
this->Start = nidx-1 % newsize;
delete [] this->Array;
this->Array = newarray;
this->Size = newsize;
}
this->Start = (this->Start + 1) % this->GetSize();
this->Array[this->Start] = static_cast<DType>( ::vtkContainerCreateMethod(a) );
this->NumberOfItems ++;
return VTK_OK;
}
template <class DType>
int vtkQueue<DType>::DequeueItem()
{
if ( this->End == (this->Start +1) % this->GetSize() &&
this->NumberOfItems == 0 )
{
//cout << "Queue is empty" << endl;
return VTK_ERROR;
}
::vtkContainerDeleteMethod(this->Array[this->End]);
this->End = (this->End + 1) % this->GetSize();
this->NumberOfItems --;
return VTK_OK;
}
template <class DType>
int vtkQueue<DType>::GetDequeueItem(DType &a)
{
if ( this->NumberOfItems == 0 )
{
return VTK_ERROR;
}
this->GetItemNoCheck(this->End, a);
return VTK_OK;
}
template <class DType>
void vtkQueue<DType>::MakeEmpty()
{
if ( this->NumberOfItems == 0 ||
this->End == (this->Start +1) % this->GetSize() )
{
return;
}
vtkIdType cc;
for ( cc = this->End; this->NumberOfItems > 0 ; cc = (cc + 1) % this->GetSize() )
{
::vtkContainerDeleteMethod(this->Array[cc]);
this->NumberOfItems --;
}
this->NumberOfItems = 0;
this->Start = this->GetSize()-1;
this->End = 0;
}
template <class DType>
void vtkQueue<DType>::DebugList()
{
vtkIdType cc;
cout << "List: " << this << " type: " << this->GetClassName() << endl;
cout << "Number of items: " << this->GetNumberOfItems()
<< " S: " << this->Start << " E: " << this->End << endl;
for ( cc = 0; cc < this->GetSize(); cc ++ )
{
vtkIdType idx = 0;
if ( this->End == (this->Start+1) % this->GetSize() && this->NumberOfItems == 0)
{
idx = -1;
}
else if ( this->Start < this->End )
{
if ( cc > this->Start && cc < this->End )
{
idx = -1;
}
else
{
if ( cc <= this->Start )
{
idx = cc + (this->GetSize() - this->End);
}
if ( cc >= this->End )
{
idx = (cc - this->End);
}
}
}
else // Start >= this->End
{
if ( cc < this->End || cc > this->Start )
{
idx = -1;
}
else
{
idx = (cc - this->End);
}
}
if ( idx >= 0 )
{
cout << "Item [" << idx << " | " << cc << "]: " << this->Array[cc];
}
else
{
cout << "Item [" << idx << " | " << cc << "]: none";
}
if ( cc == this->Start )
{
cout << " <- start";
}
if ( cc == this->End )
{
cout << " <- end";
}
cout << endl;
}
}
#if defined ( _MSC_VER )
template <class DType>
vtkQueue<DType>::vtkQueue(const vtkQueue<DType>&){}
template <class DType>
void vtkQueue<DType>::operator=(const vtkQueue<DType>&){}
#endif
#endif
|