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 typedef int ptrdiff_t;
28 struct WXDLLIMPEXP_BASE _wxHashTable_NodeBase
30 _wxHashTable_NodeBase() : m_nxt(0) {}
32 _wxHashTable_NodeBase
* m_nxt
;
35 // DECLARE_NO_COPY_CLASS(_wxHashTable_NodeBase)
36 // without rewriting the macros, which require a public copy constructor.
40 class WXDLLIMPEXP_BASE _wxHashTableBase2
43 typedef void (*NodeDtor
)(_wxHashTable_NodeBase
*);
44 typedef unsigned long (*BucketFromNode
)(_wxHashTableBase2
*,_wxHashTable_NodeBase
*);
45 typedef _wxHashTable_NodeBase
* (*ProcessNode
)(_wxHashTable_NodeBase
*);
47 static _wxHashTable_NodeBase
* DummyProcessNode(_wxHashTable_NodeBase
* node
);
48 static void DeleteNodes( size_t buckets
, _wxHashTable_NodeBase
** table
,
50 static _wxHashTable_NodeBase
* GetFirstNode( size_t buckets
,
51 _wxHashTable_NodeBase
** table
)
53 for( size_t i
= 0; i
< buckets
; ++i
)
59 // as static const unsigned prime_count = 31 but works with all compilers
60 enum { prime_count
= 31 };
61 static const unsigned long ms_primes
[prime_count
];
63 // returns the first prime in ms_primes greater than n
64 static unsigned long GetNextPrime( unsigned long n
);
66 // returns the first prime in ms_primes smaller than n
67 // ( or ms_primes[0] if n is very small )
68 static unsigned long GetPreviousPrime( unsigned long n
);
70 static void CopyHashTable( _wxHashTable_NodeBase
** srcTable
,
71 size_t srcBuckets
, _wxHashTableBase2
* dst
,
72 _wxHashTable_NodeBase
** dstTable
,
73 BucketFromNode func
, ProcessNode proc
);
75 static void** AllocTable( size_t sz
)
77 return (void **)calloc(sz
, sizeof(void*));
81 #define _WX_DECLARE_HASHTABLE( VALUE_T, KEY_T, HASH_T, KEY_EX_T, KEY_EQ_T, CLASSNAME, CLASSEXP, SHOULD_GROW, SHOULD_SHRINK ) \
82 CLASSEXP CLASSNAME : protected _wxHashTableBase2 \
85 typedef KEY_T key_type; \
86 typedef VALUE_T value_type; \
87 typedef HASH_T hasher; \
88 typedef KEY_EQ_T key_equal; \
90 typedef size_t size_type; \
91 typedef ptrdiff_t difference_type; \
92 typedef value_type* pointer; \
93 typedef const value_type* const_pointer; \
94 typedef value_type& reference; \
95 typedef const value_type& const_reference; \
96 /* should these be protected? */ \
97 typedef const KEY_T const_key_type; \
98 typedef const VALUE_T const_mapped_type; \
101 typedef KEY_EX_T key_extractor; \
102 typedef CLASSNAME Self; \
105 size_t m_tableBuckets; \
108 key_equal m_equals; \
109 key_extractor m_getKey; \
111 struct Node:public _wxHashTable_NodeBase \
114 Node( const value_type& value ) \
115 : m_value( value ) {} \
116 Node* m_next() { return (Node*)this->m_nxt; } \
118 value_type m_value; \
122 friend struct Iterator; \
124 static void DeleteNode( _wxHashTable_NodeBase* node ) \
126 delete (Node*)node; \
130 /* forward iterator */ \
137 Iterator() : m_node(0), m_ht(0) {} \
138 Iterator( Node* node, const Self* ht ) \
139 : m_node(node), m_ht((Self*)ht) {} \
140 bool operator ==( const Iterator& it ) const \
141 { return m_node == it.m_node; } \
142 bool operator !=( const Iterator& it ) const \
143 { return m_node != it.m_node; } \
145 Node* GetNextNode() \
147 size_type bucket = GetBucketForNode(m_ht,m_node); \
148 for( size_type i = bucket + 1; i < m_ht->m_tableBuckets; ++i ) \
150 if( m_ht->m_table[i] ) \
151 return m_ht->m_table[i]; \
158 Node* next = m_node->m_next(); \
159 m_node = next ? next : GetNextNode(); \
164 struct iterator:public Iterator \
166 iterator() : Iterator() {} \
167 iterator( Node* node, Self* ht ) : Iterator( node, ht ) {} \
168 iterator& operator++() { PlusPlus(); return *this; } \
169 iterator operator++(int) { iterator it=*this;PlusPlus();return it; } \
170 reference operator *() const { return m_node->m_value; } \
171 pointer operator ->() const { return &(m_node->m_value); } \
174 struct const_iterator:public Iterator \
176 const_iterator() : Iterator() {} \
177 const_iterator( Node* node, const Self* ht ) \
178 : Iterator( node, (Self*)ht ) {} \
179 const_iterator& operator++() { PlusPlus();return *this; } \
180 const_iterator operator++(int) { const_iterator it=*this;PlusPlus();return it; } \
181 const_reference operator *() const { return m_node->m_value; } \
182 const_pointer operator ->() const { return &(m_node->m_value); } \
185 CLASSNAME( size_type sz = 10, const hasher& hfun = hasher(), \
186 const key_equal& k_eq = key_equal(), \
187 const key_extractor& k_ex = key_extractor() ) \
188 : m_tableBuckets( GetNextPrime( (unsigned long) sz ) ), \
194 m_table = (Node**)AllocTable( m_tableBuckets ); \
197 CLASSNAME( const Self& ht ) \
199 m_tableBuckets( 0 ), \
200 m_items( ht.m_items ), \
201 m_hasher( ht.m_hasher ), \
202 m_equals( ht.m_equals ), \
203 m_getKey( ht.m_getKey ) \
208 const Self& operator=( const Self& ht ) \
211 m_hasher = ht.m_hasher; \
212 m_equals = ht.m_equals; \
213 m_getKey = ht.m_getKey; \
214 m_items = ht.m_items; \
226 hasher hash_funct() { return m_hasher; } \
227 key_equal key_eq() { return m_equals; } \
229 /* removes all elements from the hash table, but does not */ \
230 /* shrink it ( perhaps it should ) */ \
233 DeleteNodes( m_tableBuckets, (_wxHashTable_NodeBase**)m_table, \
238 size_type size() const { return m_items; } \
239 size_type max_size() const { return size_type(-1); } \
240 bool empty() const { return size() == 0; } \
242 const_iterator end() const { return const_iterator( 0, this ); } \
243 iterator end() { return iterator( 0, this ); } \
244 const_iterator begin() const \
245 { return const_iterator( (Node*)GetFirstNode( m_tableBuckets, (_wxHashTable_NodeBase**)m_table ), this ); }; \
247 { return iterator( (Node*)GetFirstNode( m_tableBuckets, (_wxHashTable_NodeBase**)m_table ), this ); }; \
249 size_type erase( const const_key_type& key ) \
251 Node** node = GetNodePtr( key ); \
257 Node* temp = (*node)->m_next(); \
260 if( SHOULD_SHRINK( m_tableBuckets, m_items ) ) \
261 ResizeTable( GetPreviousPrime( (unsigned long) m_tableBuckets ) - 1 ); \
266 static size_type GetBucketForNode( Self* ht, Node* node ) \
268 return ht->m_hasher( ht->m_getKey( node->m_value ) ) \
269 % ht->m_tableBuckets; \
271 static Node* CopyNode( Node* node ) { return new Node( *node ); } \
273 Node* GetOrCreateNode( const value_type& value ) \
275 const const_key_type& key = m_getKey( value ); \
276 size_t bucket = m_hasher( key ) % m_tableBuckets; \
277 Node* node = m_table[bucket]; \
281 if( m_equals( m_getKey( node->m_value ), key ) ) \
283 node = node->m_next(); \
285 return CreateNode( value , bucket); \
287 Node * CreateNode( const value_type& value, size_t bucket ) \
289 Node* node = new Node( value ); \
290 node->m_nxt = m_table[bucket]; \
291 m_table[bucket] = node; \
293 /* must be after the node is inserted */ \
295 if( SHOULD_GROW( m_tableBuckets, m_items ) ) \
296 ResizeTable( m_tableBuckets ); \
300 void CreateNode( const value_type& value ) \
302 CreateNode(value, m_hasher( m_getKey(value) ) % m_tableBuckets ); \
305 /* returns NULL if not found */ \
306 Node** GetNodePtr( 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**)&(*node)->m_nxt; \
321 /* returns NULL if not found */ \
322 /* expressing it in terms of GetNodePtr is 5-8% slower :-( */ \
323 Node* GetNode( const const_key_type& key ) const \
325 unsigned long hash = m_hasher( key ); \
326 Node* node = m_table[hash % m_tableBuckets]; \
330 if( m_equals( m_getKey( node->m_value ), key ) ) \
332 node = node->m_next(); \
338 void ResizeTable( size_t newSize ) \
340 newSize = GetNextPrime( (unsigned long)newSize ); \
341 Node** srcTable = m_table; \
342 size_t srcBuckets = m_tableBuckets; \
343 m_table = (Node**)AllocTable( newSize ); \
344 m_tableBuckets = newSize; \
346 CopyHashTable( (_wxHashTable_NodeBase**)srcTable, srcBuckets, \
347 this, (_wxHashTable_NodeBase**)m_table, \
348 (BucketFromNode)GetBucketForNode,\
349 (ProcessNode)&DummyProcessNode ); \
353 /* this must be called _after_ m_table has been cleaned */ \
354 void HashCopy( const Self& ht ) \
356 ResizeTable( ht.size() ); \
357 CopyHashTable( (_wxHashTable_NodeBase**)ht.m_table, ht.m_tableBuckets,\
358 (_wxHashTableBase2*)this, \
359 (_wxHashTable_NodeBase**)m_table, \
360 (BucketFromNode)GetBucketForNode, \
361 (ProcessNode)CopyNode ); \
365 // defines an STL-like pair class CLASSNAME storing two fields: first of type
366 // KEY_T and second of type VALUE_T
367 #define _WX_DECLARE_PAIR( KEY_T, VALUE_T, CLASSNAME, CLASSEXP ) \
372 typedef VALUE_T t2; \
373 typedef const KEY_T const_t1; \
374 typedef const VALUE_T const_t2; \
376 CLASSNAME( const const_t1& f, const const_t2& s ):first(t1(f)),second(t2(s)) {} \
382 // defines the class CLASSNAME returning the key part (of type KEY_T) from a
383 // pair of type PAIR_T
384 #define _WX_DECLARE_HASH_MAP_KEY_EX( KEY_T, PAIR_T, CLASSNAME, CLASSEXP ) \
387 typedef KEY_T key_type; \
388 typedef PAIR_T pair_type; \
389 typedef const key_type const_key_type; \
390 typedef const pair_type const_pair_type; \
391 typedef const_key_type& const_key_reference; \
392 typedef const_pair_type& const_pair_reference; \
395 const_key_reference operator()( const_pair_reference pair ) const { return pair.first; }\
397 /* the dummy assignment operator is needed to suppress compiler */ \
398 /* warnings from hash table class' operator=(): gcc complains about */ \
399 /* "statement with no effect" without it */ \
400 CLASSNAME& operator=(const CLASSNAME&) { return *this; } \
403 // grow/shrink predicates
404 inline bool never_grow( size_t, size_t ) { return FALSE
; }
405 inline bool never_shrink( size_t, size_t ) { return FALSE
; }
406 inline bool grow_lf70( size_t buckets
, size_t items
)
408 return float(items
)/float(buckets
) >= 0.85;
411 // ----------------------------------------------------------------------------
412 // hashing and comparison functors
413 // ----------------------------------------------------------------------------
415 // NB: implementation detail: all of these classes must have dummy assignment
416 // operators to suppress warnings about "statement with no effect" from gcc
417 // in the hash table class assignment operator (where they're assigned)
420 class WXDLLIMPEXP_BASE wxIntegerHash
424 unsigned long operator()( long x
) const { return (unsigned long)x
; }
425 unsigned long operator()( unsigned long x
) const { return x
; }
426 unsigned long operator()( int x
) const { return (unsigned long)x
; }
427 unsigned long operator()( unsigned int x
) const { return x
; }
428 unsigned long operator()( short x
) const { return (unsigned long)x
; }
429 unsigned long operator()( unsigned short x
) const { return x
; }
431 wxIntegerHash
& operator=(const wxIntegerHash
&) { return *this; }
434 class WXDLLIMPEXP_BASE wxIntegerEqual
438 bool operator()( long a
, long b
) const { return a
== b
; }
439 bool operator()( unsigned long a
, unsigned long b
) const { return a
== b
; }
440 bool operator()( int a
, int b
) const { return a
== b
; }
441 bool operator()( unsigned int a
, unsigned int b
) const { return a
== b
; }
442 bool operator()( short a
, short b
) const { return a
== b
; }
443 bool operator()( unsigned short a
, unsigned short b
) const { return a
== b
; }
445 wxIntegerEqual
& operator=(const wxIntegerEqual
&) { return *this; }
449 class WXDLLIMPEXP_BASE wxPointerHash
454 // TODO: this might not work well on architectures with 64 bit pointers but
455 // 32 bit longs, we should use % ULONG_MAX there
456 unsigned long operator()( const void* k
) const { return (unsigned long)wxPtrToULong(k
); }
458 wxPointerHash
& operator=(const wxPointerHash
&) { return *this; }
461 class WXDLLIMPEXP_BASE wxPointerEqual
465 bool operator()( const void* a
, const void* b
) const { return a
== b
; }
467 wxPointerEqual
& operator=(const wxPointerEqual
&) { return *this; }
470 // wxString, char*, wxChar*
471 class WXDLLIMPEXP_BASE wxStringHash
475 unsigned long operator()( const wxString
& x
) const
476 { return wxCharStringHash( x
.c_str() ); }
477 unsigned long operator()( const wxChar
* x
) const
478 { return wxCharStringHash( x
); }
479 static unsigned long wxCharStringHash( const wxChar
* );
481 unsigned long operator()( const char* x
) const
482 { return charStringHash( x
); }
483 static unsigned long charStringHash( const char* );
484 #endif // wxUSE_UNICODE
486 wxStringHash
& operator=(const wxStringHash
&) { return *this; }
489 class WXDLLIMPEXP_BASE wxStringEqual
493 bool operator()( const wxString
& a
, const wxString
& b
) const
495 bool operator()( const wxChar
* a
, const wxChar
* b
) const
496 { return wxStrcmp( a
, b
) == 0; }
498 bool operator()( const char* a
, const char* b
) const
499 { return strcmp( a
, b
) == 0; }
500 #endif // wxUSE_UNICODE
502 wxStringEqual
& operator=(const wxStringEqual
&) { return *this; }
505 #define _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, CLASSEXP ) \
506 _WX_DECLARE_PAIR( KEY_T, VALUE_T, CLASSNAME##_wxImplementation_Pair, CLASSEXP ) \
507 _WX_DECLARE_HASH_MAP_KEY_EX( KEY_T, CLASSNAME##_wxImplementation_Pair, CLASSNAME##_wxImplementation_KeyEx, CLASSEXP ) \
508 _WX_DECLARE_HASHTABLE( CLASSNAME##_wxImplementation_Pair, KEY_T, HASH_T, CLASSNAME##_wxImplementation_KeyEx, KEY_EQ_T, CLASSNAME##_wxImplementation_HashTable, CLASSEXP, grow_lf70, never_shrink ) \
509 CLASSEXP CLASSNAME:public CLASSNAME##_wxImplementation_HashTable \
512 typedef VALUE_T mapped_type; \
514 CLASSNAME( size_type hint = 100, hasher hf = hasher(), key_equal eq = key_equal() ) \
515 : CLASSNAME##_wxImplementation_HashTable( hint, hf, eq, CLASSNAME##_wxImplementation_KeyEx() ) {} \
517 mapped_type& operator[]( const const_key_type& key ) \
519 return GetOrCreateNode( CLASSNAME##_wxImplementation_Pair( key, mapped_type() ) )->m_value.second; \
522 const_iterator find( const const_key_type& key ) const \
524 return const_iterator( GetNode( key ), this ); \
527 iterator find( const const_key_type& key ) \
529 return iterator( GetNode( key ), this ); \
532 void insert( const value_type& v ) { (*this)[v.first] = v.second; } \
534 size_type erase( const key_type& k ) \
535 { return CLASSNAME##_wxImplementation_HashTable::erase( k ); } \
536 void erase( const iterator& it ) { erase( it->first ); } \
537 void erase( const const_iterator& it ) { erase( it->first ); } \
539 /* count() == 0 | 1 */ \
540 size_type count( const const_key_type& key ) \
541 { return GetNode( key ) ? 1 : 0; } \
544 // these macros are to be used in the user code
545 #define WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME) \
546 _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, class )
548 #define WX_DECLARE_STRING_HASH_MAP( VALUE_T, CLASSNAME ) \
549 _WX_DECLARE_HASH_MAP( wxString, VALUE_T, wxStringHash, wxStringEqual, \
552 #define WX_DECLARE_VOIDPTR_HASH_MAP( VALUE_T, CLASSNAME ) \
553 _WX_DECLARE_HASH_MAP( void*, VALUE_T, wxPointerHash, wxPointerEqual, \
556 // and these do exactly the same thing but should be used inside the
558 #define WX_DECLARE_EXPORTED_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME) \
559 _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, class WXDLLEXPORT )
561 #define WX_DECLARE_EXPORTED_STRING_HASH_MAP( VALUE_T, CLASSNAME ) \
562 _WX_DECLARE_HASH_MAP( wxString, VALUE_T, wxStringHash, wxStringEqual, \
563 CLASSNAME, class WXDLLEXPORT )
565 #define WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP( VALUE_T, CLASSNAME ) \
566 _WX_DECLARE_HASH_MAP( void*, VALUE_T, wxPointerHash, wxPointerEqual, \
567 CLASSNAME, class WXDLLEXPORT )
569 // delete all hash elements
571 // NB: the class declaration of the hash elements must be visible from the
572 // place where you use this macro, otherwise the proper destructor may not
573 // be called (a decent compiler should give a warning about it, but don't
575 #define WX_CLEAR_HASH_MAP(type, hashmap) \
577 type::iterator it, en; \
578 for( it = (hashmap).begin(), en = (hashmap).end(); it != en; ++it ) \
583 #endif // _WX_HASHMAP_H_