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