Annotation of parser3/src/include/pa_hash.h, revision 1.19
1.1 paf 1: /*
1.19 ! paf 2: $Id: pa_hash.h,v 1.18 2001/02/25 13:23:00 paf Exp $
1.1 paf 3: */
4:
5: /*
6:
7:
8: */
9:
10: #ifndef PA_HASH_H
11: #define PA_HASH_H
12:
13: #include <stddef.h>
14:
1.14 paf 15: #include "pa_pool.h"
1.1 paf 16: #include "pa_types.h"
17: #include "pa_string.h"
18:
1.16 paf 19: class Hash : virtual public Pooled {
1.1 paf 20: public:
21:
1.3 paf 22: typedef String Key;
23: typedef void Value;
24:
1.8 paf 25: public:
26:
1.15 paf 27: Hash(Pool& apool,bool) : Pooled(apool) { construct(apool, true); }
28: Hash(Pool& apool) : Pooled(apool) { construct(apool, false); }
1.10 paf 29:
1.8 paf 30: // useful generic hash function
1.19 ! paf 31: static uint generic_code(uint aresult, const char *start, uint allocated);
1.8 paf 32:
1.18 paf 33: // put a [value] under the [key], return existed or not
34: /*SYNCHRONIZED*/ bool put(const Key& key, Value *value);
1.8 paf 35:
36: // get associated [value] by the [key]
1.15 paf 37: /*SYNCHRONIZED*/ Value *get(const Key& key) const;
1.17 paf 38:
39: // put a [value] under the [key] if that [key] existed, return existed or not
1.18 paf 40: /*SYNCHRONIZED*/ bool put_replace(const Key& key, Value *value);
41:
42: // put a [value] under the [key] if that [key] NOT existed, return existed or not
43: /*SYNCHRONIZED*/ bool put_dont_replace(const Key& key, Value *value);
44:
45: // put all 'src' values if NO with same key existed
46: /*SYNCHRONIZED*/ void merge_dont_replace(const Hash& src);
1.11 paf 47:
1.13 paf 48: void put(const Key& key, int value) { put(key, reinterpret_cast<Value *>(value)); }
49: void put(const Key& key, String *value) { put(key, static_cast<Value *>(value)); }
1.11 paf 50:
1.13 paf 51: int get_int(const Key& key) { return reinterpret_cast<int>(get(key)); }
52: String *get_string(const Key& key) { return static_cast<String *>(get(key)); }
1.10 paf 53:
1.19 ! paf 54: int size() { return used; }
! 55:
1.15 paf 56: protected:
57:
58: void construct(Pool& apool, bool athread_safe);
59:
1.1 paf 60: private:
61:
1.19 ! paf 62: // expand when these %% of allocated exausted
1.1 paf 63: enum {
64: THRESHOLD_PERCENT=75
65: };
66:
1.9 paf 67: // am I thread-safe?
68: bool thread_safe;
69:
1.19 ! paf 70: // the index of [allocated] in [allocates]
! 71: int allocates_index;
1.1 paf 72:
1.19 ! paf 73: // possible [allocates]. prime numbers
! 74: static uint allocates[];
! 75: static int allocates_count;
1.1 paf 76:
77: // number of allocated pairs
1.19 ! paf 78: int allocated;
1.1 paf 79:
80: // helper: expanding when used == threshold
81: int threshold;
82:
83: // used pairs
84: int used;
85:
86: // main storage
87: class Pair {
1.2 paf 88: friend Hash;
89:
1.1 paf 90: uint code;
1.15 paf 91: const Key key;
1.1 paf 92: Value *value;
93: Pair *link;
1.2 paf 94:
1.19 ! paf 95: void *operator new(size_t allocated, Pool& apool);
1.2 paf 96:
1.13 paf 97: Pair(uint acode, const Key& akey, Value *avalue, Pair *alink) :
1.1 paf 98: code(acode),
99: key(akey),
100: value(avalue),
1.2 paf 101: link(alink) {}
102: } **refs;
1.1 paf 103:
1.5 paf 104: // filled to threshold: needs expanding
105: bool full() { return used==threshold; }
106:
107: // allocate larger buffer & rehash
1.1 paf 108: void expand();
1.4 paf 109:
110: private: //disabled
111:
1.11 paf 112: //Hash(Hash&) {}
1.12 paf 113: Hash& operator = (const Hash&) { return *this; }
1.1 paf 114: };
115:
116: #endif
E-mail: