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