]> git.saurik.com Git - wxWidgets.git/blame - src/common/hash.cpp
wxX11 might compile now.
[wxWidgets.git] / src / common / hash.cpp
CommitLineData
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$
8// Copyright: (c) Julian Smart and Markus Holzem
bcaa23de 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
bcaa23de
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c801d85f
KB
20#ifdef __GNUG__
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
37#include <string.h>
38#include <stdarg.h>
39
bcaa23de
VZ
40// ----------------------------------------------------------------------------
41// wxWin macros
42// ----------------------------------------------------------------------------
43
c801d85f 44IMPLEMENT_DYNAMIC_CLASS(wxHashTable, wxObject)
c801d85f 45
bcaa23de
VZ
46// ============================================================================
47// implementation
48// ============================================================================
49
50// ----------------------------------------------------------------------------
51// wxHashTablleBase for working with "void *" data
52// ----------------------------------------------------------------------------
53
54wxHashTableBase::wxHashTableBase()
55{
56 m_deleteContents = FALSE;
57 m_hashTable = (wxListBase **)NULL;
58 m_hashSize = 0;
59 m_count = 0;
60 m_keyType = wxKEY_NONE;
61}
62
63void wxHashTableBase::Create(wxKeyType keyType, size_t size)
64{
65 Destroy();
66
67 m_hashSize = size;
68 m_keyType = keyType;
69 m_hashTable = new wxListBase *[size];
70 for ( size_t n = 0; n < m_hashSize; n++ )
71 {
72 m_hashTable[n] = (wxListBase *) NULL;
73 }
74}
75
76void wxHashTableBase::Destroy()
77{
78 if ( m_hashTable )
79 {
80 for ( size_t n = 0; n < m_hashSize; n++ )
81 {
82 delete m_hashTable[n];
83 }
84
85 delete [] m_hashTable;
86
87 m_hashTable = (wxListBase **)NULL;
88
89 m_count = 0;
90 }
91}
92
93void wxHashTableBase::DeleteContents(bool flag)
94{
95 m_deleteContents = flag;
96 for ( size_t n = 0; n < m_hashSize; n++ )
97 {
98 if ( m_hashTable[n] )
99 {
100 m_hashTable[n]->DeleteContents(flag);
101 }
102 }
103}
104
105wxNodeBase *wxHashTableBase::GetNode(long key, long value) const
106{
3ca6a5f0 107 size_t slot = (size_t)abs((int)(key % (long)m_hashSize));
bcaa23de
VZ
108
109 wxNodeBase *node;
110 if ( m_hashTable[slot] )
111 {
112 node = m_hashTable[slot]->Find(wxListKey(value));
113 }
114 else
115 {
116 node = (wxNodeBase *)NULL;
117 }
118
119 return node;
120}
121
c2bb85e9
VZ
122// ----------------------------------------------------------------------------
123// wxHashTableLong
124// ----------------------------------------------------------------------------
125
a95e38c0
VZ
126wxHashTableLong::~wxHashTableLong()
127{
128 Destroy();
129}
130
c2bb85e9
VZ
131void wxHashTableLong::Init(size_t size)
132{
133 m_hashSize = size;
134 m_values = new wxArrayLong *[size];
135 m_keys = new wxArrayLong *[size];
136
137 for ( size_t n = 0; n < m_hashSize; n++ )
138 {
139 m_values[n] =
140 m_keys[n] = (wxArrayLong *)NULL;
141 }
142
143 m_count = 0;
144}
145
b7aaabf8
VS
146void wxHashTableLong::Create(size_t size)
147{
148 Init(size);
149}
150
c2bb85e9
VZ
151void wxHashTableLong::Destroy()
152{
153 for ( size_t n = 0; n < m_hashSize; n++ )
154 {
155 delete m_values[n];
156 delete m_keys[n];
157 }
158
159 delete [] m_values;
160 delete [] m_keys;
161 m_hashSize = 0;
162 m_count = 0;
163}
164
165void wxHashTableLong::Put(long key, long value)
166{
167 wxCHECK_RET( m_hashSize, _T("must call Create() first") );
168
3ca6a5f0 169 size_t slot = (size_t)abs((int)(key % (long)m_hashSize));
c2bb85e9
VZ
170
171 if ( !m_keys[slot] )
172 {
173 m_keys[slot] = new wxArrayLong;
174 m_values[slot] = new wxArrayLong;
175 }
176
177 m_keys[slot]->Add(key);
178 m_values[slot]->Add(value);
179
180 m_count++;
181}
182
183long wxHashTableLong::Get(long key) const
184{
185 wxCHECK_MSG( m_hashSize, wxNOT_FOUND, _T("must call Create() first") );
186
3ca6a5f0 187 size_t slot = (size_t)abs((int)(key % (long)m_hashSize));
c2bb85e9
VZ
188
189 wxArrayLong *keys = m_keys[slot];
190 if ( keys )
191 {
192 size_t count = keys->GetCount();
193 for ( size_t n = 0; n < count; n++ )
194 {
195 if ( keys->Item(n) == key )
196 {
197 return m_values[slot]->Item(n);
198 }
199 }
200 }
201
202 return wxNOT_FOUND;
203}
204
205long wxHashTableLong::Delete(long key)
206{
207 wxCHECK_MSG( m_hashSize, wxNOT_FOUND, _T("must call Create() first") );
208
3ca6a5f0 209 size_t slot = (size_t)abs((int)(key % (long)m_hashSize));
c2bb85e9
VZ
210
211 wxArrayLong *keys = m_keys[slot];
212 if ( keys )
213 {
214 size_t count = keys->GetCount();
215 for ( size_t n = 0; n < count; n++ )
216 {
217 if ( keys->Item(n) == key )
218 {
219 long val = m_values[slot]->Item(n);
220
221 keys->RemoveAt(n);
222 m_values[slot]->RemoveAt(n);
223
224 m_count--;
225
226 return val;
227 }
228 }
229 }
230
231 return wxNOT_FOUND;
232}
233
bd83cb56
VZ
234// ----------------------------------------------------------------------------
235// wxStringHashTable: more efficient than storing strings in a list
236// ----------------------------------------------------------------------------
237
238wxStringHashTable::wxStringHashTable(size_t sizeTable)
239{
240 m_keys = new wxArrayLong *[sizeTable];
241 m_values = new wxArrayString *[sizeTable];
242
243 m_hashSize = sizeTable;
244 for ( size_t n = 0; n < m_hashSize; n++ )
245 {
246 m_values[n] = (wxArrayString *)NULL;
247 m_keys[n] = (wxArrayLong *)NULL;
248 }
249}
250
251wxStringHashTable::~wxStringHashTable()
252{
253 Destroy();
254}
255
256void wxStringHashTable::Destroy()
257{
258 for ( size_t n = 0; n < m_hashSize; n++ )
259 {
260 delete m_values[n];
261 delete m_keys[n];
262 }
263
264 delete [] m_values;
265 delete [] m_keys;
266 m_hashSize = 0;
267}
268
269void wxStringHashTable::Put(long key, const wxString& value)
270{
271 wxCHECK_RET( m_hashSize, _T("must call Create() first") );
272
273 size_t slot = (size_t)abs((int)(key % (long)m_hashSize));
274
275 if ( !m_keys[slot] )
276 {
277 m_keys[slot] = new wxArrayLong;
278 m_values[slot] = new wxArrayString;
279 }
280
281 m_keys[slot]->Add(key);
282 m_values[slot]->Add(value);
283}
284
285wxString wxStringHashTable::Get(long key, bool *wasFound) const
286{
287 wxCHECK_MSG( m_hashSize, _T(""), _T("must call Create() first") );
288
289 size_t slot = (size_t)abs((int)(key % (long)m_hashSize));
290
291 wxArrayLong *keys = m_keys[slot];
292 if ( keys )
293 {
294 size_t count = keys->GetCount();
295 for ( size_t n = 0; n < count; n++ )
296 {
297 if ( keys->Item(n) == key )
298 {
299 if ( wasFound )
300 *wasFound = TRUE;
301
302 return m_values[slot]->Item(n);
303 }
304 }
305 }
306
307 if ( wasFound )
308 *wasFound = FALSE;
309
310 return _T("");
311}
312
bcaa23de
VZ
313// ----------------------------------------------------------------------------
314// old not type safe wxHashTable
315// ----------------------------------------------------------------------------
316
debe6624 317wxHashTable::wxHashTable (int the_key_type, int size)
c801d85f 318{
17d8ee1c
JS
319 n = 0;
320 hash_table = (wxList**) NULL;
321 Create(the_key_type, size);
5692876f 322 m_count = 0;
c7fb814a 323 m_deleteContents = FALSE;
17d8ee1c 324/*
c801d85f
KB
325 n = size;
326 current_position = -1;
c67daf87 327 current_node = (wxNode *) NULL;
c801d85f
KB
328
329 key_type = the_key_type;
330 hash_table = new wxList *[size];
331 int i;
332 for (i = 0; i < size; i++)
c67daf87 333 hash_table[i] = (wxList *) NULL;
17d8ee1c 334*/
c801d85f
KB
335}
336
bcaa23de 337wxHashTable::~wxHashTable ()
c801d85f 338{
e55ad60e
RR
339 Destroy();
340}
341
bcaa23de 342void wxHashTable::Destroy()
e55ad60e
RR
343{
344 if (!hash_table) return;
c801d85f
KB
345 int i;
346 for (i = 0; i < n; i++)
347 if (hash_table[i])
348 delete hash_table[i];
349 delete[] hash_table;
e55ad60e 350 hash_table = NULL;
c801d85f
KB
351}
352
debe6624 353bool wxHashTable::Create(int the_key_type, int size)
c801d85f 354{
17d8ee1c
JS
355 Destroy();
356
c801d85f
KB
357 n = size;
358 current_position = -1;
c67daf87 359 current_node = (wxNode *) NULL;
c801d85f
KB
360
361 key_type = the_key_type;
c801d85f
KB
362 hash_table = new wxList *[size];
363 int i;
364 for (i = 0; i < size; i++)
c67daf87 365 hash_table[i] = (wxList *) NULL;
c801d85f
KB
366 return TRUE;
367}
368
4e67bfc7
VS
369
370void wxHashTable::DoCopy(const wxHashTable& table)
371{
372 n = table.n;
ea450825 373 m_count = table.m_count;
4e67bfc7
VS
374 current_position = table.current_position;
375 current_node = NULL; // doesn't matter - Next() will reconstruct it
376 key_type = table.key_type;
377
378 hash_table = new wxList *[n];
379 for (int i = 0; i < n; i++) {
380 if (table.hash_table[i] == NULL)
381 hash_table[i] = NULL;
382 else {
383 hash_table[i] = new wxList(key_type);
384 *(hash_table[i]) = *(table.hash_table[i]);
385 }
386 }
387}
388
debe6624 389void wxHashTable::Put (long key, long value, wxObject * object)
c801d85f
KB
390{
391 // Should NEVER be
392 long k = (long) key;
c801d85f
KB
393
394 int position = (int) (k % n);
ffae916f
BJ
395 if (position < 0) position = -position;
396
c801d85f 397 if (!hash_table[position])
c7fb814a 398 {
cbb26e3f 399 hash_table[position] = new wxList (wxKEY_INTEGER);
c7fb814a
VS
400 if (m_deleteContents) hash_table[position]->DeleteContents(TRUE);
401 }
c801d85f
KB
402
403 hash_table[position]->Append (value, object);
5692876f 404 m_count++;
c801d85f
KB
405}
406
50920146 407void wxHashTable::Put (long key, const wxChar *value, wxObject * object)
c801d85f
KB
408{
409 // Should NEVER be
bcaa23de 410 long k = (long) key;
c801d85f
KB
411
412 int position = (int) (k % n);
ffae916f 413 if (position < 0) position = -position;
bcaa23de 414
c801d85f 415 if (!hash_table[position])
c7fb814a 416 {
cbb26e3f 417 hash_table[position] = new wxList (wxKEY_STRING);
c7fb814a
VS
418 if (m_deleteContents) hash_table[position]->DeleteContents(TRUE);
419 }
c801d85f
KB
420
421 hash_table[position]->Append (value, object);
5692876f 422 m_count++;
c801d85f
KB
423}
424
debe6624 425void wxHashTable::Put (long key, wxObject * object)
c801d85f
KB
426{
427 // Should NEVER be
428 long k = (long) key;
bcaa23de 429
c801d85f 430 int position = (int) (k % n);
ffae916f
BJ
431 if (position < 0) position = -position;
432
c801d85f 433 if (!hash_table[position])
c7fb814a 434 {
c801d85f 435 hash_table[position] = new wxList (wxKEY_INTEGER);
c7fb814a
VS
436 if (m_deleteContents) hash_table[position]->DeleteContents(TRUE);
437 }
bcaa23de 438
c801d85f 439 hash_table[position]->Append (k, object);
5692876f 440 m_count++;
c801d85f
KB
441}
442
50920146 443void wxHashTable::Put (const wxChar *key, wxObject * object)
c801d85f
KB
444{
445 int position = (int) (MakeKey (key) % n);
ffae916f 446 if (position < 0) position = -position;
c801d85f
KB
447
448 if (!hash_table[position])
c7fb814a 449 {
c801d85f 450 hash_table[position] = new wxList (wxKEY_STRING);
c7fb814a
VS
451 if (m_deleteContents) hash_table[position]->DeleteContents(TRUE);
452 }
c801d85f
KB
453
454 hash_table[position]->Append (key, object);
5692876f 455 m_count++;
c801d85f
KB
456}
457
debe6624 458wxObject *wxHashTable::Get (long key, long value) const
c801d85f
KB
459{
460 // Should NEVER be
461 long k = (long) key;
c801d85f
KB
462
463 int position = (int) (k % n);
ffae916f
BJ
464 if (position < 0) position = -position;
465
c801d85f 466 if (!hash_table[position])
c67daf87 467 return (wxObject *) NULL;
c801d85f
KB
468 else
469 {
470 wxNode *node = hash_table[position]->Find (value);
471 if (node)
bcaa23de 472 return node->Data ();
c801d85f 473 else
bcaa23de 474 return (wxObject *) NULL;
c801d85f
KB
475 }
476}
477
50920146 478wxObject *wxHashTable::Get (long key, const wxChar *value) const
c801d85f
KB
479{
480 // Should NEVER be
481 long k = (long) key;
bcaa23de 482
c801d85f 483 int position = (int) (k % n);
ffae916f
BJ
484 if (position < 0) position = -position;
485
c801d85f 486 if (!hash_table[position])
c67daf87 487 return (wxObject *) NULL;
c801d85f
KB
488 else
489 {
490 wxNode *node = hash_table[position]->Find (value);
491 if (node)
bcaa23de 492 return node->Data ();
c801d85f 493 else
bcaa23de 494 return (wxObject *) NULL;
c801d85f
KB
495 }
496}
497
debe6624 498wxObject *wxHashTable::Get (long key) const
c801d85f
KB
499{
500 // Should NEVER be
501 long k = (long) key;
c801d85f
KB
502
503 int position = (int) (k % n);
ffae916f
BJ
504 if (position < 0) position = -position;
505
c801d85f 506 if (!hash_table[position])
c67daf87 507 return (wxObject *) NULL;
c801d85f
KB
508 else
509 {
510 wxNode *node = hash_table[position]->Find (k);
511 return node ? node->Data () : (wxObject*)NULL;
512 }
513}
514
50920146 515wxObject *wxHashTable::Get (const wxChar *key) const
c801d85f
KB
516{
517 int position = (int) (MakeKey (key) % n);
ffae916f 518 if (position < 0) position = -position;
c801d85f
KB
519
520 if (!hash_table[position])
c67daf87 521 return (wxObject *) NULL;
c801d85f
KB
522 else
523 {
524 wxNode *node = hash_table[position]->Find (key);
525 return node ? node->Data () : (wxObject*)NULL;
526 }
527}
528
debe6624 529wxObject *wxHashTable::Delete (long key)
c801d85f
KB
530{
531 // Should NEVER be
532 long k = (long) key;
c801d85f
KB
533
534 int position = (int) (k % n);
ffae916f
BJ
535 if (position < 0) position = -position;
536
c801d85f 537 if (!hash_table[position])
c67daf87 538 return (wxObject *) NULL;
c801d85f
KB
539 else
540 {
541 wxNode *node = hash_table[position]->Find (k);
542 if (node)
bcaa23de
VZ
543 {
544 wxObject *data = node->Data ();
545 delete node;
546 m_count--;
547 return data;
548 }
c801d85f 549 else
bcaa23de 550 return (wxObject *) NULL;
c801d85f
KB
551 }
552}
553
50920146 554wxObject *wxHashTable::Delete (const wxChar *key)
c801d85f
KB
555{
556 int position = (int) (MakeKey (key) % n);
ffae916f
BJ
557 if (position < 0) position = -position;
558
c801d85f 559 if (!hash_table[position])
c67daf87 560 return (wxObject *) NULL;
c801d85f
KB
561 else
562 {
563 wxNode *node = hash_table[position]->Find (key);
564 if (node)
bcaa23de
VZ
565 {
566 wxObject *data = node->Data ();
567 delete node;
568 m_count--;
569 return data;
570 }
c801d85f 571 else
bcaa23de 572 return (wxObject *) NULL;
c801d85f
KB
573 }
574}
575
debe6624 576wxObject *wxHashTable::Delete (long key, int value)
c801d85f
KB
577{
578 // Should NEVER be
bcaa23de 579 long k = (long) key;
c801d85f
KB
580
581 int position = (int) (k % n);
ffae916f
BJ
582 if (position < 0) position = -position;
583
c801d85f 584 if (!hash_table[position])
c67daf87 585 return (wxObject *) NULL;
c801d85f
KB
586 else
587 {
588 wxNode *node = hash_table[position]->Find (value);
589 if (node)
bcaa23de
VZ
590 {
591 wxObject *data = node->Data ();
592 delete node;
593 m_count--;
594 return data;
595 }
c801d85f 596 else
bcaa23de 597 return (wxObject *) NULL;
c801d85f
KB
598 }
599}
600
50920146 601wxObject *wxHashTable::Delete (long key, const wxChar *value)
c801d85f
KB
602{
603 int position = (int) (key % n);
ffae916f
BJ
604 if (position < 0) position = -position;
605
c801d85f 606 if (!hash_table[position])
c67daf87 607 return (wxObject *) NULL;
c801d85f
KB
608 else
609 {
610 wxNode *node = hash_table[position]->Find (value);
611 if (node)
bcaa23de
VZ
612 {
613 wxObject *data = node->Data ();
614 delete node;
615 m_count--;
616 return data;
617 }
c801d85f 618 else
bcaa23de 619 return (wxObject *) NULL;
c801d85f
KB
620 }
621}
622
50920146 623long wxHashTable::MakeKey (const wxChar *string) const
c801d85f
KB
624{
625 long int_key = 0;
626
627 while (*string)
50920146 628 int_key += (wxUChar) *string++;
c801d85f
KB
629
630 return int_key;
631}
632
bcaa23de 633void wxHashTable::BeginFind ()
c801d85f
KB
634{
635 current_position = -1;
c67daf87 636 current_node = (wxNode *) NULL;
c801d85f
KB
637}
638
bcaa23de 639wxNode *wxHashTable::Next ()
c801d85f 640{
c67daf87 641 wxNode *found = (wxNode *) NULL;
c801d85f
KB
642 bool end = FALSE;
643 while (!end && !found)
644 {
645 if (!current_node)
bcaa23de
VZ
646 {
647 current_position++;
648 if (current_position >= n)
649 {
650 current_position = -1;
651 current_node = (wxNode *) NULL;
652 end = TRUE;
653 }
654 else
655 {
656 if (hash_table[current_position])
657 {
658 current_node = hash_table[current_position]->First ();
659 found = current_node;
660 }
661 }
662 }
c801d85f 663 else
bcaa23de
VZ
664 {
665 current_node = current_node->Next ();
666 found = current_node;
667 }
c801d85f
KB
668 }
669 return found;
670}
671
debe6624 672void wxHashTable::DeleteContents (bool flag)
c801d85f
KB
673{
674 int i;
c7fb814a 675 m_deleteContents = flag;
c801d85f
KB
676 for (i = 0; i < n; i++)
677 {
678 if (hash_table[i])
bcaa23de 679 hash_table[i]->DeleteContents (flag);
c801d85f
KB
680 }
681}
682
bcaa23de 683void wxHashTable::Clear ()
c801d85f 684{
19230604
RD
685 int i;
686 if (hash_table)
c801d85f 687 {
19230604
RD
688 for (i = 0; i < n; i++)
689 {
690 if (hash_table[i])
691 hash_table[i]->Clear ();
692 }
c801d85f 693 }
5692876f 694 m_count = 0;
c801d85f
KB
695}
696