]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/hash.h | |
3 | // Purpose: wxHashTable class | |
4 | // Author: Julian Smart | |
5 | // Modified by: VZ at 25.02.00: type safe hashes with WX_DECLARE_HASH() | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_HASH_H__ | |
13 | #define _WX_HASH_H__ | |
14 | ||
15 | #include "wx/defs.h" | |
16 | #include "wx/string.h" | |
17 | ||
18 | #define wxUSE_OLD_HASH_TABLE 0 | |
19 | ||
20 | #if !wxUSE_STL | |
21 | #include "wx/object.h" | |
22 | #else | |
23 | class WXDLLIMPEXP_BASE wxObject; | |
24 | #endif | |
25 | #if wxUSE_OLD_HASH_TABLE | |
26 | #include "wx/list.h" | |
27 | #endif | |
28 | ||
29 | // the default size of the hash | |
30 | #define wxHASH_SIZE_DEFAULT (1000) | |
31 | ||
32 | /* | |
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. | |
37 | */ | |
38 | ||
39 | // ---------------------------------------------------------------------------- | |
40 | // this is the base class for object hashes: hash tables which contain | |
41 | // pointers to objects | |
42 | // ---------------------------------------------------------------------------- | |
43 | ||
44 | #if wxUSE_OLD_HASH_TABLE | |
45 | ||
46 | class WXDLLIMPEXP_BASE wxHashTableBase : public wxObject | |
47 | { | |
48 | public: | |
49 | wxHashTableBase(); | |
50 | ||
51 | void Create(wxKeyType keyType = wxKEY_INTEGER, | |
52 | size_t size = wxHASH_SIZE_DEFAULT); | |
53 | void Destroy(); | |
54 | ||
55 | size_t GetSize() const { return m_hashSize; } | |
56 | size_t GetCount() const { return m_count; } | |
57 | ||
58 | void DeleteContents(bool flag); | |
59 | ||
60 | protected: | |
61 | // find the node for (key, value) | |
62 | wxNodeBase *GetNode(long key, long value) const; | |
63 | ||
64 | // the array of lists in which we store the values for given key hash | |
65 | wxListBase **m_hashTable; | |
66 | ||
67 | // the size of m_lists array | |
68 | size_t m_hashSize; | |
69 | ||
70 | // the type of indexing we use | |
71 | wxKeyType m_keyType; | |
72 | ||
73 | // the total number of elements in the hash | |
74 | size_t m_count; | |
75 | ||
76 | // should we delete our data? | |
77 | bool m_deleteContents; | |
78 | ||
79 | private: | |
80 | // no copy ctor/assignment operator (yet) | |
81 | DECLARE_NO_COPY_CLASS(wxHashTableBase) | |
82 | }; | |
83 | ||
84 | #else // if !wxUSE_OLD_HASH_TABLE | |
85 | ||
86 | #if !defined(wxENUM_KEY_TYPE_DEFINED) | |
87 | #define wxENUM_KEY_TYPE_DEFINED | |
88 | ||
89 | enum wxKeyType | |
90 | { | |
91 | wxKEY_NONE, | |
92 | wxKEY_INTEGER, | |
93 | wxKEY_STRING | |
94 | }; | |
95 | ||
96 | #endif | |
97 | ||
98 | union wxHashKeyValue | |
99 | { | |
100 | long integer; | |
101 | wxChar *string; | |
102 | }; | |
103 | ||
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; | |
107 | ||
108 | class WXDLLIMPEXP_BASE wxHashTableBase_Node | |
109 | { | |
110 | friend class WXDLLIMPEXP_BASE wxHashTableBase; | |
111 | typedef class WXDLLIMPEXP_BASE wxHashTableBase_Node _Node; | |
112 | public: | |
113 | wxHashTableBase_Node( long key, void* value, | |
114 | wxHashTableBase* table ); | |
115 | wxHashTableBase_Node( const wxChar* key, void* value, | |
116 | wxHashTableBase* table ); | |
117 | ~wxHashTableBase_Node(); | |
118 | ||
119 | long GetKeyInteger() const { return m_key.integer; } | |
120 | const wxChar* GetKeyString() const { return m_key.string; } | |
121 | ||
122 | void* GetData() const { return m_value; } | |
123 | void SetData( void* data ) { m_value = data; } | |
124 | ||
125 | protected: | |
126 | _Node* GetNext() const { return m_next; } | |
127 | ||
128 | protected: | |
129 | // next node in the chain | |
130 | wxHashTableBase_Node* m_next; | |
131 | ||
132 | // key | |
133 | wxHashKeyValue m_key; | |
134 | ||
135 | // value | |
136 | void* m_value; | |
137 | ||
138 | // pointer to the hash containing the node, used to remove the | |
139 | // node from the hash when the user deletes the node iterating | |
140 | // through it | |
141 | // TODO: move it to wxHashTable_Node (only wxHashTable supports | |
142 | // iteration) | |
143 | wxHashTableBase* m_hashPtr; | |
144 | }; | |
145 | ||
146 | class WXDLLIMPEXP_BASE wxHashTableBase | |
147 | #if !wxUSE_STL | |
148 | : public wxObject | |
149 | #endif | |
150 | { | |
151 | friend class WXDLLIMPEXP_BASE wxHashTableBase_Node; | |
152 | public: | |
153 | typedef wxHashTableBase_Node Node; | |
154 | ||
155 | wxHashTableBase(); | |
156 | virtual ~wxHashTableBase() { } | |
157 | ||
158 | void Create( wxKeyType keyType = wxKEY_INTEGER, | |
159 | size_t size = wxHASH_SIZE_DEFAULT ); | |
160 | void Clear(); | |
161 | void Destroy(); | |
162 | ||
163 | size_t GetSize() const { return m_size; } | |
164 | size_t GetCount() const { return m_count; } | |
165 | ||
166 | void DeleteContents( bool flag ) { m_deleteContents = flag; } | |
167 | ||
168 | static long MakeKey(const wxChar *string); | |
169 | ||
170 | protected: | |
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 ); | |
177 | ||
178 | private: | |
179 | // Remove the node from the hash, *only called from | |
180 | // ~wxHashTable*_Node destructor | |
181 | void DoRemoveNode( wxHashTableBase_Node* node ); | |
182 | ||
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 ); | |
187 | ||
188 | // inserts a node in the table (at the end of the chain) | |
189 | void DoInsertNode( size_t bucket, wxHashTableBase_Node* node ); | |
190 | ||
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 ); | |
195 | ||
196 | // unconditionally deletes node value (invoking the | |
197 | // correct destructor) | |
198 | virtual void DoDeleteContents( wxHashTableBase_Node* node ) = 0; | |
199 | ||
200 | protected: | |
201 | // number of buckets | |
202 | size_t m_size; | |
203 | ||
204 | // number of nodes (key/value pairs) | |
205 | size_t m_count; | |
206 | ||
207 | // table | |
208 | Node** m_table; | |
209 | ||
210 | // key typ (INTEGER/STRING) | |
211 | wxKeyType m_keyType; | |
212 | ||
213 | // delete contents when hash is cleared | |
214 | bool m_deleteContents; | |
215 | ||
216 | private: | |
217 | DECLARE_NO_COPY_CLASS(wxHashTableBase) | |
218 | }; | |
219 | ||
220 | #endif // wxUSE_OLD_HASH_TABLE | |
221 | ||
222 | // ---------------------------------------------------------------------------- | |
223 | // for compatibility only | |
224 | // ---------------------------------------------------------------------------- | |
225 | ||
226 | #if !wxUSE_OLD_HASH_TABLE | |
227 | ||
228 | class WXDLLIMPEXP_BASE wxHashTable_Node : public wxHashTableBase_Node | |
229 | { | |
230 | friend class WXDLLIMPEXP_BASE wxHashTable; | |
231 | public: | |
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 ) { } | |
238 | ||
239 | wxObject* GetData() const | |
240 | { return (wxObject*)wxHashTableBase_Node::GetData(); } | |
241 | void SetData( wxObject* data ) | |
242 | { wxHashTableBase_Node::SetData( data ); } | |
243 | ||
244 | wxHashTable_Node* GetNext() const | |
245 | { return (wxHashTable_Node*)wxHashTableBase_Node::GetNext(); } | |
246 | }; | |
247 | ||
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 | |
251 | { | |
252 | typedef wxHashTableBase hash; | |
253 | public: | |
254 | typedef wxHashTable_Node Node; | |
255 | typedef wxHashTable_Node* compatibility_iterator; | |
256 | public: | |
257 | wxHashTable( wxKeyType keyType = wxKEY_INTEGER, | |
258 | size_t size = wxHASH_SIZE_DEFAULT ) | |
259 | : wxHashTableBase() { Create( keyType, size ); BeginFind(); } | |
260 | wxHashTable( const wxHashTable& table ); | |
261 | ||
262 | virtual ~wxHashTable() { Destroy(); } | |
263 | ||
264 | const wxHashTable& operator=( const wxHashTable& ); | |
265 | ||
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 ); } | |
278 | ||
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 ); } | |
291 | ||
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 ); } | |
304 | ||
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); } | |
309 | ||
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; } | |
314 | Node* Next(); | |
315 | ||
316 | void Clear() { wxHashTableBase::Clear(); } | |
317 | ||
318 | size_t GetCount() const { return wxHashTableBase::GetCount(); } | |
319 | protected: | |
320 | // copy helper | |
321 | void DoCopy( const wxHashTable& copy ); | |
322 | ||
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 ); | |
326 | private: | |
327 | virtual void DoDeleteContents( wxHashTableBase_Node* node ); | |
328 | ||
329 | // current node | |
330 | Node* m_curr; | |
331 | ||
332 | // bucket the current node belongs to | |
333 | size_t m_currBucket; | |
334 | }; | |
335 | ||
336 | #else // if wxUSE_OLD_HASH_TABLE | |
337 | ||
338 | typedef wxNode wxHashTable_Node; | |
339 | ||
340 | class WXDLLIMPEXP_BASE wxHashTable : public wxObject | |
341 | { | |
342 | public: | |
343 | typedef wxNode Node; | |
344 | typedef wxNode* compatibility_iterator; | |
345 | ||
346 | int n; | |
347 | int current_position; | |
348 | wxNode *current_node; | |
349 | ||
350 | unsigned int key_type; | |
351 | wxList **hash_table; | |
352 | ||
353 | wxHashTable(int the_key_type = wxKEY_INTEGER, | |
354 | int size = wxHASH_SIZE_DEFAULT); | |
355 | virtual ~wxHashTable(); | |
356 | ||
357 | // copy ctor and assignment operator | |
358 | wxHashTable(const wxHashTable& table) : wxObject() | |
359 | { DoCopy(table); } | |
360 | wxHashTable& operator=(const wxHashTable& table) | |
361 | { Clear(); DoCopy(table); return *this; } | |
362 | ||
363 | void DoCopy(const wxHashTable& table); | |
364 | ||
365 | void Destroy(); | |
366 | ||
367 | bool Create(int the_key_type = wxKEY_INTEGER, | |
368 | int size = wxHASH_SIZE_DEFAULT); | |
369 | ||
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. | |
381 | ||
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); | |
385 | ||
386 | // key and value are the same | |
387 | void Put(long value, wxObject *object); | |
388 | void Put(const wxChar *value, wxObject *object); | |
389 | ||
390 | // key and value not the same | |
391 | wxObject *Get(long key, long value) const; | |
392 | wxObject *Get(long key, const wxChar *value) const; | |
393 | ||
394 | // key and value are the same | |
395 | wxObject *Get(long value) const; | |
396 | wxObject *Get(const wxChar *value) const; | |
397 | ||
398 | // Deletes entry and returns data if found | |
399 | wxObject *Delete(long key); | |
400 | wxObject *Delete(const wxChar *key); | |
401 | ||
402 | wxObject *Delete(long key, int value); | |
403 | wxObject *Delete(long key, const wxChar *value); | |
404 | ||
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; | |
408 | ||
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 | |
412 | ||
413 | void BeginFind(); | |
414 | Node* Next(); | |
415 | ||
416 | void DeleteContents(bool flag); | |
417 | void Clear(); | |
418 | ||
419 | // Returns number of nodes | |
420 | size_t GetCount() const { return m_count; } | |
421 | ||
422 | private: | |
423 | size_t m_count; // number of elements in the hashtable | |
424 | bool m_deleteContents; | |
425 | ||
426 | DECLARE_DYNAMIC_CLASS(wxHashTable) | |
427 | }; | |
428 | ||
429 | #endif // wxUSE_OLD_HASH_TABLE | |
430 | ||
431 | #if !wxUSE_OLD_HASH_TABLE | |
432 | ||
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 \ | |
437 | { \ | |
438 | public: \ | |
439 | hashclass(wxKeyType keyType = wxKEY_INTEGER, \ | |
440 | size_t size = wxHASH_SIZE_DEFAULT) \ | |
441 | : wxHashTableBase() { Create(keyType, size); } \ | |
442 | \ | |
443 | virtual ~hashclass() { Destroy(); } \ | |
444 | \ | |
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); } \ | |
454 | private: \ | |
455 | virtual void DoDeleteContents( wxHashTableBase_Node* node ) \ | |
456 | { delete (eltype*)node->GetData(); } \ | |
457 | \ | |
458 | DECLARE_NO_COPY_CLASS(hashclass) \ | |
459 | } | |
460 | ||
461 | #else // if wxUSE_OLD_HASH_TABLE | |
462 | ||
463 | #define _WX_DECLARE_HASH(eltype, listclass, hashclass, classexp) \ | |
464 | classexp hashclass : public wxHashTableBase \ | |
465 | { \ | |
466 | public: \ | |
467 | hashclass(wxKeyType keyType = wxKEY_INTEGER, \ | |
468 | size_t size = wxHASH_SIZE_DEFAULT) \ | |
469 | { Create(keyType, size); } \ | |
470 | \ | |
471 | virtual ~hashclass() { Destroy(); } \ | |
472 | \ | |
473 | void Put(long key, long val, eltype *data) { DoPut(key, val, data); } \ | |
474 | void Put(long key, eltype *data) { DoPut(key, key, data); } \ | |
475 | \ | |
476 | eltype *Get(long key, long value) const \ | |
477 | { \ | |
478 | wxNodeBase *node = GetNode(key, value); \ | |
479 | return node ? ((listclass::Node *)node)->GetData() : (eltype *)0; \ | |
480 | } \ | |
481 | eltype *Get(long key) const { return Get(key, key); } \ | |
482 | \ | |
483 | eltype *Delete(long key, long value) \ | |
484 | { \ | |
485 | eltype *data; \ | |
486 | \ | |
487 | wxNodeBase *node = GetNode(key, value); \ | |
488 | if ( node ) \ | |
489 | { \ | |
490 | data = ((listclass::Node *)node)->GetData(); \ | |
491 | \ | |
492 | delete node; \ | |
493 | m_count--; \ | |
494 | } \ | |
495 | else \ | |
496 | { \ | |
497 | data = (eltype *)0; \ | |
498 | } \ | |
499 | \ | |
500 | return data; \ | |
501 | } \ | |
502 | eltype *Delete(long key) { return Delete(key, key); } \ | |
503 | \ | |
504 | protected: \ | |
505 | void DoPut(long key, long value, eltype *data) \ | |
506 | { \ | |
507 | size_t slot = (size_t)abs((int)(key % (long)m_hashSize)); \ | |
508 | \ | |
509 | if ( !m_hashTable[slot] ) \ | |
510 | { \ | |
511 | m_hashTable[slot] = new listclass(m_keyType); \ | |
512 | if ( m_deleteContents ) \ | |
513 | m_hashTable[slot]->DeleteContents(true); \ | |
514 | } \ | |
515 | \ | |
516 | ((listclass *)m_hashTable[slot])->Append(value, data); \ | |
517 | m_count++; \ | |
518 | } \ | |
519 | \ | |
520 | DECLARE_NO_COPY_CLASS(hashclass) \ | |
521 | } | |
522 | ||
523 | #endif // wxUSE_OLD_HASH_TABLE | |
524 | ||
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) | |
528 | ||
529 | // and this one does exactly the same thing but should be used inside the | |
530 | // library | |
531 | #define WX_DECLARE_EXPORTED_HASH(el, list, hash) \ | |
532 | _WX_DECLARE_HASH(el, list, hash, class WXDLLEXPORT) | |
533 | ||
534 | #define WX_DECLARE_USER_EXPORTED_HASH(el, list, hash, usergoo) \ | |
535 | _WX_DECLARE_HASH(el, list, hash, class usergoo) | |
536 | ||
537 | // delete all hash elements | |
538 | // | |
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 | |
542 | // count on it)! | |
543 | #define WX_CLEAR_HASH_TABLE(hash) \ | |
544 | { \ | |
545 | (hash).BeginFind(); \ | |
546 | wxHashTable::compatibility_iterator it = (hash).Next(); \ | |
547 | while( it ) \ | |
548 | { \ | |
549 | delete it->GetData(); \ | |
550 | it = (hash).Next(); \ | |
551 | } \ | |
552 | (hash).Clear(); \ | |
553 | } | |
554 | ||
555 | #endif | |
556 | // _WX_HASH_H__ |