]> git.saurik.com Git - wxWidgets.git/blob - include/wx/hash.h
Ok. Vidwin works again on Windows.
[wxWidgets.git] / include / wx / hash.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/hash.h
3 // Purpose: wxHashTable class
4 // Author: Julian Smart
5 // Modified by: VZ at 25.02.00: type safe hashes with WX_DECLARE_HASH()
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_HASH_H__
13 #define _WX_HASH_H__
14
15 #ifdef __GNUG__
16 #pragma interface "hash.h"
17 #endif
18
19 #include "wx/list.h"
20
21 // the default size of the hash
22 #define wxHASH_SIZE_DEFAULT (1000)
23
24 /*
25 * A hash table is an array of user-definable size with lists
26 * of data items hanging off the array positions. Usually there'll
27 * be a hit, so no search is required; otherwise we'll have to run down
28 * the list to find the desired item.
29 */
30
31 // ----------------------------------------------------------------------------
32 // this is the base class for object hashes: hash tables which contain
33 // pointers to objects
34 // ----------------------------------------------------------------------------
35
36 class WXDLLEXPORT wxHashTableBase : public wxObject
37 {
38 public:
39 wxHashTableBase();
40
41 void Create(wxKeyType keyType = wxKEY_INTEGER,
42 size_t size = wxHASH_SIZE_DEFAULT);
43 void Destroy();
44
45 size_t GetSize() const { return m_hashSize; }
46 size_t GetCount() const { return m_count; }
47
48 void DeleteContents(bool flag);
49
50 protected:
51 // find the node for (key, value)
52 wxNodeBase *GetNode(long key, long value) const;
53
54 // the array of lists in which we store the values for given key hash
55 wxListBase **m_hashTable;
56
57 // the size of m_lists array
58 size_t m_hashSize;
59
60 // the type of indexing we use
61 wxKeyType m_keyType;
62
63 // the total number of elements in the hash
64 size_t m_count;
65
66 // should we delete our data?
67 bool m_deleteContents;
68
69 private:
70 // no copy ctor/assignment operator (yet)
71 DECLARE_NO_COPY_CLASS(wxHashTableBase);
72 };
73
74 // ----------------------------------------------------------------------------
75 // for compatibility only
76 // ----------------------------------------------------------------------------
77
78 class WXDLLEXPORT wxHashTable : public wxObject
79 {
80 public:
81 int n;
82 int current_position;
83 wxNode *current_node;
84
85 unsigned int key_type;
86 wxList **hash_table;
87
88 wxHashTable(int the_key_type = wxKEY_INTEGER,
89 int size = wxHASH_SIZE_DEFAULT);
90 ~wxHashTable();
91
92 // copy ctor and assignment operator
93 wxHashTable(const wxHashTable& table) { DoCopy(table); }
94 wxHashTable& operator=(const wxHashTable& table)
95 { Clear(); DoCopy(table); return *this; }
96
97 void DoCopy(const wxHashTable& table);
98
99 void Destroy();
100
101 bool Create(int the_key_type = wxKEY_INTEGER,
102 int size = wxHASH_SIZE_DEFAULT);
103
104 // Note that there are 2 forms of Put, Get.
105 // With a key and a value, the *value* will be checked
106 // when a collision is detected. Otherwise, if there are
107 // 2 items with a different value but the same key,
108 // we'll retrieve the WRONG ONE. So where possible,
109 // supply the required value along with the key.
110 // In fact, the value-only versions make a key, and still store
111 // the value. The use of an explicit key might be required
112 // e.g. when combining several values into one key.
113 // When doing that, it's highly likely we'll get a collision,
114 // e.g. 1 + 2 = 3, 2 + 1 = 3.
115
116 // key and value are NOT necessarily the same
117 void Put(long key, long value, wxObject *object);
118 void Put(long key, const wxChar *value, wxObject *object);
119
120 // key and value are the same
121 void Put(long value, wxObject *object);
122 void Put(const wxChar *value, wxObject *object);
123
124 // key and value not the same
125 wxObject *Get(long key, long value) const;
126 wxObject *Get(long key, const wxChar *value) const;
127
128 // key and value are the same
129 wxObject *Get(long value) const;
130 wxObject *Get(const wxChar *value) const;
131
132 // Deletes entry and returns data if found
133 wxObject *Delete(long key);
134 wxObject *Delete(const wxChar *key);
135
136 wxObject *Delete(long key, int value);
137 wxObject *Delete(long key, const wxChar *value);
138
139 // Construct your own integer key from a string, e.g. in case
140 // you need to combine it with something
141 long MakeKey(const wxChar *string) const;
142
143 // Way of iterating through whole hash table (e.g. to delete everything)
144 // Not necessary, of course, if you're only storing pointers to
145 // objects maintained separately
146
147 void BeginFind();
148 wxNode *Next();
149
150 void DeleteContents(bool flag);
151 void Clear();
152
153 // Returns number of nodes
154 size_t GetCount() const { return m_count; }
155
156 private:
157 size_t m_count; // number of elements in the hashtable
158 bool m_deleteContents;
159
160 DECLARE_DYNAMIC_CLASS(wxHashTable)
161 };
162
163 // defines a new type safe hash table which stores the elements of type eltype
164 // in lists of class listclass
165 #define WX_DECLARE_HASH(eltype, listclass, hashclass) \
166 class WXDLLEXPORT hashclass : public wxHashTableBase \
167 { \
168 public: \
169 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
170 size_t size = wxHASH_SIZE_DEFAULT) \
171 { Create(keyType, size); } \
172 \
173 ~hashclass() { Destroy(); } \
174 \
175 void Put(long key, long val, eltype *data) { DoPut(key, val, data); } \
176 void Put(long key, eltype *data) { DoPut(key, key, data); } \
177 \
178 eltype *Get(long key, long value) const \
179 { \
180 wxNodeBase *node = GetNode(key, value); \
181 return node ? ((listclass::Node *)node)->GetData() : (eltype *)0; \
182 } \
183 eltype *Get(long key) const { return Get(key, key); } \
184 \
185 eltype *Delete(long key, long value) \
186 { \
187 eltype *data; \
188 \
189 wxNodeBase *node = GetNode(key, value); \
190 if ( node ) \
191 { \
192 data = ((listclass::Node *)node)->GetData(); \
193 \
194 delete node; \
195 m_count--; \
196 } \
197 else \
198 { \
199 data = (eltype *)0; \
200 } \
201 \
202 return data; \
203 } \
204 eltype *Delete(long key) { return Delete(key, key); } \
205 \
206 protected: \
207 void DoPut(long key, long value, eltype *data) \
208 { \
209 size_t slot = (size_t)abs(key % m_hashSize); \
210 \
211 if ( !m_hashTable[slot] ) \
212 { \
213 m_hashTable[slot] = new listclass(m_keyType); \
214 if ( m_deleteContents ) \
215 m_hashTable[slot]->DeleteContents(TRUE); \
216 } \
217 \
218 ((listclass *)m_hashTable[slot])->Append(value, data); \
219 m_count++; \
220 } \
221 }
222
223 #endif
224 // _WX_HASH_H__