]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
7cb32b4b | 2 | // Name: src/common/hash.cpp |
c801d85f KB |
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 | ||
c801d85f KB |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
8ecff181 | 24 | #pragma hdrstop |
c801d85f KB |
25 | #endif |
26 | ||
27 | #ifndef WX_PRECOMP | |
8ecff181 | 28 | #include "wx/list.h" |
32d4c30a | 29 | #include "wx/hash.h" |
c801d85f KB |
30 | #endif |
31 | ||
6e86701b | 32 | #if wxUSE_OLD_HASH_TABLE |
df5168c4 | 33 | |
c801d85f KB |
34 | #include <string.h> |
35 | #include <stdarg.h> | |
36 | ||
bcaa23de VZ |
37 | // ---------------------------------------------------------------------------- |
38 | // wxWin macros | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
c801d85f | 41 | IMPLEMENT_DYNAMIC_CLASS(wxHashTable, wxObject) |
c801d85f | 42 | |
bcaa23de VZ |
43 | // ============================================================================ |
44 | // implementation | |
45 | // ============================================================================ | |
46 | ||
47 | // ---------------------------------------------------------------------------- | |
48 | // wxHashTablleBase for working with "void *" data | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
51 | wxHashTableBase::wxHashTableBase() | |
52 | { | |
f803b25b | 53 | m_deleteContents = false; |
bcaa23de VZ |
54 | m_hashTable = (wxListBase **)NULL; |
55 | m_hashSize = 0; | |
56 | m_count = 0; | |
57 | m_keyType = wxKEY_NONE; | |
58 | } | |
59 | ||
60 | void wxHashTableBase::Create(wxKeyType keyType, size_t size) | |
61 | { | |
62 | Destroy(); | |
63 | ||
64 | m_hashSize = size; | |
65 | m_keyType = keyType; | |
66 | m_hashTable = new wxListBase *[size]; | |
67 | for ( size_t n = 0; n < m_hashSize; n++ ) | |
68 | { | |
69 | m_hashTable[n] = (wxListBase *) NULL; | |
70 | } | |
71 | } | |
72 | ||
73 | void wxHashTableBase::Destroy() | |
74 | { | |
75 | if ( m_hashTable ) | |
76 | { | |
77 | for ( size_t n = 0; n < m_hashSize; n++ ) | |
78 | { | |
79 | delete m_hashTable[n]; | |
80 | } | |
81 | ||
82 | delete [] m_hashTable; | |
83 | ||
84 | m_hashTable = (wxListBase **)NULL; | |
85 | ||
86 | m_count = 0; | |
87 | } | |
88 | } | |
89 | ||
90 | void wxHashTableBase::DeleteContents(bool flag) | |
91 | { | |
92 | m_deleteContents = flag; | |
93 | for ( size_t n = 0; n < m_hashSize; n++ ) | |
94 | { | |
95 | if ( m_hashTable[n] ) | |
96 | { | |
97 | m_hashTable[n]->DeleteContents(flag); | |
98 | } | |
99 | } | |
100 | } | |
101 | ||
102 | wxNodeBase *wxHashTableBase::GetNode(long key, long value) const | |
103 | { | |
3ca6a5f0 | 104 | size_t slot = (size_t)abs((int)(key % (long)m_hashSize)); |
bcaa23de VZ |
105 | |
106 | wxNodeBase *node; | |
107 | if ( m_hashTable[slot] ) | |
108 | { | |
109 | node = m_hashTable[slot]->Find(wxListKey(value)); | |
110 | } | |
111 | else | |
112 | { | |
113 | node = (wxNodeBase *)NULL; | |
114 | } | |
115 | ||
116 | return node; | |
117 | } | |
118 | ||
119 | // ---------------------------------------------------------------------------- | |
120 | // old not type safe wxHashTable | |
121 | // ---------------------------------------------------------------------------- | |
122 | ||
debe6624 | 123 | wxHashTable::wxHashTable (int the_key_type, int size) |
c801d85f | 124 | { |
17d8ee1c JS |
125 | n = 0; |
126 | hash_table = (wxList**) NULL; | |
127 | Create(the_key_type, size); | |
5692876f | 128 | m_count = 0; |
f803b25b | 129 | m_deleteContents = false; |
17d8ee1c | 130 | /* |
c801d85f KB |
131 | n = size; |
132 | current_position = -1; | |
c67daf87 | 133 | current_node = (wxNode *) NULL; |
c801d85f KB |
134 | |
135 | key_type = the_key_type; | |
136 | hash_table = new wxList *[size]; | |
137 | int i; | |
138 | for (i = 0; i < size; i++) | |
c67daf87 | 139 | hash_table[i] = (wxList *) NULL; |
17d8ee1c | 140 | */ |
c801d85f KB |
141 | } |
142 | ||
bcaa23de | 143 | wxHashTable::~wxHashTable () |
c801d85f | 144 | { |
e55ad60e RR |
145 | Destroy(); |
146 | } | |
147 | ||
bcaa23de | 148 | void wxHashTable::Destroy() |
e55ad60e RR |
149 | { |
150 | if (!hash_table) return; | |
c801d85f KB |
151 | int i; |
152 | for (i = 0; i < n; i++) | |
153 | if (hash_table[i]) | |
154 | delete hash_table[i]; | |
155 | delete[] hash_table; | |
e55ad60e | 156 | hash_table = NULL; |
c801d85f KB |
157 | } |
158 | ||
debe6624 | 159 | bool wxHashTable::Create(int the_key_type, int size) |
c801d85f | 160 | { |
17d8ee1c JS |
161 | Destroy(); |
162 | ||
c801d85f KB |
163 | n = size; |
164 | current_position = -1; | |
c67daf87 | 165 | current_node = (wxNode *) NULL; |
c801d85f KB |
166 | |
167 | key_type = the_key_type; | |
c801d85f KB |
168 | hash_table = new wxList *[size]; |
169 | int i; | |
170 | for (i = 0; i < size; i++) | |
c67daf87 | 171 | hash_table[i] = (wxList *) NULL; |
f803b25b | 172 | return true; |
c801d85f KB |
173 | } |
174 | ||
4e67bfc7 VS |
175 | |
176 | void wxHashTable::DoCopy(const wxHashTable& table) | |
177 | { | |
178 | n = table.n; | |
ea450825 | 179 | m_count = table.m_count; |
4e67bfc7 VS |
180 | current_position = table.current_position; |
181 | current_node = NULL; // doesn't matter - Next() will reconstruct it | |
182 | key_type = table.key_type; | |
183 | ||
184 | hash_table = new wxList *[n]; | |
185 | for (int i = 0; i < n; i++) { | |
186 | if (table.hash_table[i] == NULL) | |
187 | hash_table[i] = NULL; | |
188 | else { | |
189 | hash_table[i] = new wxList(key_type); | |
190 | *(hash_table[i]) = *(table.hash_table[i]); | |
191 | } | |
192 | } | |
193 | } | |
194 | ||
debe6624 | 195 | void wxHashTable::Put (long key, long value, wxObject * object) |
c801d85f KB |
196 | { |
197 | // Should NEVER be | |
198 | long k = (long) key; | |
c801d85f KB |
199 | |
200 | int position = (int) (k % n); | |
ffae916f BJ |
201 | if (position < 0) position = -position; |
202 | ||
c801d85f | 203 | if (!hash_table[position]) |
c7fb814a | 204 | { |
cbb26e3f | 205 | hash_table[position] = new wxList (wxKEY_INTEGER); |
f803b25b | 206 | if (m_deleteContents) hash_table[position]->DeleteContents(true); |
c7fb814a | 207 | } |
c801d85f KB |
208 | |
209 | hash_table[position]->Append (value, object); | |
5692876f | 210 | m_count++; |
c801d85f KB |
211 | } |
212 | ||
50920146 | 213 | void wxHashTable::Put (long key, const wxChar *value, wxObject * object) |
c801d85f KB |
214 | { |
215 | // Should NEVER be | |
bcaa23de | 216 | long k = (long) key; |
c801d85f KB |
217 | |
218 | int position = (int) (k % n); | |
ffae916f | 219 | if (position < 0) position = -position; |
bcaa23de | 220 | |
c801d85f | 221 | if (!hash_table[position]) |
c7fb814a | 222 | { |
cbb26e3f | 223 | hash_table[position] = new wxList (wxKEY_STRING); |
f803b25b | 224 | if (m_deleteContents) hash_table[position]->DeleteContents(true); |
c7fb814a | 225 | } |
c801d85f KB |
226 | |
227 | hash_table[position]->Append (value, object); | |
5692876f | 228 | m_count++; |
c801d85f KB |
229 | } |
230 | ||
debe6624 | 231 | void wxHashTable::Put (long key, wxObject * object) |
c801d85f KB |
232 | { |
233 | // Should NEVER be | |
234 | long k = (long) key; | |
bcaa23de | 235 | |
c801d85f | 236 | int position = (int) (k % n); |
ffae916f BJ |
237 | if (position < 0) position = -position; |
238 | ||
c801d85f | 239 | if (!hash_table[position]) |
c7fb814a | 240 | { |
c801d85f | 241 | hash_table[position] = new wxList (wxKEY_INTEGER); |
f803b25b | 242 | if (m_deleteContents) hash_table[position]->DeleteContents(true); |
c7fb814a | 243 | } |
bcaa23de | 244 | |
c801d85f | 245 | hash_table[position]->Append (k, object); |
5692876f | 246 | m_count++; |
c801d85f KB |
247 | } |
248 | ||
50920146 | 249 | void wxHashTable::Put (const wxChar *key, wxObject * object) |
c801d85f KB |
250 | { |
251 | int position = (int) (MakeKey (key) % n); | |
ffae916f | 252 | if (position < 0) position = -position; |
c801d85f KB |
253 | |
254 | if (!hash_table[position]) | |
c7fb814a | 255 | { |
c801d85f | 256 | hash_table[position] = new wxList (wxKEY_STRING); |
f803b25b | 257 | if (m_deleteContents) hash_table[position]->DeleteContents(true); |
c7fb814a | 258 | } |
c801d85f KB |
259 | |
260 | hash_table[position]->Append (key, object); | |
5692876f | 261 | m_count++; |
c801d85f KB |
262 | } |
263 | ||
debe6624 | 264 | wxObject *wxHashTable::Get (long key, long value) const |
c801d85f KB |
265 | { |
266 | // Should NEVER be | |
267 | long k = (long) key; | |
c801d85f KB |
268 | |
269 | int position = (int) (k % n); | |
ffae916f BJ |
270 | if (position < 0) position = -position; |
271 | ||
c801d85f | 272 | if (!hash_table[position]) |
c67daf87 | 273 | return (wxObject *) NULL; |
c801d85f KB |
274 | else |
275 | { | |
276 | wxNode *node = hash_table[position]->Find (value); | |
277 | if (node) | |
b1d4dd7a | 278 | return node->GetData (); |
c801d85f | 279 | else |
bcaa23de | 280 | return (wxObject *) NULL; |
c801d85f KB |
281 | } |
282 | } | |
283 | ||
50920146 | 284 | wxObject *wxHashTable::Get (long key, const wxChar *value) const |
c801d85f KB |
285 | { |
286 | // Should NEVER be | |
287 | long k = (long) key; | |
bcaa23de | 288 | |
c801d85f | 289 | int position = (int) (k % n); |
ffae916f BJ |
290 | if (position < 0) position = -position; |
291 | ||
c801d85f | 292 | if (!hash_table[position]) |
c67daf87 | 293 | return (wxObject *) NULL; |
c801d85f KB |
294 | else |
295 | { | |
296 | wxNode *node = hash_table[position]->Find (value); | |
297 | if (node) | |
b1d4dd7a | 298 | return node->GetData (); |
c801d85f | 299 | else |
bcaa23de | 300 | return (wxObject *) NULL; |
c801d85f KB |
301 | } |
302 | } | |
303 | ||
debe6624 | 304 | wxObject *wxHashTable::Get (long key) const |
c801d85f KB |
305 | { |
306 | // Should NEVER be | |
307 | long k = (long) key; | |
c801d85f KB |
308 | |
309 | int position = (int) (k % n); | |
ffae916f BJ |
310 | if (position < 0) position = -position; |
311 | ||
c801d85f | 312 | if (!hash_table[position]) |
c67daf87 | 313 | return (wxObject *) NULL; |
c801d85f KB |
314 | else |
315 | { | |
316 | wxNode *node = hash_table[position]->Find (k); | |
b1d4dd7a | 317 | return node ? node->GetData () : (wxObject*)NULL; |
c801d85f KB |
318 | } |
319 | } | |
320 | ||
50920146 | 321 | wxObject *wxHashTable::Get (const wxChar *key) const |
c801d85f KB |
322 | { |
323 | int position = (int) (MakeKey (key) % n); | |
ffae916f | 324 | if (position < 0) position = -position; |
c801d85f KB |
325 | |
326 | if (!hash_table[position]) | |
c67daf87 | 327 | return (wxObject *) NULL; |
c801d85f KB |
328 | else |
329 | { | |
330 | wxNode *node = hash_table[position]->Find (key); | |
b1d4dd7a | 331 | return node ? node->GetData () : (wxObject*)NULL; |
c801d85f KB |
332 | } |
333 | } | |
334 | ||
debe6624 | 335 | wxObject *wxHashTable::Delete (long key) |
c801d85f KB |
336 | { |
337 | // Should NEVER be | |
338 | long k = (long) key; | |
c801d85f KB |
339 | |
340 | int position = (int) (k % n); | |
ffae916f BJ |
341 | if (position < 0) position = -position; |
342 | ||
c801d85f | 343 | if (!hash_table[position]) |
c67daf87 | 344 | return (wxObject *) NULL; |
c801d85f KB |
345 | else |
346 | { | |
347 | wxNode *node = hash_table[position]->Find (k); | |
348 | if (node) | |
bcaa23de | 349 | { |
b1d4dd7a | 350 | wxObject *data = node->GetData (); |
bcaa23de VZ |
351 | delete node; |
352 | m_count--; | |
353 | return data; | |
354 | } | |
c801d85f | 355 | else |
bcaa23de | 356 | return (wxObject *) NULL; |
c801d85f KB |
357 | } |
358 | } | |
359 | ||
50920146 | 360 | wxObject *wxHashTable::Delete (const wxChar *key) |
c801d85f KB |
361 | { |
362 | int position = (int) (MakeKey (key) % n); | |
ffae916f BJ |
363 | if (position < 0) position = -position; |
364 | ||
c801d85f | 365 | if (!hash_table[position]) |
c67daf87 | 366 | return (wxObject *) NULL; |
c801d85f KB |
367 | else |
368 | { | |
369 | wxNode *node = hash_table[position]->Find (key); | |
370 | if (node) | |
bcaa23de | 371 | { |
b1d4dd7a | 372 | wxObject *data = node->GetData (); |
bcaa23de VZ |
373 | delete node; |
374 | m_count--; | |
375 | return data; | |
376 | } | |
c801d85f | 377 | else |
bcaa23de | 378 | return (wxObject *) NULL; |
c801d85f KB |
379 | } |
380 | } | |
381 | ||
debe6624 | 382 | wxObject *wxHashTable::Delete (long key, int value) |
c801d85f KB |
383 | { |
384 | // Should NEVER be | |
bcaa23de | 385 | long k = (long) key; |
c801d85f KB |
386 | |
387 | int position = (int) (k % n); | |
ffae916f BJ |
388 | if (position < 0) position = -position; |
389 | ||
c801d85f | 390 | if (!hash_table[position]) |
c67daf87 | 391 | return (wxObject *) NULL; |
c801d85f KB |
392 | else |
393 | { | |
394 | wxNode *node = hash_table[position]->Find (value); | |
395 | if (node) | |
bcaa23de | 396 | { |
b1d4dd7a | 397 | wxObject *data = node->GetData (); |
bcaa23de VZ |
398 | delete node; |
399 | m_count--; | |
400 | return data; | |
401 | } | |
c801d85f | 402 | else |
bcaa23de | 403 | return (wxObject *) NULL; |
c801d85f KB |
404 | } |
405 | } | |
406 | ||
50920146 | 407 | wxObject *wxHashTable::Delete (long key, const wxChar *value) |
c801d85f KB |
408 | { |
409 | int position = (int) (key % n); | |
ffae916f BJ |
410 | if (position < 0) position = -position; |
411 | ||
c801d85f | 412 | if (!hash_table[position]) |
c67daf87 | 413 | return (wxObject *) NULL; |
c801d85f KB |
414 | else |
415 | { | |
416 | wxNode *node = hash_table[position]->Find (value); | |
417 | if (node) | |
bcaa23de | 418 | { |
b1d4dd7a | 419 | wxObject *data = node->GetData (); |
bcaa23de VZ |
420 | delete node; |
421 | m_count--; | |
422 | return data; | |
423 | } | |
c801d85f | 424 | else |
bcaa23de | 425 | return (wxObject *) NULL; |
c801d85f KB |
426 | } |
427 | } | |
428 | ||
50920146 | 429 | long wxHashTable::MakeKey (const wxChar *string) const |
c801d85f KB |
430 | { |
431 | long int_key = 0; | |
432 | ||
433 | while (*string) | |
50920146 | 434 | int_key += (wxUChar) *string++; |
c801d85f KB |
435 | |
436 | return int_key; | |
437 | } | |
438 | ||
bcaa23de | 439 | void wxHashTable::BeginFind () |
c801d85f KB |
440 | { |
441 | current_position = -1; | |
c67daf87 | 442 | current_node = (wxNode *) NULL; |
c801d85f KB |
443 | } |
444 | ||
1a6d9c76 | 445 | wxHashTable::Node* wxHashTable::Next () |
c801d85f | 446 | { |
c67daf87 | 447 | wxNode *found = (wxNode *) NULL; |
f803b25b | 448 | bool end = false; |
c801d85f KB |
449 | while (!end && !found) |
450 | { | |
451 | if (!current_node) | |
bcaa23de VZ |
452 | { |
453 | current_position++; | |
454 | if (current_position >= n) | |
455 | { | |
456 | current_position = -1; | |
457 | current_node = (wxNode *) NULL; | |
f803b25b | 458 | end = true; |
bcaa23de VZ |
459 | } |
460 | else | |
461 | { | |
462 | if (hash_table[current_position]) | |
463 | { | |
b1d4dd7a | 464 | current_node = hash_table[current_position]->GetFirst (); |
bcaa23de VZ |
465 | found = current_node; |
466 | } | |
467 | } | |
468 | } | |
c801d85f | 469 | else |
bcaa23de | 470 | { |
b1d4dd7a | 471 | current_node = current_node->GetNext (); |
bcaa23de VZ |
472 | found = current_node; |
473 | } | |
c801d85f KB |
474 | } |
475 | return found; | |
476 | } | |
477 | ||
debe6624 | 478 | void wxHashTable::DeleteContents (bool flag) |
c801d85f KB |
479 | { |
480 | int i; | |
c7fb814a | 481 | m_deleteContents = flag; |
c801d85f KB |
482 | for (i = 0; i < n; i++) |
483 | { | |
484 | if (hash_table[i]) | |
bcaa23de | 485 | hash_table[i]->DeleteContents (flag); |
c801d85f KB |
486 | } |
487 | } | |
488 | ||
bcaa23de | 489 | void wxHashTable::Clear () |
c801d85f | 490 | { |
19230604 RD |
491 | int i; |
492 | if (hash_table) | |
c801d85f | 493 | { |
19230604 RD |
494 | for (i = 0; i < n; i++) |
495 | { | |
496 | if (hash_table[i]) | |
497 | hash_table[i]->Clear (); | |
498 | } | |
c801d85f | 499 | } |
5692876f | 500 | m_count = 0; |
c801d85f KB |
501 | } |
502 | ||
6e86701b | 503 | #else // if !wxUSE_OLD_HASH_TABLE |
1a6d9c76 MB |
504 | |
505 | wxHashTableBase_Node::wxHashTableBase_Node( long key, void* value, | |
506 | wxHashTableBase* table ) | |
507 | : m_value( value ), m_hashPtr( table ) | |
508 | { | |
509 | m_key.integer = key; | |
510 | } | |
511 | ||
512 | wxHashTableBase_Node::wxHashTableBase_Node( const wxChar* key, void* value, | |
513 | wxHashTableBase* table ) | |
514 | : m_value( value ), m_hashPtr( table ) | |
515 | { | |
516 | m_key.string = wxStrcpy( new wxChar[wxStrlen( key ) + 1], key ); | |
517 | } | |
518 | ||
519 | wxHashTableBase_Node::~wxHashTableBase_Node() | |
520 | { | |
521 | if( m_hashPtr ) m_hashPtr->DoRemoveNode( this ); | |
522 | } | |
523 | ||
524 | // | |
525 | ||
526 | wxHashTableBase::wxHashTableBase() | |
527 | : m_size( 0 ), m_count( 0 ), m_table( NULL ), m_keyType( wxKEY_NONE ), | |
528 | m_deleteContents( false ) | |
529 | { | |
530 | } | |
531 | ||
1a6d9c76 MB |
532 | void wxHashTableBase::Create( wxKeyType keyType, size_t size ) |
533 | { | |
534 | m_keyType = keyType; | |
535 | m_size = size; | |
536 | m_table = new wxHashTableBase_Node*[ m_size ]; | |
537 | ||
538 | for( size_t i = 0; i < m_size; ++i ) | |
539 | m_table[i] = NULL; | |
540 | } | |
541 | ||
542 | void wxHashTableBase::Clear() | |
543 | { | |
544 | for( size_t i = 0; i < m_size; ++i ) | |
545 | { | |
546 | Node* end = m_table[i]; | |
547 | ||
548 | if( end == NULL ) | |
549 | continue; | |
550 | ||
551 | Node *curr, *next = end->GetNext(); | |
552 | ||
553 | do | |
554 | { | |
555 | curr = next; | |
556 | next = curr->GetNext(); | |
557 | ||
558 | DoDestroyNode( curr ); | |
559 | ||
560 | delete curr; | |
561 | } | |
562 | while( curr != end ); | |
563 | ||
564 | m_table[i] = NULL; | |
565 | } | |
566 | ||
567 | m_count = 0; | |
568 | } | |
569 | ||
570 | void wxHashTableBase::DoRemoveNode( wxHashTableBase_Node* node ) | |
571 | { | |
572 | size_t bucket = ( m_keyType == wxKEY_INTEGER ? | |
573 | node->m_key.integer : | |
574 | MakeKey( node->m_key.string ) ) % m_size; | |
575 | ||
576 | if( node->GetNext() == node ) | |
577 | { | |
578 | // single-node chain (common case) | |
579 | m_table[bucket] = NULL; | |
580 | } | |
581 | else | |
582 | { | |
583 | Node *start = m_table[bucket], *curr; | |
584 | Node* prev = start; | |
585 | ||
586 | for( curr = prev->GetNext(); curr != node; | |
f803b25b | 587 | prev = curr, curr = curr->GetNext() ) ; |
1a6d9c76 MB |
588 | |
589 | DoUnlinkNode( bucket, node, prev ); | |
590 | } | |
591 | ||
592 | DoDestroyNode( node ); | |
593 | } | |
594 | ||
595 | void wxHashTableBase::DoDestroyNode( wxHashTableBase_Node* node ) | |
596 | { | |
597 | // if it is called from DoRemoveNode, node has already been | |
598 | // removed, from other places it does not matter | |
599 | node->m_hashPtr = NULL; | |
600 | ||
601 | if( m_keyType == wxKEY_STRING ) | |
602 | delete[] node->m_key.string; | |
603 | if( m_deleteContents ) | |
604 | DoDeleteContents( node ); | |
605 | } | |
606 | ||
607 | void wxHashTableBase::Destroy() | |
608 | { | |
609 | Clear(); | |
610 | ||
611 | delete[] m_table; | |
612 | ||
613 | m_table = NULL; | |
614 | m_size = 0; | |
615 | } | |
616 | ||
617 | void wxHashTableBase::DoInsertNode( size_t bucket, wxHashTableBase_Node* node ) | |
618 | { | |
619 | if( m_table[bucket] == NULL ) | |
620 | { | |
621 | m_table[bucket] = node->m_next = node; | |
622 | } | |
623 | else | |
624 | { | |
625 | Node *prev = m_table[bucket]; | |
626 | Node *next = prev->m_next; | |
627 | ||
628 | prev->m_next = node; | |
629 | node->m_next = next; | |
630 | m_table[bucket] = node; | |
631 | } | |
632 | ||
633 | ++m_count; | |
634 | } | |
635 | ||
636 | void wxHashTableBase::DoPut( long key, long hash, void* data ) | |
637 | { | |
638 | wxASSERT( m_keyType == wxKEY_INTEGER ); | |
639 | ||
640 | size_t bucket = size_t(hash) % m_size; | |
641 | Node* node = new wxHashTableBase_Node( key, data, this ); | |
642 | ||
643 | DoInsertNode( bucket, node ); | |
644 | } | |
645 | ||
646 | void wxHashTableBase::DoPut( const wxChar* key, long hash, void* data ) | |
647 | { | |
648 | wxASSERT( m_keyType == wxKEY_STRING ); | |
649 | ||
650 | size_t bucket = size_t(hash) % m_size; | |
651 | Node* node = new wxHashTableBase_Node( key, data, this ); | |
652 | ||
653 | DoInsertNode( bucket, node ); | |
654 | } | |
655 | ||
656 | void* wxHashTableBase::DoGet( long key, long hash ) const | |
657 | { | |
658 | wxASSERT( m_keyType == wxKEY_INTEGER ); | |
659 | ||
660 | size_t bucket = size_t(hash) % m_size; | |
661 | ||
662 | if( m_table[bucket] == NULL ) | |
663 | return NULL; | |
664 | ||
665 | Node *first = m_table[bucket]->GetNext(), | |
666 | *curr = first; | |
667 | ||
668 | do | |
669 | { | |
670 | if( curr->m_key.integer == key ) | |
671 | return curr->m_value; | |
672 | ||
673 | curr = curr->GetNext(); | |
674 | } | |
675 | while( curr != first ); | |
676 | ||
677 | return NULL; | |
678 | } | |
679 | ||
680 | void* wxHashTableBase::DoGet( const wxChar* key, long hash ) const | |
681 | { | |
682 | wxASSERT( m_keyType == wxKEY_STRING ); | |
683 | ||
684 | size_t bucket = size_t(hash) % m_size; | |
685 | ||
686 | if( m_table[bucket] == NULL ) | |
687 | return NULL; | |
688 | ||
689 | Node *first = m_table[bucket]->GetNext(), | |
690 | *curr = first; | |
691 | ||
692 | do | |
693 | { | |
694 | if( wxStrcmp( curr->m_key.string, key ) == 0 ) | |
695 | return curr->m_value; | |
696 | ||
697 | curr = curr->GetNext(); | |
698 | } | |
699 | while( curr != first ); | |
700 | ||
701 | return NULL; | |
702 | } | |
703 | ||
704 | void wxHashTableBase::DoUnlinkNode( size_t bucket, wxHashTableBase_Node* node, | |
705 | wxHashTableBase_Node* prev ) | |
706 | { | |
707 | if( node == m_table[bucket] ) | |
708 | m_table[bucket] = prev; | |
709 | ||
710 | if( prev == node && prev == node->GetNext() ) | |
711 | m_table[bucket] = NULL; | |
712 | else | |
713 | prev->m_next = node->m_next; | |
714 | ||
715 | DoDestroyNode( node ); | |
716 | --m_count; | |
717 | } | |
718 | ||
719 | void* wxHashTableBase::DoDelete( long key, long hash ) | |
720 | { | |
721 | wxASSERT( m_keyType == wxKEY_INTEGER ); | |
722 | ||
723 | size_t bucket = size_t(hash) % m_size; | |
724 | ||
725 | if( m_table[bucket] == NULL ) | |
726 | return NULL; | |
727 | ||
728 | Node *first = m_table[bucket]->GetNext(), | |
729 | *curr = first, | |
730 | *prev = m_table[bucket]; | |
731 | ||
732 | do | |
733 | { | |
734 | if( curr->m_key.integer == key ) | |
735 | { | |
736 | void* retval = curr->m_value; | |
737 | curr->m_value = NULL; | |
738 | ||
739 | DoUnlinkNode( bucket, curr, prev ); | |
740 | delete curr; | |
741 | ||
742 | return retval; | |
743 | } | |
744 | ||
745 | prev = curr; | |
746 | curr = curr->GetNext(); | |
747 | } | |
748 | while( curr != first ); | |
749 | ||
750 | return NULL; | |
751 | } | |
752 | ||
753 | void* wxHashTableBase::DoDelete( const wxChar* key, long hash ) | |
754 | { | |
755 | wxASSERT( m_keyType == wxKEY_STRING ); | |
756 | ||
757 | size_t bucket = size_t(hash) % m_size; | |
758 | ||
759 | if( m_table[bucket] == NULL ) | |
760 | return NULL; | |
761 | ||
762 | Node *first = m_table[bucket]->GetNext(), | |
763 | *curr = first, | |
764 | *prev = m_table[bucket]; | |
765 | ||
766 | do | |
767 | { | |
768 | if( wxStrcmp( curr->m_key.string, key ) == 0 ) | |
769 | { | |
770 | void* retval = curr->m_value; | |
771 | curr->m_value = NULL; | |
772 | ||
773 | DoUnlinkNode( bucket, curr, prev ); | |
774 | delete curr; | |
775 | ||
776 | return retval; | |
777 | } | |
778 | ||
779 | prev = curr; | |
780 | curr = curr->GetNext(); | |
781 | } | |
782 | while( curr != first ); | |
783 | ||
784 | return NULL; | |
785 | } | |
786 | ||
787 | long wxHashTableBase::MakeKey( const wxChar *str ) | |
788 | { | |
789 | long int_key = 0; | |
790 | ||
791 | while( *str ) | |
792 | int_key += (wxUChar)*str++; | |
793 | ||
794 | return int_key; | |
795 | } | |
796 | ||
2441fb44 VZ |
797 | // ---------------------------------------------------------------------------- |
798 | // wxHashTable | |
799 | // ---------------------------------------------------------------------------- | |
1a6d9c76 MB |
800 | |
801 | wxHashTable::wxHashTable( const wxHashTable& table ) | |
2441fb44 | 802 | : wxHashTableBase() |
1a6d9c76 MB |
803 | { |
804 | DoCopy( table ); | |
805 | } | |
806 | ||
807 | const wxHashTable& wxHashTable::operator=( const wxHashTable& table ) | |
808 | { | |
809 | Destroy(); | |
810 | DoCopy( table ); | |
811 | ||
812 | return *this; | |
813 | } | |
814 | ||
6aa33060 | 815 | void wxHashTable::DoCopy( const wxHashTable& WXUNUSED(table) ) |
1a6d9c76 MB |
816 | { |
817 | Create( m_keyType, m_size ); | |
818 | ||
7cb32b4b | 819 | wxFAIL; |
1a6d9c76 MB |
820 | } |
821 | ||
822 | void wxHashTable::DoDeleteContents( wxHashTableBase_Node* node ) | |
823 | { | |
824 | delete ((wxHashTable_Node*)node)->GetData(); | |
825 | } | |
826 | ||
827 | void wxHashTable::GetNextNode( size_t bucketStart ) | |
828 | { | |
829 | for( size_t i = bucketStart; i < m_size; ++i ) | |
830 | { | |
831 | if( m_table[i] != NULL ) | |
832 | { | |
833 | m_curr = ((Node*)m_table[i])->GetNext(); | |
834 | m_currBucket = i; | |
835 | return; | |
836 | } | |
837 | } | |
838 | ||
839 | m_curr = NULL; | |
840 | m_currBucket = 0; | |
841 | } | |
842 | ||
843 | wxHashTable::Node* wxHashTable::Next() | |
844 | { | |
845 | if( m_curr == NULL ) | |
846 | GetNextNode( 0 ); | |
847 | else | |
848 | { | |
849 | m_curr = m_curr->GetNext(); | |
850 | ||
851 | if( m_curr == ( (Node*)m_table[m_currBucket] )->GetNext() ) | |
852 | GetNextNode( m_currBucket + 1 ); | |
853 | } | |
854 | ||
855 | return m_curr; | |
856 | } | |
857 | ||
6e86701b | 858 | #endif // !wxUSE_OLD_HASH_TABLE |