]> git.saurik.com Git - wxWidgets.git/blob - include/wx/hash.h
removed spurious semicolons
[wxWidgets.git] / include / wx / hash.h
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 class WXDLLIMPEXP_BASE wxHashTable : public wxObject
339 {
340 public:
341 typedef wxNode Node;
342 typedef wxNode* compatibility_iterator;
343
344 int n;
345 int current_position;
346 wxNode *current_node;
347
348 unsigned int key_type;
349 wxList **hash_table;
350
351 wxHashTable(int the_key_type = wxKEY_INTEGER,
352 int size = wxHASH_SIZE_DEFAULT);
353 virtual ~wxHashTable();
354
355 // copy ctor and assignment operator
356 wxHashTable(const wxHashTable& table) : wxObject()
357 { DoCopy(table); }
358 wxHashTable& operator=(const wxHashTable& table)
359 { Clear(); DoCopy(table); return *this; }
360
361 void DoCopy(const wxHashTable& table);
362
363 void Destroy();
364
365 bool Create(int the_key_type = wxKEY_INTEGER,
366 int size = wxHASH_SIZE_DEFAULT);
367
368 // Note that there are 2 forms of Put, Get.
369 // With a key and a value, the *value* will be checked
370 // when a collision is detected. Otherwise, if there are
371 // 2 items with a different value but the same key,
372 // we'll retrieve the WRONG ONE. So where possible,
373 // supply the required value along with the key.
374 // In fact, the value-only versions make a key, and still store
375 // the value. The use of an explicit key might be required
376 // e.g. when combining several values into one key.
377 // When doing that, it's highly likely we'll get a collision,
378 // e.g. 1 + 2 = 3, 2 + 1 = 3.
379
380 // key and value are NOT necessarily the same
381 void Put(long key, long value, wxObject *object);
382 void Put(long key, const wxChar *value, wxObject *object);
383
384 // key and value are the same
385 void Put(long value, wxObject *object);
386 void Put(const wxChar *value, wxObject *object);
387
388 // key and value not the same
389 wxObject *Get(long key, long value) const;
390 wxObject *Get(long key, const wxChar *value) const;
391
392 // key and value are the same
393 wxObject *Get(long value) const;
394 wxObject *Get(const wxChar *value) const;
395
396 // Deletes entry and returns data if found
397 wxObject *Delete(long key);
398 wxObject *Delete(const wxChar *key);
399
400 wxObject *Delete(long key, int value);
401 wxObject *Delete(long key, const wxChar *value);
402
403 // Construct your own integer key from a string, e.g. in case
404 // you need to combine it with something
405 long MakeKey(const wxChar *string) const;
406
407 // Way of iterating through whole hash table (e.g. to delete everything)
408 // Not necessary, of course, if you're only storing pointers to
409 // objects maintained separately
410
411 void BeginFind();
412 Node* Next();
413
414 void DeleteContents(bool flag);
415 void Clear();
416
417 // Returns number of nodes
418 size_t GetCount() const { return m_count; }
419
420 private:
421 size_t m_count; // number of elements in the hashtable
422 bool m_deleteContents;
423
424 DECLARE_DYNAMIC_CLASS(wxHashTable)
425 };
426
427 #endif // wxUSE_OLD_HASH_TABLE
428
429 #if !wxUSE_OLD_HASH_TABLE
430
431 // defines a new type safe hash table which stores the elements of type eltype
432 // in lists of class listclass
433 #define _WX_DECLARE_HASH(eltype, dummy, hashclass, classexp) \
434 classexp hashclass : public wxHashTableBase \
435 { \
436 public: \
437 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
438 size_t size = wxHASH_SIZE_DEFAULT) \
439 : wxHashTableBase() { Create(keyType, size); } \
440 \
441 virtual ~hashclass() { Destroy(); } \
442 \
443 void Put(long key, eltype *data) { DoPut(key, key, (void*)data); } \
444 void Put(long lhash, long key, eltype *data) \
445 { DoPut(key, lhash, (void*)data); } \
446 eltype *Get(long key) const { return (eltype*)DoGet(key, key); } \
447 eltype *Get(long lhash, long key) const \
448 { return (eltype*)DoGet(key, lhash); } \
449 eltype *Delete(long key) { return (eltype*)DoDelete(key, key); } \
450 eltype *Delete(long lhash, long key) \
451 { return (eltype*)DoDelete(key, lhash); } \
452 private: \
453 virtual void DoDeleteContents( wxHashTableBase_Node* node ) \
454 { delete (eltype*)node->GetData(); } \
455 \
456 DECLARE_NO_COPY_CLASS(hashclass) \
457 }
458
459 #else // if wxUSE_OLD_HASH_TABLE
460
461 #define _WX_DECLARE_HASH(eltype, listclass, hashclass, classexp) \
462 classexp hashclass : public wxHashTableBase \
463 { \
464 public: \
465 hashclass(wxKeyType keyType = wxKEY_INTEGER, \
466 size_t size = wxHASH_SIZE_DEFAULT) \
467 { Create(keyType, size); } \
468 \
469 virtual ~hashclass() { Destroy(); } \
470 \
471 void Put(long key, long val, eltype *data) { DoPut(key, val, data); } \
472 void Put(long key, eltype *data) { DoPut(key, key, data); } \
473 \
474 eltype *Get(long key, long value) const \
475 { \
476 wxNodeBase *node = GetNode(key, value); \
477 return node ? ((listclass::Node *)node)->GetData() : (eltype *)0; \
478 } \
479 eltype *Get(long key) const { return Get(key, key); } \
480 \
481 eltype *Delete(long key, long value) \
482 { \
483 eltype *data; \
484 \
485 wxNodeBase *node = GetNode(key, value); \
486 if ( node ) \
487 { \
488 data = ((listclass::Node *)node)->GetData(); \
489 \
490 delete node; \
491 m_count--; \
492 } \
493 else \
494 { \
495 data = (eltype *)0; \
496 } \
497 \
498 return data; \
499 } \
500 eltype *Delete(long key) { return Delete(key, key); } \
501 \
502 protected: \
503 void DoPut(long key, long value, eltype *data) \
504 { \
505 size_t slot = (size_t)abs((int)(key % (long)m_hashSize)); \
506 \
507 if ( !m_hashTable[slot] ) \
508 { \
509 m_hashTable[slot] = new listclass(m_keyType); \
510 if ( m_deleteContents ) \
511 m_hashTable[slot]->DeleteContents(true); \
512 } \
513 \
514 ((listclass *)m_hashTable[slot])->Append(value, data); \
515 m_count++; \
516 } \
517 \
518 DECLARE_NO_COPY_CLASS(hashclass) \
519 }
520
521 #endif // wxUSE_OLD_HASH_TABLE
522
523 // this macro is to be used in the user code
524 #define WX_DECLARE_HASH(el, list, hash) \
525 _WX_DECLARE_HASH(el, list, hash, class)
526
527 // and this one does exactly the same thing but should be used inside the
528 // library
529 #define WX_DECLARE_EXPORTED_HASH(el, list, hash) \
530 _WX_DECLARE_HASH(el, list, hash, class WXDLLEXPORT)
531
532 #define WX_DECLARE_USER_EXPORTED_HASH(el, list, hash, usergoo) \
533 _WX_DECLARE_HASH(el, list, hash, class usergoo)
534
535 // delete all hash elements
536 //
537 // NB: the class declaration of the hash elements must be visible from the
538 // place where you use this macro, otherwise the proper destructor may not
539 // be called (a decent compiler should give a warning about it, but don't
540 // count on it)!
541 #define WX_CLEAR_HASH_TABLE(hash) \
542 { \
543 (hash).BeginFind(); \
544 wxHashTable::compatibility_iterator it = (hash).Next(); \
545 while( it ) \
546 { \
547 delete it->GetData(); \
548 it = (hash).Next(); \
549 } \
550 (hash).Clear(); \
551 }
552
553 #endif
554 // _WX_HASH_H__