1 //========================================================================
5 // Copyright 2001-2003 Glyph & Cog, LLC
7 //========================================================================
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
19 //------------------------------------------------------------------------
35 //------------------------------------------------------------------------
37 GHash::GHash(GBool deleteKeysA) {
40 deleteKeys = deleteKeysA;
42 tab = (GHashBucket **)gmallocn(size, sizeof(GHashBucket *));
43 for (h = 0; h < size; ++h) {
53 for (h = 0; h < size; ++h) {
66 void GHash::add(GString *key, void *val) {
70 // expand the table if necessary
85 void GHash::add(GString *key, int val) {
89 // expand the table if necessary
104 void GHash::replace(GString *key, void *val) {
108 if ((p = find(key, &h))) {
116 void GHash::replace(GString *key, int val) {
120 if ((p = find(key, &h))) {
128 void *GHash::lookup(GString *key) {
132 if (!(p = find(key, &h))) {
138 int GHash::lookupInt(GString *key) {
142 if (!(p = find(key, &h))) {
148 void *GHash::lookup(char *key) {
152 if (!(p = find(key, &h))) {
158 int GHash::lookupInt(char *key) {
162 if (!(p = find(key, &h))) {
168 void *GHash::remove(GString *key) {
174 if (!(p = find(key, &h))) {
191 int GHash::removeInt(GString *key) {
197 if (!(p = find(key, &h))) {
214 void *GHash::remove(char *key) {
220 if (!(p = find(key, &h))) {
237 int GHash::removeInt(char *key) {
243 if (!(p = find(key, &h))) {
260 void GHash::startIter(GHashIter **iter) {
261 *iter = new GHashIter;
266 GBool GHash::getNext(GHashIter **iter, GString **key, void **val) {
271 (*iter)->p = (*iter)->p->next;
273 while (!(*iter)->p) {
274 if (++(*iter)->h == size) {
279 (*iter)->p = tab[(*iter)->h];
281 *key = (*iter)->p->key;
282 *val = (*iter)->p->val.p;
286 GBool GHash::getNext(GHashIter **iter, GString **key, int *val) {
291 (*iter)->p = (*iter)->p->next;
293 while (!(*iter)->p) {
294 if (++(*iter)->h == size) {
299 (*iter)->p = tab[(*iter)->h];
301 *key = (*iter)->p->key;
302 *val = (*iter)->p->val.i;
306 void GHash::killIter(GHashIter **iter) {
311 void GHash::expand() {
312 GHashBucket **oldTab;
319 tab = (GHashBucket **)gmallocn(size, sizeof(GHashBucket *));
320 for (h = 0; h < size; ++h) {
323 for (i = 0; i < oldSize; ++i) {
326 oldTab[i] = oldTab[i]->next;
335 GHashBucket *GHash::find(GString *key, int *h) {
339 for (p = tab[*h]; p; p = p->next) {
340 if (!p->key->cmp(key)) {
347 GHashBucket *GHash::find(char *key, int *h) {
351 for (p = tab[*h]; p; p = p->next) {
352 if (!p->key->cmp(key)) {
359 int GHash::hash(GString *key) {
365 for (p = key->getCString(), i = 0; i < key->getLength(); ++p, ++i) {
366 h = 17 * h + (int)(*p & 0xff);
368 return (int)(h % size);
371 int GHash::hash(char *key) {
376 for (p = key; *p; ++p) {
377 h = 17 * h + (int)(*p & 0xff);
379 return (int)(h % size);