| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: hashmap.h |
| 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 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
| 16 | #pragma interface "hashmap.h" |
| 17 | #endif |
| 18 | |
| 19 | #include "wx/string.h" |
| 20 | |
| 21 | #include <stddef.h> // for ptrdiff_t |
| 22 | |
| 23 | #ifdef __WXWINCE__ |
| 24 | typedef int ptrdiff_t; |
| 25 | #endif |
| 26 | |
| 27 | // private |
| 28 | struct WXDLLIMPEXP_BASE _wxHashTable_NodeBase |
| 29 | { |
| 30 | _wxHashTable_NodeBase() : m_nxt(0) {} |
| 31 | |
| 32 | _wxHashTable_NodeBase* m_nxt; |
| 33 | |
| 34 | // Cannot do this: |
| 35 | // DECLARE_NO_COPY_CLASS(_wxHashTable_NodeBase) |
| 36 | // without rewriting the macros, which require a public copy constructor. |
| 37 | }; |
| 38 | |
| 39 | // private |
| 40 | class WXDLLIMPEXP_BASE _wxHashTableBase2 |
| 41 | { |
| 42 | public: |
| 43 | typedef void (*NodeDtor)(_wxHashTable_NodeBase*); |
| 44 | typedef unsigned long (*BucketFromNode)(_wxHashTableBase2*,_wxHashTable_NodeBase*); |
| 45 | typedef _wxHashTable_NodeBase* (*ProcessNode)(_wxHashTable_NodeBase*); |
| 46 | protected: |
| 47 | static _wxHashTable_NodeBase* DummyProcessNode(_wxHashTable_NodeBase* node); |
| 48 | static void DeleteNodes( size_t buckets, _wxHashTable_NodeBase** table, |
| 49 | NodeDtor dtor ); |
| 50 | static _wxHashTable_NodeBase* GetFirstNode( size_t buckets, |
| 51 | _wxHashTable_NodeBase** table ) |
| 52 | { |
| 53 | for( size_t i = 0; i < buckets; ++i ) |
| 54 | if( table[i] ) |
| 55 | return table[i]; |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 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]; |
| 62 | |
| 63 | // returns the first prime in ms_primes greater than n |
| 64 | static unsigned long GetNextPrime( unsigned long n ); |
| 65 | |
| 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 ); |
| 69 | |
| 70 | static void CopyHashTable( _wxHashTable_NodeBase** srcTable, |
| 71 | size_t srcBuckets, _wxHashTableBase2* dst, |
| 72 | _wxHashTable_NodeBase** dstTable, |
| 73 | size_t dstBuckets, |
| 74 | BucketFromNode func, ProcessNode proc ); |
| 75 | |
| 76 | static void** AllocTable( size_t sz ) |
| 77 | { |
| 78 | return (void **)calloc(sz, sizeof(void*)); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | #define _WX_DECLARE_HASHTABLE( VALUE_T, KEY_T, HASH_T, KEY_EX_T, KEY_EQ_T, CLASSNAME, CLASSEXP, SHOULD_GROW, SHOULD_SHRINK ) \ |
| 83 | CLASSEXP CLASSNAME : protected _wxHashTableBase2 \ |
| 84 | { \ |
| 85 | public: \ |
| 86 | typedef KEY_T key_type; \ |
| 87 | typedef VALUE_T value_type; \ |
| 88 | typedef HASH_T hasher; \ |
| 89 | typedef KEY_EQ_T key_equal; \ |
| 90 | \ |
| 91 | typedef size_t size_type; \ |
| 92 | typedef ptrdiff_t difference_type; \ |
| 93 | typedef value_type* pointer; \ |
| 94 | typedef const value_type* const_pointer; \ |
| 95 | typedef value_type& reference; \ |
| 96 | typedef const value_type& const_reference; \ |
| 97 | /* should these be protected? */ \ |
| 98 | typedef const KEY_T const_key_type; \ |
| 99 | typedef const VALUE_T const_mapped_type; \ |
| 100 | public: \ |
| 101 | struct Node; \ |
| 102 | typedef KEY_EX_T key_extractor; \ |
| 103 | typedef CLASSNAME Self; \ |
| 104 | protected: \ |
| 105 | Node** m_table; \ |
| 106 | size_t m_tableBuckets; \ |
| 107 | size_t m_items; \ |
| 108 | hasher m_hasher; \ |
| 109 | key_equal m_equals; \ |
| 110 | key_extractor m_getKey; \ |
| 111 | public: \ |
| 112 | struct Node:public _wxHashTable_NodeBase \ |
| 113 | { \ |
| 114 | public: \ |
| 115 | Node( const value_type& value ) \ |
| 116 | : m_value( value ) {} \ |
| 117 | Node* m_next() { return (Node*)this->m_nxt; } \ |
| 118 | \ |
| 119 | value_type m_value; \ |
| 120 | }; \ |
| 121 | \ |
| 122 | struct Iterator; \ |
| 123 | friend struct Iterator; \ |
| 124 | protected: \ |
| 125 | static void DeleteNode( _wxHashTable_NodeBase* node ) \ |
| 126 | { \ |
| 127 | delete (Node*)node; \ |
| 128 | } \ |
| 129 | public: \ |
| 130 | /* */ \ |
| 131 | /* forward iterator */ \ |
| 132 | /* */ \ |
| 133 | struct Iterator \ |
| 134 | { \ |
| 135 | Node* m_node; \ |
| 136 | Self* m_ht; \ |
| 137 | \ |
| 138 | Iterator() : m_node(0), m_ht(0) {} \ |
| 139 | Iterator( Node* node, const Self* ht ) \ |
| 140 | : m_node(node), m_ht((Self*)ht) {} \ |
| 141 | bool operator ==( const Iterator& it ) const \ |
| 142 | { return m_node == it.m_node; } \ |
| 143 | bool operator !=( const Iterator& it ) const \ |
| 144 | { return m_node != it.m_node; } \ |
| 145 | protected: \ |
| 146 | Node* GetNextNode() \ |
| 147 | { \ |
| 148 | size_type bucket = GetBucketForNode(m_ht,m_node); \ |
| 149 | for( size_type i = bucket + 1; i < m_ht->m_tableBuckets; ++i ) \ |
| 150 | { \ |
| 151 | if( m_ht->m_table[i] ) \ |
| 152 | return m_ht->m_table[i]; \ |
| 153 | } \ |
| 154 | return 0; \ |
| 155 | } \ |
| 156 | \ |
| 157 | void PlusPlus() \ |
| 158 | { \ |
| 159 | Node* next = m_node->m_next(); \ |
| 160 | m_node = next ? next : GetNextNode(); \ |
| 161 | } \ |
| 162 | }; \ |
| 163 | \ |
| 164 | public: \ |
| 165 | struct iterator:public Iterator \ |
| 166 | { \ |
| 167 | iterator() : Iterator() {} \ |
| 168 | iterator( Node* node, Self* ht ) : Iterator( node, ht ) {} \ |
| 169 | iterator& operator++() { PlusPlus(); return *this; } \ |
| 170 | iterator operator++(int) { iterator it=*this;PlusPlus();return it; } \ |
| 171 | reference operator *() const { return m_node->m_value; } \ |
| 172 | pointer operator ->() const { return &(m_node->m_value); } \ |
| 173 | }; \ |
| 174 | \ |
| 175 | struct const_iterator:public Iterator \ |
| 176 | { \ |
| 177 | const_iterator() : Iterator() {} \ |
| 178 | const_iterator( Node* node, const Self* ht ) \ |
| 179 | : Iterator( node, (Self*)ht ) {} \ |
| 180 | const_iterator& operator++() { PlusPlus();return *this; } \ |
| 181 | const_iterator operator++(int) { const_iterator it=*this;PlusPlus();return it; } \ |
| 182 | const_reference operator *() const { return m_node->m_value; } \ |
| 183 | const_pointer operator ->() const { return &(m_node->m_value); } \ |
| 184 | }; \ |
| 185 | \ |
| 186 | CLASSNAME( size_type sz = 10, const hasher& hfun = hasher(), \ |
| 187 | const key_equal& k_eq = key_equal(), \ |
| 188 | const key_extractor& k_ex = key_extractor() ) \ |
| 189 | : m_tableBuckets( GetNextPrime( (unsigned long) sz ) ), \ |
| 190 | m_items( 0 ), \ |
| 191 | m_hasher( hfun ), \ |
| 192 | m_equals( k_eq ), \ |
| 193 | m_getKey( k_ex ) \ |
| 194 | { \ |
| 195 | m_table = (Node**)AllocTable( m_tableBuckets ); \ |
| 196 | } \ |
| 197 | \ |
| 198 | CLASSNAME( const Self& ht ) \ |
| 199 | : m_table( 0 ), \ |
| 200 | m_tableBuckets( 0 ), \ |
| 201 | m_items( ht.m_items ), \ |
| 202 | m_hasher( ht.m_hasher ), \ |
| 203 | m_equals( ht.m_equals ), \ |
| 204 | m_getKey( ht.m_getKey ) \ |
| 205 | { \ |
| 206 | HashCopy( ht ); \ |
| 207 | } \ |
| 208 | \ |
| 209 | const Self& operator=( const Self& ht ) \ |
| 210 | { \ |
| 211 | clear(); \ |
| 212 | m_hasher = ht.m_hasher; \ |
| 213 | m_equals = ht.m_equals; \ |
| 214 | m_getKey = ht.m_getKey; \ |
| 215 | m_items = ht.m_items; \ |
| 216 | HashCopy( ht ); \ |
| 217 | return *this; \ |
| 218 | } \ |
| 219 | \ |
| 220 | ~CLASSNAME() \ |
| 221 | { \ |
| 222 | clear(); \ |
| 223 | \ |
| 224 | free(m_table); \ |
| 225 | } \ |
| 226 | \ |
| 227 | hasher hash_funct() { return m_hasher; } \ |
| 228 | key_equal key_eq() { return m_equals; } \ |
| 229 | \ |
| 230 | /* removes all elements from the hash table, but does not */ \ |
| 231 | /* shrink it ( perhaps it should ) */ \ |
| 232 | void clear() \ |
| 233 | { \ |
| 234 | DeleteNodes( m_tableBuckets, (_wxHashTable_NodeBase**)m_table, \ |
| 235 | DeleteNode ); \ |
| 236 | m_items = 0; \ |
| 237 | } \ |
| 238 | \ |
| 239 | size_type size() const { return m_items; } \ |
| 240 | size_type max_size() const { return size_type(-1); } \ |
| 241 | bool empty() const { return size() == 0; } \ |
| 242 | \ |
| 243 | const_iterator end() const { return const_iterator( 0, this ); } \ |
| 244 | iterator end() { return iterator( 0, this ); } \ |
| 245 | const_iterator begin() const \ |
| 246 | { return const_iterator( (Node*)GetFirstNode( m_tableBuckets, (_wxHashTable_NodeBase**)m_table ), this ); }; \ |
| 247 | iterator begin() \ |
| 248 | { return iterator( (Node*)GetFirstNode( m_tableBuckets, (_wxHashTable_NodeBase**)m_table ), this ); }; \ |
| 249 | \ |
| 250 | size_type erase( const const_key_type& key ) \ |
| 251 | { \ |
| 252 | Node** node = GetNodePtr( key ); \ |
| 253 | \ |
| 254 | if( !node ) \ |
| 255 | return 0; \ |
| 256 | \ |
| 257 | --m_items; \ |
| 258 | Node* temp = (*node)->m_next(); \ |
| 259 | delete *node; \ |
| 260 | (*node) = temp; \ |
| 261 | if( SHOULD_SHRINK( m_tableBuckets, m_items ) ) \ |
| 262 | ResizeTable( GetPreviousPrime( (unsigned long) m_tableBuckets ) - 1 ); \ |
| 263 | return 1; \ |
| 264 | } \ |
| 265 | \ |
| 266 | protected: \ |
| 267 | static size_type GetBucketForNode( Self* ht, Node* node ) \ |
| 268 | { \ |
| 269 | return ht->m_hasher( ht->m_getKey( node->m_value ) ) \ |
| 270 | % ht->m_tableBuckets; \ |
| 271 | } \ |
| 272 | static Node* CopyNode( Node* node ) { return new Node( *node ); } \ |
| 273 | \ |
| 274 | Node* GetOrCreateNode( const value_type& value ) \ |
| 275 | { \ |
| 276 | const const_key_type& key = m_getKey( value ); \ |
| 277 | size_t bucket = m_hasher( key ) % m_tableBuckets; \ |
| 278 | Node* node = m_table[bucket]; \ |
| 279 | \ |
| 280 | while( node ) \ |
| 281 | { \ |
| 282 | if( m_equals( m_getKey( node->m_value ), key ) ) \ |
| 283 | return node; \ |
| 284 | node = node->m_next(); \ |
| 285 | } \ |
| 286 | return CreateNode( value , bucket); \ |
| 287 | }\ |
| 288 | Node * CreateNode( const value_type& value, size_t bucket ) \ |
| 289 | {\ |
| 290 | Node* node = new Node( value ); \ |
| 291 | node->m_nxt = m_table[bucket]; \ |
| 292 | m_table[bucket] = node; \ |
| 293 | \ |
| 294 | /* must be after the node is inserted */ \ |
| 295 | ++m_items; \ |
| 296 | if( SHOULD_GROW( m_tableBuckets, m_items ) ) \ |
| 297 | ResizeTable( m_tableBuckets ); \ |
| 298 | \ |
| 299 | return node; \ |
| 300 | } \ |
| 301 | void CreateNodeLast( const value_type& value ) \ |
| 302 | { \ |
| 303 | size_t bucket = m_hasher( m_getKey(value) ) % m_tableBuckets; \ |
| 304 | Node* curr = m_table[bucket], \ |
| 305 | * next = m_table[bucket]; \ |
| 306 | while( next ) { curr = next; next = next->m_next(); } \ |
| 307 | Node** ptr = curr ? (Node**)&curr->m_nxt : &m_table[bucket]; \ |
| 308 | *ptr = new Node( value ); \ |
| 309 | /* must be after the node is inserted */ \ |
| 310 | ++m_items; \ |
| 311 | if( SHOULD_GROW( m_tableBuckets, m_items ) ) \ |
| 312 | ResizeTable( m_tableBuckets ); \ |
| 313 | } \ |
| 314 | void CreateNode( const value_type& value ) \ |
| 315 | {\ |
| 316 | CreateNode(value, m_hasher( m_getKey(value) ) % m_tableBuckets ); \ |
| 317 | }\ |
| 318 | \ |
| 319 | /* returns NULL if not found */ \ |
| 320 | Node** GetNodePtr( const const_key_type& key ) const \ |
| 321 | { \ |
| 322 | unsigned long hash = m_hasher( key ); \ |
| 323 | Node** node = &m_table[hash % m_tableBuckets]; \ |
| 324 | \ |
| 325 | while( *node ) \ |
| 326 | { \ |
| 327 | if( m_equals( m_getKey( (*node)->m_value ), key ) ) \ |
| 328 | return node; \ |
| 329 | node = (Node**)&(*node)->m_nxt; \ |
| 330 | } \ |
| 331 | \ |
| 332 | return 0; \ |
| 333 | } \ |
| 334 | \ |
| 335 | /* returns NULL if not found */ \ |
| 336 | /* expressing it in terms of GetNodePtr is 5-8% slower :-( */ \ |
| 337 | Node* GetNode( const const_key_type& key ) const \ |
| 338 | { \ |
| 339 | unsigned long hash = m_hasher( key ); \ |
| 340 | Node* node = m_table[hash % m_tableBuckets]; \ |
| 341 | \ |
| 342 | while( node ) \ |
| 343 | { \ |
| 344 | if( m_equals( m_getKey( node->m_value ), key ) ) \ |
| 345 | return node; \ |
| 346 | node = node->m_next(); \ |
| 347 | } \ |
| 348 | \ |
| 349 | return 0; \ |
| 350 | } \ |
| 351 | \ |
| 352 | void ResizeTable( size_t newSize ) \ |
| 353 | { \ |
| 354 | newSize = GetNextPrime( (unsigned long)newSize ); \ |
| 355 | Node** srcTable = m_table; \ |
| 356 | size_t srcBuckets = m_tableBuckets; \ |
| 357 | m_table = (Node**)AllocTable( newSize ); \ |
| 358 | m_tableBuckets = newSize; \ |
| 359 | \ |
| 360 | CopyHashTable( (_wxHashTable_NodeBase**)srcTable, srcBuckets, \ |
| 361 | this, (_wxHashTable_NodeBase**)m_table, newSize, \ |
| 362 | (BucketFromNode)GetBucketForNode,\ |
| 363 | (ProcessNode)&DummyProcessNode ); \ |
| 364 | free(srcTable); \ |
| 365 | } \ |
| 366 | \ |
| 367 | /* this must be called _after_ m_table has been cleaned */ \ |
| 368 | void HashCopy( const Self& ht ) \ |
| 369 | { \ |
| 370 | ResizeTable( ht.size() ); \ |
| 371 | CopyHashTable( (_wxHashTable_NodeBase**)ht.m_table, ht.m_tableBuckets,\ |
| 372 | (_wxHashTableBase2*)this, \ |
| 373 | (_wxHashTable_NodeBase**)m_table, m_tableBuckets, \ |
| 374 | (BucketFromNode)GetBucketForNode, \ |
| 375 | (ProcessNode)CopyNode ); \ |
| 376 | } \ |
| 377 | }; |
| 378 | |
| 379 | // defines an STL-like pair class CLASSNAME storing two fields: first of type |
| 380 | // KEY_T and second of type VALUE_T |
| 381 | #define _WX_DECLARE_PAIR( KEY_T, VALUE_T, CLASSNAME, CLASSEXP ) \ |
| 382 | CLASSEXP CLASSNAME \ |
| 383 | { \ |
| 384 | public: \ |
| 385 | typedef KEY_T t1; \ |
| 386 | typedef VALUE_T t2; \ |
| 387 | typedef const KEY_T const_t1; \ |
| 388 | typedef const VALUE_T const_t2; \ |
| 389 | \ |
| 390 | CLASSNAME( const const_t1& f, const const_t2& s ):first(t1(f)),second(t2(s)) {} \ |
| 391 | \ |
| 392 | t1 first; \ |
| 393 | t2 second; \ |
| 394 | }; |
| 395 | |
| 396 | // defines the class CLASSNAME returning the key part (of type KEY_T) from a |
| 397 | // pair of type PAIR_T |
| 398 | #define _WX_DECLARE_HASH_MAP_KEY_EX( KEY_T, PAIR_T, CLASSNAME, CLASSEXP ) \ |
| 399 | CLASSEXP CLASSNAME \ |
| 400 | { \ |
| 401 | typedef KEY_T key_type; \ |
| 402 | typedef PAIR_T pair_type; \ |
| 403 | typedef const key_type const_key_type; \ |
| 404 | typedef const pair_type const_pair_type; \ |
| 405 | typedef const_key_type& const_key_reference; \ |
| 406 | typedef const_pair_type& const_pair_reference; \ |
| 407 | public: \ |
| 408 | CLASSNAME() { } \ |
| 409 | const_key_reference operator()( const_pair_reference pair ) const { return pair.first; }\ |
| 410 | \ |
| 411 | /* the dummy assignment operator is needed to suppress compiler */ \ |
| 412 | /* warnings from hash table class' operator=(): gcc complains about */ \ |
| 413 | /* "statement with no effect" without it */ \ |
| 414 | CLASSNAME& operator=(const CLASSNAME&) { return *this; } \ |
| 415 | }; |
| 416 | |
| 417 | // grow/shrink predicates |
| 418 | inline bool never_grow( size_t, size_t ) { return FALSE; } |
| 419 | inline bool never_shrink( size_t, size_t ) { return FALSE; } |
| 420 | inline bool grow_lf70( size_t buckets, size_t items ) |
| 421 | { |
| 422 | return float(items)/float(buckets) >= 0.85; |
| 423 | } |
| 424 | |
| 425 | // ---------------------------------------------------------------------------- |
| 426 | // hashing and comparison functors |
| 427 | // ---------------------------------------------------------------------------- |
| 428 | |
| 429 | // NB: implementation detail: all of these classes must have dummy assignment |
| 430 | // operators to suppress warnings about "statement with no effect" from gcc |
| 431 | // in the hash table class assignment operator (where they're assigned) |
| 432 | |
| 433 | // integer types |
| 434 | class WXDLLIMPEXP_BASE wxIntegerHash |
| 435 | { |
| 436 | public: |
| 437 | wxIntegerHash() { } |
| 438 | unsigned long operator()( long x ) const { return (unsigned long)x; } |
| 439 | unsigned long operator()( unsigned long x ) const { return x; } |
| 440 | unsigned long operator()( int x ) const { return (unsigned long)x; } |
| 441 | unsigned long operator()( unsigned int x ) const { return x; } |
| 442 | unsigned long operator()( short x ) const { return (unsigned long)x; } |
| 443 | unsigned long operator()( unsigned short x ) const { return x; } |
| 444 | |
| 445 | wxIntegerHash& operator=(const wxIntegerHash&) { return *this; } |
| 446 | }; |
| 447 | |
| 448 | class WXDLLIMPEXP_BASE wxIntegerEqual |
| 449 | { |
| 450 | public: |
| 451 | wxIntegerEqual() { } |
| 452 | bool operator()( long a, long b ) const { return a == b; } |
| 453 | bool operator()( unsigned long a, unsigned long b ) const { return a == b; } |
| 454 | bool operator()( int a, int b ) const { return a == b; } |
| 455 | bool operator()( unsigned int a, unsigned int b ) const { return a == b; } |
| 456 | bool operator()( short a, short b ) const { return a == b; } |
| 457 | bool operator()( unsigned short a, unsigned short b ) const { return a == b; } |
| 458 | |
| 459 | wxIntegerEqual& operator=(const wxIntegerEqual&) { return *this; } |
| 460 | }; |
| 461 | |
| 462 | // pointers |
| 463 | class WXDLLIMPEXP_BASE wxPointerHash |
| 464 | { |
| 465 | public: |
| 466 | wxPointerHash() { } |
| 467 | |
| 468 | // TODO: this might not work well on architectures with 64 bit pointers but |
| 469 | // 32 bit longs, we should use % ULONG_MAX there |
| 470 | unsigned long operator()( const void* k ) const { return (unsigned long)wxPtrToULong(k); } |
| 471 | |
| 472 | wxPointerHash& operator=(const wxPointerHash&) { return *this; } |
| 473 | }; |
| 474 | |
| 475 | class WXDLLIMPEXP_BASE wxPointerEqual |
| 476 | { |
| 477 | public: |
| 478 | wxPointerEqual() { } |
| 479 | bool operator()( const void* a, const void* b ) const { return a == b; } |
| 480 | |
| 481 | wxPointerEqual& operator=(const wxPointerEqual&) { return *this; } |
| 482 | }; |
| 483 | |
| 484 | // wxString, char*, wxChar* |
| 485 | class WXDLLIMPEXP_BASE wxStringHash |
| 486 | { |
| 487 | public: |
| 488 | wxStringHash() {} |
| 489 | unsigned long operator()( const wxString& x ) const |
| 490 | { return wxCharStringHash( x.c_str() ); } |
| 491 | unsigned long operator()( const wxChar* x ) const |
| 492 | { return wxCharStringHash( x ); } |
| 493 | static unsigned long wxCharStringHash( const wxChar* ); |
| 494 | #if wxUSE_UNICODE |
| 495 | unsigned long operator()( const char* x ) const |
| 496 | { return charStringHash( x ); } |
| 497 | static unsigned long charStringHash( const char* ); |
| 498 | #endif // wxUSE_UNICODE |
| 499 | |
| 500 | wxStringHash& operator=(const wxStringHash&) { return *this; } |
| 501 | }; |
| 502 | |
| 503 | class WXDLLIMPEXP_BASE wxStringEqual |
| 504 | { |
| 505 | public: |
| 506 | wxStringEqual() {} |
| 507 | bool operator()( const wxString& a, const wxString& b ) const |
| 508 | { return a == b; } |
| 509 | bool operator()( const wxChar* a, const wxChar* b ) const |
| 510 | { return wxStrcmp( a, b ) == 0; } |
| 511 | #if wxUSE_UNICODE |
| 512 | bool operator()( const char* a, const char* b ) const |
| 513 | { return strcmp( a, b ) == 0; } |
| 514 | #endif // wxUSE_UNICODE |
| 515 | |
| 516 | wxStringEqual& operator=(const wxStringEqual&) { return *this; } |
| 517 | }; |
| 518 | |
| 519 | #define _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, CLASSEXP ) \ |
| 520 | _WX_DECLARE_PAIR( KEY_T, VALUE_T, CLASSNAME##_wxImplementation_Pair, CLASSEXP ) \ |
| 521 | _WX_DECLARE_HASH_MAP_KEY_EX( KEY_T, CLASSNAME##_wxImplementation_Pair, CLASSNAME##_wxImplementation_KeyEx, CLASSEXP ) \ |
| 522 | _WX_DECLARE_HASHTABLE( CLASSNAME##_wxImplementation_Pair, KEY_T, HASH_T, CLASSNAME##_wxImplementation_KeyEx, KEY_EQ_T, CLASSNAME##_wxImplementation_HashTable, CLASSEXP, grow_lf70, never_shrink ) \ |
| 523 | CLASSEXP CLASSNAME:public CLASSNAME##_wxImplementation_HashTable \ |
| 524 | { \ |
| 525 | public: \ |
| 526 | typedef VALUE_T mapped_type; \ |
| 527 | \ |
| 528 | wxEXPLICIT CLASSNAME( size_type hint = 100, hasher hf = hasher(), \ |
| 529 | key_equal eq = key_equal() ) \ |
| 530 | : CLASSNAME##_wxImplementation_HashTable( hint, hf, eq, \ |
| 531 | CLASSNAME##_wxImplementation_KeyEx() ) {} \ |
| 532 | \ |
| 533 | mapped_type& operator[]( const const_key_type& key ) \ |
| 534 | { \ |
| 535 | return GetOrCreateNode( CLASSNAME##_wxImplementation_Pair( key, mapped_type() ) )->m_value.second; \ |
| 536 | } \ |
| 537 | \ |
| 538 | const_iterator find( const const_key_type& key ) const \ |
| 539 | { \ |
| 540 | return const_iterator( GetNode( key ), this ); \ |
| 541 | } \ |
| 542 | \ |
| 543 | iterator find( const const_key_type& key ) \ |
| 544 | { \ |
| 545 | return iterator( GetNode( key ), this ); \ |
| 546 | } \ |
| 547 | \ |
| 548 | void insert( const value_type& v ) { (*this)[v.first] = v.second; } \ |
| 549 | \ |
| 550 | size_type erase( const key_type& k ) \ |
| 551 | { return CLASSNAME##_wxImplementation_HashTable::erase( k ); } \ |
| 552 | void erase( const iterator& it ) { erase( it->first ); } \ |
| 553 | void erase( const const_iterator& it ) { erase( it->first ); } \ |
| 554 | \ |
| 555 | /* count() == 0 | 1 */ \ |
| 556 | size_type count( const const_key_type& key ) \ |
| 557 | { return GetNode( key ) ? 1 : 0; } \ |
| 558 | } |
| 559 | |
| 560 | // these macros are to be used in the user code |
| 561 | #define WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME) \ |
| 562 | _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, class ) |
| 563 | |
| 564 | #define WX_DECLARE_STRING_HASH_MAP( VALUE_T, CLASSNAME ) \ |
| 565 | _WX_DECLARE_HASH_MAP( wxString, VALUE_T, wxStringHash, wxStringEqual, \ |
| 566 | CLASSNAME, class ) |
| 567 | |
| 568 | #define WX_DECLARE_VOIDPTR_HASH_MAP( VALUE_T, CLASSNAME ) \ |
| 569 | _WX_DECLARE_HASH_MAP( void*, VALUE_T, wxPointerHash, wxPointerEqual, \ |
| 570 | CLASSNAME, class ) |
| 571 | |
| 572 | // and these do exactly the same thing but should be used inside the |
| 573 | // library |
| 574 | #define WX_DECLARE_HASH_MAP_WITH_DECL( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, DECL) \ |
| 575 | _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, DECL ) |
| 576 | |
| 577 | #define WX_DECLARE_EXPORTED_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME) \ |
| 578 | WX_DECLARE_HASH_MAP_WITH_DECL( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, \ |
| 579 | CLASSNAME, class WXDLLEXPORT ) |
| 580 | |
| 581 | #define WX_DECLARE_STRING_HASH_MAP_WITH_DECL( VALUE_T, CLASSNAME, DECL ) \ |
| 582 | _WX_DECLARE_HASH_MAP( wxString, VALUE_T, wxStringHash, wxStringEqual, \ |
| 583 | CLASSNAME, DECL ) |
| 584 | |
| 585 | #define WX_DECLARE_EXPORTED_STRING_HASH_MAP( VALUE_T, CLASSNAME ) \ |
| 586 | WX_DECLARE_STRING_HASH_MAP_WITH_DECL( VALUE_T, CLASSNAME, \ |
| 587 | class WXDLLEXPORT ) |
| 588 | |
| 589 | #define WX_DECLARE_VOIDPTR_HASH_MAP_WITH_DECL( VALUE_T, CLASSNAME, DECL ) \ |
| 590 | _WX_DECLARE_HASH_MAP( void*, VALUE_T, wxPointerHash, wxPointerEqual, \ |
| 591 | CLASSNAME, DECL ) |
| 592 | |
| 593 | #define WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP( VALUE_T, CLASSNAME ) \ |
| 594 | WX_DECLARE_VOIDPTR_HASH_MAP_WITH_DECL( VALUE_T, CLASSNAME, \ |
| 595 | class WXDLLEXPORT ) |
| 596 | |
| 597 | // delete all hash elements |
| 598 | // |
| 599 | // NB: the class declaration of the hash elements must be visible from the |
| 600 | // place where you use this macro, otherwise the proper destructor may not |
| 601 | // be called (a decent compiler should give a warning about it, but don't |
| 602 | // count on it)! |
| 603 | #define WX_CLEAR_HASH_MAP(type, hashmap) \ |
| 604 | { \ |
| 605 | type::iterator it, en; \ |
| 606 | for( it = (hashmap).begin(), en = (hashmap).end(); it != en; ++it ) \ |
| 607 | delete it->second; \ |
| 608 | (hashmap).clear(); \ |
| 609 | } |
| 610 | |
| 611 | #endif // _WX_HASHMAP_H_ |
| 612 | |