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