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