Annotation of parser3/src/include/pa_hash.h, revision 1.50
1.28 paf 1: /** @file
1.29 paf 2: Parser: hash class decl.
3:
1.21 paf 4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.29 paf 5:
1.50 ! paf 6: Author: Alexander Petrosyan <paf@design.ru> (http://paf.design.ru)
1.1 paf 7:
1.50 ! paf 8: $Id: pa_hash.h,v 1.49 2001/11/01 15:45:27 paf Exp $
1.1 paf 9: */
10:
11: #ifndef PA_HASH_H
12: #define PA_HASH_H
13:
1.41 parser 14: #include "pa_config_includes.h"
1.14 paf 15: #include "pa_pool.h"
1.1 paf 16: #include "pa_types.h"
17: #include "pa_string.h"
18:
1.29 paf 19: /**
20: Pooled hash.
21:
22: Automatically rehashed when almost full.
23: */
1.23 paf 24: class Hash : public Pooled {
1.1 paf 25: public:
26:
1.29 paf 27: typedef String Key; ///< hash Key type. longing for templates
1.30 paf 28: typedef void Val; ///< hash Val type. longing for templates
1.3 paf 29:
1.31 paf 30: /// for_each iterator function type
31: typedef void (*For_each_func)(const Key& key, Val *value, void *info);
1.25 paf 32:
1.45 paf 33: /// for_each iterator function type
34: typedef void (*For_each_func_refed)(const Key& key, Val *& value, void *info);
35:
1.38 paf 36: /// first_that iterator function type
1.42 parser 37: typedef void *(*First_that_func)(const Key& key, Val *value, void *info);
1.38 paf 38:
1.8 paf 39: public:
40:
1.35 paf 41: Hash(Pool& apool) : Pooled(apool) {
1.43 parser 42: construct_new();
43: }
44:
45: Hash(const Hash& source) : Pooled(source.pool()){
46: construct_copy(source);
1.24 paf 47: }
1.10 paf 48:
1.29 paf 49: /// useful generic hash function
1.19 paf 50: static uint generic_code(uint aresult, const char *start, uint allocated);
1.8 paf 51:
1.29 paf 52: /// put a [value] under the [key], return existed or not
1.40 parser 53: bool put(const Key& key, Val *value);
1.48 paf 54:
55: /// remove the [key]
56: void remove(const Key& key);
1.33 paf 57: /*
58: /// dirty hack to allow constant items storage. I long for Hash<const Val*>
1.40 parser 59: bool put(const Key& key, const Val *value) {
1.33 paf 60: return put(key, const_cast<Val *>(value));
61: }
62: */
1.29 paf 63: /// get associated [value] by the [key]
1.40 parser 64: Val *get(const Key& key) const;
1.17 paf 65:
1.29 paf 66: /// put a [value] under the [key] if that [key] existed, return existed or not
1.40 parser 67: bool put_replace(const Key& key, Val *value);
1.18 paf 68:
1.29 paf 69: /// put a [value] under the [key] if that [key] NOT existed, return existed or not
1.40 parser 70: bool put_dont_replace(const Key& key, Val *value);
1.18 paf 71:
1.29 paf 72: /// put all 'src' values if NO with same key existed
1.40 parser 73: void merge_dont_replace(const Hash& src);
1.11 paf 74:
1.30 paf 75: void put(const Key& key, int value) { put(key, reinterpret_cast<Val *>(value)); }
1.36 paf 76: void put(const Key& key, const String *value) {
77: put(key, static_cast<Val *>(const_cast<String *>(value)));
78: }
1.11 paf 79:
1.29 paf 80: //@{
81: /// handy get, longing for Hash<int>, Hash<String *>
1.32 paf 82: int get_int(const Key& key) const { return reinterpret_cast<int>(get(key)); }
83: const String *get_string(const Key& key) const { return static_cast<String *>(get(key)); }
1.29 paf 84: //@}
1.10 paf 85:
1.29 paf 86: /// number of elements in hash
1.44 parser 87: int size() const { return count; }
1.25 paf 88:
1.29 paf 89: /// iterate over all not zero elements
1.36 paf 90: void for_each(For_each_func func, void *info=0) const;
1.45 paf 91:
92: /// iterate over all not zero elements, passing references
93: void for_each(For_each_func_refed func, void *info=0) const;
1.38 paf 94:
95: /// iterate over all elements until condition
1.42 parser 96: void *first_that(First_that_func func, void *info=0) const;
1.27 paf 97:
1.29 paf 98: /// remove all elements
1.27 paf 99: void clear();
1.19 paf 100:
1.15 paf 101: protected:
102:
1.43 parser 103: void construct_new();
104: void construct_copy(const Hash& source);
1.15 paf 105:
1.1 paf 106: private:
107:
1.39 paf 108: /// expand when these %% of allocated exausted
1.1 paf 109: enum {
110: THRESHOLD_PERCENT=75
111: };
1.9 paf 112:
1.39 paf 113: /// the index of [allocated] in [allocates]
1.19 paf 114: int allocates_index;
1.1 paf 115:
1.39 paf 116: /// possible [allocates]. prime numbers
1.19 paf 117: static uint allocates[];
118: static int allocates_count;
1.1 paf 119:
1.39 paf 120: /// number of allocated pairs
1.19 paf 121: int allocated;
1.1 paf 122:
1.39 paf 123: /// helper: expanding when used == threshold
1.1 paf 124: int threshold;
125:
1.39 paf 126: /// used pairs
1.1 paf 127: int used;
1.44 parser 128:
129: /// stored pairs total (including those by links)
130: int count;
1.1 paf 131:
1.39 paf 132: /// pair storage
1.1 paf 133: class Pair {
1.46 paf 134: friend class Hash;
1.2 paf 135:
1.1 paf 136: uint code;
1.47 paf 137: const Key& key;
1.30 paf 138: Val *value;
1.1 paf 139: Pair *link;
1.2 paf 140:
1.19 paf 141: void *operator new(size_t allocated, Pool& apool);
1.2 paf 142:
1.30 paf 143: Pair(uint acode, const Key& akey, Val *avalue, Pair *alink) :
1.1 paf 144: code(acode),
145: key(akey),
146: value(avalue),
1.2 paf 147: link(alink) {}
148: } **refs;
1.1 paf 149:
1.39 paf 150: /// filled to threshold: needs expanding
1.5 paf 151: bool full() { return used==threshold; }
152:
1.39 paf 153: /// allocate larger buffer & rehash
1.1 paf 154: void expand();
1.4 paf 155:
156: private: //disabled
157:
1.12 paf 158: Hash& operator = (const Hash&) { return *this; }
1.1 paf 159: };
1.49 paf 160:
1.1 paf 161:
162: #endif
E-mail: