1 //========================================================================
5 // Copyright 2001-2003 Glyph & Cog, LLC
7 //========================================================================
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
16 #include "GooString.h"
19 //------------------------------------------------------------------------
21 struct GooHashBucket
{
35 //------------------------------------------------------------------------
37 GooHash::GooHash(GBool deleteKeysA
) {
40 deleteKeys
= deleteKeysA
;
42 tab
= (GooHashBucket
**)gmallocn(size
, sizeof(GooHashBucket
*));
43 for (h
= 0; h
< size
; ++h
) {
53 for (h
= 0; h
< size
; ++h
) {
66 void GooHash::add(GooString
*key
, void *val
) {
70 // expand the table if necessary
76 p
= new GooHashBucket
;
85 void GooHash::add(GooString
*key
, int val
) {
89 // expand the table if necessary
95 p
= new GooHashBucket
;
104 void GooHash::replace(GooString
*key
, void *val
) {
108 if ((p
= find(key
, &h
))) {
116 void GooHash::replace(GooString
*key
, int val
) {
120 if ((p
= find(key
, &h
))) {
128 void *GooHash::lookup(GooString
*key
) {
132 if (!(p
= find(key
, &h
))) {
138 int GooHash::lookupInt(GooString
*key
) {
142 if (!(p
= find(key
, &h
))) {
148 void *GooHash::lookup(char *key
) {
152 if (!(p
= find(key
, &h
))) {
158 int GooHash::lookupInt(char *key
) {
162 if (!(p
= find(key
, &h
))) {
168 void *GooHash::remove(GooString
*key
) {
174 if (!(p
= find(key
, &h
))) {
191 int GooHash::removeInt(GooString
*key
) {
197 if (!(p
= find(key
, &h
))) {
214 void *GooHash::remove(char *key
) {
220 if (!(p
= find(key
, &h
))) {
237 int GooHash::removeInt(char *key
) {
243 if (!(p
= find(key
, &h
))) {
260 void GooHash::startIter(GooHashIter
**iter
) {
261 *iter
= new GooHashIter
;
266 GBool
GooHash::getNext(GooHashIter
**iter
, GooString
**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
GooHash::getNext(GooHashIter
**iter
, GooString
**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 GooHash::killIter(GooHashIter
**iter
) {
311 void GooHash::expand() {
312 GooHashBucket
**oldTab
;
319 tab
= (GooHashBucket
**)gmallocn(size
, sizeof(GooHashBucket
*));
320 for (h
= 0; h
< size
; ++h
) {
323 for (i
= 0; i
< oldSize
; ++i
) {
326 oldTab
[i
] = oldTab
[i
]->next
;
335 GooHashBucket
*GooHash::find(GooString
*key
, int *h
) {
339 for (p
= tab
[*h
]; p
; p
= p
->next
) {
340 if (!p
->key
->cmp(key
)) {
347 GooHashBucket
*GooHash::find(char *key
, int *h
) {
351 for (p
= tab
[*h
]; p
; p
= p
->next
) {
352 if (!p
->key
->cmp(key
)) {
359 int GooHash::hash(GooString
*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 GooHash::hash(char *key
) {
376 for (p
= key
; *p
; ++p
) {
377 h
= 17 * h
+ (int)(*p
& 0xff);
379 return (int)(h
% size
);