1 //========================================================================
5 // Copyright 2001-2002 Glyph & Cog, LLC
7 //========================================================================
10 #pragma implementation
18 //------------------------------------------------------------------------
31 //------------------------------------------------------------------------
33 GHash::GHash(GBool deleteKeysA) {
36 deleteKeys = deleteKeysA;
38 tab = (GHashBucket **)gmalloc(size * sizeof(GHashBucket *));
39 for (h = 0; h < size; ++h) {
49 for (h = 0; h < size; ++h) {
62 void GHash::add(GString *key, void *val) {
67 // expand the table if necessary
72 tab = (GHashBucket **)gmalloc(size * sizeof(GHashBucket *));
73 for (h = 0; h < size; ++h) {
76 for (i = 0; i < oldSize; ++i) {
79 oldTab[i] = oldTab[i]->next;
98 void *GHash::lookup(GString *key) {
102 if (!(p = find(key, &h))) {
108 void *GHash::lookup(char *key) {
112 if (!(p = find(key, &h))) {
118 void *GHash::remove(GString *key) {
124 if (!(p = find(key, &h))) {
141 void *GHash::remove(char *key) {
147 if (!(p = find(key, &h))) {
164 void GHash::startIter(GHashIter **iter) {
165 *iter = new GHashIter;
170 GBool GHash::getNext(GHashIter **iter, GString **key, void **val) {
175 (*iter)->p = (*iter)->p->next;
177 while (!(*iter)->p) {
178 if (++(*iter)->h == size) {
183 (*iter)->p = tab[(*iter)->h];
185 *key = (*iter)->p->key;
186 *val = (*iter)->p->val;
190 void GHash::killIter(GHashIter **iter) {
195 GHashBucket *GHash::find(GString *key, int *h) {
199 for (p = tab[*h]; p; p = p->next) {
200 if (!p->key->cmp(key)) {
207 GHashBucket *GHash::find(char *key, int *h) {
211 for (p = tab[*h]; p; p = p->next) {
212 if (!p->key->cmp(key)) {
219 int GHash::hash(GString *key) {
225 for (p = key->getCString(), i = 0; i < key->getLength(); ++p, ++i) {
226 h = 17 * h + (int)(*p & 0xff);
228 return (int)(h % size);
231 int GHash::hash(char *key) {
236 for (p = key; *p; ++p) {
237 h = 17 * h + (int)(*p & 0xff);
239 return (int)(h % size);