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