1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHashMap class
4 // Author: Mattia Barbon
8 // Copyright: (c) Mattia Barbon
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_HASHMAP_H_
13 #define _WX_HASHMAP_H_
15 #if defined(__GNUG__) && !defined(__APPLE__)
16 #pragma interface "hashmap.h"
19 #include "wx/string.h"
22 struct WXDLLEXPORT _wxHashTable_NodeBase
24 _wxHashTable_NodeBase() : m_nxt(0) {}
26 _wxHashTable_NodeBase
* m_nxt
;
30 class WXDLLEXPORT _wxHashTableBase2
33 typedef void (*NodeDtor
)(_wxHashTable_NodeBase
*);
34 typedef unsigned long (*BucketFromNode
)(_wxHashTableBase2
*,_wxHashTable_NodeBase
*);
35 typedef _wxHashTable_NodeBase
* (*ProcessNode
)(_wxHashTable_NodeBase
*);
37 static _wxHashTable_NodeBase
* DummyProcessNode(_wxHashTable_NodeBase
* node
);
38 static void DeleteNodes( size_t buckets
, _wxHashTable_NodeBase
** table
,
40 static _wxHashTable_NodeBase
* GetFirstNode( size_t buckets
,
41 _wxHashTable_NodeBase
** table
)
43 for( size_t i
= 0; i
< buckets
; ++i
)
49 // as static const unsigned prime_count = 31 but works with all compilers
50 enum { prime_count
= 31 };
51 static const unsigned long ms_primes
[prime_count
];
53 // returns the first prime in ms_primes greater than n
54 static unsigned long GetNextPrime( unsigned long n
);
56 // returns the first prime in ms_primes smaller than n
57 // ( or ms_primes[0] if n is very small )
58 static unsigned long GetPreviousPrime( unsigned long n
);
60 static void CopyHashTable( _wxHashTable_NodeBase
** srcTable
,
61 size_t srcBuckets
, _wxHashTableBase2
* dst
,
62 _wxHashTable_NodeBase
** dstTable
,
63 BucketFromNode func
, ProcessNode proc
);
65 static void** AllocTable( size_t sz
)
67 return (void **)calloc(sz
, sizeof(void*));
71 #define _WX_DECLARE_HASHTABLE( VALUE_T, KEY_T, HASH_T, KEY_EX_T, KEY_EQ_T, CLASSNAME, CLASSEXP, SHOULD_GROW, SHOULD_SHRINK ) \
72 CLASSEXP CLASSNAME : protected _wxHashTableBase2 \
75 typedef KEY_T key_type; \
76 typedef VALUE_T value_type; \
77 typedef HASH_T hasher; \
78 typedef KEY_EQ_T key_equal; \
80 typedef size_t size_type; \
81 typedef ptrdiff_t difference_type; \
82 typedef value_type* pointer; \
83 typedef const value_type* const_pointer; \
84 typedef value_type& reference; \
85 typedef const value_type& const_reference; \
86 /* should these be protected? */ \
87 typedef const KEY_T const_key_type; \
88 typedef const VALUE_T const_mapped_type; \
91 typedef KEY_EX_T key_extractor; \
92 typedef CLASSNAME Self; \
95 size_t m_tableBuckets; \
99 key_extractor m_getKey; \
101 struct Node:public _wxHashTable_NodeBase \
104 Node( const value_type& value ) \
105 : m_value( value ) {} \
106 Node* m_next() { return (Node*)this->m_nxt; } \
108 value_type m_value; \
112 friend struct Iterator; \
114 static void DeleteNode( _wxHashTable_NodeBase* node ) \
116 delete (Node*)node; \
120 /* forward iterator */ \
127 Iterator() : m_node(0), m_ht(0) {} \
128 Iterator( Node* node, const Self* ht ) \
129 : m_node(node), m_ht((Self*)ht) {} \
130 bool operator ==( const Iterator& it ) const \
131 { return m_node == it.m_node; } \
132 bool operator !=( const Iterator& it ) const \
133 { return m_node != it.m_node; } \
135 Node* GetNextNode() \
137 size_type bucket = GetBucketForNode(m_ht,m_node); \
138 for( size_type i = bucket + 1; i < m_ht->m_tableBuckets; ++i ) \
140 if( m_ht->m_table[i] ) \
141 return m_ht->m_table[i]; \
148 Node* next = m_node->m_next(); \
149 m_node = next ? next : GetNextNode(); \
154 struct iterator:public Iterator \
156 iterator() : Iterator() {} \
157 iterator( Node* node, Self* ht ) : Iterator( node, ht ) {} \
158 iterator& operator++() { PlusPlus(); return *this; } \
159 iterator operator++(int) { iterator it=*this;PlusPlus();return it; } \
160 reference operator *() const { return m_node->m_value; } \
161 pointer operator ->() const { return &(m_node->m_value); } \
164 struct const_iterator:public Iterator \
166 const_iterator() : Iterator() {} \
167 const_iterator( Node* node, const Self* ht ) \
168 : Iterator( node, (Self*)ht ) {} \
169 const_iterator& operator++() { PlusPlus();return *this; } \
170 const_iterator operator++(int) { const_iterator it=*this;PlusPlus();return it; } \
171 const_reference operator *() const { return m_node->m_value; } \
172 const_pointer operator ->() const { return &(m_node->m_value); } \
175 CLASSNAME( size_type sz = 10, const hasher& hfun = hasher(), \
176 const key_equal& k_eq = key_equal(), \
177 const key_extractor& k_ex = key_extractor() ) \
178 : m_tableBuckets( GetNextPrime( sz ) ), \
184 m_table = (Node**)AllocTable( m_tableBuckets ); \
187 CLASSNAME( const Self& ht ) \
189 m_tableBuckets( 0 ), \
190 m_items( ht.m_items ), \
191 m_hasher( ht.m_hasher ), \
192 m_equals( ht.m_equals ), \
193 m_getKey( ht.m_getKey ) \
198 const Self& operator=( const Self& ht ) \
201 m_hasher = ht.m_hasher; \
202 m_equals = ht.m_equals; \
203 m_getKey = ht.m_getKey; \
204 m_items = ht.m_items; \
216 hasher hash_funct() { return m_hasher; } \
217 key_equal key_eq() { return m_equals; } \
219 /* removes all elements from the hash table, but does not */ \
220 /* shrink it ( perhaps it should ) */ \
223 DeleteNodes( m_tableBuckets, (_wxHashTable_NodeBase**)m_table, \
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; } \
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 ); }; \
237 { return iterator( (Node*)GetFirstNode( m_tableBuckets, (_wxHashTable_NodeBase**)m_table ), this ); }; \
239 size_type erase( const const_key_type& key ) \
241 Node** node = GetNodePtr( key ); \
247 Node* temp = (*node)->m_next(); \
250 if( SHOULD_SHRINK( m_tableBuckets, m_items ) ) \
251 ResizeTable( GetPreviousPrime( m_tableBuckets ) - 1 ); \
256 static size_type GetBucketForNode( Self* ht, Node* node ) \
258 return ht->m_hasher( ht->m_getKey( node->m_value ) ) \
259 % ht->m_tableBuckets; \
261 static Node* CopyNode( Node* node ) { return new Node( *node ); } \
263 Node* GetOrCreateNode( const value_type& value ) \
265 const const_key_type& key = m_getKey( value ); \
266 size_t bucket = m_hasher( key ) % m_tableBuckets; \
267 Node* node = m_table[bucket]; \
271 if( m_equals( m_getKey( node->m_value ), key ) ) \
273 node = node->m_next(); \
276 node = new Node( value ); \
277 node->m_nxt = m_table[bucket]; \
278 m_table[bucket] = node; \
280 /* must be after the node is inserted */ \
282 if( SHOULD_GROW( m_tableBuckets, m_items ) ) \
283 ResizeTable( m_tableBuckets ); \
288 /* returns NULL if not found */ \
289 Node** GetNodePtr( const const_key_type& key ) const \
291 unsigned long hash = m_hasher( key ); \
292 Node** node = &m_table[hash % m_tableBuckets]; \
296 if( m_equals( m_getKey( (*node)->m_value ), key ) ) \
298 node = (Node**)&(*node)->m_nxt; \
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 \
308 unsigned long hash = m_hasher( key ); \
309 Node* node = m_table[hash % m_tableBuckets]; \
313 if( m_equals( m_getKey( node->m_value ), key ) ) \
315 node = node->m_next(); \
321 void ResizeTable( size_t newSize ) \
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; \
329 CopyHashTable( (_wxHashTable_NodeBase**)srcTable, srcBuckets, \
330 this, (_wxHashTable_NodeBase**)m_table, \
331 (BucketFromNode)&GetBucketForNode,\
332 (ProcessNode)&DummyProcessNode ); \
336 /* this must be called _after_ m_table has been cleaned */ \
337 void HashCopy( const Self& ht ) \
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 ); \
348 // defines an STL-like pair class CLASSNAME storing two fields: first of type
349 // KEY_T and second of type VALUE_T
350 #define _WX_DECLARE_PAIR( KEY_T, VALUE_T, CLASSNAME, CLASSEXP ) \
355 typedef VALUE_T t2; \
356 typedef const KEY_T const_t1; \
357 typedef const VALUE_T const_t2; \
359 CLASSNAME( const const_t1& f, const const_t2& s ):first(t1(f)),second(t2(s)) {} \
360 CLASSNAME( const const_t1& f ):first(t1(f)),second(t2()) {} \
366 // defines the class CLASSNAME returning the key part (of type KEY_T) from a
367 // pair of type PAIR_T
368 #define _WX_DECLARE_HASH_MAP_KEY_EX( KEY_T, PAIR_T, CLASSNAME, CLASSEXP ) \
371 typedef KEY_T key_type; \
372 typedef PAIR_T pair_type; \
373 typedef const key_type const_key_type; \
374 typedef const pair_type const_pair_type; \
375 typedef const_key_type& const_key_reference; \
376 typedef const_pair_type& const_pair_reference; \
379 const_key_reference operator()( const_pair_reference pair ) const { return pair.first; }\
381 /* the dummy assignment operator is needed to suppress compiler */ \
382 /* warnings from hash table class' operator=(): gcc complains about */ \
383 /* "statement with no effect" without it */ \
384 CLASSNAME& operator=(const CLASSNAME&) { return *this; } \
387 // grow/shrink predicates
388 inline bool never_grow( size_t, size_t ) { return FALSE
; }
389 inline bool never_shrink( size_t, size_t ) { return FALSE
; }
390 inline bool grow_lf70( size_t buckets
, size_t items
)
392 return float(items
)/float(buckets
) >= 0.85;
395 // ----------------------------------------------------------------------------
396 // hashing and comparison functors
397 // ----------------------------------------------------------------------------
399 // NB: implementation detail: all of these classes must have dummy assignment
400 // operators to suppress warnings about "statement with no effect" from gcc
401 // in the hash table class assignment operator (where they're assigned)
404 class WXDLLEXPORT wxIntegerHash
408 unsigned long operator()( long x
) const { return (unsigned long)x
; }
409 unsigned long operator()( unsigned long x
) const { return x
; }
410 unsigned long operator()( int x
) const { return (unsigned long)x
; }
411 unsigned long operator()( unsigned int x
) const { return x
; }
412 unsigned long operator()( short x
) const { return (unsigned long)x
; }
413 unsigned long operator()( unsigned short x
) const { return x
; }
415 wxIntegerHash
& operator=(const wxIntegerHash
&) { return *this; }
418 class WXDLLEXPORT wxIntegerEqual
422 bool operator()( long a
, long b
) const { return a
== b
; }
423 bool operator()( unsigned long a
, unsigned long b
) const { return a
== b
; }
424 bool operator()( int a
, int b
) const { return a
== b
; }
425 bool operator()( unsigned int a
, unsigned int b
) const { return a
== b
; }
426 bool operator()( short a
, short b
) const { return a
== b
; }
427 bool operator()( unsigned short a
, unsigned short b
) const { return a
== b
; }
429 wxIntegerEqual
& operator=(const wxIntegerEqual
&) { return *this; }
433 class WXDLLEXPORT wxPointerHash
438 // TODO: this might not work well on architectures with 64 bit pointers but
439 // 32 bit longs, we should use % ULONG_MAX there
440 unsigned long operator()( const void* k
) const { return (unsigned long)k
; }
442 wxPointerHash
& operator=(const wxPointerHash
&) { return *this; }
445 class WXDLLEXPORT wxPointerEqual
449 bool operator()( const void* a
, const void* b
) const { return a
== b
; }
451 wxPointerEqual
& operator=(const wxPointerEqual
&) { return *this; }
454 // wxString, char*, wxChar*
455 class WXDLLEXPORT wxStringHash
459 unsigned long operator()( const wxString
& x
) const
460 { return wxCharStringHash( x
.c_str() ); }
461 unsigned long operator()( const wxChar
* x
) const
462 { return wxCharStringHash( x
); }
463 static unsigned long wxCharStringHash( const wxChar
* );
465 unsigned long operator()( const char* x
) const
466 { return charStringHash( x
); }
467 static unsigned long charStringHash( const char* );
468 #endif // wxUSE_UNICODE
470 wxStringHash
& operator=(const wxStringHash
&) { return *this; }
473 class WXDLLEXPORT wxStringEqual
477 bool operator()( const wxString
& a
, const wxString
& b
) const
479 bool operator()( const wxChar
* a
, const wxChar
* b
) const
480 { return wxStrcmp( a
, b
) == 0; }
482 bool operator()( const char* a
, const char* b
) const
483 { return strcmp( a
, b
) == 0; }
484 #endif // wxUSE_UNICODE
486 wxStringEqual
& operator=(const wxStringEqual
&) { return *this; }
489 #define _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, CLASSEXP ) \
490 _WX_DECLARE_PAIR( KEY_T, VALUE_T, CLASSNAME##_wxImplementation_Pair, CLASSEXP ) \
491 _WX_DECLARE_HASH_MAP_KEY_EX( KEY_T, CLASSNAME##_wxImplementation_Pair, CLASSNAME##_wxImplementation_KeyEx, CLASSEXP ) \
492 _WX_DECLARE_HASHTABLE( CLASSNAME##_wxImplementation_Pair, KEY_T, HASH_T, CLASSNAME##_wxImplementation_KeyEx, KEY_EQ_T, CLASSNAME##_wxImplementation_HashTable, CLASSEXP, grow_lf70, never_shrink ) \
493 CLASSEXP CLASSNAME:public CLASSNAME##_wxImplementation_HashTable \
496 typedef VALUE_T mapped_type; \
498 CLASSNAME( size_type hint = 100, hasher hf = hasher(), key_equal eq = key_equal() ) \
499 : CLASSNAME##_wxImplementation_HashTable( hint, hf, eq, CLASSNAME##_wxImplementation_KeyEx() ) {} \
501 mapped_type& operator[]( const const_key_type& key ) \
503 return GetOrCreateNode( CLASSNAME##_wxImplementation_Pair( key ) )->m_value.second; \
506 const_iterator find( const const_key_type& key ) const \
508 return const_iterator( GetNode( key ), this ); \
511 iterator find( const const_key_type& key ) \
513 return iterator( GetNode( key ), this ); \
516 void insert( const value_type& v ) { (*this)[v.first] = v.second; } \
518 size_type erase( const key_type& k ) \
519 { return CLASSNAME##_wxImplementation_HashTable::erase( k ); } \
520 void erase( const iterator& it ) { erase( it->first ); } \
521 void erase( const const_iterator& it ) { erase( it->first ); } \
523 /* count() == 0 | 1 */ \
524 size_type count( const const_key_type& key ) \
525 { return GetNode( key ) ? 1 : 0; } \
528 // these macros are to be used in the user code
529 #define WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME) \
530 _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, class )
532 #define WX_DECLARE_STRING_HASH_MAP( VALUE_T, CLASSNAME ) \
533 _WX_DECLARE_HASH_MAP( wxString, VALUE_T, wxStringHash, wxStringEqual, \
536 #define WX_DECLARE_VOIDPTR_HASH_MAP( VALUE_T, CLASSNAME ) \
537 _WX_DECLARE_HASH_MAP( void*, VALUE_T, wxPointerHash, wxPointerEqual, \
540 // and these do exactly the same thing but should be used inside the
542 #define WX_DECLARE_EXPORTED_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME) \
543 _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, class WXDLLEXPORT )
545 #define WX_DECLARE_EXPORTED_STRING_HASH_MAP( VALUE_T, CLASSNAME ) \
546 _WX_DECLARE_HASH_MAP( wxString, VALUE_T, wxStringHash, wxStringEqual, \
547 CLASSNAME, class WXDLLEXPORT )
549 #define WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP( VALUE_T, CLASSNAME ) \
550 _WX_DECLARE_HASH_MAP( void*, VALUE_T, wxPointerHash, wxPointerEqual, \
551 CLASSNAME, class WXDLLEXPORT )
553 #endif // _WX_HASHMAP_H_