]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: hash.cpp | |
3 | // Purpose: wxHashTable implementation | |
4 | // Author: Julian Smart | |
bcaa23de | 5 | // Modified by: VZ at 25.02.00: type safe hashes with WX_DECLARE_HASH() |
c801d85f KB |
6 | // Created: 01/02/97 |
7 | // RCS-ID: $Id$ | |
55d99c7a | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
bcaa23de VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
c801d85f KB |
21 | #pragma implementation "hash.h" |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/list.h" | |
33 | #endif | |
34 | ||
35 | #include "wx/hash.h" | |
36 | ||
6e86701b | 37 | #if wxUSE_OLD_HASH_TABLE |
df5168c4 | 38 | |
c801d85f KB |
39 | #include <string.h> |
40 | #include <stdarg.h> | |
41 | ||
bcaa23de VZ |
42 | // ---------------------------------------------------------------------------- |
43 | // wxWin macros | |
44 | // ---------------------------------------------------------------------------- | |
45 | ||
c801d85f | 46 | IMPLEMENT_DYNAMIC_CLASS(wxHashTable, wxObject) |
c801d85f | 47 | |
bcaa23de VZ |
48 | // ============================================================================ |
49 | // implementation | |
50 | // ============================================================================ | |
51 | ||
52 | // ---------------------------------------------------------------------------- | |
53 | // wxHashTablleBase for working with "void *" data | |
54 | // ---------------------------------------------------------------------------- | |
55 | ||
56 | wxHashTableBase::wxHashTableBase() | |
57 | { | |
f803b25b | 58 | m_deleteContents = false; |
bcaa23de VZ |
59 | m_hashTable = (wxListBase **)NULL; |
60 | m_hashSize = 0; | |
61 | m_count = 0; | |
62 | m_keyType = wxKEY_NONE; | |
63 | } | |
64 | ||
65 | void wxHashTableBase::Create(wxKeyType keyType, size_t size) | |
66 | { | |
67 | Destroy(); | |
68 | ||
69 | m_hashSize = size; | |
70 | m_keyType = keyType; | |
71 | m_hashTable = new wxListBase *[size]; | |
72 | for ( size_t n = 0; n < m_hashSize; n++ ) | |
73 | { | |
74 | m_hashTable[n] = (wxListBase *) NULL; | |
75 | } | |
76 | } | |
77 | ||
78 | void wxHashTableBase::Destroy() | |
79 | { | |
80 | if ( m_hashTable ) | |
81 | { | |
82 | for ( size_t n = 0; n < m_hashSize; n++ ) | |
83 | { | |
84 | delete m_hashTable[n]; | |
85 | } | |
86 | ||
87 | delete [] m_hashTable; | |
88 | ||
89 | m_hashTable = (wxListBase **)NULL; | |
90 | ||
91 | m_count = 0; | |
92 | } | |
93 | } | |
94 | ||
95 | void wxHashTableBase::DeleteContents(bool flag) | |
96 | { | |
97 | m_deleteContents = flag; | |
98 | for ( size_t n = 0; n < m_hashSize; n++ ) | |
99 | { | |
100 | if ( m_hashTable[n] ) | |
101 | { | |
102 | m_hashTable[n]->DeleteContents(flag); | |
103 | } | |
104 | } | |
105 | } | |
106 | ||
107 | wxNodeBase *wxHashTableBase::GetNode(long key, long value) const | |
108 | { | |
3ca6a5f0 | 109 | size_t slot = (size_t)abs((int)(key % (long)m_hashSize)); |
bcaa23de VZ |
110 | |
111 | wxNodeBase *node; | |
112 | if ( m_hashTable[slot] ) | |
113 | { | |
114 | node = m_hashTable[slot]->Find(wxListKey(value)); | |
115 | } | |
116 | else | |
117 | { | |
118 | node = (wxNodeBase *)NULL; | |
119 | } | |
120 | ||
121 | return node; | |
122 | } | |
123 | ||
ba8c1601 MB |
124 | #if WXWIN_COMPATIBILITY_2_4 |
125 | ||
c2bb85e9 VZ |
126 | // ---------------------------------------------------------------------------- |
127 | // wxHashTableLong | |
128 | // ---------------------------------------------------------------------------- | |
129 | ||
a95e38c0 VZ |
130 | wxHashTableLong::~wxHashTableLong() |
131 | { | |
132 | Destroy(); | |
133 | } | |
134 | ||
c2bb85e9 VZ |
135 | void wxHashTableLong::Init(size_t size) |
136 | { | |
137 | m_hashSize = size; | |
138 | m_values = new wxArrayLong *[size]; | |
139 | m_keys = new wxArrayLong *[size]; | |
140 | ||
141 | for ( size_t n = 0; n < m_hashSize; n++ ) | |
142 | { | |
143 | m_values[n] = | |
144 | m_keys[n] = (wxArrayLong *)NULL; | |
145 | } | |
146 | ||
147 | m_count = 0; | |
148 | } | |
149 | ||
b7aaabf8 VS |
150 | void wxHashTableLong::Create(size_t size) |
151 | { | |
152 | Init(size); | |
153 | } | |
154 | ||
c2bb85e9 VZ |
155 | void wxHashTableLong::Destroy() |
156 | { | |
157 | for ( size_t n = 0; n < m_hashSize; n++ ) | |
158 | { | |
159 | delete m_values[n]; | |
160 | delete m_keys[n]; | |
161 | } | |
162 | ||
163 | delete [] m_values; | |
164 | delete [] m_keys; | |
165 | m_hashSize = 0; | |
166 | m_count = 0; | |
167 | } | |
168 | ||
169 | void wxHashTableLong::Put(long key, long value) | |
170 | { | |
171 | wxCHECK_RET( m_hashSize, _T("must call Create() first") ); | |
172 | ||
3ca6a5f0 | 173 | size_t slot = (size_t)abs((int)(key % (long)m_hashSize)); |
c2bb85e9 VZ |
174 | |
175 | if ( !m_keys[slot] ) | |
176 | { | |
177 | m_keys[slot] = new wxArrayLong; | |
178 | m_values[slot] = new wxArrayLong; | |
179 | } | |
180 | ||
181 | m_keys[slot]->Add(key); | |
182 | m_values[slot]->Add(value); | |
183 | ||
184 | m_count++; | |
185 | } | |
186 | ||
187 | long wxHashTableLong::Get(long key) const | |
188 | { | |
189 | wxCHECK_MSG( m_hashSize, wxNOT_FOUND, _T("must call Create() first") ); | |
190 | ||
3ca6a5f0 | 191 | size_t slot = (size_t)abs((int)(key % (long)m_hashSize)); |
c2bb85e9 VZ |
192 | |
193 | wxArrayLong *keys = m_keys[slot]; | |
194 | if ( keys ) | |
195 | { | |
196 | size_t count = keys->GetCount(); | |
197 | for ( size_t n = 0; n < count; n++ ) | |
198 | { | |
199 | if ( keys->Item(n) == key ) | |
200 | { | |
201 | return m_values[slot]->Item(n); | |
202 | } | |
203 | } | |
204 | } | |
205 | ||
206 | return wxNOT_FOUND; | |
207 | } | |
208 | ||
209 | long wxHashTableLong::Delete(long key) | |
210 | { | |
211 | wxCHECK_MSG( m_hashSize, wxNOT_FOUND, _T("must call Create() first") ); | |
212 | ||
3ca6a5f0 | 213 | size_t slot = (size_t)abs((int)(key % (long)m_hashSize)); |
c2bb85e9 VZ |
214 | |
215 | wxArrayLong *keys = m_keys[slot]; | |
216 | if ( keys ) | |
217 | { | |
218 | size_t count = keys->GetCount(); | |
219 | for ( size_t n = 0; n < count; n++ ) | |
220 | { | |
221 | if ( keys->Item(n) == key ) | |
222 | { | |
223 | long val = m_values[slot]->Item(n); | |
224 | ||
225 | keys->RemoveAt(n); | |
226 | m_values[slot]->RemoveAt(n); | |
227 | ||
228 | m_count--; | |
229 | ||
230 | return val; | |
231 | } | |
232 | } | |
233 | } | |
234 | ||
235 | return wxNOT_FOUND; | |
236 | } | |
237 | ||
bd83cb56 VZ |
238 | // ---------------------------------------------------------------------------- |
239 | // wxStringHashTable: more efficient than storing strings in a list | |
240 | // ---------------------------------------------------------------------------- | |
241 | ||
242 | wxStringHashTable::wxStringHashTable(size_t sizeTable) | |
243 | { | |
244 | m_keys = new wxArrayLong *[sizeTable]; | |
245 | m_values = new wxArrayString *[sizeTable]; | |
246 | ||
247 | m_hashSize = sizeTable; | |
248 | for ( size_t n = 0; n < m_hashSize; n++ ) | |
249 | { | |
250 | m_values[n] = (wxArrayString *)NULL; | |
251 | m_keys[n] = (wxArrayLong *)NULL; | |
252 | } | |
253 | } | |
254 | ||
255 | wxStringHashTable::~wxStringHashTable() | |
256 | { | |
257 | Destroy(); | |
258 | } | |
259 | ||
260 | void wxStringHashTable::Destroy() | |
261 | { | |
262 | for ( size_t n = 0; n < m_hashSize; n++ ) | |
263 | { | |
264 | delete m_values[n]; | |
265 | delete m_keys[n]; | |
266 | } | |
267 | ||
268 | delete [] m_values; | |
269 | delete [] m_keys; | |
270 | m_hashSize = 0; | |
271 | } | |
272 | ||
273 | void wxStringHashTable::Put(long key, const wxString& value) | |
274 | { | |
275 | wxCHECK_RET( m_hashSize, _T("must call Create() first") ); | |
276 | ||
277 | size_t slot = (size_t)abs((int)(key % (long)m_hashSize)); | |
278 | ||
279 | if ( !m_keys[slot] ) | |
280 | { | |
281 | m_keys[slot] = new wxArrayLong; | |
282 | m_values[slot] = new wxArrayString; | |
283 | } | |
284 | ||
285 | m_keys[slot]->Add(key); | |
286 | m_values[slot]->Add(value); | |
287 | } | |
288 | ||
289 | wxString wxStringHashTable::Get(long key, bool *wasFound) const | |
290 | { | |
291 | wxCHECK_MSG( m_hashSize, _T(""), _T("must call Create() first") ); | |
292 | ||
293 | size_t slot = (size_t)abs((int)(key % (long)m_hashSize)); | |
294 | ||
295 | wxArrayLong *keys = m_keys[slot]; | |
296 | if ( keys ) | |
297 | { | |
298 | size_t count = keys->GetCount(); | |
299 | for ( size_t n = 0; n < count; n++ ) | |
300 | { | |
301 | if ( keys->Item(n) == key ) | |
302 | { | |
303 | if ( wasFound ) | |
f803b25b | 304 | *wasFound = true; |
bd83cb56 VZ |
305 | |
306 | return m_values[slot]->Item(n); | |
307 | } | |
308 | } | |
309 | } | |
310 | ||
311 | if ( wasFound ) | |
f803b25b | 312 | *wasFound = false; |
bd83cb56 VZ |
313 | |
314 | return _T(""); | |
315 | } | |
316 | ||
53e112a0 JS |
317 | bool wxStringHashTable::Delete(long key) const |
318 | { | |
f803b25b | 319 | wxCHECK_MSG( m_hashSize, false, _T("must call Create() first") ); |
53e112a0 JS |
320 | |
321 | size_t slot = (size_t)abs((int)(key % (long)m_hashSize)); | |
322 | ||
323 | wxArrayLong *keys = m_keys[slot]; | |
324 | if ( keys ) | |
325 | { | |
326 | size_t count = keys->GetCount(); | |
327 | for ( size_t n = 0; n < count; n++ ) | |
328 | { | |
329 | if ( keys->Item(n) == key ) | |
330 | { | |
f8305caf | 331 | keys->RemoveAt(n); |
53e112a0 | 332 | m_values[slot]->RemoveAt(n); |
f803b25b | 333 | return true; |
53e112a0 JS |
334 | } |
335 | } | |
336 | } | |
337 | ||
f803b25b | 338 | return false; |
53e112a0 JS |
339 | } |
340 | ||
ba8c1601 MB |
341 | #endif // WXWIN_COMPATIBILITY_2_4 |
342 | ||
bcaa23de VZ |
343 | // ---------------------------------------------------------------------------- |
344 | // old not type safe wxHashTable | |
345 | // ---------------------------------------------------------------------------- | |
346 | ||
debe6624 | 347 | wxHashTable::wxHashTable (int the_key_type, int size) |
c801d85f | 348 | { |
17d8ee1c JS |
349 | n = 0; |
350 | hash_table = (wxList**) NULL; | |
351 | Create(the_key_type, size); | |
5692876f | 352 | m_count = 0; |
f803b25b | 353 | m_deleteContents = false; |
17d8ee1c | 354 | /* |
c801d85f KB |
355 | n = size; |
356 | current_position = -1; | |
c67daf87 | 357 | current_node = (wxNode *) NULL; |
c801d85f KB |
358 | |
359 | key_type = the_key_type; | |
360 | hash_table = new wxList *[size]; | |
361 | int i; | |
362 | for (i = 0; i < size; i++) | |
c67daf87 | 363 | hash_table[i] = (wxList *) NULL; |
17d8ee1c | 364 | */ |
c801d85f KB |
365 | } |
366 | ||
bcaa23de | 367 | wxHashTable::~wxHashTable () |
c801d85f | 368 | { |
e55ad60e RR |
369 | Destroy(); |
370 | } | |
371 | ||
bcaa23de | 372 | void wxHashTable::Destroy() |
e55ad60e RR |
373 | { |
374 | if (!hash_table) return; | |
c801d85f KB |
375 | int i; |
376 | for (i = 0; i < n; i++) | |
377 | if (hash_table[i]) | |
378 | delete hash_table[i]; | |
379 | delete[] hash_table; | |
e55ad60e | 380 | hash_table = NULL; |
c801d85f KB |
381 | } |
382 | ||
debe6624 | 383 | bool wxHashTable::Create(int the_key_type, int size) |
c801d85f | 384 | { |
17d8ee1c JS |
385 | Destroy(); |
386 | ||
c801d85f KB |
387 | n = size; |
388 | current_position = -1; | |
c67daf87 | 389 | current_node = (wxNode *) NULL; |
c801d85f KB |
390 | |
391 | key_type = the_key_type; | |
c801d85f KB |
392 | hash_table = new wxList *[size]; |
393 | int i; | |
394 | for (i = 0; i < size; i++) | |
c67daf87 | 395 | hash_table[i] = (wxList *) NULL; |
f803b25b | 396 | return true; |
c801d85f KB |
397 | } |
398 | ||
4e67bfc7 VS |
399 | |
400 | void wxHashTable::DoCopy(const wxHashTable& table) | |
401 | { | |
402 | n = table.n; | |
ea450825 | 403 | m_count = table.m_count; |
4e67bfc7 VS |
404 | current_position = table.current_position; |
405 | current_node = NULL; // doesn't matter - Next() will reconstruct it | |
406 | key_type = table.key_type; | |
407 | ||
408 | hash_table = new wxList *[n]; | |
409 | for (int i = 0; i < n; i++) { | |
410 | if (table.hash_table[i] == NULL) | |
411 | hash_table[i] = NULL; | |
412 | else { | |
413 | hash_table[i] = new wxList(key_type); | |
414 | *(hash_table[i]) = *(table.hash_table[i]); | |
415 | } | |
416 | } | |
417 | } | |
418 | ||
debe6624 | 419 | void wxHashTable::Put (long key, long value, wxObject * object) |
c801d85f KB |
420 | { |
421 | // Should NEVER be | |
422 | long k = (long) key; | |
c801d85f KB |
423 | |
424 | int position = (int) (k % n); | |
ffae916f BJ |
425 | if (position < 0) position = -position; |
426 | ||
c801d85f | 427 | if (!hash_table[position]) |
c7fb814a | 428 | { |
cbb26e3f | 429 | hash_table[position] = new wxList (wxKEY_INTEGER); |
f803b25b | 430 | if (m_deleteContents) hash_table[position]->DeleteContents(true); |
c7fb814a | 431 | } |
c801d85f KB |
432 | |
433 | hash_table[position]->Append (value, object); | |
5692876f | 434 | m_count++; |
c801d85f KB |
435 | } |
436 | ||
50920146 | 437 | void wxHashTable::Put (long key, const wxChar *value, wxObject * object) |
c801d85f KB |
438 | { |
439 | // Should NEVER be | |
bcaa23de | 440 | long k = (long) key; |
c801d85f KB |
441 | |
442 | int position = (int) (k % n); | |
ffae916f | 443 | if (position < 0) position = -position; |
bcaa23de | 444 | |
c801d85f | 445 | if (!hash_table[position]) |
c7fb814a | 446 | { |
cbb26e3f | 447 | hash_table[position] = new wxList (wxKEY_STRING); |
f803b25b | 448 | if (m_deleteContents) hash_table[position]->DeleteContents(true); |
c7fb814a | 449 | } |
c801d85f KB |
450 | |
451 | hash_table[position]->Append (value, object); | |
5692876f | 452 | m_count++; |
c801d85f KB |
453 | } |
454 | ||
debe6624 | 455 | void wxHashTable::Put (long key, wxObject * object) |
c801d85f KB |
456 | { |
457 | // Should NEVER be | |
458 | long k = (long) key; | |
bcaa23de | 459 | |
c801d85f | 460 | int position = (int) (k % n); |
ffae916f BJ |
461 | if (position < 0) position = -position; |
462 | ||
c801d85f | 463 | if (!hash_table[position]) |
c7fb814a | 464 | { |
c801d85f | 465 | hash_table[position] = new wxList (wxKEY_INTEGER); |
f803b25b | 466 | if (m_deleteContents) hash_table[position]->DeleteContents(true); |
c7fb814a | 467 | } |
bcaa23de | 468 | |
c801d85f | 469 | hash_table[position]->Append (k, object); |
5692876f | 470 | m_count++; |
c801d85f KB |
471 | } |
472 | ||
50920146 | 473 | void wxHashTable::Put (const wxChar *key, wxObject * object) |
c801d85f KB |
474 | { |
475 | int position = (int) (MakeKey (key) % n); | |
ffae916f | 476 | if (position < 0) position = -position; |
c801d85f KB |
477 | |
478 | if (!hash_table[position]) | |
c7fb814a | 479 | { |
c801d85f | 480 | hash_table[position] = new wxList (wxKEY_STRING); |
f803b25b | 481 | if (m_deleteContents) hash_table[position]->DeleteContents(true); |
c7fb814a | 482 | } |
c801d85f KB |
483 | |
484 | hash_table[position]->Append (key, object); | |
5692876f | 485 | m_count++; |
c801d85f KB |
486 | } |
487 | ||
debe6624 | 488 | wxObject *wxHashTable::Get (long key, long value) const |
c801d85f KB |
489 | { |
490 | // Should NEVER be | |
491 | long k = (long) key; | |
c801d85f KB |
492 | |
493 | int position = (int) (k % n); | |
ffae916f BJ |
494 | if (position < 0) position = -position; |
495 | ||
c801d85f | 496 | if (!hash_table[position]) |
c67daf87 | 497 | return (wxObject *) NULL; |
c801d85f KB |
498 | else |
499 | { | |
500 | wxNode *node = hash_table[position]->Find (value); | |
501 | if (node) | |
b1d4dd7a | 502 | return node->GetData (); |
c801d85f | 503 | else |
bcaa23de | 504 | return (wxObject *) NULL; |
c801d85f KB |
505 | } |
506 | } | |
507 | ||
50920146 | 508 | wxObject *wxHashTable::Get (long key, const wxChar *value) const |
c801d85f KB |
509 | { |
510 | // Should NEVER be | |
511 | long k = (long) key; | |
bcaa23de | 512 | |
c801d85f | 513 | int position = (int) (k % n); |
ffae916f BJ |
514 | if (position < 0) position = -position; |
515 | ||
c801d85f | 516 | if (!hash_table[position]) |
c67daf87 | 517 | return (wxObject *) NULL; |
c801d85f KB |
518 | else |
519 | { | |
520 | wxNode *node = hash_table[position]->Find (value); | |
521 | if (node) | |
b1d4dd7a | 522 | return node->GetData (); |
c801d85f | 523 | else |
bcaa23de | 524 | return (wxObject *) NULL; |
c801d85f KB |
525 | } |
526 | } | |
527 | ||
debe6624 | 528 | wxObject *wxHashTable::Get (long key) const |
c801d85f KB |
529 | { |
530 | // Should NEVER be | |
531 | long k = (long) key; | |
c801d85f KB |
532 | |
533 | int position = (int) (k % n); | |
ffae916f BJ |
534 | if (position < 0) position = -position; |
535 | ||
c801d85f | 536 | if (!hash_table[position]) |
c67daf87 | 537 | return (wxObject *) NULL; |
c801d85f KB |
538 | else |
539 | { | |
540 | wxNode *node = hash_table[position]->Find (k); | |
b1d4dd7a | 541 | return node ? node->GetData () : (wxObject*)NULL; |
c801d85f KB |
542 | } |
543 | } | |
544 | ||
50920146 | 545 | wxObject *wxHashTable::Get (const wxChar *key) const |
c801d85f KB |
546 | { |
547 | int position = (int) (MakeKey (key) % n); | |
ffae916f | 548 | if (position < 0) position = -position; |
c801d85f KB |
549 | |
550 | if (!hash_table[position]) | |
c67daf87 | 551 | return (wxObject *) NULL; |
c801d85f KB |
552 | else |
553 | { | |
554 | wxNode *node = hash_table[position]->Find (key); | |
b1d4dd7a | 555 | return node ? node->GetData () : (wxObject*)NULL; |
c801d85f KB |
556 | } |
557 | } | |
558 | ||
debe6624 | 559 | wxObject *wxHashTable::Delete (long key) |
c801d85f KB |
560 | { |
561 | // Should NEVER be | |
562 | long k = (long) key; | |
c801d85f KB |
563 | |
564 | int position = (int) (k % n); | |
ffae916f BJ |
565 | if (position < 0) position = -position; |
566 | ||
c801d85f | 567 | if (!hash_table[position]) |
c67daf87 | 568 | return (wxObject *) NULL; |
c801d85f KB |
569 | else |
570 | { | |
571 | wxNode *node = hash_table[position]->Find (k); | |
572 | if (node) | |
bcaa23de | 573 | { |
b1d4dd7a | 574 | wxObject *data = node->GetData (); |
bcaa23de VZ |
575 | delete node; |
576 | m_count--; | |
577 | return data; | |
578 | } | |
c801d85f | 579 | else |
bcaa23de | 580 | return (wxObject *) NULL; |
c801d85f KB |
581 | } |
582 | } | |
583 | ||
50920146 | 584 | wxObject *wxHashTable::Delete (const wxChar *key) |
c801d85f KB |
585 | { |
586 | int position = (int) (MakeKey (key) % n); | |
ffae916f BJ |
587 | if (position < 0) position = -position; |
588 | ||
c801d85f | 589 | if (!hash_table[position]) |
c67daf87 | 590 | return (wxObject *) NULL; |
c801d85f KB |
591 | else |
592 | { | |
593 | wxNode *node = hash_table[position]->Find (key); | |
594 | if (node) | |
bcaa23de | 595 | { |
b1d4dd7a | 596 | wxObject *data = node->GetData (); |
bcaa23de VZ |
597 | delete node; |
598 | m_count--; | |
599 | return data; | |
600 | } | |
c801d85f | 601 | else |
bcaa23de | 602 | return (wxObject *) NULL; |
c801d85f KB |
603 | } |
604 | } | |
605 | ||
debe6624 | 606 | wxObject *wxHashTable::Delete (long key, int value) |
c801d85f KB |
607 | { |
608 | // Should NEVER be | |
bcaa23de | 609 | long k = (long) key; |
c801d85f KB |
610 | |
611 | int position = (int) (k % n); | |
ffae916f BJ |
612 | if (position < 0) position = -position; |
613 | ||
c801d85f | 614 | if (!hash_table[position]) |
c67daf87 | 615 | return (wxObject *) NULL; |
c801d85f KB |
616 | else |
617 | { | |
618 | wxNode *node = hash_table[position]->Find (value); | |
619 | if (node) | |
bcaa23de | 620 | { |
b1d4dd7a | 621 | wxObject *data = node->GetData (); |
bcaa23de VZ |
622 | delete node; |
623 | m_count--; | |
624 | return data; | |
625 | } | |
c801d85f | 626 | else |
bcaa23de | 627 | return (wxObject *) NULL; |
c801d85f KB |
628 | } |
629 | } | |
630 | ||
50920146 | 631 | wxObject *wxHashTable::Delete (long key, const wxChar *value) |
c801d85f KB |
632 | { |
633 | int position = (int) (key % n); | |
ffae916f BJ |
634 | if (position < 0) position = -position; |
635 | ||
c801d85f | 636 | if (!hash_table[position]) |
c67daf87 | 637 | return (wxObject *) NULL; |
c801d85f KB |
638 | else |
639 | { | |
640 | wxNode *node = hash_table[position]->Find (value); | |
641 | if (node) | |
bcaa23de | 642 | { |
b1d4dd7a | 643 | wxObject *data = node->GetData (); |
bcaa23de VZ |
644 | delete node; |
645 | m_count--; | |
646 | return data; | |
647 | } | |
c801d85f | 648 | else |
bcaa23de | 649 | return (wxObject *) NULL; |
c801d85f KB |
650 | } |
651 | } | |
652 | ||
50920146 | 653 | long wxHashTable::MakeKey (const wxChar *string) const |
c801d85f KB |
654 | { |
655 | long int_key = 0; | |
656 | ||
657 | while (*string) | |
50920146 | 658 | int_key += (wxUChar) *string++; |
c801d85f KB |
659 | |
660 | return int_key; | |
661 | } | |
662 | ||
bcaa23de | 663 | void wxHashTable::BeginFind () |
c801d85f KB |
664 | { |
665 | current_position = -1; | |
c67daf87 | 666 | current_node = (wxNode *) NULL; |
c801d85f KB |
667 | } |
668 | ||
1a6d9c76 | 669 | wxHashTable::Node* wxHashTable::Next () |
c801d85f | 670 | { |
c67daf87 | 671 | wxNode *found = (wxNode *) NULL; |
f803b25b | 672 | bool end = false; |
c801d85f KB |
673 | while (!end && !found) |
674 | { | |
675 | if (!current_node) | |
bcaa23de VZ |
676 | { |
677 | current_position++; | |
678 | if (current_position >= n) | |
679 | { | |
680 | current_position = -1; | |
681 | current_node = (wxNode *) NULL; | |
f803b25b | 682 | end = true; |
bcaa23de VZ |
683 | } |
684 | else | |
685 | { | |
686 | if (hash_table[current_position]) | |
687 | { | |
b1d4dd7a | 688 | current_node = hash_table[current_position]->GetFirst (); |
bcaa23de VZ |
689 | found = current_node; |
690 | } | |
691 | } | |
692 | } | |
c801d85f | 693 | else |
bcaa23de | 694 | { |
b1d4dd7a | 695 | current_node = current_node->GetNext (); |
bcaa23de VZ |
696 | found = current_node; |
697 | } | |
c801d85f KB |
698 | } |
699 | return found; | |
700 | } | |
701 | ||
debe6624 | 702 | void wxHashTable::DeleteContents (bool flag) |
c801d85f KB |
703 | { |
704 | int i; | |
c7fb814a | 705 | m_deleteContents = flag; |
c801d85f KB |
706 | for (i = 0; i < n; i++) |
707 | { | |
708 | if (hash_table[i]) | |
bcaa23de | 709 | hash_table[i]->DeleteContents (flag); |
c801d85f KB |
710 | } |
711 | } | |
712 | ||
bcaa23de | 713 | void wxHashTable::Clear () |
c801d85f | 714 | { |
19230604 RD |
715 | int i; |
716 | if (hash_table) | |
c801d85f | 717 | { |
19230604 RD |
718 | for (i = 0; i < n; i++) |
719 | { | |
720 | if (hash_table[i]) | |
721 | hash_table[i]->Clear (); | |
722 | } | |
c801d85f | 723 | } |
5692876f | 724 | m_count = 0; |
c801d85f KB |
725 | } |
726 | ||
6e86701b | 727 | #else // if !wxUSE_OLD_HASH_TABLE |
1a6d9c76 MB |
728 | |
729 | wxHashTableBase_Node::wxHashTableBase_Node( long key, void* value, | |
730 | wxHashTableBase* table ) | |
731 | : m_value( value ), m_hashPtr( table ) | |
732 | { | |
733 | m_key.integer = key; | |
734 | } | |
735 | ||
736 | wxHashTableBase_Node::wxHashTableBase_Node( const wxChar* key, void* value, | |
737 | wxHashTableBase* table ) | |
738 | : m_value( value ), m_hashPtr( table ) | |
739 | { | |
740 | m_key.string = wxStrcpy( new wxChar[wxStrlen( key ) + 1], key ); | |
741 | } | |
742 | ||
743 | wxHashTableBase_Node::~wxHashTableBase_Node() | |
744 | { | |
745 | if( m_hashPtr ) m_hashPtr->DoRemoveNode( this ); | |
746 | } | |
747 | ||
748 | // | |
749 | ||
750 | wxHashTableBase::wxHashTableBase() | |
751 | : m_size( 0 ), m_count( 0 ), m_table( NULL ), m_keyType( wxKEY_NONE ), | |
752 | m_deleteContents( false ) | |
753 | { | |
754 | } | |
755 | ||
1a6d9c76 MB |
756 | void wxHashTableBase::Create( wxKeyType keyType, size_t size ) |
757 | { | |
758 | m_keyType = keyType; | |
759 | m_size = size; | |
760 | m_table = new wxHashTableBase_Node*[ m_size ]; | |
761 | ||
762 | for( size_t i = 0; i < m_size; ++i ) | |
763 | m_table[i] = NULL; | |
764 | } | |
765 | ||
766 | void wxHashTableBase::Clear() | |
767 | { | |
768 | for( size_t i = 0; i < m_size; ++i ) | |
769 | { | |
770 | Node* end = m_table[i]; | |
771 | ||
772 | if( end == NULL ) | |
773 | continue; | |
774 | ||
775 | Node *curr, *next = end->GetNext(); | |
776 | ||
777 | do | |
778 | { | |
779 | curr = next; | |
780 | next = curr->GetNext(); | |
781 | ||
782 | DoDestroyNode( curr ); | |
783 | ||
784 | delete curr; | |
785 | } | |
786 | while( curr != end ); | |
787 | ||
788 | m_table[i] = NULL; | |
789 | } | |
790 | ||
791 | m_count = 0; | |
792 | } | |
793 | ||
794 | void wxHashTableBase::DoRemoveNode( wxHashTableBase_Node* node ) | |
795 | { | |
796 | size_t bucket = ( m_keyType == wxKEY_INTEGER ? | |
797 | node->m_key.integer : | |
798 | MakeKey( node->m_key.string ) ) % m_size; | |
799 | ||
800 | if( node->GetNext() == node ) | |
801 | { | |
802 | // single-node chain (common case) | |
803 | m_table[bucket] = NULL; | |
804 | } | |
805 | else | |
806 | { | |
807 | Node *start = m_table[bucket], *curr; | |
808 | Node* prev = start; | |
809 | ||
810 | for( curr = prev->GetNext(); curr != node; | |
f803b25b | 811 | prev = curr, curr = curr->GetNext() ) ; |
1a6d9c76 MB |
812 | |
813 | DoUnlinkNode( bucket, node, prev ); | |
814 | } | |
815 | ||
816 | DoDestroyNode( node ); | |
817 | } | |
818 | ||
819 | void wxHashTableBase::DoDestroyNode( wxHashTableBase_Node* node ) | |
820 | { | |
821 | // if it is called from DoRemoveNode, node has already been | |
822 | // removed, from other places it does not matter | |
823 | node->m_hashPtr = NULL; | |
824 | ||
825 | if( m_keyType == wxKEY_STRING ) | |
826 | delete[] node->m_key.string; | |
827 | if( m_deleteContents ) | |
828 | DoDeleteContents( node ); | |
829 | } | |
830 | ||
831 | void wxHashTableBase::Destroy() | |
832 | { | |
833 | Clear(); | |
834 | ||
835 | delete[] m_table; | |
836 | ||
837 | m_table = NULL; | |
838 | m_size = 0; | |
839 | } | |
840 | ||
841 | void wxHashTableBase::DoInsertNode( size_t bucket, wxHashTableBase_Node* node ) | |
842 | { | |
843 | if( m_table[bucket] == NULL ) | |
844 | { | |
845 | m_table[bucket] = node->m_next = node; | |
846 | } | |
847 | else | |
848 | { | |
849 | Node *prev = m_table[bucket]; | |
850 | Node *next = prev->m_next; | |
851 | ||
852 | prev->m_next = node; | |
853 | node->m_next = next; | |
854 | m_table[bucket] = node; | |
855 | } | |
856 | ||
857 | ++m_count; | |
858 | } | |
859 | ||
860 | void wxHashTableBase::DoPut( long key, long hash, void* data ) | |
861 | { | |
862 | wxASSERT( m_keyType == wxKEY_INTEGER ); | |
863 | ||
864 | size_t bucket = size_t(hash) % m_size; | |
865 | Node* node = new wxHashTableBase_Node( key, data, this ); | |
866 | ||
867 | DoInsertNode( bucket, node ); | |
868 | } | |
869 | ||
870 | void wxHashTableBase::DoPut( const wxChar* key, long hash, void* data ) | |
871 | { | |
872 | wxASSERT( m_keyType == wxKEY_STRING ); | |
873 | ||
874 | size_t bucket = size_t(hash) % m_size; | |
875 | Node* node = new wxHashTableBase_Node( key, data, this ); | |
876 | ||
877 | DoInsertNode( bucket, node ); | |
878 | } | |
879 | ||
880 | void* wxHashTableBase::DoGet( long key, long hash ) const | |
881 | { | |
882 | wxASSERT( m_keyType == wxKEY_INTEGER ); | |
883 | ||
884 | size_t bucket = size_t(hash) % m_size; | |
885 | ||
886 | if( m_table[bucket] == NULL ) | |
887 | return NULL; | |
888 | ||
889 | Node *first = m_table[bucket]->GetNext(), | |
890 | *curr = first; | |
891 | ||
892 | do | |
893 | { | |
894 | if( curr->m_key.integer == key ) | |
895 | return curr->m_value; | |
896 | ||
897 | curr = curr->GetNext(); | |
898 | } | |
899 | while( curr != first ); | |
900 | ||
901 | return NULL; | |
902 | } | |
903 | ||
904 | void* wxHashTableBase::DoGet( const wxChar* key, long hash ) const | |
905 | { | |
906 | wxASSERT( m_keyType == wxKEY_STRING ); | |
907 | ||
908 | size_t bucket = size_t(hash) % m_size; | |
909 | ||
910 | if( m_table[bucket] == NULL ) | |
911 | return NULL; | |
912 | ||
913 | Node *first = m_table[bucket]->GetNext(), | |
914 | *curr = first; | |
915 | ||
916 | do | |
917 | { | |
918 | if( wxStrcmp( curr->m_key.string, key ) == 0 ) | |
919 | return curr->m_value; | |
920 | ||
921 | curr = curr->GetNext(); | |
922 | } | |
923 | while( curr != first ); | |
924 | ||
925 | return NULL; | |
926 | } | |
927 | ||
928 | void wxHashTableBase::DoUnlinkNode( size_t bucket, wxHashTableBase_Node* node, | |
929 | wxHashTableBase_Node* prev ) | |
930 | { | |
931 | if( node == m_table[bucket] ) | |
932 | m_table[bucket] = prev; | |
933 | ||
934 | if( prev == node && prev == node->GetNext() ) | |
935 | m_table[bucket] = NULL; | |
936 | else | |
937 | prev->m_next = node->m_next; | |
938 | ||
939 | DoDestroyNode( node ); | |
940 | --m_count; | |
941 | } | |
942 | ||
943 | void* wxHashTableBase::DoDelete( long key, long hash ) | |
944 | { | |
945 | wxASSERT( m_keyType == wxKEY_INTEGER ); | |
946 | ||
947 | size_t bucket = size_t(hash) % m_size; | |
948 | ||
949 | if( m_table[bucket] == NULL ) | |
950 | return NULL; | |
951 | ||
952 | Node *first = m_table[bucket]->GetNext(), | |
953 | *curr = first, | |
954 | *prev = m_table[bucket]; | |
955 | ||
956 | do | |
957 | { | |
958 | if( curr->m_key.integer == key ) | |
959 | { | |
960 | void* retval = curr->m_value; | |
961 | curr->m_value = NULL; | |
962 | ||
963 | DoUnlinkNode( bucket, curr, prev ); | |
964 | delete curr; | |
965 | ||
966 | return retval; | |
967 | } | |
968 | ||
969 | prev = curr; | |
970 | curr = curr->GetNext(); | |
971 | } | |
972 | while( curr != first ); | |
973 | ||
974 | return NULL; | |
975 | } | |
976 | ||
977 | void* wxHashTableBase::DoDelete( const wxChar* key, long hash ) | |
978 | { | |
979 | wxASSERT( m_keyType == wxKEY_STRING ); | |
980 | ||
981 | size_t bucket = size_t(hash) % m_size; | |
982 | ||
983 | if( m_table[bucket] == NULL ) | |
984 | return NULL; | |
985 | ||
986 | Node *first = m_table[bucket]->GetNext(), | |
987 | *curr = first, | |
988 | *prev = m_table[bucket]; | |
989 | ||
990 | do | |
991 | { | |
992 | if( wxStrcmp( curr->m_key.string, key ) == 0 ) | |
993 | { | |
994 | void* retval = curr->m_value; | |
995 | curr->m_value = NULL; | |
996 | ||
997 | DoUnlinkNode( bucket, curr, prev ); | |
998 | delete curr; | |
999 | ||
1000 | return retval; | |
1001 | } | |
1002 | ||
1003 | prev = curr; | |
1004 | curr = curr->GetNext(); | |
1005 | } | |
1006 | while( curr != first ); | |
1007 | ||
1008 | return NULL; | |
1009 | } | |
1010 | ||
1011 | long wxHashTableBase::MakeKey( const wxChar *str ) | |
1012 | { | |
1013 | long int_key = 0; | |
1014 | ||
1015 | while( *str ) | |
1016 | int_key += (wxUChar)*str++; | |
1017 | ||
1018 | return int_key; | |
1019 | } | |
1020 | ||
1021 | // | |
1022 | ||
1023 | wxHashTable::wxHashTable( const wxHashTable& table ) | |
1024 | { | |
1025 | DoCopy( table ); | |
1026 | } | |
1027 | ||
1028 | const wxHashTable& wxHashTable::operator=( const wxHashTable& table ) | |
1029 | { | |
1030 | Destroy(); | |
1031 | DoCopy( table ); | |
1032 | ||
1033 | return *this; | |
1034 | } | |
1035 | ||
6aa33060 | 1036 | void wxHashTable::DoCopy( const wxHashTable& WXUNUSED(table) ) |
1a6d9c76 MB |
1037 | { |
1038 | Create( m_keyType, m_size ); | |
1039 | ||
1040 | wxASSERT( false ); | |
1041 | } | |
1042 | ||
1043 | void wxHashTable::DoDeleteContents( wxHashTableBase_Node* node ) | |
1044 | { | |
1045 | delete ((wxHashTable_Node*)node)->GetData(); | |
1046 | } | |
1047 | ||
1048 | void wxHashTable::GetNextNode( size_t bucketStart ) | |
1049 | { | |
1050 | for( size_t i = bucketStart; i < m_size; ++i ) | |
1051 | { | |
1052 | if( m_table[i] != NULL ) | |
1053 | { | |
1054 | m_curr = ((Node*)m_table[i])->GetNext(); | |
1055 | m_currBucket = i; | |
1056 | return; | |
1057 | } | |
1058 | } | |
1059 | ||
1060 | m_curr = NULL; | |
1061 | m_currBucket = 0; | |
1062 | } | |
1063 | ||
1064 | wxHashTable::Node* wxHashTable::Next() | |
1065 | { | |
1066 | if( m_curr == NULL ) | |
1067 | GetNextNode( 0 ); | |
1068 | else | |
1069 | { | |
1070 | m_curr = m_curr->GetNext(); | |
1071 | ||
1072 | if( m_curr == ( (Node*)m_table[m_currBucket] )->GetNext() ) | |
1073 | GetNextNode( m_currBucket + 1 ); | |
1074 | } | |
1075 | ||
1076 | return m_curr; | |
1077 | } | |
1078 | ||
6e86701b | 1079 | #endif // !wxUSE_OLD_HASH_TABLE |