]> git.saurik.com Git - wxWidgets.git/blame - include/wx/hash.h
Refactored wxSizerXmlHandler and added support for wxGridBagSizer
[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
12028905 15#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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,
760d3c7c
MB
143 wxHashTableBaseBaseBase );
144
145// hack: we should really have HASH_MULTI(MAP|SET), but this requires
146// less work
147
148class WXDLLIMPEXP_BASE wxHashTableBaseBase : public wxHashTableBaseBaseBase
149{
150public:
151 wxHashTableBaseBase(size_t size, const wxHashTableHash& hash,
152 const wxHashTableEqual& equal)
153 : wxHashTableBaseBaseBase(size, hash, equal)
154 { }
155
156 void multi_insert(const wxHashKeyValue& key, void* value)
157 {
158 CreateNodeLast(value_type(key, value));
159 }
160};
df5168c4
MB
161
162class WXDLLIMPEXP_BASE wxHashTableBase
163{
164public:
165 wxHashTableBase( wxKeyType keyType = wxKEY_INTEGER,
166 size_t size = wxHASH_SIZE_DEFAULT )
167 : m_map( size, wxHashTableHash( keyType ),
168 wxHashTableEqual( keyType ) ),
169 m_keyType( keyType ) { }
170
d8274c92
MB
171 ~wxHashTableBase() { Clear(); }
172
173 size_t GetCount() const { return m_map.size(); }
174
175 void Clear()
df5168c4
MB
176 {
177 if( m_keyType == wxKEY_STRING )
178 {
179 for( wxHashTableBaseBase::iterator it = m_map.begin(),
180 en = m_map.end();
181 it != en; )
182 {
183 wxChar* tmp = it->first.string;
184 ++it;
185 delete[] tmp; // used in operator++
186 }
187 }
d8274c92 188 m_map.clear();
df5168c4 189 }
df5168c4
MB
190protected:
191 void DoPut( long key, void* data )
192 {
d8274c92
MB
193 wxASSERT( m_keyType == wxKEY_INTEGER );
194
df5168c4 195 wxHashKeyValue k; k.integer = key;
760d3c7c 196 m_map.multi_insert(k, data);
df5168c4
MB
197 }
198
199 void DoPut( const wxChar* key, void* data )
200 {
d8274c92
MB
201 wxASSERT( m_keyType == wxKEY_STRING );
202
df5168c4 203 wxHashKeyValue k;
760d3c7c
MB
204 k.string = wxStrcpy(new wxChar[wxStrlen(key) + 1], key);
205 m_map.multi_insert(k, data);
df5168c4
MB
206 }
207
208 void* DoGet( long key ) const
209 {
d8274c92
MB
210 wxASSERT( m_keyType == wxKEY_INTEGER );
211
df5168c4
MB
212 wxHashKeyValue k; k.integer = key;
213 wxHashTableBaseBase::const_iterator it = m_map.find( k );
214
215 return it != m_map.end() ? it->second : NULL;
216 }
217
218 void* DoGet( const wxChar* key ) const
219 {
d8274c92
MB
220 wxASSERT( m_keyType == wxKEY_STRING );
221
df5168c4
MB
222 wxHashKeyValue k; k.string = (wxChar*)key;
223 wxHashTableBaseBase::const_iterator it = m_map.find( k );
224
225 return it != m_map.end() ? it->second : NULL;
226 }
227
228 void* DoDelete( long key )
229 {
d8274c92
MB
230 wxASSERT( m_keyType == wxKEY_INTEGER );
231
df5168c4
MB
232 wxHashKeyValue k; k.integer = key;
233 wxHashTableBaseBase::iterator it = m_map.find( k );
234
235 if( it != m_map.end() )
236 {
237 void* data = it->second;
238
239 m_map.erase( it );
240 return data;
241 }
242
243 return NULL;
244 }
245
246 void* DoDelete( const wxChar* key )
247 {
d8274c92
MB
248 wxASSERT( m_keyType == wxKEY_STRING );
249
df5168c4
MB
250 wxHashKeyValue k; k.string = (wxChar*)key;
251 wxHashTableBaseBase::iterator it = m_map.find( k );
252
253 if( it != m_map.end() )
254 {
255 void* data = it->second;
256 wxChar* k = it->first.string;
257
258 m_map.erase( it );
259 delete[] k;
260 return data;
261 }
262
263 return NULL;
264 }
265
266 wxHashTableBaseBase m_map;
267 wxKeyType m_keyType;
268};
269
270#endif // !wxUSE_STL
271
272#if !wxUSE_STL
273
ba8c1601
MB
274#if WXWIN_COMPATIBILITY_2_4
275
c2bb85e9
VZ
276// ----------------------------------------------------------------------------
277// a hash table which stores longs
278// ----------------------------------------------------------------------------
279
bddd7a8d 280class WXDLLIMPEXP_BASE wxHashTableLong : public wxObject
c2bb85e9
VZ
281{
282public:
d84afea9
GD
283 wxHashTableLong(size_t size = wxHASH_SIZE_DEFAULT)
284 { Init(size); }
a95e38c0 285 virtual ~wxHashTableLong();
c2bb85e9
VZ
286
287 void Create(size_t size = wxHASH_SIZE_DEFAULT);
288 void Destroy();
289
290 size_t GetSize() const { return m_hashSize; }
291 size_t GetCount() const { return m_count; }
292
293 void Put(long key, long value);
294 long Get(long key) const;
295 long Delete(long key);
296
297protected:
298 void Init(size_t size);
299
300private:
301 wxArrayLong **m_values,
302 **m_keys;
303
304 // the size of array above
305 size_t m_hashSize;
306
307 // the total number of elements in the hash
308 size_t m_count;
309
310 // not implemented yet
1a0d517e 311 DECLARE_NO_COPY_CLASS(wxHashTableLong)
c2bb85e9
VZ
312};
313
bd83cb56
VZ
314// ----------------------------------------------------------------------------
315// wxStringHashTable: a hash table which indexes strings with longs
316// ----------------------------------------------------------------------------
317
bddd7a8d 318class WXDLLIMPEXP_BASE wxStringHashTable : public wxObject
bd83cb56
VZ
319{
320public:
321 wxStringHashTable(size_t sizeTable = wxHASH_SIZE_DEFAULT);
322 virtual ~wxStringHashTable();
323
324 // add a string associated with this key to the table
325 void Put(long key, const wxString& value);
326
327 // get the string from the key: if not found, an empty string is returned
328 // and the wasFound is set to FALSE if not NULL
329 wxString Get(long key, bool *wasFound = NULL) const;
330
53e112a0
JS
331 // remove the item, returning TRUE if the item was found and deleted
332 bool Delete(long key) const;
333
bd83cb56
VZ
334 // clean up
335 void Destroy();
336
337private:
338 wxArrayLong **m_keys;
339 wxArrayString **m_values;
340
341 // the size of array above
342 size_t m_hashSize;
343
1a0d517e 344 DECLARE_NO_COPY_CLASS(wxStringHashTable)
bd83cb56
VZ
345};
346
df5168c4
MB
347#endif // WXWIN_COMPATIBILITY_2_4
348
349#endif // !wxUSE_STL
ba8c1601 350
bcaa23de
VZ
351// ----------------------------------------------------------------------------
352// for compatibility only
353// ----------------------------------------------------------------------------
354
df5168c4
MB
355#if wxUSE_STL
356
357class WXDLLIMPEXP_BASE wxHashTable : protected wxHashTableBase
358{
359 typedef wxHashTableBaseBase hash;
360public:
361 class dummy;
362
363 struct compatibility_iterator
364 {
365 hash::iterator m_iter;
366 hash* m_hash;
367
368 operator bool() const { return m_iter != m_hash->end(); }
369 bool operator !() const { return m_iter == m_hash->end(); }
370 compatibility_iterator( hash* li, hash::iterator it )
371 : m_iter( it ), m_hash( li ) {}
372 compatibility_iterator() { }
373
374 dummy* operator->() { return (dummy*)this; }
375 };
29a2a24d 376 typedef compatibility_iterator citer;
df5168c4
MB
377
378 class dummy
379 {
380 typedef hash::iterator it;
381 typedef compatibility_iterator citer;
382 public:
383 wxObject* GetData() const
384 {
385 citer* i = (citer*)this;
386 return (wxObject*)i->m_iter->second;
387 }
388 citer GetNext() const
389 {
390 citer* i = (citer*)this;
391 it lit = i->m_iter;
392 return citer( i->m_hash, ++lit );
393 }
df5168c4
MB
394 void SetData( wxObject* e )
395 {
396 citer* i = (citer*)this;
397 i->m_iter->second = e;
398 }
399 private:
400 dummy();
401 };
402public:
403 wxHashTable( wxKeyType keyType = wxKEY_INTEGER,
404 size_t size = wxHASH_SIZE_DEFAULT )
405 : wxHashTableBase( keyType, size ) { }
406
407 void Destroy() { Clear(); }
408
409 // key and value are the same
410 void Put(long value, wxObject *object) { DoPut( value, object ); }
411 void Put(const wxChar *value, wxObject *object) { DoPut( value, object ); }
412
413 // key and value are the same
414 wxObject *Get(long value) const { return (wxObject*)DoGet( value ); }
415 wxObject *Get(const wxChar *value) const { return (wxObject*)DoGet( value ); }
416
417 // Deletes entry and returns data if found
760d3c7c
MB
418 wxObject *Delete(long key) { return (wxObject*)DoDelete( key ); }
419 wxObject *Delete(const wxChar *key) { return (wxObject*)DoDelete( key ); }
df5168c4
MB
420
421#if 0
422 // Construct your own integer key from a string, e.g. in case
423 // you need to combine it with something
424 long MakeKey(const wxChar *string) const;
425#endif
426 // Way of iterating through whole hash table (e.g. to delete everything)
427 // Not necessary, of course, if you're only storing pointers to
428 // objects maintained separately
429 void BeginFind() { m_iter = citer( &this->m_map, this->m_map.begin() ); }
430 compatibility_iterator Next()
431 {
432 compatibility_iterator it = m_iter;
433 if( m_iter )
434 m_iter = m_iter->GetNext();
435 return it;
436 }
437
d8274c92 438 void Clear() { wxHashTableBase::Clear(); }
72eef316
MB
439
440 size_t GetCount() const { return wxHashTableBase::GetCount(); }
df5168c4
MB
441private:
442 compatibility_iterator m_iter;
443};
444
445#else // if !wxUSE_STL
446
bddd7a8d 447class WXDLLIMPEXP_BASE wxHashTable : public wxObject
bcaa23de
VZ
448{
449public:
7057f62a
MB
450 typedef wxNode Node;
451 typedef wxNode* compatibility_iterator;
452
bcaa23de
VZ
453 int n;
454 int current_position;
455 wxNode *current_node;
456
457 unsigned int key_type;
458 wxList **hash_table;
459
460 wxHashTable(int the_key_type = wxKEY_INTEGER,
461 int size = wxHASH_SIZE_DEFAULT);
462 ~wxHashTable();
463
464 // copy ctor and assignment operator
d84afea9
GD
465 wxHashTable(const wxHashTable& table) : wxObject()
466 { DoCopy(table); }
bcaa23de
VZ
467 wxHashTable& operator=(const wxHashTable& table)
468 { Clear(); DoCopy(table); return *this; }
469
470 void DoCopy(const wxHashTable& table);
471
472 void Destroy();
473
474 bool Create(int the_key_type = wxKEY_INTEGER,
475 int size = wxHASH_SIZE_DEFAULT);
476
477 // Note that there are 2 forms of Put, Get.
478 // With a key and a value, the *value* will be checked
479 // when a collision is detected. Otherwise, if there are
480 // 2 items with a different value but the same key,
481 // we'll retrieve the WRONG ONE. So where possible,
482 // supply the required value along with the key.
483 // In fact, the value-only versions make a key, and still store
484 // the value. The use of an explicit key might be required
485 // e.g. when combining several values into one key.
486 // When doing that, it's highly likely we'll get a collision,
487 // e.g. 1 + 2 = 3, 2 + 1 = 3.
488
489 // key and value are NOT necessarily the same
490 void Put(long key, long value, wxObject *object);
491 void Put(long key, const wxChar *value, wxObject *object);
492
493 // key and value are the same
494 void Put(long value, wxObject *object);
495 void Put(const wxChar *value, wxObject *object);
496
497 // key and value not the same
498 wxObject *Get(long key, long value) const;
499 wxObject *Get(long key, const wxChar *value) const;
500
501 // key and value are the same
502 wxObject *Get(long value) const;
503 wxObject *Get(const wxChar *value) const;
504
505 // Deletes entry and returns data if found
506 wxObject *Delete(long key);
507 wxObject *Delete(const wxChar *key);
508
509 wxObject *Delete(long key, int value);
510 wxObject *Delete(long key, const wxChar *value);
511
512 // Construct your own integer key from a string, e.g. in case
513 // you need to combine it with something
514 long MakeKey(const wxChar *string) const;
515
516 // Way of iterating through whole hash table (e.g. to delete everything)
517 // Not necessary, of course, if you're only storing pointers to
518 // objects maintained separately
519
520 void BeginFind();
521 wxNode *Next();
522
523 void DeleteContents(bool flag);
524 void Clear();
525
526 // Returns number of nodes
527 size_t GetCount() const { return m_count; }
528
529private:
530 size_t m_count; // number of elements in the hashtable
531 bool m_deleteContents;
532
533 DECLARE_DYNAMIC_CLASS(wxHashTable)
534};
535
df5168c4
MB
536#endif
537
538#if wxUSE_STL
539
bcaa23de
VZ
540// defines a new type safe hash table which stores the elements of type eltype
541// in lists of class listclass
df5168c4
MB
542#define _WX_DECLARE_HASH(eltype, dummy, hashclass, classexp) \
543 classexp hashclass : public wxHashTableBase \
544 { \
545 public: \
546 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
547 size_t size = wxHASH_SIZE_DEFAULT) \
548 : wxHashTableBase(keyType, size) { } \
549 \
550 ~hashclass() { Destroy(); } \
551 \
552 void Destroy() { m_map.clear(); } \
553 void Put(long key, eltype *data) { DoPut(key, (void*)data); } \
554 eltype *Get(long key) const { return (eltype*)DoGet(key); } \
555 eltype *Delete(long key) { return (eltype*)DoDelete(key); } \
556 }
557
558#else // if !wxUSE_STL
559
f6bcfd97
BP
560#define _WX_DECLARE_HASH(eltype, listclass, hashclass, classexp) \
561 classexp hashclass : public wxHashTableBase \
bcaa23de
VZ
562 { \
563 public: \
564 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
565 size_t size = wxHASH_SIZE_DEFAULT) \
566 { Create(keyType, size); } \
567 \
568 ~hashclass() { Destroy(); } \
569 \
570 void Put(long key, long val, eltype *data) { DoPut(key, val, data); } \
571 void Put(long key, eltype *data) { DoPut(key, key, data); } \
572 \
573 eltype *Get(long key, long value) const \
574 { \
575 wxNodeBase *node = GetNode(key, value); \
576 return node ? ((listclass::Node *)node)->GetData() : (eltype *)0; \
577 } \
578 eltype *Get(long key) const { return Get(key, key); } \
579 \
580 eltype *Delete(long key, long value) \
581 { \
582 eltype *data; \
583 \
584 wxNodeBase *node = GetNode(key, value); \
585 if ( node ) \
586 { \
587 data = ((listclass::Node *)node)->GetData(); \
588 \
589 delete node; \
590 m_count--; \
591 } \
592 else \
593 { \
594 data = (eltype *)0; \
595 } \
596 \
597 return data; \
598 } \
599 eltype *Delete(long key) { return Delete(key, key); } \
600 \
601 protected: \
602 void DoPut(long key, long value, eltype *data) \
603 { \
6a5391af 604 size_t slot = (size_t)abs((int)(key % (long)m_hashSize)); \
bcaa23de
VZ
605 \
606 if ( !m_hashTable[slot] ) \
607 { \
608 m_hashTable[slot] = new listclass(m_keyType); \
609 if ( m_deleteContents ) \
610 m_hashTable[slot]->DeleteContents(TRUE); \
611 } \
612 \
613 ((listclass *)m_hashTable[slot])->Append(value, data); \
614 m_count++; \
615 } \
fc7a2a60
VZ
616 \
617 DECLARE_NO_COPY_CLASS(hashclass) \
bcaa23de
VZ
618 }
619
df5168c4
MB
620#endif
621
f6bcfd97
BP
622// this macro is to be used in the user code
623#define WX_DECLARE_HASH(el, list, hash) \
624 _WX_DECLARE_HASH(el, list, hash, class)
625
626// and this one does exactly the same thing but should be used inside the
627// library
628#define WX_DECLARE_EXPORTED_HASH(el, list, hash) \
629 _WX_DECLARE_HASH(el, list, hash, class WXDLLEXPORT)
630
0b9ab0bd
RL
631#define WX_DECLARE_USER_EXPORTED_HASH(el, list, hash, usergoo) \
632 _WX_DECLARE_HASH(el, list, hash, class usergoo)
633
ba8c1601
MB
634// delete all hash elements
635//
636// NB: the class declaration of the hash elements must be visible from the
637// place where you use this macro, otherwise the proper destructor may not
638// be called (a decent compiler should give a warning about it, but don't
639// count on it)!
df5168c4 640#define WX_CLEAR_HASH_TABLE(hash) \
ba8c1601 641 { \
df5168c4
MB
642 (hash).BeginFind(); \
643 wxHashTable::compatibility_iterator it = (hash).Next(); \
ba8c1601
MB
644 while( it ) \
645 { \
646 delete it->GetData(); \
df5168c4 647 it = (hash).Next(); \
ba8c1601 648 } \
df5168c4 649 (hash).Clear(); \
ba8c1601
MB
650 }
651
c801d85f 652#endif
bcaa23de 653 // _WX_HASH_H__