1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/hashmap.cpp
3 // Purpose: wxHashMap implementation
4 // Author: Mattia Barbon
8 // Copyright: (c) Mattia Barbon
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #include "wx/hashmap.h"
21 /* FYI: This is the "One-at-a-Time" algorithm by Bob Jenkins */
22 /* from requirements by Colin Plumb. */
23 /* (http://burtleburtle.net/bob/hash/doobs.html) */
24 /* adapted from Perl sources ( hv.h ) */
25 unsigned long wxStringHash::wxCharStringHash( const wxChar* k )
27 unsigned long hash = 0;
38 return hash + (hash << 15);
42 unsigned long wxStringHash::charStringHash( const char* k )
44 unsigned long hash = 0;
55 return hash + (hash << 15);
59 #if !wxUSE_STL || !defined(HAVE_STL_HASH_MAP)
62 const unsigned long _wxHashTableBase2::ms_primes[prime_count] =
65 53ul, 97ul, 193ul, 389ul, 769ul,
66 1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
67 49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
68 1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
69 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
70 1610612741ul, 3221225473ul, 4294967291ul
73 unsigned long _wxHashTableBase2::GetNextPrime( unsigned long n )
75 const unsigned long* ptr = &ms_primes[0];
76 for( size_t i = 0; i < prime_count; ++i, ++ptr )
82 /* someone might try to alloc a 2^32-element hash table */
83 wxFAIL_MSG( _T("hash table too big?") );
89 unsigned long _wxHashTableBase2::GetPreviousPrime( unsigned long n )
91 const unsigned long* ptr = &ms_primes[prime_count - 1];
93 for( size_t i = 0; i < prime_count; ++i, --ptr )
103 void _wxHashTableBase2::DeleteNodes( size_t buckets,
104 _wxHashTable_NodeBase** table,
109 for( i = 0; i < buckets; ++i )
111 _wxHashTable_NodeBase* node = table[i];
112 _wxHashTable_NodeBase* tmp;
122 memset( table, 0, buckets * sizeof(void*) );
125 void _wxHashTableBase2::CopyHashTable( _wxHashTable_NodeBase** srcTable,
127 _wxHashTableBase2* dst,
128 _wxHashTable_NodeBase** dstTable,
129 BucketFromNode func, ProcessNode proc )
131 for( size_t i = 0; i < srcBuckets; ++i )
133 _wxHashTable_NodeBase* nextnode;
135 for( _wxHashTable_NodeBase* node = srcTable[i]; node; node = nextnode )
137 size_t bucket = func( dst, node );
139 nextnode = node->m_nxt;
140 _wxHashTable_NodeBase* newnode = proc( node );
141 newnode->m_nxt = dstTable[bucket];
142 dstTable[bucket] = newnode;
147 _wxHashTable_NodeBase* _wxHashTableBase2::DummyProcessNode(_wxHashTable_NodeBase* node)
152 #endif // !wxUSE_STL || !defined(HAVE_STL_HASH_MAP)