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
**)gmalloc(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::lookup(GString
*key
) {
108 if (!(p
= find(key
, &h
))) {
114 int GHash::lookupInt(GString
*key
) {
118 if (!(p
= find(key
, &h
))) {
124 void *GHash::lookup(char *key
) {
128 if (!(p
= find(key
, &h
))) {
134 int GHash::lookupInt(char *key
) {
138 if (!(p
= find(key
, &h
))) {
144 void *GHash::remove(GString
*key
) {
150 if (!(p
= find(key
, &h
))) {
167 int GHash::removeInt(GString
*key
) {
173 if (!(p
= find(key
, &h
))) {
190 void *GHash::remove(char *key
) {
196 if (!(p
= find(key
, &h
))) {
213 int GHash::removeInt(char *key
) {
219 if (!(p
= find(key
, &h
))) {
236 void GHash::startIter(GHashIter
**iter
) {
237 *iter
= new GHashIter
;
242 GBool
GHash::getNext(GHashIter
**iter
, GString
**key
, void **val
) {
247 (*iter
)->p
= (*iter
)->p
->next
;
249 while (!(*iter
)->p
) {
250 if (++(*iter
)->h
== size
) {
255 (*iter
)->p
= tab
[(*iter
)->h
];
257 *key
= (*iter
)->p
->key
;
258 *val
= (*iter
)->p
->val
.p
;
262 GBool
GHash::getNext(GHashIter
**iter
, GString
**key
, int *val
) {
267 (*iter
)->p
= (*iter
)->p
->next
;
269 while (!(*iter
)->p
) {
270 if (++(*iter
)->h
== size
) {
275 (*iter
)->p
= tab
[(*iter
)->h
];
277 *key
= (*iter
)->p
->key
;
278 *val
= (*iter
)->p
->val
.i
;
282 void GHash::killIter(GHashIter
**iter
) {
287 void GHash::expand() {
288 GHashBucket
**oldTab
;
295 tab
= (GHashBucket
**)gmalloc(size
* sizeof(GHashBucket
*));
296 for (h
= 0; h
< size
; ++h
) {
299 for (i
= 0; i
< oldSize
; ++i
) {
302 oldTab
[i
] = oldTab
[i
]->next
;
311 GHashBucket
*GHash::find(GString
*key
, int *h
) {
315 for (p
= tab
[*h
]; p
; p
= p
->next
) {
316 if (!p
->key
->cmp(key
)) {
323 GHashBucket
*GHash::find(char *key
, int *h
) {
327 for (p
= tab
[*h
]; p
; p
= p
->next
) {
328 if (!p
->key
->cmp(key
)) {
335 int GHash::hash(GString
*key
) {
341 for (p
= key
->getCString(), i
= 0; i
< key
->getLength(); ++p
, ++i
) {
342 h
= 17 * h
+ (int)(*p
& 0xff);
344 return (int)(h
% size
);
347 int GHash::hash(char *key
) {
352 for (p
= key
; *p
; ++p
) {
353 h
= 17 * h
+ (int)(*p
& 0xff);
355 return (int)(h
% size
);