lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / sal / rtl / hash.cxx
blobe12095f3d9e50cd7ba125d4767ebc8a367ca137a
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 <sal/config.h>
22 #include <stdlib.h>
24 #include "hash.hxx"
25 #include "strimp.hxx"
26 #include <osl/diagnose.h>
27 #include <sal/macros.h>
29 struct StringHashTableImpl {
30 sal_uInt32 nEntries;
31 sal_uInt32 nSize;
32 rtl_uString **pData;
35 typedef StringHashTableImpl StringHashTable;
37 // Only for use in the implementation
38 static StringHashTable *rtl_str_hash_new(sal_uInt32 nSize);
39 static void rtl_str_hash_free(StringHashTable *pHash);
41 static StringHashTable * getHashTable()
43 static StringHashTable *pInternPool = nullptr;
44 if (!pInternPool)
46 static StringHashTable* pHash = rtl_str_hash_new(1024);
47 pInternPool = pHash;
49 return pInternPool;
52 // Better / smaller / faster hash set ....
54 // TODO: add bottom bit-set list terminator to string list
56 static sal_uInt32 getNextSize(sal_uInt32 nSize)
58 // Sedgewick - Algorithms in C P577.
59 static const sal_uInt32 nPrimes[] = { 1021, 2039, 4093, 8191, 16381, 32749,
60 65521, 131071,262139, 524287, 1048573,
61 2097143, 4194301, 8388593, 16777213,
62 33554393, 67108859, 134217689 };
64 for (sal_uInt32 nPrime : nPrimes)
66 if (nPrime > nSize)
67 return nPrime;
69 return nSize * 2;
72 static sal_uInt32 hashString(rtl_uString *pString)
74 return static_cast<sal_uInt32>(rtl_ustr_hashCode_WithLength(pString->buffer,
75 pString->length));
78 static StringHashTable * rtl_str_hash_new(sal_uInt32 nSize)
80 StringHashTable *pHash = static_cast<StringHashTable *>(malloc(sizeof(StringHashTable)));
82 pHash->nEntries = 0;
83 pHash->nSize = getNextSize (nSize);
84 pHash->pData = static_cast< rtl_uString ** >(calloc(sizeof(rtl_uString *), pHash->nSize));
86 return pHash;
89 static void rtl_str_hash_free(StringHashTable *pHash)
91 if (!pHash)
92 return;
94 if (pHash->pData)
95 free(pHash->pData);
97 free(pHash);
100 static void
101 rtl_str_hash_insert_nonequal(StringHashTable *pHash,
102 rtl_uString *pString)
104 sal_uInt32 nHash = hashString(pString);
105 sal_uInt32 n;
107 n = nHash % pHash->nSize;
108 while (pHash->pData[n])
110 n++;
111 if (n >= pHash->nSize)
112 n = 0;
114 pHash->pData[n] = pString;
117 static void rtl_str_hash_resize(sal_uInt32 nNewSize)
119 sal_uInt32 i;
120 StringHashTable *pNewHash;
121 StringHashTable *pHash = getHashTable();
123 OSL_ASSERT(nNewSize > pHash->nEntries);
125 pNewHash = rtl_str_hash_new(nNewSize);
127 for (i = 0; i < pHash->nSize; i++)
129 if (pHash->pData[i])
130 rtl_str_hash_insert_nonequal(pNewHash, pHash->pData[i]);
133 pNewHash->nEntries = pHash->nEntries;
134 free(pHash->pData);
135 *pHash = *pNewHash;
136 pNewHash->pData = nullptr;
137 rtl_str_hash_free(pNewHash);
140 static bool compareEqual(rtl_uString *pStringA, rtl_uString *pStringB)
142 if (pStringA == pStringB)
143 return true;
145 if (pStringA->length != pStringB->length)
146 return false;
148 return !rtl_ustr_compare_WithLength( pStringA->buffer, pStringA->length,
149 pStringB->buffer, pStringB->length);
152 rtl_uString * rtl_str_hash_intern (
153 rtl_uString *pString,
154 int can_return)
156 sal_uInt32 nHash = hashString(pString);
157 sal_uInt32 n;
158 rtl_uString *pHashStr;
160 StringHashTable *pHash = getHashTable();
162 // Should we resize ?
163 if (pHash->nEntries >= pHash->nSize/2)
164 rtl_str_hash_resize(getNextSize(pHash->nSize));
166 n = nHash % pHash->nSize;
167 while ((pHashStr = pHash->pData[n]))
169 if (compareEqual(pHashStr, pString))
171 rtl_uString_acquire(pHashStr);
172 return pHashStr;
175 n++;
176 if (n >= pHash->nSize)
177 n = 0;
180 if (!can_return)
182 rtl_uString *pCopy = nullptr;
183 rtl_uString_newFromString( &pCopy, pString );
184 pString = pCopy;
186 if (!pString)
187 return nullptr;
190 if (!SAL_STRING_IS_STATIC(pString))
191 pString->refCount |= SAL_STRING_INTERN_FLAG;
193 pHash->pData[n] = pString;
194 pHash->nEntries++;
196 return pString;
199 void rtl_str_hash_remove(rtl_uString *pString)
201 sal_uInt32 n;
202 sal_uInt32 nHash = hashString(pString);
203 rtl_uString *pHashStr;
205 StringHashTable *pHash = getHashTable();
207 n = nHash % pHash->nSize;
208 while ((pHashStr = pHash->pData[n]))
210 if (compareEqual(pHashStr, pString))
211 break;
213 n++;
215 if (n >= pHash->nSize)
216 n = 0;
219 OSL_ASSERT(pHash->pData[n]);
220 if (!pHash->pData[n])
221 return;
223 pHash->pData[n++] = nullptr;
224 pHash->nEntries--;
226 if (n >= pHash->nSize)
227 n = 0;
229 while ((pHashStr = pHash->pData[n]))
231 pHash->pData[n] = nullptr;
232 // FIXME: rather unsophisticated and N^2 in chain-length, but robust.
233 rtl_str_hash_insert_nonequal(pHash, pHashStr);
234 n++;
236 if (n >= pHash->nSize)
237 n = 0;
239 // FIXME: Should we down-size ?
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */