]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: hash.cpp | |
3 | // Purpose: wxHashTable implementation | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "hash.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/list.h" | |
25 | #endif | |
26 | ||
27 | #include "wx/hash.h" | |
28 | ||
29 | #include <string.h> | |
30 | #include <stdarg.h> | |
31 | ||
c801d85f | 32 | IMPLEMENT_DYNAMIC_CLASS(wxHashTable, wxObject) |
c801d85f | 33 | |
debe6624 | 34 | wxHashTable::wxHashTable (int the_key_type, int size) |
c801d85f | 35 | { |
17d8ee1c JS |
36 | n = 0; |
37 | hash_table = (wxList**) NULL; | |
38 | Create(the_key_type, size); | |
5692876f | 39 | m_count = 0; |
c7fb814a | 40 | m_deleteContents = FALSE; |
17d8ee1c | 41 | /* |
c801d85f KB |
42 | n = size; |
43 | current_position = -1; | |
c67daf87 | 44 | current_node = (wxNode *) NULL; |
c801d85f KB |
45 | |
46 | key_type = the_key_type; | |
47 | hash_table = new wxList *[size]; | |
48 | int i; | |
49 | for (i = 0; i < size; i++) | |
c67daf87 | 50 | hash_table[i] = (wxList *) NULL; |
17d8ee1c | 51 | */ |
c801d85f KB |
52 | } |
53 | ||
54 | wxHashTable::~wxHashTable (void) | |
55 | { | |
e55ad60e RR |
56 | Destroy(); |
57 | } | |
58 | ||
59 | void wxHashTable::Destroy(void) | |
60 | { | |
61 | if (!hash_table) return; | |
c801d85f KB |
62 | int i; |
63 | for (i = 0; i < n; i++) | |
64 | if (hash_table[i]) | |
65 | delete hash_table[i]; | |
66 | delete[] hash_table; | |
e55ad60e | 67 | hash_table = NULL; |
c801d85f KB |
68 | } |
69 | ||
debe6624 | 70 | bool wxHashTable::Create(int the_key_type, int size) |
c801d85f | 71 | { |
17d8ee1c JS |
72 | Destroy(); |
73 | ||
c801d85f KB |
74 | n = size; |
75 | current_position = -1; | |
c67daf87 | 76 | current_node = (wxNode *) NULL; |
c801d85f KB |
77 | |
78 | key_type = the_key_type; | |
c801d85f KB |
79 | hash_table = new wxList *[size]; |
80 | int i; | |
81 | for (i = 0; i < size; i++) | |
c67daf87 | 82 | hash_table[i] = (wxList *) NULL; |
c801d85f KB |
83 | return TRUE; |
84 | } | |
85 | ||
4e67bfc7 VS |
86 | |
87 | void wxHashTable::DoCopy(const wxHashTable& table) | |
88 | { | |
89 | n = table.n; | |
90 | current_position = table.current_position; | |
91 | current_node = NULL; // doesn't matter - Next() will reconstruct it | |
92 | key_type = table.key_type; | |
93 | ||
94 | hash_table = new wxList *[n]; | |
95 | for (int i = 0; i < n; i++) { | |
96 | if (table.hash_table[i] == NULL) | |
97 | hash_table[i] = NULL; | |
98 | else { | |
99 | hash_table[i] = new wxList(key_type); | |
100 | *(hash_table[i]) = *(table.hash_table[i]); | |
101 | } | |
102 | } | |
103 | } | |
104 | ||
debe6624 | 105 | void wxHashTable::Put (long key, long value, wxObject * object) |
c801d85f KB |
106 | { |
107 | // Should NEVER be | |
108 | long k = (long) key; | |
c801d85f KB |
109 | |
110 | int position = (int) (k % n); | |
ffae916f BJ |
111 | if (position < 0) position = -position; |
112 | ||
c801d85f | 113 | if (!hash_table[position]) |
c7fb814a | 114 | { |
c801d85f | 115 | hash_table[position] = new wxList (wxKEY_INTEGER); |
c7fb814a VS |
116 | if (m_deleteContents) hash_table[position]->DeleteContents(TRUE); |
117 | } | |
c801d85f KB |
118 | |
119 | hash_table[position]->Append (value, object); | |
5692876f | 120 | m_count++; |
c801d85f KB |
121 | } |
122 | ||
50920146 | 123 | void wxHashTable::Put (long key, const wxChar *value, wxObject * object) |
c801d85f KB |
124 | { |
125 | // Should NEVER be | |
ffae916f | 126 | long k = (long) key; |
c801d85f KB |
127 | |
128 | int position = (int) (k % n); | |
ffae916f BJ |
129 | if (position < 0) position = -position; |
130 | ||
c801d85f | 131 | if (!hash_table[position]) |
c7fb814a | 132 | { |
c801d85f | 133 | hash_table[position] = new wxList (wxKEY_INTEGER); |
c7fb814a VS |
134 | if (m_deleteContents) hash_table[position]->DeleteContents(TRUE); |
135 | } | |
c801d85f KB |
136 | |
137 | hash_table[position]->Append (value, object); | |
5692876f | 138 | m_count++; |
c801d85f KB |
139 | } |
140 | ||
debe6624 | 141 | void wxHashTable::Put (long key, wxObject * object) |
c801d85f KB |
142 | { |
143 | // Should NEVER be | |
144 | long k = (long) key; | |
ffae916f | 145 | |
c801d85f | 146 | int position = (int) (k % n); |
ffae916f BJ |
147 | if (position < 0) position = -position; |
148 | ||
c801d85f | 149 | if (!hash_table[position]) |
c7fb814a | 150 | { |
c801d85f | 151 | hash_table[position] = new wxList (wxKEY_INTEGER); |
c7fb814a VS |
152 | if (m_deleteContents) hash_table[position]->DeleteContents(TRUE); |
153 | } | |
154 | ||
c801d85f | 155 | hash_table[position]->Append (k, object); |
5692876f | 156 | m_count++; |
c801d85f KB |
157 | } |
158 | ||
50920146 | 159 | void wxHashTable::Put (const wxChar *key, wxObject * object) |
c801d85f KB |
160 | { |
161 | int position = (int) (MakeKey (key) % n); | |
ffae916f | 162 | if (position < 0) position = -position; |
c801d85f KB |
163 | |
164 | if (!hash_table[position]) | |
c7fb814a | 165 | { |
c801d85f | 166 | hash_table[position] = new wxList (wxKEY_STRING); |
c7fb814a VS |
167 | if (m_deleteContents) hash_table[position]->DeleteContents(TRUE); |
168 | } | |
c801d85f KB |
169 | |
170 | hash_table[position]->Append (key, object); | |
5692876f | 171 | m_count++; |
c801d85f KB |
172 | } |
173 | ||
debe6624 | 174 | wxObject *wxHashTable::Get (long key, long value) const |
c801d85f KB |
175 | { |
176 | // Should NEVER be | |
177 | long k = (long) key; | |
c801d85f KB |
178 | |
179 | int position = (int) (k % n); | |
ffae916f BJ |
180 | if (position < 0) position = -position; |
181 | ||
c801d85f | 182 | if (!hash_table[position]) |
c67daf87 | 183 | return (wxObject *) NULL; |
c801d85f KB |
184 | else |
185 | { | |
186 | wxNode *node = hash_table[position]->Find (value); | |
187 | if (node) | |
188 | return node->Data (); | |
189 | else | |
c67daf87 | 190 | return (wxObject *) NULL; |
c801d85f KB |
191 | } |
192 | } | |
193 | ||
50920146 | 194 | wxObject *wxHashTable::Get (long key, const wxChar *value) const |
c801d85f KB |
195 | { |
196 | // Should NEVER be | |
197 | long k = (long) key; | |
ffae916f | 198 | |
c801d85f | 199 | int position = (int) (k % n); |
ffae916f BJ |
200 | if (position < 0) position = -position; |
201 | ||
c801d85f | 202 | if (!hash_table[position]) |
c67daf87 | 203 | return (wxObject *) NULL; |
c801d85f KB |
204 | else |
205 | { | |
206 | wxNode *node = hash_table[position]->Find (value); | |
207 | if (node) | |
208 | return node->Data (); | |
209 | else | |
c67daf87 | 210 | return (wxObject *) NULL; |
c801d85f KB |
211 | } |
212 | } | |
213 | ||
debe6624 | 214 | wxObject *wxHashTable::Get (long key) const |
c801d85f KB |
215 | { |
216 | // Should NEVER be | |
217 | long k = (long) key; | |
c801d85f KB |
218 | |
219 | int position = (int) (k % n); | |
ffae916f BJ |
220 | if (position < 0) position = -position; |
221 | ||
c801d85f | 222 | if (!hash_table[position]) |
c67daf87 | 223 | return (wxObject *) NULL; |
c801d85f KB |
224 | else |
225 | { | |
226 | wxNode *node = hash_table[position]->Find (k); | |
227 | return node ? node->Data () : (wxObject*)NULL; | |
228 | } | |
229 | } | |
230 | ||
50920146 | 231 | wxObject *wxHashTable::Get (const wxChar *key) const |
c801d85f KB |
232 | { |
233 | int position = (int) (MakeKey (key) % n); | |
ffae916f | 234 | if (position < 0) position = -position; |
c801d85f KB |
235 | |
236 | if (!hash_table[position]) | |
c67daf87 | 237 | return (wxObject *) NULL; |
c801d85f KB |
238 | else |
239 | { | |
240 | wxNode *node = hash_table[position]->Find (key); | |
241 | return node ? node->Data () : (wxObject*)NULL; | |
242 | } | |
243 | } | |
244 | ||
debe6624 | 245 | wxObject *wxHashTable::Delete (long key) |
c801d85f KB |
246 | { |
247 | // Should NEVER be | |
248 | long k = (long) key; | |
c801d85f KB |
249 | |
250 | int position = (int) (k % n); | |
ffae916f BJ |
251 | if (position < 0) position = -position; |
252 | ||
c801d85f | 253 | if (!hash_table[position]) |
c67daf87 | 254 | return (wxObject *) NULL; |
c801d85f KB |
255 | else |
256 | { | |
257 | wxNode *node = hash_table[position]->Find (k); | |
258 | if (node) | |
259 | { | |
260 | wxObject *data = node->Data (); | |
261 | delete node; | |
5692876f | 262 | m_count--; |
c801d85f KB |
263 | return data; |
264 | } | |
265 | else | |
c67daf87 | 266 | return (wxObject *) NULL; |
c801d85f KB |
267 | } |
268 | } | |
269 | ||
50920146 | 270 | wxObject *wxHashTable::Delete (const wxChar *key) |
c801d85f KB |
271 | { |
272 | int position = (int) (MakeKey (key) % n); | |
ffae916f BJ |
273 | if (position < 0) position = -position; |
274 | ||
c801d85f | 275 | if (!hash_table[position]) |
c67daf87 | 276 | return (wxObject *) NULL; |
c801d85f KB |
277 | else |
278 | { | |
279 | wxNode *node = hash_table[position]->Find (key); | |
280 | if (node) | |
281 | { | |
282 | wxObject *data = node->Data (); | |
283 | delete node; | |
5692876f | 284 | m_count--; |
c801d85f KB |
285 | return data; |
286 | } | |
287 | else | |
c67daf87 | 288 | return (wxObject *) NULL; |
c801d85f KB |
289 | } |
290 | } | |
291 | ||
debe6624 | 292 | wxObject *wxHashTable::Delete (long key, int value) |
c801d85f KB |
293 | { |
294 | // Should NEVER be | |
ffae916f | 295 | long k = (long) key; |
c801d85f KB |
296 | |
297 | int position = (int) (k % n); | |
ffae916f BJ |
298 | if (position < 0) position = -position; |
299 | ||
c801d85f | 300 | if (!hash_table[position]) |
c67daf87 | 301 | return (wxObject *) NULL; |
c801d85f KB |
302 | else |
303 | { | |
304 | wxNode *node = hash_table[position]->Find (value); | |
305 | if (node) | |
306 | { | |
307 | wxObject *data = node->Data (); | |
308 | delete node; | |
5692876f | 309 | m_count--; |
c801d85f KB |
310 | return data; |
311 | } | |
312 | else | |
c67daf87 | 313 | return (wxObject *) NULL; |
c801d85f KB |
314 | } |
315 | } | |
316 | ||
50920146 | 317 | wxObject *wxHashTable::Delete (long key, const wxChar *value) |
c801d85f KB |
318 | { |
319 | int position = (int) (key % n); | |
ffae916f BJ |
320 | if (position < 0) position = -position; |
321 | ||
c801d85f | 322 | if (!hash_table[position]) |
c67daf87 | 323 | return (wxObject *) NULL; |
c801d85f KB |
324 | else |
325 | { | |
326 | wxNode *node = hash_table[position]->Find (value); | |
327 | if (node) | |
328 | { | |
329 | wxObject *data = node->Data (); | |
330 | delete node; | |
5692876f | 331 | m_count--; |
c801d85f KB |
332 | return data; |
333 | } | |
334 | else | |
c67daf87 | 335 | return (wxObject *) NULL; |
c801d85f KB |
336 | } |
337 | } | |
338 | ||
50920146 | 339 | long wxHashTable::MakeKey (const wxChar *string) const |
c801d85f KB |
340 | { |
341 | long int_key = 0; | |
342 | ||
343 | while (*string) | |
50920146 | 344 | int_key += (wxUChar) *string++; |
c801d85f KB |
345 | |
346 | return int_key; | |
347 | } | |
348 | ||
349 | void wxHashTable::BeginFind (void) | |
350 | { | |
351 | current_position = -1; | |
c67daf87 | 352 | current_node = (wxNode *) NULL; |
c801d85f KB |
353 | } |
354 | ||
355 | wxNode *wxHashTable::Next (void) | |
356 | { | |
c67daf87 | 357 | wxNode *found = (wxNode *) NULL; |
c801d85f KB |
358 | bool end = FALSE; |
359 | while (!end && !found) | |
360 | { | |
361 | if (!current_node) | |
362 | { | |
363 | current_position++; | |
364 | if (current_position >= n) | |
365 | { | |
366 | current_position = -1; | |
c67daf87 | 367 | current_node = (wxNode *) NULL; |
c801d85f KB |
368 | end = TRUE; |
369 | } | |
370 | else | |
371 | { | |
372 | if (hash_table[current_position]) | |
373 | { | |
374 | current_node = hash_table[current_position]->First (); | |
375 | found = current_node; | |
376 | } | |
377 | } | |
378 | } | |
379 | else | |
380 | { | |
381 | current_node = current_node->Next (); | |
382 | found = current_node; | |
383 | } | |
384 | } | |
385 | return found; | |
386 | } | |
387 | ||
debe6624 | 388 | void wxHashTable::DeleteContents (bool flag) |
c801d85f KB |
389 | { |
390 | int i; | |
c7fb814a | 391 | m_deleteContents = flag; |
c801d85f KB |
392 | for (i = 0; i < n; i++) |
393 | { | |
394 | if (hash_table[i]) | |
395 | hash_table[i]->DeleteContents (flag); | |
396 | } | |
397 | } | |
398 | ||
399 | void wxHashTable::Clear (void) | |
400 | { | |
401 | int i; | |
402 | for (i = 0; i < n; i++) | |
403 | { | |
404 | if (hash_table[i]) | |
405 | hash_table[i]->Clear (); | |
406 | } | |
5692876f | 407 | m_count = 0; |
c801d85f KB |
408 | } |
409 |