X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f5db5af932c3255815542f4151b02367ea73c208..a5de860f0b8994b2d87d0a0d3c3dedf750fa7ddb:/include/wx/hashmap.h diff --git a/include/wx/hashmap.h b/include/wx/hashmap.h index b8689b86f6..47d583b840 100644 --- a/include/wx/hashmap.h +++ b/include/wx/hashmap.h @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: hashmap.h +// Name: wx/hashmap.h // Purpose: wxHashMap class // Author: Mattia Barbon // Modified by: @@ -12,11 +12,8 @@ #ifndef _WX_HASHMAP_H_ #define _WX_HASHMAP_H_ -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) -#pragma interface "hashmap.h" -#endif - #include "wx/string.h" +#include "wx/wxcrt.h" #if (defined(HAVE_EXT_HASH_MAP) || defined(HAVE_HASH_MAP)) \ && (defined(HAVE_GNU_CXX_HASH_MAP) || defined(HAVE_STD_HASH_MAP)) @@ -38,7 +35,7 @@ #endif #define _WX_DECLARE_HASH_MAP( KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME, CLASSEXP ) \ - typedef WX_HASH_MAP_NAMESPACE::hash_map< KEY_T, VALUE_T, HASH_T, KEY_EQ_T > CLASSNAME; + typedef WX_HASH_MAP_NAMESPACE::hash_map< KEY_T, VALUE_T, HASH_T, KEY_EQ_T > CLASSNAME #else // !wxUSE_STL || !defined(HAVE_STL_HASH_MAP) @@ -147,8 +144,6 @@ public: \ value_type m_value; \ }; \ \ - CLASSEXP Iterator; \ - friend CLASSEXP Iterator; \ protected: \ static void DeleteNode( _wxHashTable_NodeBase* node ) \ { \ @@ -189,6 +184,7 @@ public: \ m_node = next ? next : GetNextNode(); \ } \ }; \ + friend class Iterator; \ \ public: \ CLASSEXP iterator : public Iterator \ @@ -206,6 +202,7 @@ public: \ { \ public: \ const_iterator() : Iterator() {} \ + const_iterator(iterator i) : Iterator(i) {} \ const_iterator( Node* node, const Self* ht ) \ : Iterator( node, (Self*)ht ) {} \ const_iterator& operator++() { PlusPlus();return *this; } \ @@ -274,9 +271,9 @@ public: \ const_iterator end() const { return const_iterator( 0, this ); } \ iterator end() { return iterator( 0, this ); } \ const_iterator begin() const \ - { return const_iterator( (Node*)GetFirstNode( m_tableBuckets, (_wxHashTable_NodeBase**)m_table ), this ); }; \ + { return const_iterator( (Node*)GetFirstNode( m_tableBuckets, (_wxHashTable_NodeBase**)m_table ), this ); } \ iterator begin() \ - { return iterator( (Node*)GetFirstNode( m_tableBuckets, (_wxHashTable_NodeBase**)m_table ), this ); }; \ + { return iterator( (Node*)GetFirstNode( m_tableBuckets, (_wxHashTable_NodeBase**)m_table ), this ); } \ \ size_type erase( const const_key_type& key ) \ { \ @@ -341,25 +338,26 @@ protected: \ /* returns NULL if not found */ \ Node** GetNodePtr( const const_key_type& key ) const \ { \ - unsigned long hash = m_hasher( key ); \ - Node** node = &m_table[hash % m_tableBuckets]; \ + size_t bucket = m_hasher( key ) % m_tableBuckets; \ + Node** node = &m_table[bucket]; \ \ while( *node ) \ { \ if( m_equals( m_getKey( (*node)->m_value ), key ) ) \ return node; \ + /* Tell the compiler to not do any strict-aliasing assumptions with a void cast? Can we make such a runtime guarantee? */ \ node = (Node**)&(*node)->m_nxt; \ } \ \ - return 0; \ + return NULL; \ } \ \ /* returns NULL if not found */ \ /* expressing it in terms of GetNodePtr is 5-8% slower :-( */ \ Node* GetNode( const const_key_type& key ) const \ { \ - unsigned long hash = m_hasher( key ); \ - Node* node = m_table[hash % m_tableBuckets]; \ + size_t bucket = m_hasher( key ) % m_tableBuckets; \ + Node* node = m_table[bucket]; \ \ while( node ) \ { \ @@ -465,6 +463,23 @@ class WXDLLIMPEXP_BASE wxIntegerHash WX_HASH_MAP_NAMESPACE::hash uintHash; WX_HASH_MAP_NAMESPACE::hash shortHash; WX_HASH_MAP_NAMESPACE::hash ushortHash; + +#if defined wxLongLong_t && !defined wxLongLongIsLong + // hash ought to work but doesn't on some compilers + #if (!defined SIZEOF_LONG_LONG && SIZEOF_LONG == 4) \ + || (defined SIZEOF_LONG_LONG && SIZEOF_LONG_LONG == SIZEOF_LONG * 2) + size_t longlongHash( wxLongLong_t x ) const + { + return longHash( wx_truncate_cast(long, x) ) ^ + longHash( wx_truncate_cast(long, x >> (sizeof(long) * 8)) ); + } + #elif defined SIZEOF_LONG_LONG && SIZEOF_LONG_LONG == SIZEOF_LONG + WX_HASH_MAP_NAMESPACE::hash longlongHash; + #else + WX_HASH_MAP_NAMESPACE::hash longlongHash; + #endif +#endif + public: wxIntegerHash() { } size_t operator()( long x ) const { return longHash( x ); } @@ -473,6 +488,10 @@ public: size_t operator()( unsigned int x ) const { return uintHash( x ); } size_t operator()( short x ) const { return shortHash( x ); } size_t operator()( unsigned short x ) const { return ushortHash( x ); } +#if defined wxLongLong_t && !defined wxLongLongIsLong + size_t operator()( wxLongLong_t x ) const { return longlongHash(x); } + size_t operator()( wxULongLong_t x ) const { return longlongHash(x); } +#endif wxIntegerHash& operator=(const wxIntegerHash&) { return *this; } }; @@ -490,6 +509,10 @@ public: unsigned long operator()( unsigned int x ) const { return x; } unsigned long operator()( short x ) const { return (unsigned long)x; } unsigned long operator()( unsigned short x ) const { return x; } +#if defined wxLongLong_t && !defined wxLongLongIsLong + wxULongLong_t operator()( wxLongLong_t x ) const { return wx_static_cast(wxULongLong_t, x); } + wxULongLong_t operator()( wxULongLong_t x ) const { return x; } +#endif wxIntegerHash& operator=(const wxIntegerHash&) { return *this; } }; @@ -506,6 +529,10 @@ public: bool operator()( unsigned int a, unsigned int b ) const { return a == b; } bool operator()( short a, short b ) const { return a == b; } bool operator()( unsigned short a, unsigned short b ) const { return a == b; } +#if defined wxLongLong_t && !defined wxLongLongIsLong + bool operator()( wxLongLong_t a, wxLongLong_t b ) const { return a == b; } + bool operator()( wxULongLong_t a, wxULongLong_t b ) const { return a == b; } +#endif wxIntegerEqual& operator=(const wxIntegerEqual&) { return *this; } }; @@ -534,21 +561,29 @@ public: wxPointerEqual& operator=(const wxPointerEqual&) { return *this; } }; -// wxString, char*, wxChar* +// wxString, char*, wchar_t* class WXDLLIMPEXP_BASE wxStringHash { public: wxStringHash() {} unsigned long operator()( const wxString& x ) const - { return wxCharStringHash( x.c_str() ); } - unsigned long operator()( const wxChar* x ) const - { return wxCharStringHash( x ); } - static unsigned long wxCharStringHash( const wxChar* ); -#if wxUSE_UNICODE + { return stringHash( x.wx_str() ); } + unsigned long operator()( const wchar_t* x ) const + { return stringHash( x ); } unsigned long operator()( const char* x ) const - { return charStringHash( x ); } - static unsigned long charStringHash( const char* ); -#endif // wxUSE_UNICODE + { return stringHash( x ); } + +#if WXWIN_COMPATIBILITY_2_8 + static unsigned long wxCharStringHash( const wxChar* x ) + { return stringHash(x); } + #if wxUSE_UNICODE + static unsigned long charStringHash( const char* x ) + { return stringHash(x); } + #endif +#endif // WXWIN_COMPATIBILITY_2_8 + + static unsigned long stringHash( const wchar_t* ); + static unsigned long stringHash( const char* ); wxStringHash& operator=(const wxStringHash&) { return *this; } }; @@ -622,7 +657,10 @@ public: \ \ /* count() == 0 | 1 */ \ size_type count( const const_key_type& key ) \ - { return GetNode( key ) ? 1 : 0; } \ + { \ + /* explicit cast needed to suppress CodeWarrior warnings */ \ + return (size_type)(GetNode( key ) ? 1 : 0); \ + } \ } #endif // !wxUSE_STL || !defined(HAVE_STL_HASH_MAP) @@ -686,4 +724,3 @@ WX_DECLARE_HASH_MAP_WITH_DECL( long, long, wxIntegerHash, wxIntegerEqual, #endif // _WX_HASHMAP_H_ -