]> git.saurik.com Git - wxWidgets.git/blame - src/common/hashmap.cpp
Update version to 2.9.4 in version.bkl too and rebake everything.
[wxWidgets.git] / src / common / hashmap.cpp
CommitLineData
0508ba2a 1/////////////////////////////////////////////////////////////////////////////
df69528b 2// Name: src/common/hashmap.cpp
0508ba2a
MB
3// Purpose: wxHashMap implementation
4// Author: Mattia Barbon
376e1129 5// Modified by:
0508ba2a
MB
6// Created: 29/01/2002
7// RCS-ID: $Id$
8// Copyright: (c) Mattia Barbon
65571936 9// Licence: wxWindows licence
0508ba2a
MB
10/////////////////////////////////////////////////////////////////////////////
11
0508ba2a
MB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
df69528b 16 #pragma hdrstop
0508ba2a
MB
17#endif
18
19#include "wx/hashmap.h"
20
0508ba2a
MB
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 ) */
ad78ab8c
VS
25template<typename T>
26static unsigned long DoStringHash(T *k)
0508ba2a
MB
27{
28 unsigned long hash = 0;
29
30 while( *k )
31 {
32 hash += *k++;
33 hash += (hash << 10);
34 hash ^= (hash >> 6);
35 }
36 hash += (hash << 3);
37 hash ^= (hash >> 11);
e05c8fa7 38
0508ba2a
MB
39 return hash + (hash << 15);
40}
41
ad78ab8c
VS
42unsigned long wxStringHash::stringHash( const char* k )
43 { return DoStringHash(k); }
0508ba2a 44
ad78ab8c
VS
45unsigned long wxStringHash::stringHash( const wchar_t* k )
46 { return DoStringHash(k); }
e05c8fa7 47
0508ba2a 48
6631cbd9 49#ifdef wxNEEDS_WX_HASH_MAP
bdcade0a 50
0508ba2a 51/* from SGI STL */
376e1129 52const unsigned long _wxHashTableBase2::ms_primes[prime_count] =
0508ba2a
MB
53{
54 7ul, 13ul, 29ul,
55 53ul, 97ul, 193ul, 389ul, 769ul,
56 1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
57 49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
58 1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
e05c8fa7 59 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
0508ba2a
MB
60 1610612741ul, 3221225473ul, 4294967291ul
61};
62
63unsigned long _wxHashTableBase2::GetNextPrime( unsigned long n )
64{
376e1129 65 const unsigned long* ptr = &ms_primes[0];
0508ba2a
MB
66 for( size_t i = 0; i < prime_count; ++i, ++ptr )
67 {
68 if( n < *ptr )
69 return *ptr;
70 }
71
72 /* someone might try to alloc a 2^32-element hash table */
9a83f860 73 wxFAIL_MSG( wxT("hash table too big?") );
376e1129 74
0508ba2a
MB
75 /* quiet warning */
76 return 0;
77}
78
79unsigned long _wxHashTableBase2::GetPreviousPrime( unsigned long n )
80{
376e1129 81 const unsigned long* ptr = &ms_primes[prime_count - 1];
0508ba2a
MB
82
83 for( size_t i = 0; i < prime_count; ++i, --ptr )
84 {
85 if( n > *ptr )
86 return *ptr;
87 }
88
89 /* quiet warning */
90 return 1;
91}
92
93void _wxHashTableBase2::DeleteNodes( size_t buckets,
94 _wxHashTable_NodeBase** table,
95 NodeDtor dtor )
96{
97 size_t i;
98
99 for( i = 0; i < buckets; ++i )
100 {
101 _wxHashTable_NodeBase* node = table[i];
102 _wxHashTable_NodeBase* tmp;
103
104 while( node )
105 {
872051d8 106 tmp = node->m_next;
0508ba2a
MB
107 dtor( node );
108 node = tmp;
109 }
110 }
111
112 memset( table, 0, buckets * sizeof(void*) );
113}
114
115void _wxHashTableBase2::CopyHashTable( _wxHashTable_NodeBase** srcTable,
116 size_t srcBuckets,
117 _wxHashTableBase2* dst,
118 _wxHashTable_NodeBase** dstTable,
119 BucketFromNode func, ProcessNode proc )
120{
1a6d9c76 121 for( size_t i = 0; i < srcBuckets; ++i )
0508ba2a
MB
122 {
123 _wxHashTable_NodeBase* nextnode;
e05c8fa7 124
0508ba2a
MB
125 for( _wxHashTable_NodeBase* node = srcTable[i]; node; node = nextnode )
126 {
127 size_t bucket = func( dst, node );
128
872051d8 129 nextnode = node->m_next;
0508ba2a 130 _wxHashTable_NodeBase* newnode = proc( node );
872051d8 131 newnode->m_next = dstTable[bucket];
1a6d9c76 132 dstTable[bucket] = newnode;
0508ba2a
MB
133 }
134 }
135}
136
137_wxHashTable_NodeBase* _wxHashTableBase2::DummyProcessNode(_wxHashTable_NodeBase* node)
138{
139 return node;
140}
141
6631cbd9 142#endif // wxNEEDS_WX_HASH_MAP