File: vtkHashMap.txx

package info (click to toggle)
volview 3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 25,204 kB
  • sloc: cpp: 132,585; ansic: 11,612; tcl: 236; sh: 64; makefile: 25; xml: 8
file content (285 lines) | stat: -rw-r--r-- 8,883 bytes parent folder | download
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*=========================================================================

  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 __vtkHashMap_txx
#define __vtkHashMap_txx

#include "vtkHashMap.h"
#include "vtkVector.txx"
#include "vtkAbstractMap.txx"
#include "vtkHashMapIterator.txx"

//----------------------------------------------------------------------------
template <class KeyType,class DataType>
vtkHashMap<KeyType,DataType> *vtkHashMap<KeyType,DataType>::New()
{ 
#ifdef VTK_DEBUG_LEAKS
  vtkDebugLeaks::ConstructClass("vtkHashMap");
#endif
  return new vtkHashMap<KeyType,DataType>(); 
}

//----------------------------------------------------------------------------
template <class KeyType, class DataType>
vtkHashMapIterator<KeyType,DataType>*
vtkHashMap<KeyType,DataType>::NewIterator()
{
  IteratorType* it = IteratorType::New();
  it->SetContainer(this);
  it->InitTraversal();
  return it;
}

//----------------------------------------------------------------------------
template<class KeyType, class DataType>
vtkIdType vtkHashMap<KeyType,DataType>::HashKey(const KeyType& key,
                                                vtkIdType nbuckets)
{
  return static_cast<vtkIdType>(
    vtkHashMapHashMethod(key) % 
    static_cast<unsigned long>(nbuckets) );
}

//----------------------------------------------------------------------------
template<class KeyType, class DataType>
int vtkHashMap<KeyType,DataType>::SetItem(const KeyType& key,
                                          const DataType& data)
{
  vtkIdType bucket = this->HashKey(key, this->NumberOfBuckets);
  ItemType item = { key, data };
  vtkIdType index=0;
  
  if(this->Buckets[bucket]->FindItem(item, index) == VTK_OK)
    {
    this->Buckets[bucket]->SetItemNoCheck(index, item);
    return VTK_OK;
    }
  if(this->Buckets[bucket]->AppendItem(item))
    {
    ++this->NumberOfItems;
    this->CheckLoadFactor();
    return VTK_OK;
    }
  return VTK_ERROR;
}

//----------------------------------------------------------------------------
template<class KeyType, class DataType>
int vtkHashMap<KeyType,DataType>::RemoveItem(const KeyType& key)
{
  vtkIdType bucket = this->HashKey(key, this->NumberOfBuckets);
  ItemType item = { key, DataType() };
  vtkIdType index=0;
  
  if(this->Buckets[bucket]->FindItem(item, index) == VTK_OK)
    {
    --this->NumberOfItems;
    return this->Buckets[bucket]->RemoveItem(index);
    }
  return VTK_ERROR;
}

//----------------------------------------------------------------------------
template<class KeyType, class DataType>
void vtkHashMap<KeyType,DataType>::RemoveAllItems()
{
  vtkIdType i;
  for(i=0; i < this->NumberOfBuckets; ++i)
    {
    this->Buckets[i]->RemoveAllItems();
    }
  this->NumberOfItems = 0;
}

//----------------------------------------------------------------------------
template<class KeyType, class DataType>
int vtkHashMap<KeyType,DataType>::GetItem(const KeyType& key, DataType& data)
{
  vtkIdType bucket = this->HashKey(key, this->NumberOfBuckets);
  ItemType item = { key, DataType() };
  vtkIdType index=0;
  
  if(this->Buckets[bucket]->FindItem(item, index) == VTK_OK)
    {
    this->Buckets[bucket]->GetItemNoCheck(index, item);
    data = item.Data;
    return VTK_OK;
    }
  return VTK_ERROR;
}

//----------------------------------------------------------------------------
template<class KeyType, class DataType>
vtkIdType vtkHashMap<KeyType,DataType>::GetNumberOfItems() const
{
  return this->NumberOfItems;
}

//----------------------------------------------------------------------------
template<class KeyType, class DataType>
void vtkHashMap<KeyType,DataType>::SetMaximumLoadFactor(float factor)
{
  // Set the load factor.
  this->MaximumLoadFactor = factor;
  if(this->MaximumLoadFactor < 0)
    {
    this->MaximumLoadFactor = 0;
    }
  
  // Re-hash if necessary.
  this->CheckLoadFactor();
}

//----------------------------------------------------------------------------
template<class KeyType, class DataType>
float vtkHashMap<KeyType,DataType>::GetMaximumLoadFactor() const
{
  return this->MaximumLoadFactor;
}

//----------------------------------------------------------------------------
template<class KeyType, class DataType>
void vtkHashMap<KeyType,DataType>::SetNumberOfBuckets(vtkIdType n)
{
  // Disable automatic rehashing.
  this->MaximumLoadFactor = 0;
  
  // Set the number of buckets.
  this->RehashItems((n > 0)? n : 1);
}

//----------------------------------------------------------------------------
template<class KeyType, class DataType>
vtkIdType vtkHashMap<KeyType,DataType>::GetNumberOfBuckets() const
{
  return this->NumberOfBuckets;
}

//----------------------------------------------------------------------------
template<class KeyType, class DataType>
void vtkHashMap<KeyType,DataType>::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  vtkIdType i;
  vtkIdType min = this->NumberOfItems;
  vtkIdType max = 0;
  for(i=0;i < this->NumberOfBuckets; ++i)
    {
    vtkIdType num = this->Buckets[i]->GetNumberOfItems();
    if(num < min) { min = num; }
    if(num > max) { max = num; }
    }
  float loadFactor = float(this->NumberOfItems) / float(this->NumberOfBuckets);
  
  os << indent << "NumberOfItems: " << this->NumberOfItems << "\n";
  os << indent << "NumberOfBuckets: " << this->NumberOfBuckets << "\n";
  os << indent << "MaximumLoadFactor: " << this->MaximumLoadFactor << "\n";
  os << indent << "Current Load Factor: " << loadFactor << "\n";
  os << indent << "Min in Bucket: " << min << "\n";
  os << indent << "Max in Bucket: " << max << "\n";
}

//----------------------------------------------------------------------------
template<class KeyType, class DataType>
vtkHashMap<KeyType,DataType>::vtkHashMap() 
{
  this->NumberOfItems = 0;
  this->NumberOfBuckets = 17;
  this->Buckets = new BucketType*[this->NumberOfBuckets];
  vtkIdType i;
  for(i=0; i < this->NumberOfBuckets; ++i)
    {
    this->Buckets[i] = BucketType::New();
    }
  this->MaximumLoadFactor = 2.0;
}

//----------------------------------------------------------------------------
template<class KeyType, class DataType>
vtkHashMap<KeyType,DataType>::~vtkHashMap() 
{
  vtkIdType i;
  for(i=0; i < this->NumberOfBuckets; ++i)
    {
    this->Buckets[i]->Delete();
    }
  delete [] this->Buckets;
}

//----------------------------------------------------------------------------
template<class KeyType, class DataType>
void vtkHashMap<KeyType,DataType>::CheckLoadFactor()
{
  if(this->MaximumLoadFactor == 0) { return; }
  float loadFactor = float(this->NumberOfItems) / float(this->NumberOfBuckets);
  if(loadFactor > this->MaximumLoadFactor)
    {
    // Load factor is too big.  Increase the number of buckets.
    this->RehashItems((this->NumberOfBuckets*2)+7);
    }
}

//----------------------------------------------------------------------------
template<class KeyType, class DataType>
void vtkHashMap<KeyType,DataType>::RehashItems(vtkIdType newNumberOfBuckets)
{
  if(newNumberOfBuckets < 1) { return; }
  if(this->NumberOfBuckets == newNumberOfBuckets) { return; }
  
  // Create new buckets.
  BucketType** newBuckets = new BucketType*[newNumberOfBuckets];
  vtkIdType i;
  for(i=0;i < newNumberOfBuckets;++i)
    {
    newBuckets[i] = BucketType::New();
    }
  
  // Re-hash the items.
  ItemType item = { KeyType(), DataType() };
  vtkIdType bucket;
  IteratorType* it = this->NewIterator();
  while(!it->IsDoneWithTraversal())
    {
    //it->GetKeyAndData(item.Key, item.Data);
    it->Iterator->GetData(item); // more efficient than GetKeyAndData()
    bucket = this->HashKey(item.Key, newNumberOfBuckets);
    newBuckets[bucket]->AppendItem(item);
    it->GoToNextItem();
    }
  it->Delete();

  // Delete the old buckets.
  for(i=0; i < this->NumberOfBuckets; ++i)
    {
    this->Buckets[i]->Delete();
    }
  delete [] this->Buckets;
  
  // Save the new buckets.
  this->Buckets = newBuckets;
  this->NumberOfBuckets = newNumberOfBuckets;
}

//----------------------------------------------------------------------------

#if defined ( _MSC_VER )
template <class KeyType,class DataType>
vtkHashMap<KeyType,DataType>::vtkHashMap(const vtkHashMap<KeyType,DataType>&){}
template <class KeyType,class DataType>
void vtkHashMap<KeyType,DataType>::operator=(const vtkHashMap<KeyType,DataType>&){}
#endif

#endif