46 #include <Inventor/lists/SbList.h> 91 for (i = 0; i < 32; i++) {
110 template <
class Type,
class Key>
118 return (
void*) entry;
120 void operator delete(
void * ptr) {
137 template <
class Type,
class Key>
144 SmHash(
unsigned int sizearg = 256,
float loadfactorarg = 0.0f)
146 this->commonConstructor(sizearg, loadfactorarg);
151 this->commonConstructor(from.size, from.loadfactor);
158 from.
apply(SmHash::copy_data,
this);
166 delete [] this->buckets;
172 for ( i = 0; i < this->size; i++ ) {
173 while ( this->buckets[i] ) {
175 this->buckets[i] = entry->
next;
183 SbBool
put(
const Key & key,
const Type & obj)
185 unsigned int i = this->
getIndex(key);
188 if ( entry->
key == key ) {
202 entry->
next = this->buckets[i];
203 this->buckets[i] = entry;
205 if ( this->elements++ >= this->threshold ) {
211 SbBool
get(
const Key & key, Type & obj)
const 214 unsigned int i = this->
getIndex(key);
215 entry = this->buckets[i];
217 if ( entry->
key == key ) {
226 SbBool
remove(
const Key & key)
228 unsigned int i = this->
getIndex(key);
232 if ( entry->
key == key ) {
235 this->buckets[i] = next;
253 for ( i = 0; i < this->size; i++ ) {
254 elem = this->buckets[i];
256 func(elem->
key, elem->
obj, closure);
264 this->
apply(SmHash::add_to_list, &l);
273 return (uintptr_t) key;
277 unsigned int idx = this->hashfunc(key);
278 return (idx % this->size);
283 if (this->size >= newsize)
return;
285 unsigned int oldsize = this->size;
288 this->size = newsize;
290 this->threshold = (
unsigned int) (newsize * this->loadfactor);
296 for ( i = 0; i < oldsize; i++ ) {
305 delete [] oldbuckets;
309 void commonConstructor(
unsigned int sizearg,
float loadfactorarg)
311 if ( loadfactorarg <= 0.0f ) { loadfactorarg = 0.75f; }
316 this->threshold = (
unsigned int) (s * loadfactorarg);
317 this->loadfactor = loadfactorarg;
323 void getStats(
int & buckets_used,
int & buckets,
int & elements,
float & chain_length_avg,
int & chain_length_max)
326 buckets_used = 0, chain_length_max = 0;
327 for ( i = 0; i < this->size; i++ ) {
328 if ( this->buckets[i] ) {
329 unsigned int chain_l = 0;
336 if ( chain_l > chain_length_max ) { chain_length_max = chain_l; }
339 buckets = this->size;
340 elements = this->elements;
341 chain_length_avg = (float) this->elements / buckets_used;
344 static void copy_data(
const Key & key,
const Type & obj,
void * closure)
347 thisp->
put(key, obj);
350 static void add_to_list(
const Key & key,
const Type & obj,
void * closure)
358 unsigned int elements;
359 unsigned int threshold;
struct cc_memalloc cc_memalloc
Type obj
Definition: SmHash.h:130
Key key
Definition: SmHash.h:129
void append(const Type item)
Definition: SbList.h:187
~SmHash()
Definition: SmHash.h:162
cc_memalloc * cc_memalloc_construct(const unsigned int unitsize)
SmHash(const SmHash &from)
Definition: SmHash.h:149
unsigned int getNumElements(void) const
Definition: SmHash.h:267
static unsigned long smhash_prime_table[32]
Definition: SmHash.h:52
void cc_memalloc_destruct(cc_memalloc *allocator)
void resize(unsigned int newsize)
Definition: SmHash.h:281
void SmHashApplyFunc(const Key &key, const Type &obj, void *closure)
Definition: SmHash.h:141
SmHash & operator=(const SmHash &from)
Definition: SmHash.h:155
void * cc_memalloc_allocate(cc_memalloc *allocator)
SmHash(unsigned int sizearg=256, float loadfactorarg=0.0f)
Definition: SmHash.h:144
void makeKeyList(SbList< Key > &l) const
Definition: SmHash.h:262
SbBool put(const Key &key, const Type &obj)
Definition: SmHash.h:183
static uintptr_t default_hash_func(const Key &key)
Definition: SmHash.h:272
SmHashEntry< Type, Key > * next
Definition: SmHash.h:131
cc_memalloc * memhandler
Definition: SmHash.h:132
void setHashFunc(SmHashFunc *func)
Definition: SmHash.h:269
unsigned long smhash_geq_prime_number(unsigned long num)
Definition: SmHash.h:88
void cc_memalloc_deallocate(cc_memalloc *allocator, void *ptr)
void clear(void)
Definition: SmHash.h:169
void apply(SmHashApplyFunc *func, void *closure) const
Definition: SmHash.h:249
uintptr_t SmHashFunc(const Key &key)
Definition: SmHash.h:140
unsigned int getIndex(const Key &key) const
Definition: SmHash.h:276