Annotation of parser3/src/include/pa_hash.h, revision 1.34
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.34 ! paf 8: $Id: pa_hash.h,v 1.33 2001/04/03 07:20:52 paf Exp $
1.1 paf 9: */
10:
11: #ifndef PA_HASH_H
12: #define PA_HASH_H
13:
14: #include <stddef.h>
15:
1.14 paf 16: #include "pa_pool.h"
1.1 paf 17: #include "pa_types.h"
18: #include "pa_string.h"
19:
1.29 paf 20:
21: /**
22: Pooled hash.
23:
24: Automatically rehashed when almost full.
25:
1.34 ! paf 26: can be thread safe, wich is specified in constructor,
1.29 paf 27: default 'not safe'.
28: */
1.23 paf 29: class Hash : public Pooled {
1.1 paf 30: public:
31:
1.29 paf 32: typedef String Key; ///< hash Key type. longing for templates
1.30 paf 33: typedef void Val; ///< hash Val type. longing for templates
1.3 paf 34:
1.31 paf 35: /// for_each iterator function type
36: typedef void (*For_each_func)(const Key& key, Val *value, void *info);
1.25 paf 37:
1.8 paf 38: public:
39:
1.24 paf 40: Hash(Pool& apool,bool athread_safe=false) : Pooled(apool) {
41: construct(apool, athread_safe);
42: }
1.10 paf 43:
1.29 paf 44: /// useful generic hash function
1.19 paf 45: static uint generic_code(uint aresult, const char *start, uint allocated);
1.8 paf 46:
1.29 paf 47: /// put a [value] under the [key], return existed or not
1.30 paf 48: /*SYNCHRONIZED*/ bool put(const Key& key, Val *value);
1.33 paf 49: /*
50: /// dirty hack to allow constant items storage. I long for Hash<const Val*>
51: /*SYNCHRONIZED* / bool put(const Key& key, const Val *value) {
52: return put(key, const_cast<Val *>(value));
53: }
54: */
1.29 paf 55: /// get associated [value] by the [key]
1.30 paf 56: /*SYNCHRONIZED*/ Val *get(const Key& key) const;
1.17 paf 57:
1.29 paf 58: /// put a [value] under the [key] if that [key] existed, return existed or not
1.30 paf 59: /*SYNCHRONIZED*/ bool put_replace(const Key& key, Val *value);
1.18 paf 60:
1.29 paf 61: /// put a [value] under the [key] if that [key] NOT existed, return existed or not
1.30 paf 62: /*SYNCHRONIZED*/ bool put_dont_replace(const Key& key, Val *value);
1.18 paf 63:
1.29 paf 64: /// put all 'src' values if NO with same key existed
1.18 paf 65: /*SYNCHRONIZED*/ void merge_dont_replace(const Hash& src);
1.11 paf 66:
1.30 paf 67: void put(const Key& key, int value) { put(key, reinterpret_cast<Val *>(value)); }
68: void put(const Key& key, String *value) { put(key, static_cast<Val *>(value)); }
1.11 paf 69:
1.29 paf 70: //@{
71: /// handy get, longing for Hash<int>, Hash<String *>
1.32 paf 72: int get_int(const Key& key) const { return reinterpret_cast<int>(get(key)); }
73: const String *get_string(const Key& key) const { return static_cast<String *>(get(key)); }
1.29 paf 74: //@}
1.10 paf 75:
1.29 paf 76: /// number of elements in hash
1.19 paf 77: int size() { return used; }
1.25 paf 78:
1.29 paf 79: /// iterate over all not zero elements
1.31 paf 80: void for_each(For_each_func func, void *info=0);
1.27 paf 81:
1.29 paf 82: /// remove all elements
1.27 paf 83: void clear();
1.19 paf 84:
1.15 paf 85: protected:
86:
87: void construct(Pool& apool, bool athread_safe);
88:
1.1 paf 89: private:
90:
1.19 paf 91: // expand when these %% of allocated exausted
1.1 paf 92: enum {
93: THRESHOLD_PERCENT=75
94: };
95:
1.9 paf 96: // am I thread-safe?
1.34 ! paf 97: bool fthread_safe;
1.9 paf 98:
1.19 paf 99: // the index of [allocated] in [allocates]
100: int allocates_index;
1.1 paf 101:
1.19 paf 102: // possible [allocates]. prime numbers
103: static uint allocates[];
104: static int allocates_count;
1.1 paf 105:
106: // number of allocated pairs
1.19 paf 107: int allocated;
1.1 paf 108:
109: // helper: expanding when used == threshold
110: int threshold;
111:
112: // used pairs
113: int used;
114:
115: // main storage
116: class Pair {
1.2 paf 117: friend Hash;
118:
1.1 paf 119: uint code;
1.15 paf 120: const Key key;
1.30 paf 121: Val *value;
1.1 paf 122: Pair *link;
1.2 paf 123:
1.19 paf 124: void *operator new(size_t allocated, Pool& apool);
1.2 paf 125:
1.30 paf 126: Pair(uint acode, const Key& akey, Val *avalue, Pair *alink) :
1.1 paf 127: code(acode),
128: key(akey),
129: value(avalue),
1.2 paf 130: link(alink) {}
131: } **refs;
1.1 paf 132:
1.5 paf 133: // filled to threshold: needs expanding
134: bool full() { return used==threshold; }
135:
136: // allocate larger buffer & rehash
1.1 paf 137: void expand();
1.4 paf 138:
139: private: //disabled
140:
1.11 paf 141: //Hash(Hash&) {}
1.12 paf 142: Hash& operator = (const Hash&) { return *this; }
1.1 paf 143: };
144:
145: #endif
E-mail: