1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHashTable class
4 // Author: Julian Smart
5 // Modified by: VZ at 25.02.00: type safe hashes with WX_DECLARE_HASH()
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
17 #define wxUSE_OLD_HASH_TABLE 0
20 #include "wx/object.h"
22 class WXDLLIMPEXP_BASE wxObject
;
24 #if wxUSE_OLD_HASH_TABLE
28 // the default size of the hash
29 #define wxHASH_SIZE_DEFAULT (1000)
32 * A hash table is an array of user-definable size with lists
33 * of data items hanging off the array positions. Usually there'll
34 * be a hit, so no search is required; otherwise we'll have to run down
35 * the list to find the desired item.
38 // ----------------------------------------------------------------------------
39 // this is the base class for object hashes: hash tables which contain
40 // pointers to objects
41 // ----------------------------------------------------------------------------
43 #if wxUSE_OLD_HASH_TABLE
45 class WXDLLIMPEXP_BASE wxHashTableBase
: public wxObject
50 void Create(wxKeyType keyType
= wxKEY_INTEGER
,
51 size_t size
= wxHASH_SIZE_DEFAULT
);
54 size_t GetSize() const { return m_hashSize
; }
55 size_t GetCount() const { return m_count
; }
57 void DeleteContents(bool flag
);
60 // find the node for (key, value)
61 wxNodeBase
*GetNode(long key
, long value
) const;
63 // the array of lists in which we store the values for given key hash
64 wxListBase
**m_hashTable
;
66 // the size of m_lists array
69 // the type of indexing we use
72 // the total number of elements in the hash
75 // should we delete our data?
76 bool m_deleteContents
;
79 // no copy ctor/assignment operator (yet)
80 DECLARE_NO_COPY_CLASS(wxHashTableBase
)
83 #else // if !wxUSE_OLD_HASH_TABLE
85 #if !defined(wxENUM_KEY_TYPE_DEFINED)
86 #define wxENUM_KEY_TYPE_DEFINED
103 // for some compilers (AIX xlC), defining it as friend inside the class is not
104 // enough, so provide a real forward declaration
105 class WXDLLIMPEXP_BASE wxHashTableBase
;
107 class WXDLLIMPEXP_BASE wxHashTableBase_Node
109 friend class WXDLLIMPEXP_BASE wxHashTableBase
;
110 typedef class WXDLLIMPEXP_BASE wxHashTableBase_Node _Node
;
112 wxHashTableBase_Node( long key
, void* value
,
113 wxHashTableBase
* table
);
114 wxHashTableBase_Node( const wxChar
* key
, void* value
,
115 wxHashTableBase
* table
);
116 ~wxHashTableBase_Node();
118 long GetKeyInteger() const { return m_key
.integer
; }
119 const wxChar
* GetKeyString() const { return m_key
.string
; }
121 void* GetData() const { return m_value
; }
122 void SetData( void* data
) { m_value
= data
; }
125 _Node
* GetNext() const { return m_next
; }
128 // next node in the chain
129 wxHashTableBase_Node
* m_next
;
132 wxHashKeyValue m_key
;
137 // pointer to the hash containing the node, used to remove the
138 // node from the hash when the user deletes the node iterating
140 // TODO: move it to wxHashTable_Node (only wxHashTable supports
142 wxHashTableBase
* m_hashPtr
;
145 class WXDLLIMPEXP_BASE wxHashTableBase
150 friend class WXDLLIMPEXP_BASE wxHashTableBase_Node
;
152 typedef wxHashTableBase_Node Node
;
155 virtual ~wxHashTableBase() { };
157 void Create( wxKeyType keyType
= wxKEY_INTEGER
,
158 size_t size
= wxHASH_SIZE_DEFAULT
);
162 size_t GetSize() const { return m_size
; }
163 size_t GetCount() const { return m_count
; }
165 void DeleteContents( bool flag
) { m_deleteContents
= flag
; }
167 static long MakeKey(const wxChar
*string
);
170 void DoPut( long key
, long hash
, void* data
);
171 void DoPut( const wxChar
* key
, long hash
, void* data
);
172 void* DoGet( long key
, long hash
) const;
173 void* DoGet( const wxChar
* key
, long hash
) const;
174 void* DoDelete( long key
, long hash
);
175 void* DoDelete( const wxChar
* key
, long hash
);
178 // Remove the node from the hash, *only called from
179 // ~wxHashTable*_Node destructor
180 void DoRemoveNode( wxHashTableBase_Node
* node
);
182 // destroys data contained in the node if appropriate:
183 // deletes the key if it is a string and destrys
184 // the value if m_deleteContents is true
185 void DoDestroyNode( wxHashTableBase_Node
* node
);
187 // inserts a node in the table (at the end of the chain)
188 void DoInsertNode( size_t bucket
, wxHashTableBase_Node
* node
);
190 // removes a node from the table (fiven a pointer to the previous
191 // but does not delete it (only deletes its contents)
192 void DoUnlinkNode( size_t bucket
, wxHashTableBase_Node
* node
,
193 wxHashTableBase_Node
* prev
);
195 // unconditionally deletes node value (invoking the
196 // correct destructor)
197 virtual void DoDeleteContents( wxHashTableBase_Node
* node
) = 0;
203 // number of nodes (key/value pairs)
209 // key typ (INTEGER/STRING)
212 // delete contents when hash is cleared
213 bool m_deleteContents
;
216 DECLARE_NO_COPY_CLASS(wxHashTableBase
)
219 #endif // wxUSE_OLD_HASH_TABLE
221 // ----------------------------------------------------------------------------
222 // for compatibility only
223 // ----------------------------------------------------------------------------
225 #if !wxUSE_OLD_HASH_TABLE
227 class WXDLLIMPEXP_BASE wxHashTable_Node
: public wxHashTableBase_Node
229 friend class WXDLLIMPEXP_BASE wxHashTable
;
231 wxHashTable_Node( long key
, void* value
,
232 wxHashTableBase
* table
)
233 : wxHashTableBase_Node( key
, value
, table
) { }
234 wxHashTable_Node( const wxChar
* key
, void* value
,
235 wxHashTableBase
* table
)
236 : wxHashTableBase_Node( key
, value
, table
) { }
238 wxObject
* GetData() const
239 { return (wxObject
*)wxHashTableBase_Node::GetData(); }
240 void SetData( wxObject
* data
)
241 { wxHashTableBase_Node::SetData( data
); }
243 wxHashTable_Node
* GetNext() const
244 { return (wxHashTable_Node
*)wxHashTableBase_Node::GetNext(); }
247 // should inherit protectedly, but it is public for compatibility in
248 // order to publicly inherit from wxObject
249 class WXDLLIMPEXP_BASE wxHashTable
: public wxHashTableBase
251 typedef wxHashTableBase hash
;
253 typedef wxHashTable_Node Node
;
254 typedef wxHashTable_Node
* compatibility_iterator
;
256 wxHashTable( wxKeyType keyType
= wxKEY_INTEGER
,
257 size_t size
= wxHASH_SIZE_DEFAULT
)
258 : wxHashTableBase() { Create( keyType
, size
); BeginFind(); }
259 wxHashTable( const wxHashTable
& table
);
261 virtual ~wxHashTable() { Destroy(); }
263 const wxHashTable
& operator=( const wxHashTable
& );
265 // key and value are the same
266 void Put(long value
, wxObject
*object
)
267 { DoPut( value
, value
, object
); }
268 void Put(long lhash
, long value
, wxObject
*object
)
269 { DoPut( value
, lhash
, object
); }
270 void Put(const wxChar
*value
, wxObject
*object
)
271 { DoPut( value
, MakeKey( value
), object
); }
272 void Put(long lhash
, const wxChar
*value
, wxObject
*object
)
273 { DoPut( value
, lhash
, object
); }
275 // key and value are the same
276 wxObject
*Get(long value
) const
277 { return (wxObject
*)DoGet( value
, value
); }
278 wxObject
*Get(long lhash
, long value
) const
279 { return (wxObject
*)DoGet( value
, lhash
); }
280 wxObject
*Get(const wxChar
*value
) const
281 { return (wxObject
*)DoGet( value
, MakeKey( value
) ); }
282 wxObject
*Get(long lhash
, const wxChar
*value
) const
283 { return (wxObject
*)DoGet( value
, lhash
); }
285 // Deletes entry and returns data if found
286 wxObject
*Delete(long key
)
287 { return (wxObject
*)DoDelete( key
, key
); }
288 wxObject
*Delete(long lhash
, long key
)
289 { return (wxObject
*)DoDelete( key
, lhash
); }
290 wxObject
*Delete(const wxChar
*key
)
291 { return (wxObject
*)DoDelete( key
, MakeKey( key
) ); }
292 wxObject
*Delete(long lhash
, const wxChar
*key
)
293 { return (wxObject
*)DoDelete( key
, lhash
); }
295 // Construct your own integer key from a string, e.g. in case
296 // you need to combine it with something
297 long MakeKey(const wxChar
*string
) const
298 { return wxHashTableBase::MakeKey(string
); }
300 // Way of iterating through whole hash table (e.g. to delete everything)
301 // Not necessary, of course, if you're only storing pointers to
302 // objects maintained separately
303 void BeginFind() { m_curr
= NULL
; m_currBucket
= 0; }
306 void Clear() { wxHashTableBase::Clear(); }
308 size_t GetCount() const { return wxHashTableBase::GetCount(); }
311 void DoCopy( const wxHashTable
& copy
);
313 // searches the next node starting from bucket bucketStart and sets
314 // m_curr to it and m_currBucket to its bucket
315 void GetNextNode( size_t bucketStart
);
317 virtual void DoDeleteContents( wxHashTableBase_Node
* node
);
322 // bucket the current node belongs to
326 #else // if wxUSE_OLD_HASH_TABLE
328 class WXDLLIMPEXP_BASE wxHashTable
: public wxObject
332 typedef wxNode
* compatibility_iterator
;
335 int current_position
;
336 wxNode
*current_node
;
338 unsigned int key_type
;
341 wxHashTable(int the_key_type
= wxKEY_INTEGER
,
342 int size
= wxHASH_SIZE_DEFAULT
);
343 virtual ~wxHashTable();
345 // copy ctor and assignment operator
346 wxHashTable(const wxHashTable
& table
) : wxObject()
348 wxHashTable
& operator=(const wxHashTable
& table
)
349 { Clear(); DoCopy(table
); return *this; }
351 void DoCopy(const wxHashTable
& table
);
355 bool Create(int the_key_type
= wxKEY_INTEGER
,
356 int size
= wxHASH_SIZE_DEFAULT
);
358 // Note that there are 2 forms of Put, Get.
359 // With a key and a value, the *value* will be checked
360 // when a collision is detected. Otherwise, if there are
361 // 2 items with a different value but the same key,
362 // we'll retrieve the WRONG ONE. So where possible,
363 // supply the required value along with the key.
364 // In fact, the value-only versions make a key, and still store
365 // the value. The use of an explicit key might be required
366 // e.g. when combining several values into one key.
367 // When doing that, it's highly likely we'll get a collision,
368 // e.g. 1 + 2 = 3, 2 + 1 = 3.
370 // key and value are NOT necessarily the same
371 void Put(long key
, long value
, wxObject
*object
);
372 void Put(long key
, const wxChar
*value
, wxObject
*object
);
374 // key and value are the same
375 void Put(long value
, wxObject
*object
);
376 void Put(const wxChar
*value
, wxObject
*object
);
378 // key and value not the same
379 wxObject
*Get(long key
, long value
) const;
380 wxObject
*Get(long key
, const wxChar
*value
) const;
382 // key and value are the same
383 wxObject
*Get(long value
) const;
384 wxObject
*Get(const wxChar
*value
) const;
386 // Deletes entry and returns data if found
387 wxObject
*Delete(long key
);
388 wxObject
*Delete(const wxChar
*key
);
390 wxObject
*Delete(long key
, int value
);
391 wxObject
*Delete(long key
, const wxChar
*value
);
393 // Construct your own integer key from a string, e.g. in case
394 // you need to combine it with something
395 long MakeKey(const wxChar
*string
) const;
397 // Way of iterating through whole hash table (e.g. to delete everything)
398 // Not necessary, of course, if you're only storing pointers to
399 // objects maintained separately
404 void DeleteContents(bool flag
);
407 // Returns number of nodes
408 size_t GetCount() const { return m_count
; }
411 size_t m_count
; // number of elements in the hashtable
412 bool m_deleteContents
;
414 DECLARE_DYNAMIC_CLASS(wxHashTable
)
417 #endif // wxUSE_OLD_HASH_TABLE
419 #if !wxUSE_OLD_HASH_TABLE
421 // defines a new type safe hash table which stores the elements of type eltype
422 // in lists of class listclass
423 #define _WX_DECLARE_HASH(eltype, dummy, hashclass, classexp) \
424 classexp hashclass : public wxHashTableBase \
427 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
428 size_t size = wxHASH_SIZE_DEFAULT) \
429 : wxHashTableBase() { Create(keyType, size); } \
431 virtual ~hashclass() { Destroy(); } \
433 void Put(long key, eltype *data) { DoPut(key, key, (void*)data); } \
434 void Put(long lhash, long key, eltype *data) \
435 { DoPut(key, lhash, (void*)data); } \
436 eltype *Get(long key) const { return (eltype*)DoGet(key, key); } \
437 eltype *Get(long lhash, long key) const \
438 { return (eltype*)DoGet(key, lhash); } \
439 eltype *Delete(long key) { return (eltype*)DoDelete(key, key); } \
440 eltype *Delete(long lhash, long key) \
441 { return (eltype*)DoDelete(key, lhash); } \
443 virtual void DoDeleteContents( wxHashTableBase_Node* node ) \
444 { delete (eltype*)node->GetData(); } \
446 DECLARE_NO_COPY_CLASS(hashclass) \
449 #else // if wxUSE_OLD_HASH_TABLE
451 #define _WX_DECLARE_HASH(eltype, listclass, hashclass, classexp) \
452 classexp hashclass : public wxHashTableBase \
455 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
456 size_t size = wxHASH_SIZE_DEFAULT) \
457 { Create(keyType, size); } \
459 virtual ~hashclass() { Destroy(); } \
461 void Put(long key, long val, eltype *data) { DoPut(key, val, data); } \
462 void Put(long key, eltype *data) { DoPut(key, key, data); } \
464 eltype *Get(long key, long value) const \
466 wxNodeBase *node = GetNode(key, value); \
467 return node ? ((listclass::Node *)node)->GetData() : (eltype *)0; \
469 eltype *Get(long key) const { return Get(key, key); } \
471 eltype *Delete(long key, long value) \
475 wxNodeBase *node = GetNode(key, value); \
478 data = ((listclass::Node *)node)->GetData(); \
485 data = (eltype *)0; \
490 eltype *Delete(long key) { return Delete(key, key); } \
493 void DoPut(long key, long value, eltype *data) \
495 size_t slot = (size_t)abs((int)(key % (long)m_hashSize)); \
497 if ( !m_hashTable[slot] ) \
499 m_hashTable[slot] = new listclass(m_keyType); \
500 if ( m_deleteContents ) \
501 m_hashTable[slot]->DeleteContents(true); \
504 ((listclass *)m_hashTable[slot])->Append(value, data); \
508 DECLARE_NO_COPY_CLASS(hashclass) \
511 #endif // wxUSE_OLD_HASH_TABLE
513 // this macro is to be used in the user code
514 #define WX_DECLARE_HASH(el, list, hash) \
515 _WX_DECLARE_HASH(el, list, hash, class)
517 // and this one does exactly the same thing but should be used inside the
519 #define WX_DECLARE_EXPORTED_HASH(el, list, hash) \
520 _WX_DECLARE_HASH(el, list, hash, class WXDLLEXPORT)
522 #define WX_DECLARE_USER_EXPORTED_HASH(el, list, hash, usergoo) \
523 _WX_DECLARE_HASH(el, list, hash, class usergoo)
525 // delete all hash elements
527 // NB: the class declaration of the hash elements must be visible from the
528 // place where you use this macro, otherwise the proper destructor may not
529 // be called (a decent compiler should give a warning about it, but don't
531 #define WX_CLEAR_HASH_TABLE(hash) \
533 (hash).BeginFind(); \
534 wxHashTable::compatibility_iterator it = (hash).Next(); \
537 delete it->GetData(); \
538 it = (hash).Next(); \