1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHashTable class
4 // Author: Julian Smart
5 // Modified by: VZ at 25.02.00: type safe hashes with WX_DECLARE_HASH()
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 #if defined(__GNUG__) && !defined(__APPLE__)
16 #pragma interface "hash.h"
24 #if WXWIN_COMPATIBILITY_2_4
25 #include "wx/dynarray.h"
28 class WXDLLIMPEXP_BASE wxObject
;
30 // the default size of the hash
31 #define wxHASH_SIZE_DEFAULT (1000)
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.
40 // ----------------------------------------------------------------------------
41 // this is the base class for object hashes: hash tables which contain
42 // pointers to objects
43 // ----------------------------------------------------------------------------
47 class WXDLLIMPEXP_BASE wxHashTableBase
: public wxObject
52 void Create(wxKeyType keyType
= wxKEY_INTEGER
,
53 size_t size
= wxHASH_SIZE_DEFAULT
);
56 size_t GetSize() const { return m_hashSize
; }
57 size_t GetCount() const { return m_count
; }
59 void DeleteContents(bool flag
);
62 // find the node for (key, value)
63 wxNodeBase
*GetNode(long key
, long value
) const;
65 // the array of lists in which we store the values for given key hash
66 wxListBase
**m_hashTable
;
68 // the size of m_lists array
71 // the type of indexing we use
74 // the total number of elements in the hash
77 // should we delete our data?
78 bool m_deleteContents
;
81 // no copy ctor/assignment operator (yet)
82 DECLARE_NO_COPY_CLASS(wxHashTableBase
)
87 #include "wx/hashmap.h"
89 #if !defined(wxENUM_KEY_TYPE_DEFINED)
90 #define wxENUM_KEY_TYPE_DEFINED
107 struct WXDLLIMPEXP_BASE wxHashTableHash
109 wxHashTableHash() { }
110 wxHashTableHash( wxKeyType keyType
) : m_keyType( keyType
) { }
114 unsigned long operator ()( const wxHashKeyValue
& k
) const
116 if( m_keyType
== wxKEY_STRING
)
117 return wxStringHash::wxCharStringHash( k
.string
);
119 return (unsigned long)k
.integer
;
123 struct WXDLLIMPEXP_BASE wxHashTableEqual
125 wxHashTableEqual() { }
126 wxHashTableEqual( wxKeyType keyType
) : m_keyType( keyType
) { }
130 bool operator ()( const wxHashKeyValue
& k1
, const wxHashKeyValue
& k2
) const
132 if( m_keyType
== wxKEY_STRING
)
133 return wxStrcmp( k1
.string
, k2
.string
) == 0;
135 return k1
.integer
== k2
.integer
;
139 WX_DECLARE_EXPORTED_HASH_MAP( wxHashKeyValue
,
143 wxHashTableBaseBase
);
145 class WXDLLIMPEXP_BASE wxHashTableBase
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
) { }
156 if( m_keyType
== wxKEY_STRING
)
158 for( wxHashTableBaseBase::iterator it
= m_map
.begin(),
162 wxChar
* tmp
= it
->first
.string
;
164 delete[] tmp
; // used in operator++
169 size_t GetCount() const { return m_map
.size(); }
171 void DoPut( long key
, void* data
)
173 wxHashKeyValue k
; k
.integer
= key
;
177 void DoPut( const wxChar
* key
, void* data
)
180 k
.string
= new wxChar
[wxStrlen(key
) + 1];
181 wxStrcpy(k
.string
, key
);
185 void* DoGet( long key
) const
187 wxHashKeyValue k
; k
.integer
= key
;
188 wxHashTableBaseBase::const_iterator it
= m_map
.find( k
);
190 return it
!= m_map
.end() ? it
->second
: NULL
;
193 void* DoGet( const wxChar
* key
) const
195 wxHashKeyValue k
; k
.string
= (wxChar
*)key
;
196 wxHashTableBaseBase::const_iterator it
= m_map
.find( k
);
198 return it
!= m_map
.end() ? it
->second
: NULL
;
201 void* DoDelete( long key
)
203 wxHashKeyValue k
; k
.integer
= key
;
204 wxHashTableBaseBase::iterator it
= m_map
.find( k
);
206 if( it
!= m_map
.end() )
208 void* data
= it
->second
;
217 void* DoDelete( const wxChar
* key
)
219 wxHashKeyValue k
; k
.string
= (wxChar
*)key
;
220 wxHashTableBaseBase::iterator it
= m_map
.find( k
);
222 if( it
!= m_map
.end() )
224 void* data
= it
->second
;
225 wxChar
* k
= it
->first
.string
;
235 wxHashTableBaseBase m_map
;
243 #if WXWIN_COMPATIBILITY_2_4
245 // ----------------------------------------------------------------------------
246 // a hash table which stores longs
247 // ----------------------------------------------------------------------------
249 class WXDLLIMPEXP_BASE wxHashTableLong
: public wxObject
252 wxHashTableLong(size_t size
= wxHASH_SIZE_DEFAULT
)
254 virtual ~wxHashTableLong();
256 void Create(size_t size
= wxHASH_SIZE_DEFAULT
);
259 size_t GetSize() const { return m_hashSize
; }
260 size_t GetCount() const { return m_count
; }
262 void Put(long key
, long value
);
263 long Get(long key
) const;
264 long Delete(long key
);
267 void Init(size_t size
);
270 wxArrayLong
**m_values
,
273 // the size of array above
276 // the total number of elements in the hash
279 // not implemented yet
280 DECLARE_NO_COPY_CLASS(wxHashTableLong
)
283 // ----------------------------------------------------------------------------
284 // wxStringHashTable: a hash table which indexes strings with longs
285 // ----------------------------------------------------------------------------
287 class WXDLLIMPEXP_BASE wxStringHashTable
: public wxObject
290 wxStringHashTable(size_t sizeTable
= wxHASH_SIZE_DEFAULT
);
291 virtual ~wxStringHashTable();
293 // add a string associated with this key to the table
294 void Put(long key
, const wxString
& value
);
296 // get the string from the key: if not found, an empty string is returned
297 // and the wasFound is set to FALSE if not NULL
298 wxString
Get(long key
, bool *wasFound
= NULL
) const;
300 // remove the item, returning TRUE if the item was found and deleted
301 bool Delete(long key
) const;
307 wxArrayLong
**m_keys
;
308 wxArrayString
**m_values
;
310 // the size of array above
313 DECLARE_NO_COPY_CLASS(wxStringHashTable
)
316 #endif // WXWIN_COMPATIBILITY_2_4
320 // ----------------------------------------------------------------------------
321 // for compatibility only
322 // ----------------------------------------------------------------------------
326 class WXDLLIMPEXP_BASE wxHashTable
: protected wxHashTableBase
328 typedef wxHashTableBaseBase hash
;
332 struct compatibility_iterator
334 hash::iterator m_iter
;
337 operator bool() const { return m_iter
!= m_hash
->end(); }
338 bool operator !() const { return m_iter
== m_hash
->end(); }
339 compatibility_iterator( hash
* li
, hash::iterator it
)
340 : m_iter( it
), m_hash( li
) {}
341 compatibility_iterator() { }
343 dummy
* operator->() { return (dummy
*)this; }
345 typedef compatibility_iterator citer
;
349 typedef hash::iterator it
;
350 typedef compatibility_iterator citer
;
352 wxObject
* GetData() const
354 citer
* i
= (citer
*)this;
355 return (wxObject
*)i
->m_iter
->second
;
357 citer
GetNext() const
359 citer
* i
= (citer
*)this;
361 return citer( i
->m_hash
, ++lit
);
363 citer
GetPrevious() const
365 citer
* i
= (citer
*)this;
367 return citer( i
->m_hash
, ++lit
);
369 void SetData( wxObject
* e
)
371 citer
* i
= (citer
*)this;
372 i
->m_iter
->second
= e
;
378 wxHashTable( wxKeyType keyType
= wxKEY_INTEGER
,
379 size_t size
= wxHASH_SIZE_DEFAULT
)
380 : wxHashTableBase( keyType
, size
) { }
382 void Destroy() { Clear(); }
384 // key and value are the same
385 void Put(long value
, wxObject
*object
) { DoPut( value
, object
); }
386 void Put(const wxChar
*value
, wxObject
*object
) { DoPut( value
, object
); }
388 // key and value are the same
389 wxObject
*Get(long value
) const { return (wxObject
*)DoGet( value
); }
390 wxObject
*Get(const wxChar
*value
) const { return (wxObject
*)DoGet( value
); }
392 // Deletes entry and returns data if found
393 wxObject
*Delete(long key
) { return (wxObject
*)DoGet( key
); }
394 wxObject
*Delete(const wxChar
*key
) { return (wxObject
*)DoGet( key
); }
397 // Construct your own integer key from a string, e.g. in case
398 // you need to combine it with something
399 long MakeKey(const wxChar
*string
) const;
401 // Way of iterating through whole hash table (e.g. to delete everything)
402 // Not necessary, of course, if you're only storing pointers to
403 // objects maintained separately
404 void BeginFind() { m_iter
= citer( &this->m_map
, this->m_map
.begin() ); }
405 compatibility_iterator
Next()
407 compatibility_iterator it
= m_iter
;
409 m_iter
= m_iter
->GetNext();
413 void Clear() { m_map
.clear(); }
415 compatibility_iterator m_iter
;
418 #else // if !wxUSE_STL
420 class WXDLLIMPEXP_BASE wxHashTable
: public wxObject
424 int current_position
;
425 wxNode
*current_node
;
427 unsigned int key_type
;
430 wxHashTable(int the_key_type
= wxKEY_INTEGER
,
431 int size
= wxHASH_SIZE_DEFAULT
);
434 // copy ctor and assignment operator
435 wxHashTable(const wxHashTable
& table
) : wxObject()
437 wxHashTable
& operator=(const wxHashTable
& table
)
438 { Clear(); DoCopy(table
); return *this; }
440 void DoCopy(const wxHashTable
& table
);
444 bool Create(int the_key_type
= wxKEY_INTEGER
,
445 int size
= wxHASH_SIZE_DEFAULT
);
447 // Note that there are 2 forms of Put, Get.
448 // With a key and a value, the *value* will be checked
449 // when a collision is detected. Otherwise, if there are
450 // 2 items with a different value but the same key,
451 // we'll retrieve the WRONG ONE. So where possible,
452 // supply the required value along with the key.
453 // In fact, the value-only versions make a key, and still store
454 // the value. The use of an explicit key might be required
455 // e.g. when combining several values into one key.
456 // When doing that, it's highly likely we'll get a collision,
457 // e.g. 1 + 2 = 3, 2 + 1 = 3.
459 // key and value are NOT necessarily the same
460 void Put(long key
, long value
, wxObject
*object
);
461 void Put(long key
, const wxChar
*value
, wxObject
*object
);
463 // key and value are the same
464 void Put(long value
, wxObject
*object
);
465 void Put(const wxChar
*value
, wxObject
*object
);
467 // key and value not the same
468 wxObject
*Get(long key
, long value
) const;
469 wxObject
*Get(long key
, const wxChar
*value
) const;
471 // key and value are the same
472 wxObject
*Get(long value
) const;
473 wxObject
*Get(const wxChar
*value
) const;
475 // Deletes entry and returns data if found
476 wxObject
*Delete(long key
);
477 wxObject
*Delete(const wxChar
*key
);
479 wxObject
*Delete(long key
, int value
);
480 wxObject
*Delete(long key
, const wxChar
*value
);
482 // Construct your own integer key from a string, e.g. in case
483 // you need to combine it with something
484 long MakeKey(const wxChar
*string
) const;
486 // Way of iterating through whole hash table (e.g. to delete everything)
487 // Not necessary, of course, if you're only storing pointers to
488 // objects maintained separately
493 void DeleteContents(bool flag
);
496 // Returns number of nodes
497 size_t GetCount() const { return m_count
; }
499 typedef wxNode
* compatibility_iterator
;
501 size_t m_count
; // number of elements in the hashtable
502 bool m_deleteContents
;
504 DECLARE_DYNAMIC_CLASS(wxHashTable
)
511 // defines a new type safe hash table which stores the elements of type eltype
512 // in lists of class listclass
513 #define _WX_DECLARE_HASH(eltype, dummy, hashclass, classexp) \
514 classexp hashclass : public wxHashTableBase \
517 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
518 size_t size = wxHASH_SIZE_DEFAULT) \
519 : wxHashTableBase(keyType, size) { } \
521 ~hashclass() { Destroy(); } \
523 void Destroy() { m_map.clear(); } \
524 void Put(long key, eltype *data) { DoPut(key, (void*)data); } \
525 eltype *Get(long key) const { return (eltype*)DoGet(key); } \
526 eltype *Delete(long key) { return (eltype*)DoDelete(key); } \
529 #else // if !wxUSE_STL
531 #define _WX_DECLARE_HASH(eltype, listclass, hashclass, classexp) \
532 classexp hashclass : public wxHashTableBase \
535 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
536 size_t size = wxHASH_SIZE_DEFAULT) \
537 { Create(keyType, size); } \
539 ~hashclass() { Destroy(); } \
541 void Put(long key, long val, eltype *data) { DoPut(key, val, data); } \
542 void Put(long key, eltype *data) { DoPut(key, key, data); } \
544 eltype *Get(long key, long value) const \
546 wxNodeBase *node = GetNode(key, value); \
547 return node ? ((listclass::Node *)node)->GetData() : (eltype *)0; \
549 eltype *Get(long key) const { return Get(key, key); } \
551 eltype *Delete(long key, long value) \
555 wxNodeBase *node = GetNode(key, value); \
558 data = ((listclass::Node *)node)->GetData(); \
565 data = (eltype *)0; \
570 eltype *Delete(long key) { return Delete(key, key); } \
573 void DoPut(long key, long value, eltype *data) \
575 size_t slot = (size_t)abs((int)(key % (long)m_hashSize)); \
577 if ( !m_hashTable[slot] ) \
579 m_hashTable[slot] = new listclass(m_keyType); \
580 if ( m_deleteContents ) \
581 m_hashTable[slot]->DeleteContents(TRUE); \
584 ((listclass *)m_hashTable[slot])->Append(value, data); \
588 DECLARE_NO_COPY_CLASS(hashclass) \
593 // this macro is to be used in the user code
594 #define WX_DECLARE_HASH(el, list, hash) \
595 _WX_DECLARE_HASH(el, list, hash, class)
597 // and this one does exactly the same thing but should be used inside the
599 #define WX_DECLARE_EXPORTED_HASH(el, list, hash) \
600 _WX_DECLARE_HASH(el, list, hash, class WXDLLEXPORT)
602 #define WX_DECLARE_USER_EXPORTED_HASH(el, list, hash, usergoo) \
603 _WX_DECLARE_HASH(el, list, hash, class usergoo)
605 // delete all hash elements
607 // NB: the class declaration of the hash elements must be visible from the
608 // place where you use this macro, otherwise the proper destructor may not
609 // be called (a decent compiler should give a warning about it, but don't
611 #define WX_CLEAR_HASH_TABLE(hash) \
613 (hash).BeginFind(); \
614 wxHashTable::compatibility_iterator it = (hash).Next(); \
617 delete it->GetData(); \
618 it = (hash).Next(); \