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(NO_GCC_PRAGMA)
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 wxHashTableBaseBaseBase
);
145 // hack: we should really have HASH_MULTI(MAP|SET), but this requires
148 class WXDLLIMPEXP_BASE wxHashTableBaseBase
: public wxHashTableBaseBaseBase
151 wxHashTableBaseBase(size_t size
, const wxHashTableHash
& hash
,
152 const wxHashTableEqual
& equal
)
153 : wxHashTableBaseBaseBase(size
, hash
, equal
)
156 void multi_insert(const wxHashKeyValue
& key
, void* value
)
158 CreateNodeLast(value_type(key
, value
));
162 class WXDLLIMPEXP_BASE wxHashTableBase
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
) { }
171 ~wxHashTableBase() { Clear(); }
173 size_t GetCount() const { return m_map
.size(); }
177 if( m_keyType
== wxKEY_STRING
)
179 for( wxHashTableBaseBase::iterator it
= m_map
.begin(),
183 wxChar
* tmp
= it
->first
.string
;
185 delete[] tmp
; // used in operator++
191 void DoPut( long key
, void* data
)
193 wxASSERT( m_keyType
== wxKEY_INTEGER
);
195 wxHashKeyValue k
; k
.integer
= key
;
196 m_map
.multi_insert(k
, data
);
199 void DoPut( const wxChar
* key
, void* data
)
201 wxASSERT( m_keyType
== wxKEY_STRING
);
204 k
.string
= wxStrcpy(new wxChar
[wxStrlen(key
) + 1], key
);
205 m_map
.multi_insert(k
, data
);
208 void* DoGet( long key
) const
210 wxASSERT( m_keyType
== wxKEY_INTEGER
);
212 wxHashKeyValue k
; k
.integer
= key
;
213 wxHashTableBaseBase::const_iterator it
= m_map
.find( k
);
215 return it
!= m_map
.end() ? it
->second
: NULL
;
218 void* DoGet( const wxChar
* key
) const
220 wxASSERT( m_keyType
== wxKEY_STRING
);
222 wxHashKeyValue k
; k
.string
= (wxChar
*)key
;
223 wxHashTableBaseBase::const_iterator it
= m_map
.find( k
);
225 return it
!= m_map
.end() ? it
->second
: NULL
;
228 void* DoDelete( long key
)
230 wxASSERT( m_keyType
== wxKEY_INTEGER
);
232 wxHashKeyValue k
; k
.integer
= key
;
233 wxHashTableBaseBase::iterator it
= m_map
.find( k
);
235 if( it
!= m_map
.end() )
237 void* data
= it
->second
;
246 void* DoDelete( const wxChar
* key
)
248 wxASSERT( m_keyType
== wxKEY_STRING
);
250 wxHashKeyValue k
; k
.string
= (wxChar
*)key
;
251 wxHashTableBaseBase::iterator it
= m_map
.find( k
);
253 if( it
!= m_map
.end() )
255 void* data
= it
->second
;
256 wxChar
* k
= it
->first
.string
;
266 wxHashTableBaseBase m_map
;
274 #if WXWIN_COMPATIBILITY_2_4
276 // ----------------------------------------------------------------------------
277 // a hash table which stores longs
278 // ----------------------------------------------------------------------------
280 class WXDLLIMPEXP_BASE wxHashTableLong
: public wxObject
283 wxHashTableLong(size_t size
= wxHASH_SIZE_DEFAULT
)
285 virtual ~wxHashTableLong();
287 void Create(size_t size
= wxHASH_SIZE_DEFAULT
);
290 size_t GetSize() const { return m_hashSize
; }
291 size_t GetCount() const { return m_count
; }
293 void Put(long key
, long value
);
294 long Get(long key
) const;
295 long Delete(long key
);
298 void Init(size_t size
);
301 wxArrayLong
**m_values
,
304 // the size of array above
307 // the total number of elements in the hash
310 // not implemented yet
311 DECLARE_NO_COPY_CLASS(wxHashTableLong
)
314 // ----------------------------------------------------------------------------
315 // wxStringHashTable: a hash table which indexes strings with longs
316 // ----------------------------------------------------------------------------
318 class WXDLLIMPEXP_BASE wxStringHashTable
: public wxObject
321 wxStringHashTable(size_t sizeTable
= wxHASH_SIZE_DEFAULT
);
322 virtual ~wxStringHashTable();
324 // add a string associated with this key to the table
325 void Put(long key
, const wxString
& value
);
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;
331 // remove the item, returning TRUE if the item was found and deleted
332 bool Delete(long key
) const;
338 wxArrayLong
**m_keys
;
339 wxArrayString
**m_values
;
341 // the size of array above
344 DECLARE_NO_COPY_CLASS(wxStringHashTable
)
347 #endif // WXWIN_COMPATIBILITY_2_4
351 // ----------------------------------------------------------------------------
352 // for compatibility only
353 // ----------------------------------------------------------------------------
357 class WXDLLIMPEXP_BASE wxHashTable
: protected wxHashTableBase
359 typedef wxHashTableBaseBase hash
;
363 struct compatibility_iterator
365 hash::iterator m_iter
;
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() { }
374 dummy
* operator->() { return (dummy
*)this; }
376 typedef compatibility_iterator citer
;
380 typedef hash::iterator it
;
381 typedef compatibility_iterator citer
;
383 wxObject
* GetData() const
385 citer
* i
= (citer
*)this;
386 return (wxObject
*)i
->m_iter
->second
;
388 citer
GetNext() const
390 citer
* i
= (citer
*)this;
392 return citer( i
->m_hash
, ++lit
);
394 void SetData( wxObject
* e
)
396 citer
* i
= (citer
*)this;
397 i
->m_iter
->second
= e
;
403 wxHashTable( wxKeyType keyType
= wxKEY_INTEGER
,
404 size_t size
= wxHASH_SIZE_DEFAULT
)
405 : wxHashTableBase( keyType
, size
) { }
407 void Destroy() { Clear(); }
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
); }
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
); }
417 // Deletes entry and returns data if found
418 wxObject
*Delete(long key
) { return (wxObject
*)DoDelete( key
); }
419 wxObject
*Delete(const wxChar
*key
) { return (wxObject
*)DoDelete( key
); }
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;
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()
432 compatibility_iterator it
= m_iter
;
434 m_iter
= m_iter
->GetNext();
438 void Clear() { wxHashTableBase::Clear(); }
440 size_t GetCount() const { return wxHashTableBase::GetCount(); }
442 compatibility_iterator m_iter
;
445 #else // if !wxUSE_STL
447 class WXDLLIMPEXP_BASE wxHashTable
: public wxObject
451 int current_position
;
452 wxNode
*current_node
;
454 unsigned int key_type
;
457 wxHashTable(int the_key_type
= wxKEY_INTEGER
,
458 int size
= wxHASH_SIZE_DEFAULT
);
461 // copy ctor and assignment operator
462 wxHashTable(const wxHashTable
& table
) : wxObject()
464 wxHashTable
& operator=(const wxHashTable
& table
)
465 { Clear(); DoCopy(table
); return *this; }
467 void DoCopy(const wxHashTable
& table
);
471 bool Create(int the_key_type
= wxKEY_INTEGER
,
472 int size
= wxHASH_SIZE_DEFAULT
);
474 // Note that there are 2 forms of Put, Get.
475 // With a key and a value, the *value* will be checked
476 // when a collision is detected. Otherwise, if there are
477 // 2 items with a different value but the same key,
478 // we'll retrieve the WRONG ONE. So where possible,
479 // supply the required value along with the key.
480 // In fact, the value-only versions make a key, and still store
481 // the value. The use of an explicit key might be required
482 // e.g. when combining several values into one key.
483 // When doing that, it's highly likely we'll get a collision,
484 // e.g. 1 + 2 = 3, 2 + 1 = 3.
486 // key and value are NOT necessarily the same
487 void Put(long key
, long value
, wxObject
*object
);
488 void Put(long key
, const wxChar
*value
, wxObject
*object
);
490 // key and value are the same
491 void Put(long value
, wxObject
*object
);
492 void Put(const wxChar
*value
, wxObject
*object
);
494 // key and value not the same
495 wxObject
*Get(long key
, long value
) const;
496 wxObject
*Get(long key
, const wxChar
*value
) const;
498 // key and value are the same
499 wxObject
*Get(long value
) const;
500 wxObject
*Get(const wxChar
*value
) const;
502 // Deletes entry and returns data if found
503 wxObject
*Delete(long key
);
504 wxObject
*Delete(const wxChar
*key
);
506 wxObject
*Delete(long key
, int value
);
507 wxObject
*Delete(long key
, const wxChar
*value
);
509 // Construct your own integer key from a string, e.g. in case
510 // you need to combine it with something
511 long MakeKey(const wxChar
*string
) const;
513 // Way of iterating through whole hash table (e.g. to delete everything)
514 // Not necessary, of course, if you're only storing pointers to
515 // objects maintained separately
520 void DeleteContents(bool flag
);
523 // Returns number of nodes
524 size_t GetCount() const { return m_count
; }
526 typedef wxNode
* compatibility_iterator
;
528 size_t m_count
; // number of elements in the hashtable
529 bool m_deleteContents
;
531 DECLARE_DYNAMIC_CLASS(wxHashTable
)
538 // defines a new type safe hash table which stores the elements of type eltype
539 // in lists of class listclass
540 #define _WX_DECLARE_HASH(eltype, dummy, hashclass, classexp) \
541 classexp hashclass : public wxHashTableBase \
544 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
545 size_t size = wxHASH_SIZE_DEFAULT) \
546 : wxHashTableBase(keyType, size) { } \
548 ~hashclass() { Destroy(); } \
550 void Destroy() { m_map.clear(); } \
551 void Put(long key, eltype *data) { DoPut(key, (void*)data); } \
552 eltype *Get(long key) const { return (eltype*)DoGet(key); } \
553 eltype *Delete(long key) { return (eltype*)DoDelete(key); } \
556 #else // if !wxUSE_STL
558 #define _WX_DECLARE_HASH(eltype, listclass, hashclass, classexp) \
559 classexp hashclass : public wxHashTableBase \
562 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
563 size_t size = wxHASH_SIZE_DEFAULT) \
564 { Create(keyType, size); } \
566 ~hashclass() { Destroy(); } \
568 void Put(long key, long val, eltype *data) { DoPut(key, val, data); } \
569 void Put(long key, eltype *data) { DoPut(key, key, data); } \
571 eltype *Get(long key, long value) const \
573 wxNodeBase *node = GetNode(key, value); \
574 return node ? ((listclass::Node *)node)->GetData() : (eltype *)0; \
576 eltype *Get(long key) const { return Get(key, key); } \
578 eltype *Delete(long key, long value) \
582 wxNodeBase *node = GetNode(key, value); \
585 data = ((listclass::Node *)node)->GetData(); \
592 data = (eltype *)0; \
597 eltype *Delete(long key) { return Delete(key, key); } \
600 void DoPut(long key, long value, eltype *data) \
602 size_t slot = (size_t)abs((int)(key % (long)m_hashSize)); \
604 if ( !m_hashTable[slot] ) \
606 m_hashTable[slot] = new listclass(m_keyType); \
607 if ( m_deleteContents ) \
608 m_hashTable[slot]->DeleteContents(TRUE); \
611 ((listclass *)m_hashTable[slot])->Append(value, data); \
615 DECLARE_NO_COPY_CLASS(hashclass) \
620 // this macro is to be used in the user code
621 #define WX_DECLARE_HASH(el, list, hash) \
622 _WX_DECLARE_HASH(el, list, hash, class)
624 // and this one does exactly the same thing but should be used inside the
626 #define WX_DECLARE_EXPORTED_HASH(el, list, hash) \
627 _WX_DECLARE_HASH(el, list, hash, class WXDLLEXPORT)
629 #define WX_DECLARE_USER_EXPORTED_HASH(el, list, hash, usergoo) \
630 _WX_DECLARE_HASH(el, list, hash, class usergoo)
632 // delete all hash elements
634 // NB: the class declaration of the hash elements must be visible from the
635 // place where you use this macro, otherwise the proper destructor may not
636 // be called (a decent compiler should give a warning about it, but don't
638 #define WX_CLEAR_HASH_TABLE(hash) \
640 (hash).BeginFind(); \
641 wxHashTable::compatibility_iterator it = (hash).Next(); \
644 delete it->GetData(); \
645 it = (hash).Next(); \