]> git.saurik.com Git - wxWidgets.git/blame - include/wx/hash.h
compilation fix
[wxWidgets.git] / include / wx / hash.h
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
bcaa23de 2// Name: wx/hash.h
c801d85f
KB
3// Purpose: wxHashTable class
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)
bcaa23de 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
bcaa23de
VZ
12#ifndef _WX_HASH_H__
13#define _WX_HASH_H__
c801d85f 14
af49c4b8 15#if defined(__GNUG__) && !defined(__APPLE__)
c801d85f
KB
16#pragma interface "hash.h"
17#endif
18
df5168c4
MB
19#include "wx/defs.h"
20
21#if !wxUSE_STL
22 #include "wx/list.h"
23#endif
24#if WXWIN_COMPATIBILITY_2_4
25 #include "wx/dynarray.h"
26#endif
27
28class WXDLLIMPEXP_BASE wxObject;
c801d85f 29
bcaa23de
VZ
30// the default size of the hash
31#define wxHASH_SIZE_DEFAULT (1000)
32
c801d85f
KB
33/*
34 * A hash table is an array of user-definable size with lists
35 * of data items hanging off the array positions. Usually there'll
36 * be a hit, so no search is required; otherwise we'll have to run down
37 * the list to find the desired item.
38*/
39
bcaa23de
VZ
40// ----------------------------------------------------------------------------
41// this is the base class for object hashes: hash tables which contain
42// pointers to objects
43// ----------------------------------------------------------------------------
44
df5168c4
MB
45#if !wxUSE_STL
46
bddd7a8d 47class WXDLLIMPEXP_BASE wxHashTableBase : public wxObject
c801d85f 48{
bcaa23de
VZ
49public:
50 wxHashTableBase();
51
52 void Create(wxKeyType keyType = wxKEY_INTEGER,
53 size_t size = wxHASH_SIZE_DEFAULT);
54 void Destroy();
55
56 size_t GetSize() const { return m_hashSize; }
57 size_t GetCount() const { return m_count; }
58
59 void DeleteContents(bool flag);
60
61protected:
62 // find the node for (key, value)
63 wxNodeBase *GetNode(long key, long value) const;
64
65 // the array of lists in which we store the values for given key hash
66 wxListBase **m_hashTable;
67
68 // the size of m_lists array
69 size_t m_hashSize;
70
71 // the type of indexing we use
72 wxKeyType m_keyType;
73
74 // the total number of elements in the hash
75 size_t m_count;
76
77 // should we delete our data?
78 bool m_deleteContents;
79
80private:
81 // no copy ctor/assignment operator (yet)
1a0d517e 82 DECLARE_NO_COPY_CLASS(wxHashTableBase)
c801d85f
KB
83};
84
df5168c4
MB
85#else
86
87#include "wx/hashmap.h"
88
89#if !defined(wxENUM_KEY_TYPE_DEFINED)
90#define wxENUM_KEY_TYPE_DEFINED
91
92enum wxKeyType
93{
94 wxKEY_NONE,
95 wxKEY_INTEGER,
96 wxKEY_STRING
97};
98
99#endif
100
101union wxHashKeyValue
102{
103 long integer;
104 wxChar *string;
105};
106
107struct WXDLLIMPEXP_BASE wxHashTableHash
108{
109 wxHashTableHash() { }
110 wxHashTableHash( wxKeyType keyType ) : m_keyType( keyType ) { }
111
112 wxKeyType m_keyType;
113
114 unsigned long operator ()( const wxHashKeyValue& k ) const
115 {
116 if( m_keyType == wxKEY_STRING )
117 return wxStringHash::wxCharStringHash( k.string );
118 else
119 return (unsigned long)k.integer;
120 }
121};
122
123struct WXDLLIMPEXP_BASE wxHashTableEqual
124{
125 wxHashTableEqual() { }
126 wxHashTableEqual( wxKeyType keyType ) : m_keyType( keyType ) { }
127
128 wxKeyType m_keyType;
129
130 bool operator ()( const wxHashKeyValue& k1, const wxHashKeyValue& k2 ) const
131 {
132 if( m_keyType == wxKEY_STRING )
133 return wxStrcmp( k1.string, k2.string ) == 0;
134 else
135 return k1.integer == k2.integer;
136 }
137};
138
139WX_DECLARE_EXPORTED_HASH_MAP( wxHashKeyValue,
140 void*,
141 wxHashTableHash,
142 wxHashTableEqual,
143 wxHashTableBaseBase );
144
145class WXDLLIMPEXP_BASE wxHashTableBase
146{
147public:
148 wxHashTableBase( wxKeyType keyType = wxKEY_INTEGER,
149 size_t size = wxHASH_SIZE_DEFAULT )
150 : m_map( size, wxHashTableHash( keyType ),
151 wxHashTableEqual( keyType ) ),
152 m_keyType( keyType ) { }
153
d8274c92
MB
154 ~wxHashTableBase() { Clear(); }
155
156 size_t GetCount() const { return m_map.size(); }
157
158 void Clear()
df5168c4
MB
159 {
160 if( m_keyType == wxKEY_STRING )
161 {
162 for( wxHashTableBaseBase::iterator it = m_map.begin(),
163 en = m_map.end();
164 it != en; )
165 {
166 wxChar* tmp = it->first.string;
167 ++it;
168 delete[] tmp; // used in operator++
169 }
170 }
d8274c92 171 m_map.clear();
df5168c4 172 }
df5168c4
MB
173protected:
174 void DoPut( long key, void* data )
175 {
d8274c92
MB
176 wxASSERT( m_keyType == wxKEY_INTEGER );
177
df5168c4
MB
178 wxHashKeyValue k; k.integer = key;
179 m_map[k] = data;
180 }
181
182 void DoPut( const wxChar* key, void* data )
183 {
d8274c92
MB
184 wxASSERT( m_keyType == wxKEY_STRING );
185
df5168c4 186 wxHashKeyValue k;
d8274c92
MB
187 k.string = (wxChar*)key;
188 wxHashTableBaseBase::iterator it = m_map.find(k);
189
190 if( it == m_map.end() )
191 {
192 k.string = new wxChar[wxStrlen(key) + 1];
193 wxStrcpy(k.string, key);
194 m_map[k] = data;
195 }
196 else
197 it->second = data;
df5168c4
MB
198 }
199
200 void* DoGet( long key ) const
201 {
d8274c92
MB
202 wxASSERT( m_keyType == wxKEY_INTEGER );
203
df5168c4
MB
204 wxHashKeyValue k; k.integer = key;
205 wxHashTableBaseBase::const_iterator it = m_map.find( k );
206
207 return it != m_map.end() ? it->second : NULL;
208 }
209
210 void* DoGet( const wxChar* key ) const
211 {
d8274c92
MB
212 wxASSERT( m_keyType == wxKEY_STRING );
213
df5168c4
MB
214 wxHashKeyValue k; k.string = (wxChar*)key;
215 wxHashTableBaseBase::const_iterator it = m_map.find( k );
216
217 return it != m_map.end() ? it->second : NULL;
218 }
219
220 void* DoDelete( long key )
221 {
d8274c92
MB
222 wxASSERT( m_keyType == wxKEY_INTEGER );
223
df5168c4
MB
224 wxHashKeyValue k; k.integer = key;
225 wxHashTableBaseBase::iterator it = m_map.find( k );
226
227 if( it != m_map.end() )
228 {
229 void* data = it->second;
230
231 m_map.erase( it );
232 return data;
233 }
234
235 return NULL;
236 }
237
238 void* DoDelete( const wxChar* key )
239 {
d8274c92
MB
240 wxASSERT( m_keyType == wxKEY_STRING );
241
df5168c4
MB
242 wxHashKeyValue k; k.string = (wxChar*)key;
243 wxHashTableBaseBase::iterator it = m_map.find( k );
244
245 if( it != m_map.end() )
246 {
247 void* data = it->second;
248 wxChar* k = it->first.string;
249
250 m_map.erase( it );
251 delete[] k;
252 return data;
253 }
254
255 return NULL;
256 }
257
258 wxHashTableBaseBase m_map;
259 wxKeyType m_keyType;
260};
261
262#endif // !wxUSE_STL
263
264#if !wxUSE_STL
265
ba8c1601
MB
266#if WXWIN_COMPATIBILITY_2_4
267
c2bb85e9
VZ
268// ----------------------------------------------------------------------------
269// a hash table which stores longs
270// ----------------------------------------------------------------------------
271
bddd7a8d 272class WXDLLIMPEXP_BASE wxHashTableLong : public wxObject
c2bb85e9
VZ
273{
274public:
d84afea9
GD
275 wxHashTableLong(size_t size = wxHASH_SIZE_DEFAULT)
276 { Init(size); }
a95e38c0 277 virtual ~wxHashTableLong();
c2bb85e9
VZ
278
279 void Create(size_t size = wxHASH_SIZE_DEFAULT);
280 void Destroy();
281
282 size_t GetSize() const { return m_hashSize; }
283 size_t GetCount() const { return m_count; }
284
285 void Put(long key, long value);
286 long Get(long key) const;
287 long Delete(long key);
288
289protected:
290 void Init(size_t size);
291
292private:
293 wxArrayLong **m_values,
294 **m_keys;
295
296 // the size of array above
297 size_t m_hashSize;
298
299 // the total number of elements in the hash
300 size_t m_count;
301
302 // not implemented yet
1a0d517e 303 DECLARE_NO_COPY_CLASS(wxHashTableLong)
c2bb85e9
VZ
304};
305
bd83cb56
VZ
306// ----------------------------------------------------------------------------
307// wxStringHashTable: a hash table which indexes strings with longs
308// ----------------------------------------------------------------------------
309
bddd7a8d 310class WXDLLIMPEXP_BASE wxStringHashTable : public wxObject
bd83cb56
VZ
311{
312public:
313 wxStringHashTable(size_t sizeTable = wxHASH_SIZE_DEFAULT);
314 virtual ~wxStringHashTable();
315
316 // add a string associated with this key to the table
317 void Put(long key, const wxString& value);
318
319 // get the string from the key: if not found, an empty string is returned
320 // and the wasFound is set to FALSE if not NULL
321 wxString Get(long key, bool *wasFound = NULL) const;
322
53e112a0
JS
323 // remove the item, returning TRUE if the item was found and deleted
324 bool Delete(long key) const;
325
bd83cb56
VZ
326 // clean up
327 void Destroy();
328
329private:
330 wxArrayLong **m_keys;
331 wxArrayString **m_values;
332
333 // the size of array above
334 size_t m_hashSize;
335
1a0d517e 336 DECLARE_NO_COPY_CLASS(wxStringHashTable)
bd83cb56
VZ
337};
338
df5168c4
MB
339#endif // WXWIN_COMPATIBILITY_2_4
340
341#endif // !wxUSE_STL
ba8c1601 342
bcaa23de
VZ
343// ----------------------------------------------------------------------------
344// for compatibility only
345// ----------------------------------------------------------------------------
346
df5168c4
MB
347#if wxUSE_STL
348
349class WXDLLIMPEXP_BASE wxHashTable : protected wxHashTableBase
350{
351 typedef wxHashTableBaseBase hash;
352public:
353 class dummy;
354
355 struct compatibility_iterator
356 {
357 hash::iterator m_iter;
358 hash* m_hash;
359
360 operator bool() const { return m_iter != m_hash->end(); }
361 bool operator !() const { return m_iter == m_hash->end(); }
362 compatibility_iterator( hash* li, hash::iterator it )
363 : m_iter( it ), m_hash( li ) {}
364 compatibility_iterator() { }
365
366 dummy* operator->() { return (dummy*)this; }
367 };
29a2a24d 368 typedef compatibility_iterator citer;
df5168c4
MB
369
370 class dummy
371 {
372 typedef hash::iterator it;
373 typedef compatibility_iterator citer;
374 public:
375 wxObject* GetData() const
376 {
377 citer* i = (citer*)this;
378 return (wxObject*)i->m_iter->second;
379 }
380 citer GetNext() const
381 {
382 citer* i = (citer*)this;
383 it lit = i->m_iter;
384 return citer( i->m_hash, ++lit );
385 }
df5168c4
MB
386 void SetData( wxObject* e )
387 {
388 citer* i = (citer*)this;
389 i->m_iter->second = e;
390 }
391 private:
392 dummy();
393 };
394public:
395 wxHashTable( wxKeyType keyType = wxKEY_INTEGER,
396 size_t size = wxHASH_SIZE_DEFAULT )
397 : wxHashTableBase( keyType, size ) { }
398
399 void Destroy() { Clear(); }
400
401 // key and value are the same
402 void Put(long value, wxObject *object) { DoPut( value, object ); }
403 void Put(const wxChar *value, wxObject *object) { DoPut( value, object ); }
404
405 // key and value are the same
406 wxObject *Get(long value) const { return (wxObject*)DoGet( value ); }
407 wxObject *Get(const wxChar *value) const { return (wxObject*)DoGet( value ); }
408
409 // Deletes entry and returns data if found
410 wxObject *Delete(long key) { return (wxObject*)DoGet( key ); }
411 wxObject *Delete(const wxChar *key) { return (wxObject*)DoGet( key ); }
412
413#if 0
414 // Construct your own integer key from a string, e.g. in case
415 // you need to combine it with something
416 long MakeKey(const wxChar *string) const;
417#endif
418 // Way of iterating through whole hash table (e.g. to delete everything)
419 // Not necessary, of course, if you're only storing pointers to
420 // objects maintained separately
421 void BeginFind() { m_iter = citer( &this->m_map, this->m_map.begin() ); }
422 compatibility_iterator Next()
423 {
424 compatibility_iterator it = m_iter;
425 if( m_iter )
426 m_iter = m_iter->GetNext();
427 return it;
428 }
429
d8274c92 430 void Clear() { wxHashTableBase::Clear(); }
df5168c4
MB
431private:
432 compatibility_iterator m_iter;
433};
434
435#else // if !wxUSE_STL
436
bddd7a8d 437class WXDLLIMPEXP_BASE wxHashTable : public wxObject
bcaa23de
VZ
438{
439public:
440 int n;
441 int current_position;
442 wxNode *current_node;
443
444 unsigned int key_type;
445 wxList **hash_table;
446
447 wxHashTable(int the_key_type = wxKEY_INTEGER,
448 int size = wxHASH_SIZE_DEFAULT);
449 ~wxHashTable();
450
451 // copy ctor and assignment operator
d84afea9
GD
452 wxHashTable(const wxHashTable& table) : wxObject()
453 { DoCopy(table); }
bcaa23de
VZ
454 wxHashTable& operator=(const wxHashTable& table)
455 { Clear(); DoCopy(table); return *this; }
456
457 void DoCopy(const wxHashTable& table);
458
459 void Destroy();
460
461 bool Create(int the_key_type = wxKEY_INTEGER,
462 int size = wxHASH_SIZE_DEFAULT);
463
464 // Note that there are 2 forms of Put, Get.
465 // With a key and a value, the *value* will be checked
466 // when a collision is detected. Otherwise, if there are
467 // 2 items with a different value but the same key,
468 // we'll retrieve the WRONG ONE. So where possible,
469 // supply the required value along with the key.
470 // In fact, the value-only versions make a key, and still store
471 // the value. The use of an explicit key might be required
472 // e.g. when combining several values into one key.
473 // When doing that, it's highly likely we'll get a collision,
474 // e.g. 1 + 2 = 3, 2 + 1 = 3.
475
476 // key and value are NOT necessarily the same
477 void Put(long key, long value, wxObject *object);
478 void Put(long key, const wxChar *value, wxObject *object);
479
480 // key and value are the same
481 void Put(long value, wxObject *object);
482 void Put(const wxChar *value, wxObject *object);
483
484 // key and value not the same
485 wxObject *Get(long key, long value) const;
486 wxObject *Get(long key, const wxChar *value) const;
487
488 // key and value are the same
489 wxObject *Get(long value) const;
490 wxObject *Get(const wxChar *value) const;
491
492 // Deletes entry and returns data if found
493 wxObject *Delete(long key);
494 wxObject *Delete(const wxChar *key);
495
496 wxObject *Delete(long key, int value);
497 wxObject *Delete(long key, const wxChar *value);
498
499 // Construct your own integer key from a string, e.g. in case
500 // you need to combine it with something
501 long MakeKey(const wxChar *string) const;
502
503 // Way of iterating through whole hash table (e.g. to delete everything)
504 // Not necessary, of course, if you're only storing pointers to
505 // objects maintained separately
506
507 void BeginFind();
508 wxNode *Next();
509
510 void DeleteContents(bool flag);
511 void Clear();
512
513 // Returns number of nodes
514 size_t GetCount() const { return m_count; }
515
df5168c4 516 typedef wxNode* compatibility_iterator;
bcaa23de
VZ
517private:
518 size_t m_count; // number of elements in the hashtable
519 bool m_deleteContents;
520
521 DECLARE_DYNAMIC_CLASS(wxHashTable)
522};
523
df5168c4
MB
524#endif
525
526#if wxUSE_STL
527
bcaa23de
VZ
528// defines a new type safe hash table which stores the elements of type eltype
529// in lists of class listclass
df5168c4
MB
530#define _WX_DECLARE_HASH(eltype, dummy, hashclass, classexp) \
531 classexp hashclass : public wxHashTableBase \
532 { \
533 public: \
534 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
535 size_t size = wxHASH_SIZE_DEFAULT) \
536 : wxHashTableBase(keyType, size) { } \
537 \
538 ~hashclass() { Destroy(); } \
539 \
540 void Destroy() { m_map.clear(); } \
541 void Put(long key, eltype *data) { DoPut(key, (void*)data); } \
542 eltype *Get(long key) const { return (eltype*)DoGet(key); } \
543 eltype *Delete(long key) { return (eltype*)DoDelete(key); } \
544 }
545
546#else // if !wxUSE_STL
547
f6bcfd97
BP
548#define _WX_DECLARE_HASH(eltype, listclass, hashclass, classexp) \
549 classexp hashclass : public wxHashTableBase \
bcaa23de
VZ
550 { \
551 public: \
552 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
553 size_t size = wxHASH_SIZE_DEFAULT) \
554 { Create(keyType, size); } \
555 \
556 ~hashclass() { Destroy(); } \
557 \
558 void Put(long key, long val, eltype *data) { DoPut(key, val, data); } \
559 void Put(long key, eltype *data) { DoPut(key, key, data); } \
560 \
561 eltype *Get(long key, long value) const \
562 { \
563 wxNodeBase *node = GetNode(key, value); \
564 return node ? ((listclass::Node *)node)->GetData() : (eltype *)0; \
565 } \
566 eltype *Get(long key) const { return Get(key, key); } \
567 \
568 eltype *Delete(long key, long value) \
569 { \
570 eltype *data; \
571 \
572 wxNodeBase *node = GetNode(key, value); \
573 if ( node ) \
574 { \
575 data = ((listclass::Node *)node)->GetData(); \
576 \
577 delete node; \
578 m_count--; \
579 } \
580 else \
581 { \
582 data = (eltype *)0; \
583 } \
584 \
585 return data; \
586 } \
587 eltype *Delete(long key) { return Delete(key, key); } \
588 \
589 protected: \
590 void DoPut(long key, long value, eltype *data) \
591 { \
6a5391af 592 size_t slot = (size_t)abs((int)(key % (long)m_hashSize)); \
bcaa23de
VZ
593 \
594 if ( !m_hashTable[slot] ) \
595 { \
596 m_hashTable[slot] = new listclass(m_keyType); \
597 if ( m_deleteContents ) \
598 m_hashTable[slot]->DeleteContents(TRUE); \
599 } \
600 \
601 ((listclass *)m_hashTable[slot])->Append(value, data); \
602 m_count++; \
603 } \
fc7a2a60
VZ
604 \
605 DECLARE_NO_COPY_CLASS(hashclass) \
bcaa23de
VZ
606 }
607
df5168c4
MB
608#endif
609
f6bcfd97
BP
610// this macro is to be used in the user code
611#define WX_DECLARE_HASH(el, list, hash) \
612 _WX_DECLARE_HASH(el, list, hash, class)
613
614// and this one does exactly the same thing but should be used inside the
615// library
616#define WX_DECLARE_EXPORTED_HASH(el, list, hash) \
617 _WX_DECLARE_HASH(el, list, hash, class WXDLLEXPORT)
618
0b9ab0bd
RL
619#define WX_DECLARE_USER_EXPORTED_HASH(el, list, hash, usergoo) \
620 _WX_DECLARE_HASH(el, list, hash, class usergoo)
621
ba8c1601
MB
622// delete all hash elements
623//
624// NB: the class declaration of the hash elements must be visible from the
625// place where you use this macro, otherwise the proper destructor may not
626// be called (a decent compiler should give a warning about it, but don't
627// count on it)!
df5168c4 628#define WX_CLEAR_HASH_TABLE(hash) \
ba8c1601 629 { \
df5168c4
MB
630 (hash).BeginFind(); \
631 wxHashTable::compatibility_iterator it = (hash).Next(); \
ba8c1601
MB
632 while( it ) \
633 { \
634 delete it->GetData(); \
df5168c4 635 it = (hash).Next(); \
ba8c1601 636 } \
df5168c4 637 (hash).Clear(); \
ba8c1601
MB
638 }
639
c801d85f 640#endif
bcaa23de 641 // _WX_HASH_H__