]> git.saurik.com Git - wxWidgets.git/blob - include/wx/hashmap.h
In the object destructor, Disassociate the object from its Cocoa counterpart
[wxWidgets.git] / include / wx / hashmap.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: hashmap.h
3 // Purpose: wxHashMap class
4 // Author: Mattia Barbon
5 // Modified by:
6 // Created: 29/01/2002
7 // RCS-ID: $Id$
8 // Copyright: (c) Mattia Barbon
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_HASHMAP_H_
13 #define _WX_HASHMAP_H_
14
15 #if defined(__GNUG__) && !defined(__APPLE__)
16 #pragma interface "hashmap.h"
17 #endif
18
19 #include "wx/string.h"
20
21 #include <stddef.h> // for ptrdiff_t
22
23 #ifdef __WXWINCE__
24 typedef int ptrdiff_t;
25 #endif
26
27 // private
28 struct WXDLLIMPEXP_BASE _wxHashTable_NodeBase
29 {
30 _wxHashTable_NodeBase() : m_nxt(0) {}
31
32 _wxHashTable_NodeBase* m_nxt;
33
34 // Cannot do this:
35 // DECLARE_NO_COPY_CLASS(_wxHashTable_NodeBase)
36 // without rewriting the macros, which require a public copy constructor.
37 };
38
39 // private
40 class WXDLLIMPEXP_BASE _wxHashTableBase2
41 {
42 public:
43 typedef void (*NodeDtor)(_wxHashTable_NodeBase*);
44 typedef unsigned long (*BucketFromNode)(_wxHashTableBase2*,_wxHashTable_NodeBase*);
45 typedef _wxHashTable_NodeBase* (*ProcessNode)(_wxHashTable_NodeBase*);
46 protected:
47 static _wxHashTable_NodeBase* DummyProcessNode(_wxHashTable_NodeBase* node);
48 static void DeleteNodes( size_t buckets, _wxHashTable_NodeBase** table,
49 NodeDtor dtor );
50 static _wxHashTable_NodeBase* GetFirstNode( size_t buckets,
51 _wxHashTable_NodeBase** table )
52 {
53 for( size_t i = 0; i < buckets; ++i )
54 if( table[i] )
55 return table[i];
56 return 0;
57 }
58
59 // as static const unsigned prime_count = 31 but works with all compilers
60 enum { prime_count = 31 };
61 static const unsigned long ms_primes[prime_count];
62
63 // returns the first prime in ms_primes greater than n
64 static unsigned long GetNextPrime( unsigned long n );
65
66 // returns the first prime in ms_primes smaller than n
67 // ( or ms_primes[0] if n is very small )
68 static unsigned long GetPreviousPrime( unsigned long n );
69
70 static void CopyHashTable( _wxHashTable_NodeBase** srcTable,
71 size_t srcBuckets, _wxHashTableBase2* dst,
72 _wxHashTable_NodeBase** dstTable,
73 BucketFromNode func, ProcessNode proc );
74
75 static void** AllocTable( size_t sz )
76 {
77 #ifdef __WXWINCE__
78 return (void **)malloc(sz * sizeof(void*));
79 #else
80 return (void **)calloc(sz, sizeof(void*));
81 #endif
82 }
83 };
84
85 #define _WX_DECLARE_HASHTABLE( VALUE_T, KEY_T, HASH_T, KEY_EX_T, KEY_EQ_T, CLASSNAME, CLASSEXP, SHOULD_GROW, SHOULD_SHRINK ) \
86 CLASSEXP CLASSNAME : protected _wxHashTableBase2 \
87 { \
88 public: \
89 typedef KEY_T key_type; \
90 typedef VALUE_T value_type; \
91 typedef HASH_T hasher; \
92 typedef KEY_EQ_T key_equal; \
93 \
94 typedef size_t size_type; \
95 typedef ptrdiff_t difference_type; \
96 typedef value_type* pointer; \
97 typedef const value_type* const_pointer; \
98 typedef value_type& reference; \
99 typedef const value_type& const_reference; \
100 /* should these be protected? */ \
101 typedef const KEY_T const_key_type; \
102 typedef const VALUE_T const_mapped_type; \
103 public: \
104 struct Node; \
105 typedef KEY_EX_T key_extractor; \
106 typedef CLASSNAME Self; \
107 protected: \
108 Node** m_table; \
109 size_t m_tableBuckets; \
110 size_t m_items; \
111 hasher m_hasher; \
112 key_equal m_equals; \
113 key_extractor m_getKey; \
114 public: \
115 struct Node:public _wxHashTable_NodeBase \
116 { \
117 public: \
118 Node( const value_type& value ) \
119 : m_value( value ) {} \
120 Node* m_next() { return (Node*)this->m_nxt; } \
121 \
122 value_type m_value; \
123 }; \
124 \
125 struct Iterator; \
126 friend struct Iterator; \
127 protected: \
128 static void DeleteNode( _wxHashTable_NodeBase* node ) \
129 { \
130 delete (Node*)node; \
131 } \
132 public: \
133 /* */ \
134 /* forward iterator */ \
135 /* */ \
136 struct Iterator \
137 { \
138 Node* m_node; \
139 Self* m_ht; \
140 \
141 Iterator() : m_node(0), m_ht(0) {} \
142 Iterator( Node* node, const Self* ht ) \
143 : m_node(node), m_ht((Self*)ht) {} \
144 bool operator ==( const Iterator& it ) const \
145 { return m_node == it.m_node; } \
146 bool operator !=( const Iterator& it ) const \
147 { return m_node != it.m_node; } \
148 protected: \
149 Node* GetNextNode() \
150 { \
151 size_type bucket = GetBucketForNode(m_ht,m_node); \
152 for( size_type i = bucket + 1; i < m_ht->m_tableBuckets; ++i ) \
153 { \
154 if( m_ht->m_table[i] ) \
155 return m_ht->m_table[i]; \
156 } \
157 return 0; \
158 } \
159 \
160 void PlusPlus() \
161 { \
162 Node* next = m_node->m_next(); \
163 m_node = next ? next : GetNextNode(); \
164 } \
165 }; \
166 \
167 public: \
168 struct iterator:public Iterator \
169 { \
170 iterator() : Iterator() {} \
171 iterator( Node* node, Self* ht ) : Iterator( node, ht ) {} \
172 iterator& operator++() { PlusPlus(); return *this; } \
173 iterator operator++(int) { iterator it=*this;PlusPlus();return it; } \
174 reference operator *() const { return m_node->m_value; } \
175 pointer operator ->() const { return &(m_node->m_value); } \
176 }; \
177 \
178 struct const_iterator:public Iterator \
179 { \
180 const_iterator() : Iterator() {} \
181 const_iterator( Node* node, const Self* ht ) \
182 : Iterator( node, (Self*)ht ) {} \
183 const_iterator& operator++() { PlusPlus();return *this; } \
184 const_iterator operator++(int) { const_iterator it=*this;PlusPlus();return it; } \
185 const_reference operator *() const { return m_node->m_value; } \
186 const_pointer operator ->() const { return &(m_node->m_value); } \
187 }; \
188 \
189 CLASSNAME( size_type sz = 10, const hasher& hfun = hasher(), \
190 const key_equal& k_eq = key_equal(), \
191 const key_extractor& k_ex = key_extractor() ) \
192 : m_tableBuckets( GetNextPrime( (unsigned long) sz ) ), \
193 m_items( 0 ), \
194 m_hasher( hfun ), \
195 m_equals( k_eq ), \
196 m_getKey( k_ex ) \
197 { \
198 m_table = (Node**)AllocTable( m_tableBuckets ); \
199 } \
200 \
201 CLASSNAME( const Self& ht ) \
202 : m_table( 0 ), \
203 m_tableBuckets( 0 ), \
204 m_items( ht.m_items ), \
205 m_hasher( ht.m_hasher ), \
206 m_equals( ht.m_equals ), \
207 m_getKey( ht.m_getKey ) \
208 { \
209 HashCopy( ht ); \
210 } \
211 \
212 const Self& operator=( const Self& ht ) \
213 { \
214 clear(); \
215 m_hasher = ht.m_hasher; \
216 m_equals = ht.m_equals; \
217 m_getKey = ht.m_getKey; \
218 m_items = ht.m_items; \
219 HashCopy( ht ); \
220 return *this; \
221 } \
222 \
223 ~CLASSNAME() \
224 { \
225 clear(); \
226 \
227 free(m_table); \
228 } \
229 \
230 hasher hash_funct() { return m_hasher; } \
231 key_equal key_eq() { return m_equals; } \
232 \
233 /* removes all elements from the hash table, but does not */ \
234 /* shrink it ( perhaps it should ) */ \
235 void clear() \
236 { \
237 DeleteNodes( m_tableBuckets, (_wxHashTable_NodeBase**)m_table, \
238 DeleteNode ); \
239 m_items = 0; \
240 } \
241 \
242 size_type size() const { return m_items; } \
243 size_type max_size() const { return size_type(-1); } \
244 bool empty() const { return size() == 0; } \
245 \
246 const_iterator end() const { return const_iterator( 0, this ); } \
247 iterator end() { return iterator( 0, this ); } \
248 const_iterator begin() const \
249 { return const_iterator( (Node*)GetFirstNode( m_tableBuckets, (_wxHashTable_NodeBase**)m_table ), this ); }; \
250 iterator begin() \
251 { return iterator( (Node*)GetFirstNode( m_tableBuckets, (_wxHashTable_NodeBase**)m_table ), this ); }; \
252 \
253 size_type erase( const const_key_type& key ) \
254 { \
255 Node** node = GetNodePtr( key ); \
256 \
257 if( !node ) \
258 return 0; \
259 \
260 --m_items; \
261 Node* temp = (*node)->m_next(); \
262 delete *node; \
263 (*node) = temp; \
264 if( SHOULD_SHRINK( m_tableBuckets, m_items ) ) \
265 ResizeTable( GetPreviousPrime( (unsigned long) m_tableBuckets ) - 1 ); \
266 return 1; \
267 } \
268 \
269 protected: \
270 static size_type GetBucketForNode( Self* ht, Node* node ) \
271 { \
272 return ht->m_hasher( ht->m_getKey( node->m_value ) ) \
273 % ht->m_tableBuckets; \
274 } \
275 static Node* CopyNode( Node* node ) { return new Node( *node ); } \
276 \
277 Node* GetOrCreateNode( const value_type& value ) \
278 { \
279 const const_key_type& key = m_getKey( value ); \
280 size_t bucket = m_hasher( key ) % m_tableBuckets; \
281 Node* node = m_table[bucket]; \
282 \
283 while( node ) \
284 { \
285 if( m_equals( m_getKey( node->m_value ), key ) ) \
286 return node; \
287 node = node->m_next(); \
288 } \
289 return CreateNode( value , bucket); \
290 }\
291 Node * CreateNode( const value_type& value, size_t bucket ) \
292 {\
293 Node* node = new Node( value ); \
294 node->m_nxt = m_table[bucket]; \
295 m_table[bucket] = node; \
296 \
297 /* must be after the node is inserted */ \
298 ++m_items; \
299 if( SHOULD_GROW( m_tableBuckets, m_items ) ) \
300 ResizeTable( m_tableBuckets ); \
301 \
302 return node; \
303 } \
304 void CreateNode( const value_type& value ) \
305 {\
306 CreateNode(value, m_hasher( m_getKey(value) ) % m_tableBuckets ); \
307 }\
308 \
309 /* returns NULL if not found */ \
310 Node** GetNodePtr( const const_key_type& key ) const \
311 { \
312 unsigned long hash = m_hasher( key ); \
313 Node** node = &m_table[hash % m_tableBuckets]; \
314 \
315 while( *node ) \
316 { \
317 if( m_equals( m_getKey( (*node)->m_value ), key ) ) \
318 return node; \
319 node = (Node**)&(*node)->m_nxt; \
320 } \
321 \
322 return 0; \
323 } \
324 \
325 /* returns NULL if not found */ \
326 /* expressing it in terms of GetNodePtr is 5-8% slower :-( */ \
327 Node* GetNode( const const_key_type& key ) const \
328 { \
329 unsigned long hash = m_hasher( key ); \
330 Node* node = m_table[hash % m_tableBuckets]; \
331 \
332 while( node ) \
333 { \
334 if( m_equals( m_getKey( node->m_value ), key ) ) \
335 return node; \
336 node = node->m_next(); \
337 } \
338 \
339 return 0; \
340 } \
341 \
342 void ResizeTable( size_t newSize ) \
343 { \
344 newSize = GetNextPrime( (unsigned long)newSize ); \
345 Node** srcTable = m_table; \
346 size_t srcBuckets = m_tableBuckets; \
347 m_table = (Node**)AllocTable( newSize ); \
348 m_tableBuckets = newSize; \
349 \
350 CopyHashTable( (_wxHashTable_NodeBase**)srcTable, srcBuckets, \
351 this, (_wxHashTable_NodeBase**)m_table, \
352 (BucketFromNode)GetBucketForNode,\
353 (ProcessNode)&DummyProcessNode ); \
354 free(srcTable); \
355 } \
356 \
357 /* this must be called _after_ m_table has been cleaned */ \
358 void HashCopy( const Self& ht ) \
359 { \
360 ResizeTable( ht.size() ); \
361 CopyHashTable( (_wxHashTable_NodeBase**)ht.m_table, ht.m_tableBuckets,\
362 (_wxHashTableBase2*)this, \
363 (_wxHashTable_NodeBase**)m_table, \
364 (BucketFromNode)GetBucketForNode, \
365 (ProcessNode)CopyNode ); \
366 } \
367 };
368
369 // defines an STL-like pair class CLASSNAME storing two fields: first of type
370 // KEY_T and second of type VALUE_T
371 #define _WX_DECLARE_PAIR( KEY_T, VALUE_T, CLASSNAME, CLASSEXP ) \
372 CLASSEXP CLASSNAME \
373 { \
374 public: \
375 typedef KEY_T t1; \
376 typedef VALUE_T t2; \
377 typedef const KEY_T const_t1; \
378 typedef const VALUE_T const_t2; \
379 \
380 CLASSNAME( const const_t1& f, const const_t2& s ):first(t1(f)),second(t2(s)) {} \
381 CLASSNAME( const const_t1& f ):first(t1(f)),second(t2()) {} \
382 \
383 t1 first; \
384 t2 second; \
385 };
386
387 // defines the class CLASSNAME returning the key part (of type KEY_T) from a
388 // pair of type PAIR_T
389 #define _WX_DECLARE_HASH_MAP_KEY_EX( KEY_T, PAIR_T, CLASSNAME, CLASSEXP ) \
390 CLASSEXP CLASSNAME \
391 { \
392 typedef KEY_T key_type; \
393 typedef PAIR_T pair_type; \
394 typedef const key_type const_key_type; \
395 typedef const pair_type const_pair_type; \
396 typedef const_key_type& const_key_reference; \
397 typedef const_pair_type& const_pair_reference; \
398 public: \
399 CLASSNAME() { } \
400 const_key_reference operator()( const_pair_reference pair ) const { return pair.first; }\
401 \
402 /* the dummy assignment operator is needed to suppress compiler */ \
403 /* warnings from hash table class' operator=(): gcc complains about */ \
404 /* "statement with no effect" without it */ \
405 CLASSNAME& operator=(const CLASSNAME&) { return *this; } \
406 };
407
408 // grow/shrink predicates
409 inline bool never_grow( size_t, size_t ) { return FALSE; }
410 inline bool never_shrink( size_t, size_t ) { return FALSE; }
411 inline bool grow_lf70( size_t buckets, size_t items )
412 {
413 return float(items)/float(buckets) >= 0.85;
414 }
415
416 // ----------------------------------------------------------------------------
417 // hashing and comparison functors
418 // ----------------------------------------------------------------------------
419
420 // NB: implementation detail: all of these classes must have dummy assignment
421 // operators to suppress warnings about "statement with no effect" from gcc
422 // in the hash table class assignment operator (where they're assigned)
423
424 // integer types
425 class WXDLLIMPEXP_BASE wxIntegerHash
426 {
427 public:
428 wxIntegerHash() { }
429 unsigned long operator()( long x ) const { return (unsigned long)x; }
430 unsigned long operator()( unsigned long x ) const { return x; }
431 unsigned long operator()( int x ) const { return (unsigned long)x; }
432 unsigned long operator()( unsigned int x ) const { return x; }
433 unsigned long operator()( short x ) const { return (unsigned long)x; }
434 unsigned long operator()( unsigned short x ) const { return x; }
435
436 wxIntegerHash& operator=(const wxIntegerHash&) { return *this; }
437 };
438
439 class WXDLLIMPEXP_BASE wxIntegerEqual
440 {
441 public:
442 wxIntegerEqual() { }
443 bool operator()( long a, long b ) const { return a == b; }
444 bool operator()( unsigned long a, unsigned long b ) const { return a == b; }
445 bool operator()( int a, int b ) const { return a == b; }
446 bool operator()( unsigned int a, unsigned int b ) const { return a == b; }
447 bool operator()( short a, short b ) const { return a == b; }
448 bool operator()( unsigned short a, unsigned short b ) const { return a == b; }
449
450 wxIntegerEqual& operator=(const wxIntegerEqual&) { return *this; }
451 };
452
453 // pointers
454 class WXDLLIMPEXP_BASE wxPointerHash
455 {
456 public:
457 wxPointerHash() { }
458
459 // TODO: this might not work well on architectures with 64 bit pointers but
460 // 32 bit longs, we should use % ULONG_MAX there
461 unsigned long operator()( const void* k ) const { return (unsigned long)wxPtrToULong(k); }
462
463 wxPointerHash& operator=(const wxPointerHash&) { return *this; }
464 };
465
466 class WXDLLIMPEXP_BASE wxPointerEqual
467 {
468 public:
469 wxPointerEqual() { }
470 bool operator()( const void* a, const void* b ) const { return a == b; }
471
472 wxPointerEqual& operator=(const wxPointerEqual&) { return *this; }
473 };
474
475 // wxString, char*, wxChar*
476 class WXDLLIMPEXP_BASE wxStringHash
477 {
478 public:
479 wxStringHash() {}
480 unsigned long operator()( const wxString& x ) const
481 { return wxCharStringHash( x.c_str() ); }
482 unsigned long operator()( const wxChar* x ) const
483 { return wxCharStringHash( x ); }
484 static unsigned long wxCharStringHash( const wxChar* );
485 #if wxUSE_UNICODE
486 unsigned long operator()( const char* x ) const
487 { return charStringHash( x ); }
488 static unsigned long charStringHash( const char* );
489 #endif // wxUSE_UNICODE
490
491 wxStringHash& operator=(const wxStringHash&) { return *this; }
492 };
493
494 class WXDLLIMPEXP_BASE wxStringEqual
495 {
496 public:
497 wxStringEqual() {}
498 bool operator()( const wxString& a, const wxString& b ) const
499 { return a == b; }
500 bool operator()( const wxChar* a, const wxChar* b ) const
501 { return wxStrcmp( a, b ) == 0; }
502 #if wxUSE_UNICODE
503 bool operator()( const char* a, const char* b ) const
504 { return strcmp( a, b ) == 0; }
505 #endif // wxUSE_UNICODE
506
507 wxStringEqual& operator=(const wxStringEqual&) { return *this; }
508 };
509
510 #define _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, CLASSEXP ) \
511 _WX_DECLARE_PAIR( KEY_T, VALUE_T, CLASSNAME##_wxImplementation_Pair, CLASSEXP ) \
512 _WX_DECLARE_HASH_MAP_KEY_EX( KEY_T, CLASSNAME##_wxImplementation_Pair, CLASSNAME##_wxImplementation_KeyEx, CLASSEXP ) \
513 _WX_DECLARE_HASHTABLE( CLASSNAME##_wxImplementation_Pair, KEY_T, HASH_T, CLASSNAME##_wxImplementation_KeyEx, KEY_EQ_T, CLASSNAME##_wxImplementation_HashTable, CLASSEXP, grow_lf70, never_shrink ) \
514 CLASSEXP CLASSNAME:public CLASSNAME##_wxImplementation_HashTable \
515 { \
516 public: \
517 typedef VALUE_T mapped_type; \
518 \
519 CLASSNAME( size_type hint = 100, hasher hf = hasher(), key_equal eq = key_equal() ) \
520 : CLASSNAME##_wxImplementation_HashTable( hint, hf, eq, CLASSNAME##_wxImplementation_KeyEx() ) {} \
521 \
522 mapped_type& operator[]( const const_key_type& key ) \
523 { \
524 return GetOrCreateNode( CLASSNAME##_wxImplementation_Pair( key ) )->m_value.second; \
525 } \
526 \
527 const_iterator find( const const_key_type& key ) const \
528 { \
529 return const_iterator( GetNode( key ), this ); \
530 } \
531 \
532 iterator find( const const_key_type& key ) \
533 { \
534 return iterator( GetNode( key ), this ); \
535 } \
536 \
537 void insert( const value_type& v ) { (*this)[v.first] = v.second; } \
538 \
539 size_type erase( const key_type& k ) \
540 { return CLASSNAME##_wxImplementation_HashTable::erase( k ); } \
541 void erase( const iterator& it ) { erase( it->first ); } \
542 void erase( const const_iterator& it ) { erase( it->first ); } \
543 \
544 /* count() == 0 | 1 */ \
545 size_type count( const const_key_type& key ) \
546 { return GetNode( key ) ? 1 : 0; } \
547 }
548
549 // these macros are to be used in the user code
550 #define WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME) \
551 _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, class )
552
553 #define WX_DECLARE_STRING_HASH_MAP( VALUE_T, CLASSNAME ) \
554 _WX_DECLARE_HASH_MAP( wxString, VALUE_T, wxStringHash, wxStringEqual, \
555 CLASSNAME, class )
556
557 #define WX_DECLARE_VOIDPTR_HASH_MAP( VALUE_T, CLASSNAME ) \
558 _WX_DECLARE_HASH_MAP( void*, VALUE_T, wxPointerHash, wxPointerEqual, \
559 CLASSNAME, class )
560
561 // and these do exactly the same thing but should be used inside the
562 // library
563 #define WX_DECLARE_EXPORTED_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME) \
564 _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, class WXDLLEXPORT )
565
566 #define WX_DECLARE_EXPORTED_STRING_HASH_MAP( VALUE_T, CLASSNAME ) \
567 _WX_DECLARE_HASH_MAP( wxString, VALUE_T, wxStringHash, wxStringEqual, \
568 CLASSNAME, class WXDLLEXPORT )
569
570 #define WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP( VALUE_T, CLASSNAME ) \
571 _WX_DECLARE_HASH_MAP( void*, VALUE_T, wxPointerHash, wxPointerEqual, \
572 CLASSNAME, class WXDLLEXPORT )
573
574 // delete all hash elements
575 //
576 // NB: the class declaration of the hash elements must be visible from the
577 // place where you use this macro, otherwise the proper destructor may not
578 // be called (a decent compiler should give a warning about it, but don't
579 // count on it)!
580 #define WX_CLEAR_HASH_MAP(type, hashmap) \
581 { \
582 type::iterator it, en; \
583 for( it = (hashmap).begin(), en = (hashmap).end(); it != en; ++it ) \
584 delete it->second; \
585 (hashmap).clear(); \
586 }
587
588 #endif // _WX_HASHMAP_H_
589