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"
21 #include <stddef.h> // for ptrdiff_t
24 struct WXDLLEXPORT _wxHashTable_NodeBase
26 _wxHashTable_NodeBase() : m_nxt(0) {}
28 _wxHashTable_NodeBase
* m_nxt
;
31 // DECLARE_NO_COPY_CLASS(_wxHashTable_NodeBase)
32 // without rewriting the macros, which require a public copy constructor.
36 class WXDLLEXPORT _wxHashTableBase2
39 typedef void (*NodeDtor
)(_wxHashTable_NodeBase
*);
40 typedef unsigned long (*BucketFromNode
)(_wxHashTableBase2
*,_wxHashTable_NodeBase
*);
41 typedef _wxHashTable_NodeBase
* (*ProcessNode
)(_wxHashTable_NodeBase
*);
43 static _wxHashTable_NodeBase
* DummyProcessNode(_wxHashTable_NodeBase
* node
);
44 static void DeleteNodes( size_t buckets
, _wxHashTable_NodeBase
** table
,
46 static _wxHashTable_NodeBase
* GetFirstNode( size_t buckets
,
47 _wxHashTable_NodeBase
** table
)
49 for( size_t i
= 0; i
< buckets
; ++i
)
55 // as static const unsigned prime_count = 31 but works with all compilers
56 enum { prime_count
= 31 };
57 static const unsigned long ms_primes
[prime_count
];
59 // returns the first prime in ms_primes greater than n
60 static unsigned long GetNextPrime( unsigned long n
);
62 // returns the first prime in ms_primes smaller than n
63 // ( or ms_primes[0] if n is very small )
64 static unsigned long GetPreviousPrime( unsigned long n
);
66 static void CopyHashTable( _wxHashTable_NodeBase
** srcTable
,
67 size_t srcBuckets
, _wxHashTableBase2
* dst
,
68 _wxHashTable_NodeBase
** dstTable
,
69 BucketFromNode func
, ProcessNode proc
);
71 static void** AllocTable( size_t sz
)
73 return (void **)calloc(sz
, sizeof(void*));
77 #define _WX_DECLARE_HASHTABLE( VALUE_T, KEY_T, HASH_T, KEY_EX_T, KEY_EQ_T, CLASSNAME, CLASSEXP, SHOULD_GROW, SHOULD_SHRINK ) \
78 CLASSEXP CLASSNAME : protected _wxHashTableBase2 \
81 typedef KEY_T key_type; \
82 typedef VALUE_T value_type; \
83 typedef HASH_T hasher; \
84 typedef KEY_EQ_T key_equal; \
86 typedef size_t size_type; \
87 typedef ptrdiff_t difference_type; \
88 typedef value_type* pointer; \
89 typedef const value_type* const_pointer; \
90 typedef value_type& reference; \
91 typedef const value_type& const_reference; \
92 /* should these be protected? */ \
93 typedef const KEY_T const_key_type; \
94 typedef const VALUE_T const_mapped_type; \
97 typedef KEY_EX_T key_extractor; \
98 typedef CLASSNAME Self; \
101 size_t m_tableBuckets; \
104 key_equal m_equals; \
105 key_extractor m_getKey; \
107 struct Node:public _wxHashTable_NodeBase \
110 Node( const value_type& value ) \
111 : m_value( value ) {} \
112 Node* m_next() { return (Node*)this->m_nxt; } \
114 value_type m_value; \
118 friend struct Iterator; \
120 static void DeleteNode( _wxHashTable_NodeBase* node ) \
122 delete (Node*)node; \
126 /* forward iterator */ \
133 Iterator() : m_node(0), m_ht(0) {} \
134 Iterator( Node* node, const Self* ht ) \
135 : m_node(node), m_ht((Self*)ht) {} \
136 bool operator ==( const Iterator& it ) const \
137 { return m_node == it.m_node; } \
138 bool operator !=( const Iterator& it ) const \
139 { return m_node != it.m_node; } \
141 Node* GetNextNode() \
143 size_type bucket = GetBucketForNode(m_ht,m_node); \
144 for( size_type i = bucket + 1; i < m_ht->m_tableBuckets; ++i ) \
146 if( m_ht->m_table[i] ) \
147 return m_ht->m_table[i]; \
154 Node* next = m_node->m_next(); \
155 m_node = next ? next : GetNextNode(); \
160 struct iterator:public Iterator \
162 iterator() : Iterator() {} \
163 iterator( Node* node, Self* ht ) : Iterator( node, ht ) {} \
164 iterator& operator++() { PlusPlus(); return *this; } \
165 iterator operator++(int) { iterator it=*this;PlusPlus();return it; } \
166 reference operator *() const { return m_node->m_value; } \
167 pointer operator ->() const { return &(m_node->m_value); } \
170 struct const_iterator:public Iterator \
172 const_iterator() : Iterator() {} \
173 const_iterator( Node* node, const Self* ht ) \
174 : Iterator( node, (Self*)ht ) {} \
175 const_iterator& operator++() { PlusPlus();return *this; } \
176 const_iterator operator++(int) { const_iterator it=*this;PlusPlus();return it; } \
177 const_reference operator *() const { return m_node->m_value; } \
178 const_pointer operator ->() const { return &(m_node->m_value); } \
181 CLASSNAME( size_type sz = 10, const hasher& hfun = hasher(), \
182 const key_equal& k_eq = key_equal(), \
183 const key_extractor& k_ex = key_extractor() ) \
184 : m_tableBuckets( GetNextPrime( (unsigned long) sz ) ), \
190 m_table = (Node**)AllocTable( m_tableBuckets ); \
193 CLASSNAME( const Self& ht ) \
195 m_tableBuckets( 0 ), \
196 m_items( ht.m_items ), \
197 m_hasher( ht.m_hasher ), \
198 m_equals( ht.m_equals ), \
199 m_getKey( ht.m_getKey ) \
204 const Self& operator=( const Self& ht ) \
207 m_hasher = ht.m_hasher; \
208 m_equals = ht.m_equals; \
209 m_getKey = ht.m_getKey; \
210 m_items = ht.m_items; \
222 hasher hash_funct() { return m_hasher; } \
223 key_equal key_eq() { return m_equals; } \
225 /* removes all elements from the hash table, but does not */ \
226 /* shrink it ( perhaps it should ) */ \
229 DeleteNodes( m_tableBuckets, (_wxHashTable_NodeBase**)m_table, \
234 size_type size() const { return m_items; } \
235 size_type max_size() const { return size_type(-1); } \
236 bool empty() const { return size() == 0; } \
238 const_iterator end() const { return const_iterator( 0, this ); } \
239 iterator end() { return iterator( 0, this ); } \
240 const_iterator begin() const \
241 { return const_iterator( (Node*)GetFirstNode( m_tableBuckets, (_wxHashTable_NodeBase**)m_table ), this ); }; \
243 { return iterator( (Node*)GetFirstNode( m_tableBuckets, (_wxHashTable_NodeBase**)m_table ), this ); }; \
245 size_type erase( const const_key_type& key ) \
247 Node** node = GetNodePtr( key ); \
253 Node* temp = (*node)->m_next(); \
256 if( SHOULD_SHRINK( m_tableBuckets, m_items ) ) \
257 ResizeTable( GetPreviousPrime( (unsigned long) m_tableBuckets ) - 1 ); \
262 static size_type GetBucketForNode( Self* ht, Node* node ) \
264 return ht->m_hasher( ht->m_getKey( node->m_value ) ) \
265 % ht->m_tableBuckets; \
267 static Node* CopyNode( Node* node ) { return new Node( *node ); } \
269 Node* GetOrCreateNode( const value_type& value ) \
271 const const_key_type& key = m_getKey( value ); \
272 size_t bucket = m_hasher( key ) % m_tableBuckets; \
273 Node* node = m_table[bucket]; \
277 if( m_equals( m_getKey( node->m_value ), key ) ) \
279 node = node->m_next(); \
282 node = new Node( value ); \
283 node->m_nxt = m_table[bucket]; \
284 m_table[bucket] = node; \
286 /* must be after the node is inserted */ \
288 if( SHOULD_GROW( m_tableBuckets, m_items ) ) \
289 ResizeTable( m_tableBuckets ); \
294 /* returns NULL if not found */ \
295 Node** GetNodePtr( const const_key_type& key ) const \
297 unsigned long hash = m_hasher( key ); \
298 Node** node = &m_table[hash % m_tableBuckets]; \
302 if( m_equals( m_getKey( (*node)->m_value ), key ) ) \
304 node = (Node**)&(*node)->m_nxt; \
310 /* returns NULL if not found */ \
311 /* expressing it in terms of GetNodePtr is 5-8% slower :-( */ \
312 Node* GetNode( const const_key_type& key ) const \
314 unsigned long hash = m_hasher( key ); \
315 Node* node = m_table[hash % m_tableBuckets]; \
319 if( m_equals( m_getKey( node->m_value ), key ) ) \
321 node = node->m_next(); \
327 void ResizeTable( size_t newSize ) \
329 newSize = GetNextPrime( (unsigned long)newSize ); \
330 Node** srcTable = m_table; \
331 size_t srcBuckets = m_tableBuckets; \
332 m_table = (Node**)AllocTable( newSize ); \
333 m_tableBuckets = newSize; \
335 CopyHashTable( (_wxHashTable_NodeBase**)srcTable, srcBuckets, \
336 this, (_wxHashTable_NodeBase**)m_table, \
337 (BucketFromNode)GetBucketForNode,\
338 (ProcessNode)&DummyProcessNode ); \
342 /* this must be called _after_ m_table has been cleaned */ \
343 void HashCopy( const Self& ht ) \
345 ResizeTable( ht.size() ); \
346 CopyHashTable( (_wxHashTable_NodeBase**)ht.m_table, ht.m_tableBuckets,\
347 (_wxHashTableBase2*)this, \
348 (_wxHashTable_NodeBase**)m_table, \
349 (BucketFromNode)GetBucketForNode, \
350 (ProcessNode)CopyNode ); \
354 // defines an STL-like pair class CLASSNAME storing two fields: first of type
355 // KEY_T and second of type VALUE_T
356 #define _WX_DECLARE_PAIR( KEY_T, VALUE_T, CLASSNAME, CLASSEXP ) \
361 typedef VALUE_T t2; \
362 typedef const KEY_T const_t1; \
363 typedef const VALUE_T const_t2; \
365 CLASSNAME( const const_t1& f, const const_t2& s ):first(t1(f)),second(t2(s)) {} \
366 CLASSNAME( const const_t1& f ):first(t1(f)),second(t2()) {} \
372 // defines the class CLASSNAME returning the key part (of type KEY_T) from a
373 // pair of type PAIR_T
374 #define _WX_DECLARE_HASH_MAP_KEY_EX( KEY_T, PAIR_T, CLASSNAME, CLASSEXP ) \
377 typedef KEY_T key_type; \
378 typedef PAIR_T pair_type; \
379 typedef const key_type const_key_type; \
380 typedef const pair_type const_pair_type; \
381 typedef const_key_type& const_key_reference; \
382 typedef const_pair_type& const_pair_reference; \
385 const_key_reference operator()( const_pair_reference pair ) const { return pair.first; }\
387 /* the dummy assignment operator is needed to suppress compiler */ \
388 /* warnings from hash table class' operator=(): gcc complains about */ \
389 /* "statement with no effect" without it */ \
390 CLASSNAME& operator=(const CLASSNAME&) { return *this; } \
393 // grow/shrink predicates
394 inline bool never_grow( size_t, size_t ) { return FALSE
; }
395 inline bool never_shrink( size_t, size_t ) { return FALSE
; }
396 inline bool grow_lf70( size_t buckets
, size_t items
)
398 return float(items
)/float(buckets
) >= 0.85;
401 // ----------------------------------------------------------------------------
402 // hashing and comparison functors
403 // ----------------------------------------------------------------------------
405 // NB: implementation detail: all of these classes must have dummy assignment
406 // operators to suppress warnings about "statement with no effect" from gcc
407 // in the hash table class assignment operator (where they're assigned)
410 class WXDLLEXPORT wxIntegerHash
414 unsigned long operator()( long x
) const { return (unsigned long)x
; }
415 unsigned long operator()( unsigned long x
) const { return x
; }
416 unsigned long operator()( int x
) const { return (unsigned long)x
; }
417 unsigned long operator()( unsigned int x
) const { return x
; }
418 unsigned long operator()( short x
) const { return (unsigned long)x
; }
419 unsigned long operator()( unsigned short x
) const { return x
; }
421 wxIntegerHash
& operator=(const wxIntegerHash
&) { return *this; }
424 class WXDLLEXPORT wxIntegerEqual
428 bool operator()( long a
, long b
) const { return a
== b
; }
429 bool operator()( unsigned long a
, unsigned long b
) const { return a
== b
; }
430 bool operator()( int a
, int b
) const { return a
== b
; }
431 bool operator()( unsigned int a
, unsigned int b
) const { return a
== b
; }
432 bool operator()( short a
, short b
) const { return a
== b
; }
433 bool operator()( unsigned short a
, unsigned short b
) const { return a
== b
; }
435 wxIntegerEqual
& operator=(const wxIntegerEqual
&) { return *this; }
439 class WXDLLEXPORT wxPointerHash
444 // TODO: this might not work well on architectures with 64 bit pointers but
445 // 32 bit longs, we should use % ULONG_MAX there
446 unsigned long operator()( const void* k
) const { return (unsigned long)wxPtrToULong(k
); }
448 wxPointerHash
& operator=(const wxPointerHash
&) { return *this; }
451 class WXDLLEXPORT wxPointerEqual
455 bool operator()( const void* a
, const void* b
) const { return a
== b
; }
457 wxPointerEqual
& operator=(const wxPointerEqual
&) { return *this; }
460 // wxString, char*, wxChar*
461 class WXDLLEXPORT wxStringHash
465 unsigned long operator()( const wxString
& x
) const
466 { return wxCharStringHash( x
.c_str() ); }
467 unsigned long operator()( const wxChar
* x
) const
468 { return wxCharStringHash( x
); }
469 static unsigned long wxCharStringHash( const wxChar
* );
471 unsigned long operator()( const char* x
) const
472 { return charStringHash( x
); }
473 static unsigned long charStringHash( const char* );
474 #endif // wxUSE_UNICODE
476 wxStringHash
& operator=(const wxStringHash
&) { return *this; }
479 class WXDLLEXPORT wxStringEqual
483 bool operator()( const wxString
& a
, const wxString
& b
) const
485 bool operator()( const wxChar
* a
, const wxChar
* b
) const
486 { return wxStrcmp( a
, b
) == 0; }
488 bool operator()( const char* a
, const char* b
) const
489 { return strcmp( a
, b
) == 0; }
490 #endif // wxUSE_UNICODE
492 wxStringEqual
& operator=(const wxStringEqual
&) { return *this; }
495 #define _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, CLASSEXP ) \
496 _WX_DECLARE_PAIR( KEY_T, VALUE_T, CLASSNAME##_wxImplementation_Pair, CLASSEXP ) \
497 _WX_DECLARE_HASH_MAP_KEY_EX( KEY_T, CLASSNAME##_wxImplementation_Pair, CLASSNAME##_wxImplementation_KeyEx, CLASSEXP ) \
498 _WX_DECLARE_HASHTABLE( CLASSNAME##_wxImplementation_Pair, KEY_T, HASH_T, CLASSNAME##_wxImplementation_KeyEx, KEY_EQ_T, CLASSNAME##_wxImplementation_HashTable, CLASSEXP, grow_lf70, never_shrink ) \
499 CLASSEXP CLASSNAME:public CLASSNAME##_wxImplementation_HashTable \
502 typedef VALUE_T mapped_type; \
504 CLASSNAME( size_type hint = 100, hasher hf = hasher(), key_equal eq = key_equal() ) \
505 : CLASSNAME##_wxImplementation_HashTable( hint, hf, eq, CLASSNAME##_wxImplementation_KeyEx() ) {} \
507 mapped_type& operator[]( const const_key_type& key ) \
509 return GetOrCreateNode( CLASSNAME##_wxImplementation_Pair( key ) )->m_value.second; \
512 const_iterator find( const const_key_type& key ) const \
514 return const_iterator( GetNode( key ), this ); \
517 iterator find( const const_key_type& key ) \
519 return iterator( GetNode( key ), this ); \
522 void insert( const value_type& v ) { (*this)[v.first] = v.second; } \
524 size_type erase( const key_type& k ) \
525 { return CLASSNAME##_wxImplementation_HashTable::erase( k ); } \
526 void erase( const iterator& it ) { erase( it->first ); } \
527 void erase( const const_iterator& it ) { erase( it->first ); } \
529 /* count() == 0 | 1 */ \
530 size_type count( const const_key_type& key ) \
531 { return GetNode( key ) ? 1 : 0; } \
534 // these macros are to be used in the user code
535 #define WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME) \
536 _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, class )
538 #define WX_DECLARE_STRING_HASH_MAP( VALUE_T, CLASSNAME ) \
539 _WX_DECLARE_HASH_MAP( wxString, VALUE_T, wxStringHash, wxStringEqual, \
542 #define WX_DECLARE_VOIDPTR_HASH_MAP( VALUE_T, CLASSNAME ) \
543 _WX_DECLARE_HASH_MAP( void*, VALUE_T, wxPointerHash, wxPointerEqual, \
546 // and these do exactly the same thing but should be used inside the
548 #define WX_DECLARE_EXPORTED_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME) \
549 _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, class WXDLLEXPORT )
551 #define WX_DECLARE_EXPORTED_STRING_HASH_MAP( VALUE_T, CLASSNAME ) \
552 _WX_DECLARE_HASH_MAP( wxString, VALUE_T, wxStringHash, wxStringEqual, \
553 CLASSNAME, class WXDLLEXPORT )
555 #define WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP( VALUE_T, CLASSNAME ) \
556 _WX_DECLARE_HASH_MAP( void*, VALUE_T, wxPointerHash, wxPointerEqual, \
557 CLASSNAME, class WXDLLEXPORT )
559 #endif // _WX_HASHMAP_H_