Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "hash.hxx"
21 : #include "strimp.hxx"
22 : #include <osl/diagnose.h>
23 : #include <sal/macros.h>
24 :
25 : struct StringHashTableImpl {
26 : sal_uInt32 nEntries;
27 : sal_uInt32 nSize;
28 : rtl_uString **pData;
29 : };
30 :
31 : typedef StringHashTableImpl StringHashTable;
32 :
33 : // Only for use in the implementation
34 : static StringHashTable *rtl_str_hash_new (sal_uInt32 nSize);
35 : static void rtl_str_hash_free (StringHashTable *pHash);
36 :
37 : StringHashTable *
38 817808 : getHashTable ()
39 : {
40 : static StringHashTable *pInternPool = NULL;
41 817808 : if (pInternPool == NULL) {
42 105 : static StringHashTable* pHash = rtl_str_hash_new(1024);
43 105 : pInternPool = pHash;
44 : }
45 817808 : return pInternPool;
46 : }
47 :
48 : // Better / smaller / faster hash set ....
49 :
50 : // TODO: add bottom bit-set list terminator to string list
51 :
52 : static sal_uInt32
53 251 : getNextSize (sal_uInt32 nSize)
54 : {
55 : // Sedgewick - Algorithms in C P577.
56 : static const sal_uInt32 nPrimes[] = { 1021, 2039, 4093, 8191, 16381, 32749,
57 : 65521, 131071,262139, 524287, 1048573,
58 : 2097143, 4194301, 8388593, 16777213,
59 : 33554393, 67108859, 134217689 };
60 :
61 737 : for (sal_uInt32 i = 0; i < SAL_N_ELEMENTS(nPrimes); i++)
62 : {
63 737 : if (nPrimes[i] > nSize)
64 251 : return nPrimes[i];
65 : }
66 0 : return nSize * 2;
67 : }
68 :
69 : static sal_uInt32
70 1046994 : hashString (rtl_uString *pString)
71 : {
72 : return (sal_uInt32) rtl_ustr_hashCode_WithLength (pString->buffer,
73 1046994 : pString->length);
74 : }
75 :
76 : static StringHashTable *
77 178 : rtl_str_hash_new (sal_uInt32 nSize)
78 : {
79 178 : StringHashTable *pHash = (StringHashTable *)malloc (sizeof (StringHashTable));
80 :
81 178 : pHash->nEntries = 0;
82 178 : pHash->nSize = getNextSize (nSize);
83 178 : pHash->pData = (rtl_uString **) calloc (sizeof (rtl_uString *), pHash->nSize);
84 :
85 178 : return pHash;
86 : }
87 :
88 : static void
89 73 : rtl_str_hash_free (StringHashTable *pHash)
90 : {
91 73 : if (!pHash)
92 73 : return;
93 73 : if (pHash->pData)
94 0 : free (pHash->pData);
95 73 : free (pHash);
96 : }
97 :
98 : static void
99 229259 : rtl_str_hash_insert_nonequal (StringHashTable *pHash,
100 : rtl_uString *pString)
101 : {
102 229259 : sal_uInt32 nHash = hashString (pString);
103 : sal_uInt32 n;
104 :
105 229259 : n = nHash % pHash->nSize;
106 510799 : while (pHash->pData[n] != NULL) {
107 52281 : n++;
108 52281 : if (n >= pHash->nSize)
109 0 : n = 0;
110 : }
111 229259 : pHash->pData[n] = pString;
112 229259 : }
113 :
114 : static void
115 73 : rtl_str_hash_resize (sal_uInt32 nNewSize)
116 : {
117 : sal_uInt32 i;
118 : StringHashTable *pNewHash;
119 73 : StringHashTable *pHash = getHashTable();
120 :
121 : OSL_ASSERT (nNewSize > pHash->nEntries);
122 :
123 73 : pNewHash = rtl_str_hash_new (nNewSize);
124 :
125 173528 : for (i = 0; i < pHash->nSize; i++)
126 : {
127 173455 : if (pHash->pData[i] != NULL)
128 86691 : rtl_str_hash_insert_nonequal (pNewHash, pHash->pData[i]);
129 : }
130 73 : pNewHash->nEntries = pHash->nEntries;
131 73 : free (pHash->pData);
132 73 : *pHash = *pNewHash;
133 73 : pNewHash->pData = NULL;
134 73 : rtl_str_hash_free (pNewHash);
135 73 : }
136 :
137 : static bool
138 824289 : compareEqual (rtl_uString *pStringA, rtl_uString *pStringB)
139 : {
140 824289 : if (pStringA == pStringB)
141 354941 : return true;
142 469348 : if (pStringA->length != pStringB->length)
143 338186 : return false;
144 : return !rtl_ustr_compare_WithLength( pStringA->buffer, pStringA->length,
145 131162 : pStringB->buffer, pStringB->length);
146 : }
147 :
148 : rtl_uString *
149 462794 : rtl_str_hash_intern (rtl_uString *pString,
150 : int can_return)
151 : {
152 462794 : sal_uInt32 nHash = hashString (pString);
153 : sal_uInt32 n;
154 : rtl_uString *pHashStr;
155 :
156 462794 : StringHashTable *pHash = getHashTable();
157 :
158 : // Should we resize ?
159 462794 : if (pHash->nEntries >= pHash->nSize/2)
160 73 : rtl_str_hash_resize (getNextSize(pHash->nSize));
161 :
162 462794 : n = nHash % pHash->nSize;
163 1165628 : while ((pHashStr = pHash->pData[n]) != NULL) {
164 347767 : if (compareEqual (pHashStr, pString))
165 : {
166 107727 : rtl_uString_acquire (pHashStr);
167 107727 : return pHashStr;
168 : }
169 240040 : n++;
170 240040 : if (n >= pHash->nSize)
171 2 : n = 0;
172 : }
173 :
174 355067 : if (!can_return)
175 : {
176 29226 : rtl_uString *pCopy = NULL;
177 29226 : rtl_uString_newFromString( &pCopy, pString );
178 29226 : pString = pCopy;
179 29226 : if (!pString)
180 0 : return NULL;
181 : }
182 :
183 355067 : if (!SAL_STRING_IS_STATIC (pString))
184 355066 : pString->refCount |= SAL_STRING_INTERN_FLAG;
185 355067 : pHash->pData[n] = pString;
186 355067 : pHash->nEntries++;
187 :
188 355067 : return pString;
189 : }
190 :
191 : void
192 354941 : rtl_str_hash_remove (rtl_uString *pString)
193 : {
194 : sal_uInt32 n;
195 354941 : sal_uInt32 nHash = hashString (pString);
196 : rtl_uString *pHashStr;
197 :
198 354941 : StringHashTable *pHash = getHashTable();
199 :
200 354941 : n = nHash % pHash->nSize;
201 831463 : while ((pHashStr = pHash->pData[n]) != NULL) {
202 476522 : if (compareEqual (pHashStr, pString))
203 354941 : break;
204 121581 : n++;
205 121581 : if (n >= pHash->nSize)
206 0 : n = 0;
207 : }
208 : OSL_ASSERT (pHash->pData[n] != 0);
209 354941 : if (pHash->pData[n] == NULL)
210 354941 : return;
211 :
212 354941 : pHash->pData[n++] = NULL;
213 354941 : pHash->nEntries--;
214 :
215 354941 : if (n >= pHash->nSize)
216 0 : n = 0;
217 :
218 852450 : while ((pHashStr = pHash->pData[n]) != NULL) {
219 142568 : pHash->pData[n] = NULL;
220 : // FIXME: rather unsophisticated and N^2 in chain-length, but robust.
221 142568 : rtl_str_hash_insert_nonequal (pHash, pHashStr);
222 142568 : n++;
223 142568 : if (n >= pHash->nSize)
224 0 : n = 0;
225 : }
226 : // FIXME: Should we down-size ?
227 : }
228 :
229 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|