1 Index: sal/rtl/source/hash.cxx
2 ===================================================================
3 RCS file: /cvs/porting/sal/rtl/source/hash.cxx,v
4 retrieving revision 1.3
6 --- sal/rtl/source/hash.cxx 19 Apr 2007 11:56:36 -0000 1.3
7 +++ sal/rtl/source/hash.cxx 15 Jun 2007 09:06:33 -0000
9 ************************************************************************/
11 // MARKER(update_precomp.py): autogen include statement, do not remove
12 -#include "precompiled_sal.hxx"
13 -#include "rtl/allocator.hxx"
14 +// #include "precompiled_sal.hxx"
18 +#include <osl/diagnose.h>
22 +#include "rtl/allocator.hxx"
28 pHash->erase(pString);
33 +// --------------------------- start here ---------------------------
35 +struct StringHashTableImpl {
36 + sal_uInt32 nEntries;
38 + rtl_uString **pData;
41 +// Better / smaller / faster hash set ....
43 +// TODO: add bottom bit-set list terminator to string list
46 +getNextSize (sal_uInt32 nSize)
48 + // Sedgewick - Algorithms in C P577.
49 + static const sal_uInt32 nPrimes[] = { 1021, 2039, 4093, 8191, 16381, 32749,
50 + 65521, 131071,262139, 524287, 1048573,
51 + 2097143, 4194301, 8388593, 16777213,
52 + 33554393, 67108859, 134217689 };
53 + #define NUM_PRIMES (sizeof (nPrimes)/ sizeof (nPrimes[0]))
54 + for (int i = 0; i < NUM_PRIMES; i++)
56 + if (nPrimes[i] > nSize)
63 +hashString (rtl_uString *pString)
65 + return (sal_uInt32) rtl_ustr_hashCode_WithLength (pString->buffer,
70 +rtl_str_hash_new (sal_uInt32 nSize)
72 + StringHashTable *pHash = (StringHashTable *)malloc (sizeof (StringHashTable));
74 + pHash->nEntries = 0;
75 + pHash->nSize = getNextSize (nSize);
76 + pHash->pData = (rtl_uString **) calloc (sizeof (rtl_uString *), pHash->nSize);
82 +rtl_str_hash_insert_nonequal (StringHashTable *pHash,
83 + rtl_uString *pString)
85 + sal_uInt32 nHash = hashString (pString);
87 + rtl_uString *pHashStr;
89 + n = nHash % pHash->nSize;
90 + while ((pHashStr = pHash->pData[n]) != NULL) {
92 + if (n >= pHash->nSize)
95 + pHash->pData[n] = pString;
99 +rtl_str_hash_resize (StringHashTable *pHash,
100 + sal_uInt32 nNewSize)
103 + StringHashTable *pNewHash;
105 + OSL_ASSERT (nNewSize > pHash->nEntries);
107 + pNewHash = rtl_str_hash_new (nNewSize);
109 + for (i = 0; i < pHash->nSize; i++)
111 + if (pHash->pData[i] != NULL)
112 + rtl_str_hash_insert_nonequal (pNewHash, pHash->pData[i]);
114 + pNewHash->nEntries = pHash->nEntries;
115 + free (pHash->pData);
116 + *pHash = *pNewHash;
117 + pNewHash->pData = NULL;
118 + rtl_str_hash_free (pNewHash);
122 +rtl_str_hash_free (StringHashTable *pHash)
127 + free (pHash->pData);
132 +compareEqual (rtl_uString *pStringA, rtl_uString *pStringB)
134 + if (pStringA == pStringB)
136 + if (pStringA->length != pStringB->length)
138 + return !rtl_ustr_compare_WithLength( pStringA->buffer, pStringA->length,
139 + pStringB->buffer, pStringB->length);
143 +rtl_str_hash_intern (StringHashTable *pHash,
144 + rtl_uString *pString,
147 + sal_uInt32 nHash = hashString (pString);
149 + rtl_uString *pHashStr;
151 + // Should we resize ?
152 + if (pHash->nEntries >= pHash->nSize/2)
153 + rtl_str_hash_resize (pHash, getNextSize(pHash->nSize));
155 + n = nHash % pHash->nSize;
156 + while ((pHashStr = pHash->pData[n]) != NULL) {
157 + if (compareEqual (pHashStr, pString))
159 + rtl_uString_acquire (pHashStr);
163 + if (n >= pHash->nSize)
169 + rtl_uString *pCopy = NULL;
170 + rtl_uString_newFromString( &pCopy, pString );
176 + if (!SAL_STRING_IS_STATIC (pString))
177 + pString->refCount |= SAL_STRING_INTERN_FLAG;
178 + pHash->pData[n] = pString;
185 +rtl_str_hash_remove (StringHashTable *pHash,
186 + rtl_uString *pString)
189 + sal_uInt32 nHash = hashString (pString);
190 + rtl_uString *pHashStr;
192 + n = nHash % pHash->nSize;
193 + while ((pHashStr = pHash->pData[n]) != NULL) {
194 + if (compareEqual (pHashStr, pString))
197 + if (n >= pHash->nSize)
200 + OSL_ASSERT (pHash->pData[n] != 0);
201 + if (pHash->pData[n] == NULL)
204 + pHash->pData[n++] = NULL;
207 + while ((pHashStr = pHash->pData[n]) != NULL) {
208 + pHash->pData[n] = NULL;
209 + // FIXME: rather unsophisticated and N^2 in chain-length, but robust.
210 + rtl_str_hash_insert_nonequal (pHash, pHashStr);
212 + if (n >= pHash->nSize)
215 + // FIXME: Should we down-size ?