39#ifndef DICTIONARY_HEADER
40#define DICTIONARY_HEADER
52template <
class K,
class V>
61template <
class K,
class V>
72 SHRINK_THRESHOLD(0.20),
73 EXPAND_THRESHOLD(0.80),
76 log2Size=MIN_TABLE_SIZE;
80 while(log2Size) { tableBits++; log2Size>>=1; }
90 memset((
void *)table,0,size*
sizeof(
void *));
131 const double SHRINK_THRESHOLD;
132 const double EXPAND_THRESHOLD;
133 const int MIN_TABLE_SIZE;
139template <
class K,
class V>
147template <
class K,
class V>
153 for (i=0; i<size; i++)
166 while ((getSize()>(
uint32)MIN_TABLE_SIZE)&&(keepSize==
FALSE))
170template <
class K,
class V>
173 uint32 retval=hashFunc(key);
174 retval &= ((1<<tableBits)-1);
175 assert(retval<getSize());
180template <
class K,
class V>
186 fprintf(out,
"--------------------\n");
196 fprintf(out,
"--[ ]");
201 fprintf(out,
"--------------------\n");
205template <
class K,
class V>
217template <
class K,
class V>
224 if ((index<0)||(index >= (
int)
getSize()))
238 while ((temp!=
NULL) && ((
int)i < offset))
266template <
class K,
class V>
273 if ((index<0)||(index >= (
int)
getSize()))
287 while ((temp!=
NULL) && ((
int)i < offset))
313template <
class K,
class V>
319template <
class K,
class V>
325template <
class K,
class V>
340 if ((node->
key)==key)
349template <
class K,
class V>
364template <
class K,
class V>
375 memcpy(&(item->
key),&key,
sizeof(K));
396 { table[offset]=item; }
407 if (
percent>= EXPAND_THRESHOLD ) expand();
413template <
class K,
class V>
436 if (0==memcmp(&(node->
key),&key,
sizeof(K)))
438 if ((node->
key)==key)
447 delete(table[offset]);
450 if (
percent <= SHRINK_THRESHOLD)
462 if (0==memcmp(&(node->
key),&key,
sizeof(K)))
482 if (
percent <= SHRINK_THRESHOLD)
488template <
class K,
class V>
492 return(remove(key,temp));
497template <
class K,
class V>
512 for (i=0; i<(int)getSize(); i++)
526 memcpy(&key,&(node->
key),
sizeof(K));
537 delete(table[offset]);
540 if (
percent <= SHRINK_THRESHOLD)
546template <
class K,
class V>
550 bool retval=getPointer(key,&valptr);
551 if (retval && valptr)
564template <
class K,
class V>
581 while ((node!=
NULL)&&(memcmp(&(node->
key),&key,
sizeof(K))))
583 while ((node!=
NULL)&&( ! ((node->
key)==key)) )
590 *valptr=&(node->
value);
601template <
class K,
class V>
602void Dictionary<K,V>::shrink(
void)
609 if ((size<=(
uint32)MIN_TABLE_SIZE)||(keepSize==
TRUE))
621 memset((
void *)table,0,size*
sizeof(
void *));
623 for (i=0; i<oldsize; i++)
628 offset=keyHash(temp->
key);
640template <
class K,
class V>
641void Dictionary<K,V>::expand(
void)
660 memset((
void *)table,0,size*
sizeof(
void *));
662 for (i=0; i<oldsize; i++)
667 offset=keyHash(temp->
key);
void add(float *sum, float *addend)
bit8 contains(IN K &key) RO
bit8 iterate(INOUT int &index, INOUT int &offset, OUT V &value) const
void print(IN FILE *out) const
bit8 add(IN K &key, IN V &value)
bit8 removeAny(OUT K &key, OUT V &value)
bit8 iterate(INOUT int &index, INOUT int &offset, OUT K &key, OUT V &value) RO
bit8 getValue(IN K &key, OUT V &value)
Dictionary(uint32(*hashFn)(Wstring &key))
bit8 iterate(INOUT int &index, INOUT int &offset, OUT V &value) RO
uint32 getEntries(void) const
bool getPointer(IN K &key, OUT V **value) RO
bool getValue(IN K &key, OUT V &value) RO
Dictionary(uint32(*hashFn)(const K &key))
bit8 remove(IN K &key, OUT V &value)
uint32 getEntries(void) RO
uint32 getSize(void) const
Dictionary< K, V > & operator=(Dictionary< K, V > &other)
bit8 updateValue(IN K &key, IN V &value)