lok: Don't attempt to select the exact text after a failed search.
[LibreOffice.git] / sal / rtl / hash.cxx
blob03ae02dcc4eeb0f9ebfc33dab1152ebd9e972b4a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "hash.hxx"
21 #include "strimp.hxx"
22 #include <osl/diagnose.h>
23 #include <sal/macros.h>
25 struct StringHashTableImpl {
26 sal_uInt32 nEntries;
27 sal_uInt32 nSize;
28 rtl_uString **pData;
31 typedef StringHashTableImpl StringHashTable;
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);
37 StringHashTable *
38 getHashTable ()
40 static StringHashTable *pInternPool = NULL;
41 if (pInternPool == NULL) {
42 static StringHashTable* pHash = rtl_str_hash_new(1024);
43 pInternPool = pHash;
45 return pInternPool;
48 // Better / smaller / faster hash set ....
50 // TODO: add bottom bit-set list terminator to string list
52 static sal_uInt32
53 getNextSize (sal_uInt32 nSize)
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 };
61 for (sal_uInt32 i = 0; i < SAL_N_ELEMENTS(nPrimes); i++)
63 if (nPrimes[i] > nSize)
64 return nPrimes[i];
66 return nSize * 2;
69 static sal_uInt32
70 hashString (rtl_uString *pString)
72 return (sal_uInt32) rtl_ustr_hashCode_WithLength (pString->buffer,
73 pString->length);
76 static StringHashTable *
77 rtl_str_hash_new (sal_uInt32 nSize)
79 StringHashTable *pHash = static_cast<StringHashTable *>(malloc (sizeof (StringHashTable)));
81 pHash->nEntries = 0;
82 pHash->nSize = getNextSize (nSize);
83 pHash->pData = static_cast<rtl_uString **>(calloc (sizeof (rtl_uString *), pHash->nSize));
85 return pHash;
88 static void
89 rtl_str_hash_free (StringHashTable *pHash)
91 if (!pHash)
92 return;
93 if (pHash->pData)
94 free (pHash->pData);
95 free (pHash);
98 static void
99 rtl_str_hash_insert_nonequal (StringHashTable *pHash,
100 rtl_uString *pString)
102 sal_uInt32 nHash = hashString (pString);
103 sal_uInt32 n;
105 n = nHash % pHash->nSize;
106 while (pHash->pData[n] != NULL) {
107 n++;
108 if (n >= pHash->nSize)
109 n = 0;
111 pHash->pData[n] = pString;
114 static void
115 rtl_str_hash_resize (sal_uInt32 nNewSize)
117 sal_uInt32 i;
118 StringHashTable *pNewHash;
119 StringHashTable *pHash = getHashTable();
121 OSL_ASSERT (nNewSize > pHash->nEntries);
123 pNewHash = rtl_str_hash_new (nNewSize);
125 for (i = 0; i < pHash->nSize; i++)
127 if (pHash->pData[i] != NULL)
128 rtl_str_hash_insert_nonequal (pNewHash, pHash->pData[i]);
130 pNewHash->nEntries = pHash->nEntries;
131 free (pHash->pData);
132 *pHash = *pNewHash;
133 pNewHash->pData = NULL;
134 rtl_str_hash_free (pNewHash);
137 static bool
138 compareEqual (rtl_uString *pStringA, rtl_uString *pStringB)
140 if (pStringA == pStringB)
141 return true;
142 if (pStringA->length != pStringB->length)
143 return false;
144 return !rtl_ustr_compare_WithLength( pStringA->buffer, pStringA->length,
145 pStringB->buffer, pStringB->length);
148 rtl_uString *
149 rtl_str_hash_intern (rtl_uString *pString,
150 int can_return)
152 sal_uInt32 nHash = hashString (pString);
153 sal_uInt32 n;
154 rtl_uString *pHashStr;
156 StringHashTable *pHash = getHashTable();
158 // Should we resize ?
159 if (pHash->nEntries >= pHash->nSize/2)
160 rtl_str_hash_resize (getNextSize(pHash->nSize));
162 n = nHash % pHash->nSize;
163 while ((pHashStr = pHash->pData[n]) != NULL) {
164 if (compareEqual (pHashStr, pString))
166 rtl_uString_acquire (pHashStr);
167 return pHashStr;
169 n++;
170 if (n >= pHash->nSize)
171 n = 0;
174 if (!can_return)
176 rtl_uString *pCopy = NULL;
177 rtl_uString_newFromString( &pCopy, pString );
178 pString = pCopy;
179 if (!pString)
180 return NULL;
183 if (!SAL_STRING_IS_STATIC (pString))
184 pString->refCount |= SAL_STRING_INTERN_FLAG;
185 pHash->pData[n] = pString;
186 pHash->nEntries++;
188 return pString;
191 void
192 rtl_str_hash_remove (rtl_uString *pString)
194 sal_uInt32 n;
195 sal_uInt32 nHash = hashString (pString);
196 rtl_uString *pHashStr;
198 StringHashTable *pHash = getHashTable();
200 n = nHash % pHash->nSize;
201 while ((pHashStr = pHash->pData[n]) != NULL) {
202 if (compareEqual (pHashStr, pString))
203 break;
204 n++;
205 if (n >= pHash->nSize)
206 n = 0;
208 OSL_ASSERT (pHash->pData[n] != 0);
209 if (pHash->pData[n] == NULL)
210 return;
212 pHash->pData[n++] = NULL;
213 pHash->nEntries--;
215 if (n >= pHash->nSize)
216 n = 0;
218 while ((pHashStr = pHash->pData[n]) != NULL) {
219 pHash->pData[n] = NULL;
220 // FIXME: rather unsophisticated and N^2 in chain-length, but robust.
221 rtl_str_hash_insert_nonequal (pHash, pHashStr);
222 n++;
223 if (n >= pHash->nSize)
224 n = 0;
226 // FIXME: Should we down-size ?
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */