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 <stddef.h> // for ptrdiff_t
21 #include "wx/string.h"
24 struct WXDLLEXPORT _wxHashTable_NodeBase
26 _wxHashTable_NodeBase() : m_nxt(0) {}
28 _wxHashTable_NodeBase
* m_nxt
;
32 class WXDLLEXPORT _wxHashTableBase2
35 typedef void (*NodeDtor
)(_wxHashTable_NodeBase
*);
36 typedef unsigned long (*BucketFromNode
)(_wxHashTableBase2
*,_wxHashTable_NodeBase
*);
37 typedef _wxHashTable_NodeBase
* (*ProcessNode
)(_wxHashTable_NodeBase
*);
39 static _wxHashTable_NodeBase
* DummyProcessNode(_wxHashTable_NodeBase
* node
);
40 static void DeleteNodes( size_t buckets
, _wxHashTable_NodeBase
** table
,
42 static _wxHashTable_NodeBase
* GetFirstNode( size_t buckets
,
43 _wxHashTable_NodeBase
** table
)
45 for( size_t i
= 0; i
< buckets
; ++i
)
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
];
55 // returns the first prime in ms_primes greater than n
56 static unsigned long GetNextPrime( unsigned long n
);
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
);
62 static void CopyHashTable( _wxHashTable_NodeBase
** srcTable
,
63 size_t srcBuckets
, _wxHashTableBase2
* dst
,
64 _wxHashTable_NodeBase
** dstTable
,
65 BucketFromNode func
, ProcessNode proc
);
67 static void** AllocTable( size_t size
)
69 return (void **)calloc(size
, sizeof(void*));
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 \
77 typedef KEY_T key_type; \
78 typedef VALUE_T value_type; \
79 typedef HASH_T hasher; \
80 typedef KEY_EQ_T key_equal; \
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; \
93 typedef KEY_EX_T key_extractor; \
94 typedef CLASSNAME Self; \
97 size_t m_tableBuckets; \
100 key_equal m_equals; \
101 key_extractor m_getKey; \
103 struct Node:public _wxHashTable_NodeBase \
106 Node( const value_type& value ) \
107 : m_value( value ) {} \
108 Node* m_next() { return (Node*)this->m_nxt; } \
110 value_type m_value; \
114 friend struct Iterator; \
116 static void DeleteNode( _wxHashTable_NodeBase* node ) \
118 delete (Node*)node; \
122 /* forward iterator */ \
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; } \
137 Node* GetNextNode() \
139 size_type bucket = GetBucketForNode(m_ht,m_node); \
140 for( size_type i = bucket + 1; i < m_ht->m_tableBuckets; ++i ) \
142 if( m_ht->m_table[i] ) \
143 return m_ht->m_table[i]; \
150 Node* next = m_node->m_next(); \
151 m_node = next ? next : GetNextNode(); \
156 struct iterator:public Iterator \
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); } \
166 struct const_iterator:public Iterator \
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); } \
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 ) ), \
186 m_table = (Node**)AllocTable( m_tableBuckets ); \
189 CLASSNAME( const Self& ht ) \
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 ) \
200 const Self& operator=( const Self& ht ) \
203 m_hasher = ht.m_hasher; \
204 m_equals = ht.m_equals; \
205 m_getKey = ht.m_getKey; \
206 m_items = ht.m_items; \
218 hasher hash_funct() { return m_hasher; } \
219 key_equal key_eq() { return m_equals; } \
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 ); } \
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; } \
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 ); }; \
234 { return iterator( (Node*)GetFirstNode( m_tableBuckets, (_wxHashTable_NodeBase**)m_table ), this ); }; \
236 size_type erase( const const_key_type& key ) \
238 Node** node = GetNodePtr( key ); \
244 Node* temp = (*node)->m_next(); \
247 if( SHOULD_SHRINK( m_tableBuckets, m_items ) ) \
248 ResizeTable( GetPreviousPrime( m_tableBuckets ) - 1 ); \
253 static size_type GetBucketForNode( Self* ht, Node* node ) \
255 return ht->m_hasher( ht->m_getKey( node->m_value ) ) \
256 % ht->m_tableBuckets; \
258 static Node* CopyNode( Node* node ) { return new Node( *node ); } \
260 Node* GetOrCreateNode( const value_type& value ) \
262 const const_key_type& key = m_getKey( value ); \
263 size_t bucket = m_hasher( key ) % m_tableBuckets; \
264 Node* node = m_table[bucket]; \
268 if( m_equals( m_getKey( node->m_value ), key ) ) \
270 node = node->m_next(); \
273 node = new Node( value ); \
274 node->m_nxt = m_table[bucket]; \
275 m_table[bucket] = node; \
277 /* must be after the node is inserted */ \
279 if( SHOULD_GROW( m_tableBuckets, m_items ) ) \
280 ResizeTable( m_tableBuckets ); \
285 /* returns NULL if not found */ \
286 Node** GetNodePtr( const const_key_type& key ) const \
288 unsigned long hash = m_hasher( key ); \
289 Node** node = &m_table[hash % m_tableBuckets]; \
293 if( m_equals( m_getKey( (*node)->m_value ), key ) ) \
295 node = (Node**)&(*node)->m_nxt; \
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 \
305 unsigned long hash = m_hasher( key ); \
306 Node* node = m_table[hash % m_tableBuckets]; \
310 if( m_equals( m_getKey( node->m_value ), key ) ) \
312 node = node->m_next(); \
318 void ResizeTable( size_t newSize ) \
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; \
326 CopyHashTable( (_wxHashTable_NodeBase**)srcTable, srcBuckets, \
327 this, (_wxHashTable_NodeBase**)m_table, \
328 (BucketFromNode)&GetBucketForNode,\
329 (ProcessNode)&DummyProcessNode ); \
333 /* this must be called _after_ m_table has been cleaned */ \
334 void HashCopy( const Self& ht ) \
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 ); \
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 ) \
352 typedef VALUE_T t2; \
353 typedef const KEY_T const_t1; \
354 typedef const VALUE_T const_t2; \
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()) {} \
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 ) \
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; \
376 const_key_reference operator()( const_pair_reference pair ) const { return pair.first; }\
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; } \
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
)
389 return float(items
)/float(buckets
) >= 0.85;
392 // ----------------------------------------------------------------------------
393 // hashing and comparison functors
394 // ----------------------------------------------------------------------------
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)
401 class WXDLLEXPORT 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
; }
412 wxIntegerHash
& operator=(const wxIntegerHash
&) { return *this; }
415 class WXDLLEXPORT 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
; }
426 wxIntegerEqual
& operator=(const wxIntegerEqual
&) { return *this; }
430 class WXDLLEXPORT wxPointerHash
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
; }
439 wxPointerHash
& operator=(const wxPointerHash
&) { return *this; }
442 class WXDLLEXPORT wxPointerEqual
446 bool operator()( const void* a
, const void* b
) const { return a
== b
; }
448 wxPointerEqual
& operator=(const wxPointerEqual
&) { return *this; }
451 // wxString, char*, wxChar*
452 class WXDLLEXPORT 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
* );
462 unsigned long operator()( const char* x
) const
463 { return charStringHash( x
); }
464 static unsigned long charStringHash( const char* );
465 #endif // wxUSE_UNICODE
467 wxStringHash
& operator=(const wxStringHash
&) { return *this; }
470 class WXDLLEXPORT wxStringEqual
474 bool operator()( const wxString
& a
, const wxString
& b
) const
476 bool operator()( const wxChar
* a
, const wxChar
* b
) const
477 { return wxStrcmp( a
, b
) == 0; }
479 bool operator()( const char* a
, const char* b
) const
480 { return strcmp( a
, b
) == 0; }
481 #endif // wxUSE_UNICODE
483 wxStringEqual
& operator=(const wxStringEqual
&) { return *this; }
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 \
493 typedef VALUE_T mapped_type; \
495 CLASSNAME( size_type hint = 100, hasher hf = hasher(), key_equal eq = key_equal() ) \
496 : CLASSNAME##_wxImplementation_HashTable( hint, hf, eq, CLASSNAME##_wxImplementation_KeyEx() ) {} \
498 mapped_type& operator[]( const const_key_type& key ) \
500 return GetOrCreateNode( CLASSNAME##_wxImplementation_Pair( key ) )->m_value.second; \
503 const_iterator find( const const_key_type& key ) const \
505 return const_iterator( GetNode( key ), this ); \
508 iterator find( const const_key_type& key ) \
510 return iterator( GetNode( key ), this ); \
513 void insert( const value_type& v ) { (*this)[v.first] = v.second; } \
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 ); } \
520 /* count() == 0 | 1 */ \
521 size_type count( const const_key_type& key ) \
522 { return GetNode( key ) ? 1 : 0; } \
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 )
529 #define WX_DECLARE_STRING_HASH_MAP( VALUE_T, CLASSNAME ) \
530 _WX_DECLARE_HASH_MAP( wxString, VALUE_T, wxStringHash, wxStringEqual, \
533 #define WX_DECLARE_VOIDPTR_HASH_MAP( VALUE_T, CLASSNAME ) \
534 _WX_DECLARE_HASH_MAP( void*, VALUE_T, wxPointerHash, wxPointerEqual, \
537 // and these do exactly the same thing but should be used inside the
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 )
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 );
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 );
550 #endif // _WX_HASHMAP_H_