Annotation of parser3/src/include/pa_hash.h, revision 1.28
1.28 ! paf 1: /** @file
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.28 ! paf 6: $Id: pa_hash.h,v 1.27 2001/03/18 13:22:05 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.27 paf 59:
60: void clear();
1.19 paf 61:
1.15 paf 62: protected:
63:
64: void construct(Pool& apool, bool athread_safe);
65:
1.1 paf 66: private:
67:
1.19 paf 68: // expand when these %% of allocated exausted
1.1 paf 69: enum {
70: THRESHOLD_PERCENT=75
71: };
72:
1.9 paf 73: // am I thread-safe?
74: bool thread_safe;
75:
1.19 paf 76: // the index of [allocated] in [allocates]
77: int allocates_index;
1.1 paf 78:
1.19 paf 79: // possible [allocates]. prime numbers
80: static uint allocates[];
81: static int allocates_count;
1.1 paf 82:
83: // number of allocated pairs
1.19 paf 84: int allocated;
1.1 paf 85:
86: // helper: expanding when used == threshold
87: int threshold;
88:
89: // used pairs
90: int used;
91:
92: // main storage
93: class Pair {
1.2 paf 94: friend Hash;
95:
1.1 paf 96: uint code;
1.15 paf 97: const Key key;
1.1 paf 98: Value *value;
99: Pair *link;
1.2 paf 100:
1.19 paf 101: void *operator new(size_t allocated, Pool& apool);
1.2 paf 102:
1.13 paf 103: Pair(uint acode, const Key& akey, Value *avalue, Pair *alink) :
1.1 paf 104: code(acode),
105: key(akey),
106: value(avalue),
1.2 paf 107: link(alink) {}
108: } **refs;
1.1 paf 109:
1.5 paf 110: // filled to threshold: needs expanding
111: bool full() { return used==threshold; }
112:
113: // allocate larger buffer & rehash
1.1 paf 114: void expand();
1.4 paf 115:
116: private: //disabled
117:
1.11 paf 118: //Hash(Hash&) {}
1.12 paf 119: Hash& operator = (const Hash&) { return *this; }
1.1 paf 120: };
121:
122: #endif
E-mail: