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