4a6e08d68965d2c5ac360c89c55d474c75bbece0
[swftools.git] / GHash.h
1 //========================================================================
2 //
3 // GHash.h
4 //
5 // Copyright 2001-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #ifndef GHASH_H
10 #define GHASH_H
11
12 #include <aconf.h>
13
14 #ifdef USE_GCC_PRAGMAS
15 #pragma interface
16 #endif
17
18 #include "gtypes.h"
19
20 class GString;
21 struct GHashBucket;
22 struct GHashIter;
23
24 //------------------------------------------------------------------------
25
26 class GHash {
27 public:
28
29   GHash(GBool deleteKeysA = gFalse);
30   ~GHash();
31   void add(GString *key, void *val);
32   void add(GString *key, int val);
33   void *lookup(GString *key);
34   int lookupInt(GString *key);
35   void *lookup(char *key);
36   int lookupInt(char *key);
37   void *remove(GString *key);
38   int removeInt(GString *key);
39   void *remove(char *key);
40   int removeInt(char *key);
41   int getLength() { return len; }
42   void startIter(GHashIter **iter);
43   GBool getNext(GHashIter **iter, GString **key, void **val);
44   GBool getNext(GHashIter **iter, GString **key, int *val);
45   void killIter(GHashIter **iter);
46
47 private:
48
49   void expand();
50   GHashBucket *find(GString *key, int *h);
51   GHashBucket *find(char *key, int *h);
52   int hash(GString *key);
53   int hash(char *key);
54
55   GBool deleteKeys;             // set if key strings should be deleted
56   int size;                     // number of buckets
57   int len;                      // number of entries
58   GHashBucket **tab;
59 };
60
61 #define deleteGHash(hash, T)                       \
62   do {                                             \
63     GHash *_hash = (hash);                         \
64     {                                              \
65       GHashIter *_iter;                            \
66       GString *_key;                               \
67       void *_p;                                    \
68       _hash->startIter(&_iter);                    \
69       while (_hash->getNext(&_iter, &_key, &_p)) { \
70         delete (T*)_p;                             \
71       }                                            \
72       delete _hash;                                \
73     }                                              \
74   } while(0)
75
76 #endif