]>
git.saurik.com Git - wxWidgets.git/blob - src/common/hashmap.cpp
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 ) */
26 static unsigned long DoStringHash(T
*k
)
28 unsigned long hash
= 0;
39 return hash
+ (hash
<< 15);
42 unsigned long wxStringHash::stringHash( const char* k
)
43 { return DoStringHash(k
); }
45 unsigned long wxStringHash::stringHash( const wchar_t* k
)
46 { return DoStringHash(k
); }
49 #if !wxUSE_STL || !defined(HAVE_STL_HASH_MAP)
52 const unsigned long _wxHashTableBase2::ms_primes
[prime_count
] =
55 53ul, 97ul, 193ul, 389ul, 769ul,
56 1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
57 49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
58 1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
59 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
60 1610612741ul, 3221225473ul, 4294967291ul
63 unsigned long _wxHashTableBase2::GetNextPrime( unsigned long n
)
65 const unsigned long* ptr
= &ms_primes
[0];
66 for( size_t i
= 0; i
< prime_count
; ++i
, ++ptr
)
72 /* someone might try to alloc a 2^32-element hash table */
73 wxFAIL_MSG( _T("hash table too big?") );
79 unsigned long _wxHashTableBase2::GetPreviousPrime( unsigned long n
)
81 const unsigned long* ptr
= &ms_primes
[prime_count
- 1];
83 for( size_t i
= 0; i
< prime_count
; ++i
, --ptr
)
93 void _wxHashTableBase2::DeleteNodes( size_t buckets
,
94 _wxHashTable_NodeBase
** table
,
99 for( i
= 0; i
< buckets
; ++i
)
101 _wxHashTable_NodeBase
* node
= table
[i
];
102 _wxHashTable_NodeBase
* tmp
;
112 memset( table
, 0, buckets
* sizeof(void*) );
115 void _wxHashTableBase2::CopyHashTable( _wxHashTable_NodeBase
** srcTable
,
117 _wxHashTableBase2
* dst
,
118 _wxHashTable_NodeBase
** dstTable
,
119 BucketFromNode func
, ProcessNode proc
)
121 for( size_t i
= 0; i
< srcBuckets
; ++i
)
123 _wxHashTable_NodeBase
* nextnode
;
125 for( _wxHashTable_NodeBase
* node
= srcTable
[i
]; node
; node
= nextnode
)
127 size_t bucket
= func( dst
, node
);
129 nextnode
= node
->m_nxt
;
130 _wxHashTable_NodeBase
* newnode
= proc( node
);
131 newnode
->m_nxt
= dstTable
[bucket
];
132 dstTable
[bucket
] = newnode
;
137 _wxHashTable_NodeBase
* _wxHashTableBase2::DummyProcessNode(_wxHashTable_NodeBase
* node
)
142 #endif // !wxUSE_STL || !defined(HAVE_STL_HASH_MAP)