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 214 215 216 217 218 219 220 221 222 223 224
|
/*=========================================================================
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 vtkHashMap.h includes this file
// when VTK_NO_EXPLICIT_TEMPLATE_INSTANTIATION is defined.
#ifndef __vtkHashMapIterator_txx
#define __vtkHashMapIterator_txx
#include "vtkHashMapIterator.h"
#include "vtkAbstractIterator.txx"
#include "vtkHashMap.h"
//----------------------------------------------------------------------------
template <class KeyType,class DataType>
vtkHashMapIterator<KeyType,DataType> *vtkHashMapIterator<KeyType,DataType>::New()
{
#ifdef VTK_DEBUG_LEAKS
vtkDebugLeaks::ConstructClass("vtkHashMapIterator");
#endif
return new vtkHashMapIterator<KeyType,DataType>();
}
//----------------------------------------------------------------------------
template<class KeyType,class DataType>
void vtkHashMapIterator<KeyType,DataType>::InitTraversal()
{
vtkHashMap<KeyType,DataType>* hmap
= static_cast<vtkHashMap<KeyType,DataType>*>(this->Container);
if(!this->Iterator)
{
this->Iterator = hmap->Buckets[0]->NewIterator();
}
this->GoToFirstItem();
}
//----------------------------------------------------------------------------
template<class KeyType,class DataType>
int vtkHashMapIterator<KeyType,DataType>::GetKey(KeyType& key)
{
if(this->IsDoneWithTraversal()) { return VTK_ERROR; }
ItemType item = { KeyType(), DataType() };
if(this->Iterator->GetData(item) == VTK_OK)
{
key = item.Key;
return VTK_OK;
}
return VTK_ERROR;
}
//----------------------------------------------------------------------------
template<class KeyType,class DataType>
int vtkHashMapIterator<KeyType,DataType>::GetData(DataType& data)
{
if(this->IsDoneWithTraversal()) { return VTK_ERROR; }
ItemType item = { KeyType(), DataType() };
if(this->Iterator->GetData(item) == VTK_OK)
{
data = item.Data;
return VTK_OK;
}
return VTK_ERROR;
}
//----------------------------------------------------------------------------
template<class KeyType,class DataType>
int vtkHashMapIterator<KeyType,DataType>::GetKeyAndData(KeyType& key,
DataType& data)
{
if(this->IsDoneWithTraversal()) { return VTK_ERROR; }
ItemType item;
if(this->Iterator->GetData(item) == VTK_OK)
{
key = item.Key;
data = item.Data;
return VTK_OK;
}
return VTK_ERROR;
}
//----------------------------------------------------------------------------
template<class KeyType,class DataType>
int vtkHashMapIterator<KeyType,DataType>::SetData(const DataType& data)
{
if(this->IsDoneWithTraversal()) { return VTK_ERROR; }
ItemType item;
if(this->Iterator->GetData(item) == VTK_OK)
{
item.Data = data;
return this->Iterator->SetData(item);
}
return VTK_ERROR;
}
//----------------------------------------------------------------------------
template<class KeyType,class DataType>
int vtkHashMapIterator<KeyType,DataType>::IsDoneWithTraversal()
{
vtkHashMap<KeyType,DataType>* hmap
= static_cast<vtkHashMap<KeyType,DataType>*>(this->Container);
return (this->Bucket == hmap->NumberOfBuckets) ? 1:0;
}
//----------------------------------------------------------------------------
template<class KeyType,class DataType>
void vtkHashMapIterator<KeyType,DataType>::GoToNextItem()
{
if(this->IsDoneWithTraversal()) { this->GoToFirstItem(); }
this->Iterator->GoToNextItem();
this->ScanForward();
}
//----------------------------------------------------------------------------
template<class KeyType,class DataType>
void vtkHashMapIterator<KeyType,DataType>::GoToPreviousItem()
{
if(this->IsDoneWithTraversal())
{
this->GoToLastItem();
}
else
{
this->Iterator->GoToPreviousItem();
this->ScanBackward();
}
}
//----------------------------------------------------------------------------
template<class KeyType,class DataType>
void vtkHashMapIterator<KeyType,DataType>::GoToFirstItem()
{
vtkHashMap<KeyType,DataType>* hmap
= static_cast<vtkHashMap<KeyType,DataType>*>(this->Container);
this->Bucket = 0;
this->Iterator->SetContainer(hmap->Buckets[this->Bucket]);
this->Iterator->GoToFirstItem();
this->ScanForward();
}
//----------------------------------------------------------------------------
template<class KeyType,class DataType>
void vtkHashMapIterator<KeyType,DataType>::GoToLastItem()
{
vtkHashMap<KeyType,DataType>* hmap
= static_cast<vtkHashMap<KeyType,DataType>*>(this->Container);
this->Bucket = hmap->NumberOfBuckets-1;
this->Iterator->SetContainer(hmap->Buckets[this->Bucket]);
this->Iterator->GoToLastItem();
this->ScanBackward();
}
//----------------------------------------------------------------------------
template<class KeyType,class DataType>
vtkHashMapIterator<KeyType,DataType>::vtkHashMapIterator()
{
this->Iterator = 0;
}
//----------------------------------------------------------------------------
template<class KeyType,class DataType>
vtkHashMapIterator<KeyType,DataType>::~vtkHashMapIterator()
{
if(this->Iterator) { this->Iterator->Delete(); }
}
//----------------------------------------------------------------------------
template<class KeyType,class DataType>
void vtkHashMapIterator<KeyType,DataType>::ScanForward()
{
// Move the iterator forward until a valid item is reached.
vtkHashMap<KeyType,DataType>* hmap
= static_cast<vtkHashMap<KeyType,DataType>*>(this->Container);
while(this->Iterator->IsDoneWithTraversal() &&
(++this->Bucket < hmap->NumberOfBuckets))
{
this->Iterator->SetContainer(hmap->Buckets[this->Bucket]);
this->Iterator->GoToFirstItem();
}
}
//----------------------------------------------------------------------------
template<class KeyType,class DataType>
void vtkHashMapIterator<KeyType,DataType>::ScanBackward()
{
// Move the iterator backward until a valid item is reached.
vtkHashMap<KeyType,DataType>* hmap
= static_cast<vtkHashMap<KeyType,DataType>*>(this->Container);
while(this->Iterator->IsDoneWithTraversal() &&
(this->Bucket > 0))
{
this->Iterator->SetContainer(hmap->Buckets[--this->Bucket]);
this->Iterator->GoToLastItem();
}
// If no valid item was reached, indicate that the traversal is done.
if(this->Iterator->IsDoneWithTraversal())
{
this->Bucket = hmap->NumberOfBuckets;
}
}
//----------------------------------------------------------------------------
#if defined ( _MSC_VER )
template <class KeyType,class DataType>
vtkHashMapIterator<KeyType,DataType>::vtkHashMapIterator(const vtkHashMapIterator<KeyType,DataType>&){}
template <class KeyType,class DataType>
void vtkHashMapIterator<KeyType,DataType>::operator=(const vtkHashMapIterator<KeyType,DataType>&){}
#endif
#endif
|