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 /////////////////////////////////////////////////////////////////////////////
16 #include "wx/string.h"
18 #define wxUSE_OLD_HASH_TABLE 0
21 #include "wx/object.h"
23 class WXDLLIMPEXP_BASE wxObject
;
25 #if wxUSE_OLD_HASH_TABLE
29 // the default size of the hash
30 #define wxHASH_SIZE_DEFAULT (1000)
33 * A hash table is an array of user-definable size with lists
34 * of data items hanging off the array positions. Usually there'll
35 * be a hit, so no search is required; otherwise we'll have to run down
36 * the list to find the desired item.
39 // ----------------------------------------------------------------------------
40 // this is the base class for object hashes: hash tables which contain
41 // pointers to objects
42 // ----------------------------------------------------------------------------
44 #if wxUSE_OLD_HASH_TABLE
46 class WXDLLIMPEXP_BASE wxHashTableBase
: public wxObject
51 void Create(wxKeyType keyType
= wxKEY_INTEGER
,
52 size_t size
= wxHASH_SIZE_DEFAULT
);
55 size_t GetSize() const { return m_hashSize
; }
56 size_t GetCount() const { return m_count
; }
58 void DeleteContents(bool flag
);
61 // find the node for (key, value)
62 wxNodeBase
*GetNode(long key
, long value
) const;
64 // the array of lists in which we store the values for given key hash
65 wxListBase
**m_hashTable
;
67 // the size of m_lists array
70 // the type of indexing we use
73 // the total number of elements in the hash
76 // should we delete our data?
77 bool m_deleteContents
;
80 // no copy ctor/assignment operator (yet)
81 DECLARE_NO_COPY_CLASS(wxHashTableBase
)
84 #else // if !wxUSE_OLD_HASH_TABLE
86 #if !defined(wxENUM_KEY_TYPE_DEFINED)
87 #define wxENUM_KEY_TYPE_DEFINED
104 // for some compilers (AIX xlC), defining it as friend inside the class is not
105 // enough, so provide a real forward declaration
106 class WXDLLIMPEXP_BASE wxHashTableBase
;
108 class WXDLLIMPEXP_BASE wxHashTableBase_Node
110 friend class WXDLLIMPEXP_BASE wxHashTableBase
;
111 typedef class WXDLLIMPEXP_BASE wxHashTableBase_Node _Node
;
113 wxHashTableBase_Node( long key
, void* value
,
114 wxHashTableBase
* table
);
115 wxHashTableBase_Node( const wxChar
* key
, void* value
,
116 wxHashTableBase
* table
);
117 ~wxHashTableBase_Node();
119 long GetKeyInteger() const { return m_key
.integer
; }
120 const wxChar
* GetKeyString() const { return m_key
.string
; }
122 void* GetData() const { return m_value
; }
123 void SetData( void* data
) { m_value
= data
; }
126 _Node
* GetNext() const { return m_next
; }
129 // next node in the chain
130 wxHashTableBase_Node
* m_next
;
133 wxHashKeyValue m_key
;
138 // pointer to the hash containing the node, used to remove the
139 // node from the hash when the user deletes the node iterating
141 // TODO: move it to wxHashTable_Node (only wxHashTable supports
143 wxHashTableBase
* m_hashPtr
;
146 class WXDLLIMPEXP_BASE wxHashTableBase
151 friend class WXDLLIMPEXP_BASE wxHashTableBase_Node
;
153 typedef wxHashTableBase_Node Node
;
156 virtual ~wxHashTableBase() { }
158 void Create( wxKeyType keyType
= wxKEY_INTEGER
,
159 size_t size
= wxHASH_SIZE_DEFAULT
);
163 size_t GetSize() const { return m_size
; }
164 size_t GetCount() const { return m_count
; }
166 void DeleteContents( bool flag
) { m_deleteContents
= flag
; }
168 static long MakeKey(const wxChar
*string
);
171 void DoPut( long key
, long hash
, void* data
);
172 void DoPut( const wxChar
* key
, long hash
, void* data
);
173 void* DoGet( long key
, long hash
) const;
174 void* DoGet( const wxChar
* key
, long hash
) const;
175 void* DoDelete( long key
, long hash
);
176 void* DoDelete( const wxChar
* key
, long hash
);
179 // Remove the node from the hash, *only called from
180 // ~wxHashTable*_Node destructor
181 void DoRemoveNode( wxHashTableBase_Node
* node
);
183 // destroys data contained in the node if appropriate:
184 // deletes the key if it is a string and destrys
185 // the value if m_deleteContents is true
186 void DoDestroyNode( wxHashTableBase_Node
* node
);
188 // inserts a node in the table (at the end of the chain)
189 void DoInsertNode( size_t bucket
, wxHashTableBase_Node
* node
);
191 // removes a node from the table (fiven a pointer to the previous
192 // but does not delete it (only deletes its contents)
193 void DoUnlinkNode( size_t bucket
, wxHashTableBase_Node
* node
,
194 wxHashTableBase_Node
* prev
);
196 // unconditionally deletes node value (invoking the
197 // correct destructor)
198 virtual void DoDeleteContents( wxHashTableBase_Node
* node
) = 0;
204 // number of nodes (key/value pairs)
210 // key typ (INTEGER/STRING)
213 // delete contents when hash is cleared
214 bool m_deleteContents
;
217 DECLARE_NO_COPY_CLASS(wxHashTableBase
)
220 #endif // wxUSE_OLD_HASH_TABLE
222 // ----------------------------------------------------------------------------
223 // for compatibility only
224 // ----------------------------------------------------------------------------
226 #if !wxUSE_OLD_HASH_TABLE
228 class WXDLLIMPEXP_BASE wxHashTable_Node
: public wxHashTableBase_Node
230 friend class WXDLLIMPEXP_BASE wxHashTable
;
232 wxHashTable_Node( long key
, void* value
,
233 wxHashTableBase
* table
)
234 : wxHashTableBase_Node( key
, value
, table
) { }
235 wxHashTable_Node( const wxChar
* key
, void* value
,
236 wxHashTableBase
* table
)
237 : wxHashTableBase_Node( key
, value
, table
) { }
239 wxObject
* GetData() const
240 { return (wxObject
*)wxHashTableBase_Node::GetData(); }
241 void SetData( wxObject
* data
)
242 { wxHashTableBase_Node::SetData( data
); }
244 wxHashTable_Node
* GetNext() const
245 { return (wxHashTable_Node
*)wxHashTableBase_Node::GetNext(); }
248 // should inherit protectedly, but it is public for compatibility in
249 // order to publicly inherit from wxObject
250 class WXDLLIMPEXP_BASE wxHashTable
: public wxHashTableBase
252 typedef wxHashTableBase hash
;
254 typedef wxHashTable_Node Node
;
255 typedef wxHashTable_Node
* compatibility_iterator
;
257 wxHashTable( wxKeyType keyType
= wxKEY_INTEGER
,
258 size_t size
= wxHASH_SIZE_DEFAULT
)
259 : wxHashTableBase() { Create( keyType
, size
); BeginFind(); }
260 wxHashTable( const wxHashTable
& table
);
262 virtual ~wxHashTable() { Destroy(); }
264 const wxHashTable
& operator=( const wxHashTable
& );
266 // key and value are the same
267 void Put(long value
, wxObject
*object
)
268 { DoPut( value
, value
, object
); }
269 void Put(long lhash
, long value
, wxObject
*object
)
270 { DoPut( value
, lhash
, object
); }
271 void Put(const wxChar
*value
, wxObject
*object
)
272 { DoPut( value
, MakeKey( value
), object
); }
273 // FIXME-UTF8: have only wxString forms here
274 void Put(const wxString
& value
, wxObject
*object
)
275 { DoPut( value
, MakeKey( value
), object
); }
276 void Put(long lhash
, const wxChar
*value
, wxObject
*object
)
277 { DoPut( value
, lhash
, object
); }
279 // key and value are the same
280 wxObject
*Get(long value
) const
281 { return (wxObject
*)DoGet( value
, value
); }
282 wxObject
*Get(long lhash
, long value
) const
283 { return (wxObject
*)DoGet( value
, lhash
); }
284 wxObject
*Get(const wxChar
*value
) const
285 { return (wxObject
*)DoGet( value
, MakeKey( value
) ); }
286 // FIXME-UTF8: have only wxString forms here
287 wxObject
*Get(const wxString
& value
) const
288 { return (wxObject
*)DoGet( value
, MakeKey( value
) ); }
289 wxObject
*Get(long lhash
, const wxChar
*value
) const
290 { return (wxObject
*)DoGet( value
, lhash
); }
292 // Deletes entry and returns data if found
293 wxObject
*Delete(long key
)
294 { return (wxObject
*)DoDelete( key
, key
); }
295 wxObject
*Delete(long lhash
, long key
)
296 { return (wxObject
*)DoDelete( key
, lhash
); }
297 wxObject
*Delete(const wxChar
*key
)
298 { return (wxObject
*)DoDelete( key
, MakeKey( key
) ); }
299 // FIXME-UTF8: have only wxString forms here
300 wxObject
*Delete(const wxString
& key
)
301 { return (wxObject
*)DoDelete( key
, MakeKey( key
) ); }
302 wxObject
*Delete(long lhash
, const wxChar
*key
)
303 { return (wxObject
*)DoDelete( key
, lhash
); }
305 // Construct your own integer key from a string, e.g. in case
306 // you need to combine it with something
307 long MakeKey(const wxChar
*string
) const
308 { return wxHashTableBase::MakeKey(string
); }
310 // Way of iterating through whole hash table (e.g. to delete everything)
311 // Not necessary, of course, if you're only storing pointers to
312 // objects maintained separately
313 void BeginFind() { m_curr
= NULL
; m_currBucket
= 0; }
316 void Clear() { wxHashTableBase::Clear(); }
318 size_t GetCount() const { return wxHashTableBase::GetCount(); }
321 void DoCopy( const wxHashTable
& copy
);
323 // searches the next node starting from bucket bucketStart and sets
324 // m_curr to it and m_currBucket to its bucket
325 void GetNextNode( size_t bucketStart
);
327 virtual void DoDeleteContents( wxHashTableBase_Node
* node
);
332 // bucket the current node belongs to
336 #else // if wxUSE_OLD_HASH_TABLE
338 typedef wxNode wxHashTable_Node
;
340 class WXDLLIMPEXP_BASE wxHashTable
: public wxObject
344 typedef wxNode
* compatibility_iterator
;
347 int current_position
;
348 wxNode
*current_node
;
350 unsigned int key_type
;
353 wxHashTable(int the_key_type
= wxKEY_INTEGER
,
354 int size
= wxHASH_SIZE_DEFAULT
);
355 virtual ~wxHashTable();
357 // copy ctor and assignment operator
358 wxHashTable(const wxHashTable
& table
) : wxObject()
360 wxHashTable
& operator=(const wxHashTable
& table
)
361 { Clear(); DoCopy(table
); return *this; }
363 void DoCopy(const wxHashTable
& table
);
367 bool Create(int the_key_type
= wxKEY_INTEGER
,
368 int size
= wxHASH_SIZE_DEFAULT
);
370 // Note that there are 2 forms of Put, Get.
371 // With a key and a value, the *value* will be checked
372 // when a collision is detected. Otherwise, if there are
373 // 2 items with a different value but the same key,
374 // we'll retrieve the WRONG ONE. So where possible,
375 // supply the required value along with the key.
376 // In fact, the value-only versions make a key, and still store
377 // the value. The use of an explicit key might be required
378 // e.g. when combining several values into one key.
379 // When doing that, it's highly likely we'll get a collision,
380 // e.g. 1 + 2 = 3, 2 + 1 = 3.
382 // key and value are NOT necessarily the same
383 void Put(long key
, long value
, wxObject
*object
);
384 void Put(long key
, const wxChar
*value
, wxObject
*object
);
386 // key and value are the same
387 void Put(long value
, wxObject
*object
);
388 void Put(const wxChar
*value
, wxObject
*object
);
390 // key and value not the same
391 wxObject
*Get(long key
, long value
) const;
392 wxObject
*Get(long key
, const wxChar
*value
) const;
394 // key and value are the same
395 wxObject
*Get(long value
) const;
396 wxObject
*Get(const wxChar
*value
) const;
398 // Deletes entry and returns data if found
399 wxObject
*Delete(long key
);
400 wxObject
*Delete(const wxChar
*key
);
402 wxObject
*Delete(long key
, int value
);
403 wxObject
*Delete(long key
, const wxChar
*value
);
405 // Construct your own integer key from a string, e.g. in case
406 // you need to combine it with something
407 long MakeKey(const wxChar
*string
) const;
409 // Way of iterating through whole hash table (e.g. to delete everything)
410 // Not necessary, of course, if you're only storing pointers to
411 // objects maintained separately
416 void DeleteContents(bool flag
);
419 // Returns number of nodes
420 size_t GetCount() const { return m_count
; }
423 size_t m_count
; // number of elements in the hashtable
424 bool m_deleteContents
;
426 DECLARE_DYNAMIC_CLASS(wxHashTable
)
429 #endif // wxUSE_OLD_HASH_TABLE
431 #if !wxUSE_OLD_HASH_TABLE
433 // defines a new type safe hash table which stores the elements of type eltype
434 // in lists of class listclass
435 #define _WX_DECLARE_HASH(eltype, dummy, hashclass, classexp) \
436 classexp hashclass : public wxHashTableBase \
439 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
440 size_t size = wxHASH_SIZE_DEFAULT) \
441 : wxHashTableBase() { Create(keyType, size); } \
443 virtual ~hashclass() { Destroy(); } \
445 void Put(long key, eltype *data) { DoPut(key, key, (void*)data); } \
446 void Put(long lhash, long key, eltype *data) \
447 { DoPut(key, lhash, (void*)data); } \
448 eltype *Get(long key) const { return (eltype*)DoGet(key, key); } \
449 eltype *Get(long lhash, long key) const \
450 { return (eltype*)DoGet(key, lhash); } \
451 eltype *Delete(long key) { return (eltype*)DoDelete(key, key); } \
452 eltype *Delete(long lhash, long key) \
453 { return (eltype*)DoDelete(key, lhash); } \
455 virtual void DoDeleteContents( wxHashTableBase_Node* node ) \
456 { delete (eltype*)node->GetData(); } \
458 DECLARE_NO_COPY_CLASS(hashclass) \
461 #else // if wxUSE_OLD_HASH_TABLE
463 #define _WX_DECLARE_HASH(eltype, listclass, hashclass, classexp) \
464 classexp hashclass : public wxHashTableBase \
467 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
468 size_t size = wxHASH_SIZE_DEFAULT) \
469 { Create(keyType, size); } \
471 virtual ~hashclass() { Destroy(); } \
473 void Put(long key, long val, eltype *data) { DoPut(key, val, data); } \
474 void Put(long key, eltype *data) { DoPut(key, key, data); } \
476 eltype *Get(long key, long value) const \
478 wxNodeBase *node = GetNode(key, value); \
479 return node ? ((listclass::Node *)node)->GetData() : (eltype *)0; \
481 eltype *Get(long key) const { return Get(key, key); } \
483 eltype *Delete(long key, long value) \
487 wxNodeBase *node = GetNode(key, value); \
490 data = ((listclass::Node *)node)->GetData(); \
497 data = (eltype *)0; \
502 eltype *Delete(long key) { return Delete(key, key); } \
505 void DoPut(long key, long value, eltype *data) \
507 size_t slot = (size_t)abs((int)(key % (long)m_hashSize)); \
509 if ( !m_hashTable[slot] ) \
511 m_hashTable[slot] = new listclass(m_keyType); \
512 if ( m_deleteContents ) \
513 m_hashTable[slot]->DeleteContents(true); \
516 ((listclass *)m_hashTable[slot])->Append(value, data); \
520 DECLARE_NO_COPY_CLASS(hashclass) \
523 #endif // wxUSE_OLD_HASH_TABLE
525 // this macro is to be used in the user code
526 #define WX_DECLARE_HASH(el, list, hash) \
527 _WX_DECLARE_HASH(el, list, hash, class)
529 // and this one does exactly the same thing but should be used inside the
531 #define WX_DECLARE_EXPORTED_HASH(el, list, hash) \
532 _WX_DECLARE_HASH(el, list, hash, class WXDLLEXPORT)
534 #define WX_DECLARE_USER_EXPORTED_HASH(el, list, hash, usergoo) \
535 _WX_DECLARE_HASH(el, list, hash, class usergoo)
537 // delete all hash elements
539 // NB: the class declaration of the hash elements must be visible from the
540 // place where you use this macro, otherwise the proper destructor may not
541 // be called (a decent compiler should give a warning about it, but don't
543 #define WX_CLEAR_HASH_TABLE(hash) \
545 (hash).BeginFind(); \
546 wxHashTable::compatibility_iterator it = (hash).Next(); \
549 delete it->GetData(); \
550 it = (hash).Next(); \