Annotation of parser3/src/include/pa_hash.h, revision 1.24
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.24 ! paf 6: $Id: pa_hash.h,v 1.23 2001/03/11 12:04:43 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.8 paf 24: public:
25:
1.24 ! paf 26: Hash(Pool& apool,bool athread_safe=false) : Pooled(apool) {
! 27: construct(apool, athread_safe);
! 28: }
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)); }
1.20 paf 52: const 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: