hashmap.h 26 KB

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