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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkInstantiator.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.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 "vtkInstantiator.h"
#include "vtkObjectFactory.h"
vtkStandardNewMacro(vtkInstantiator);
// Node in hash table.
class vtkInstantiatorHashNode
{
public:
typedef vtkInstantiator::CreateFunction CreateFunction;
vtkInstantiatorHashNode() { this->ClassName = 0; this->Function = 0; }
void SetClassName(const char* className) { this->ClassName = className; }
const char* GetClassName() { return this->ClassName; }
void SetFunction(CreateFunction function) { this->Function = function; }
CreateFunction GetFunction() { return this->Function; }
private:
const char* ClassName;
CreateFunction Function;
};
// Hash table used by vtkInstantiator. Must not be a vtkObject.
class vtkInstantiatorHashTable
{
public:
vtkInstantiatorHashTable();
~vtkInstantiatorHashTable();
void PrintSelf(ostream& os, vtkIndent indent);
typedef vtkInstantiator::CreateFunction CreateFunction;
void Insert(const char* className, CreateFunction function);
void Erase(const char* className, CreateFunction function);
CreateFunction Find(const char* className);
protected:
unsigned long Hash(const char* s);
void ExtendBucket(unsigned long bucket);
const char* AddClassName(const char* className);
vtkInstantiatorHashNode** Buckets;
unsigned int* BucketCounts;
unsigned int* BucketSizes;
unsigned long NumberOfBuckets;
char** ClassNames;
unsigned long NumberOfClassNames;
unsigned long ClassNamesSize;
private:
vtkInstantiatorHashTable(const vtkInstantiatorHashTable&) VTK_DELETE_FUNCTION;
void operator=(const vtkInstantiatorHashTable&) VTK_DELETE_FUNCTION;
};
//----------------------------------------------------------------------------
vtkInstantiatorHashTable::vtkInstantiatorHashTable()
{
this->NumberOfBuckets = 101;
this->Buckets = new vtkInstantiatorHashNode*[this->NumberOfBuckets];
this->BucketCounts = new unsigned int[this->NumberOfBuckets];
this->BucketSizes = new unsigned int[this->NumberOfBuckets];
unsigned int i;
for(i=0;i < this->NumberOfBuckets;++i)
{
this->BucketCounts[i] = 0;
this->BucketSizes[i] = 16;
this->Buckets[i] = new vtkInstantiatorHashNode[this->BucketSizes[i]];
}
this->NumberOfClassNames = 0;
this->ClassNamesSize = 256;
this->ClassNames = new char*[this->ClassNamesSize];
}
//----------------------------------------------------------------------------
vtkInstantiatorHashTable::~vtkInstantiatorHashTable()
{
unsigned long i;
for(i=0; i < this->NumberOfBuckets;++i)
{
delete [] this->Buckets[i];
}
delete [] this->BucketSizes;
delete [] this->BucketCounts;
delete [] this->Buckets;
for(i=0;i < this->NumberOfClassNames;++i)
{
delete [] this->ClassNames[i];
}
delete [] this->ClassNames;
}
//----------------------------------------------------------------------------
void vtkInstantiatorHashTable::PrintSelf(ostream& os, vtkIndent indent)
{
os << indent << "NumberOfBuckets: " << this->NumberOfBuckets << "\n";
double avgBucketSize = 0;
unsigned long maxBucketSize = 0;
unsigned long minBucketSize = this->NumberOfClassNames;
for(unsigned long i=0;i < this->NumberOfBuckets;++i)
{
avgBucketSize += this->BucketCounts[i];
if(this->BucketCounts[i] > maxBucketSize)
{ maxBucketSize = this->BucketCounts[i]; }
if(this->BucketCounts[i] < minBucketSize)
{ minBucketSize = this->BucketCounts[i]; }
}
avgBucketSize /= double(this->NumberOfBuckets);
os << indent << "Average Bucket Size: " << avgBucketSize << "\n";
os << indent << "Minimum Bucket Size: " << minBucketSize << "\n";
os << indent << "Maximum Bucket Size: " << maxBucketSize << "\n";
}
//----------------------------------------------------------------------------
void vtkInstantiatorHashTable::Insert(const char* className,
CreateFunction function)
{
unsigned long bucket = this->Hash(className);
if(this->BucketCounts[bucket] == this->BucketSizes[bucket])
{ this->ExtendBucket(bucket); }
// Do not check if the class is already registered. It is possible
// that more than one create function will be registered for the
// same class, and even that the same function is registered more
// than once. Each register should have a corresponding unregister.
// As long as any register has not had its corresponding unregister,
// we want to allow the class to be created.
unsigned int pos = this->BucketCounts[bucket]++;
this->Buckets[bucket][pos].SetClassName(this->AddClassName(className));
this->Buckets[bucket][pos].SetFunction(function);
}
//----------------------------------------------------------------------------
void vtkInstantiatorHashTable::Erase(const char* className,
CreateFunction function)
{
unsigned long bucket = this->Hash(className);
// Find the exact registration function we have been given, and
// remove it only once. If more than one funcion has been
// registered for this class, or the same function more than once,
// each register should have its corresponding unregister.
unsigned int i;
for(i=0; i < this->BucketCounts[bucket];++i)
{
if(((this->Buckets[bucket][i].GetFunction() == function)
&& (strcmp(this->Buckets[bucket][i].GetClassName(), className) == 0)))
{
unsigned int j;
--this->BucketCounts[bucket];
for(j=i;j < this->BucketCounts[bucket];++j)
{
this->Buckets[bucket][j] = this->Buckets[bucket][j+1];
}
return;
}
}
}
//----------------------------------------------------------------------------
vtkInstantiatorHashTable::CreateFunction
vtkInstantiatorHashTable::Find(const char* className)
{
unsigned long bucket = this->Hash(className);
unsigned int i;
for(i=0; i < this->BucketCounts[bucket];++i)
{
if(strcmp(this->Buckets[bucket][i].GetClassName(), className) == 0)
{ return this->Buckets[bucket][i].GetFunction(); }
}
return 0;
}
//----------------------------------------------------------------------------
unsigned long vtkInstantiatorHashTable::Hash(const char* s)
{
unsigned long h = 0;
for(;*s;++s) { h = 5*h + *s; }
return h % this->NumberOfBuckets;
}
//----------------------------------------------------------------------------
void vtkInstantiatorHashTable::ExtendBucket(unsigned long bucket)
{
unsigned int newSize = this->BucketSizes[bucket] * 2;
vtkInstantiatorHashNode* newBucket =
new vtkInstantiatorHashNode[newSize];
unsigned int i;
for(i=0; i < this->BucketCounts[bucket];++i)
{ newBucket[i] = this->Buckets[bucket][i]; }
delete [] this->Buckets[bucket];
this->Buckets[bucket] = newBucket;
this->BucketSizes[bucket] = newSize;
}
//----------------------------------------------------------------------------
const char* vtkInstantiatorHashTable::AddClassName(const char* className)
{
if(this->NumberOfClassNames == this->ClassNamesSize)
{
unsigned long newSize = this->ClassNamesSize * 2;
char** newNames = new char*[newSize];
unsigned long i;
for(i=0;i < this->NumberOfClassNames;++i)
{ newNames[i] = this->ClassNames[i]; }
delete [] this->ClassNames;
this->ClassNames = newNames;
this->ClassNamesSize = newSize;
}
char* newName = new char[strlen(className)+1];
strcpy(newName, className);
this->ClassNames[this->NumberOfClassNames++] = newName;
return newName;
}
//----------------------------------------------------------------------------
// Implementation of actual vtkInstantiator class.
//----------------------------------------------------------------------------
vtkInstantiator::vtkInstantiator()
{
}
//----------------------------------------------------------------------------
vtkInstantiator::~vtkInstantiator()
{
}
//----------------------------------------------------------------------------
void vtkInstantiator::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
vtkInstantiator::CreatorTable->PrintSelf(os, indent);
}
//----------------------------------------------------------------------------
vtkObject* vtkInstantiator::CreateInstance(const char* className)
{
CreateFunction function = vtkInstantiator::CreatorTable->Find(className);
if(function) { return function(); }
return 0;
}
//----------------------------------------------------------------------------
void vtkInstantiator::RegisterInstantiator(const char* className,
CreateFunction createFunction)
{
vtkInstantiator::CreatorTable->Insert(className, createFunction);
}
//----------------------------------------------------------------------------
void vtkInstantiator::UnRegisterInstantiator(const char* className,
CreateFunction createFunction)
{
vtkInstantiator::CreatorTable->Erase(className, createFunction);
}
//----------------------------------------------------------------------------
void vtkInstantiator::ClassInitialize()
{
vtkInstantiator::CreatorTable = new vtkInstantiatorHashTable;
}
//----------------------------------------------------------------------------
void vtkInstantiator::ClassFinalize()
{
delete vtkInstantiator::CreatorTable;
}
//----------------------------------------------------------------------------
vtkInstantiatorInitialize::vtkInstantiatorInitialize()
{
if(++vtkInstantiatorInitialize::Count == 1)
{ vtkInstantiator::ClassInitialize(); }
}
//----------------------------------------------------------------------------
vtkInstantiatorInitialize::~vtkInstantiatorInitialize()
{
if(--vtkInstantiatorInitialize::Count == 0)
{ vtkInstantiator::ClassFinalize(); }
}
//----------------------------------------------------------------------------
unsigned int vtkInstantiatorInitialize::Count;
vtkInstantiatorHashTable* vtkInstantiator::CreatorTable;
|