tdf#155486 Adding fonts to .odt when there is "no perfect match"
[LibreOffice.git] / sal / rtl / hash.cxx
blob4fed60889f50b23bb7c4cff5273c3349bbf4af91
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 namespace {
31 struct StringHashTableImpl {
32 sal_uInt32 nEntries;
33 sal_uInt32 nSize;
34 rtl_uString **pData;
39 typedef StringHashTableImpl StringHashTable;
41 // Only for use in the implementation
42 static StringHashTable *rtl_str_hash_new(sal_uInt32 nSize);
43 static void rtl_str_hash_free(StringHashTable *pHash);
45 static StringHashTable * getHashTable()
47 static StringHashTable* pInternPool = rtl_str_hash_new(1024);
48 return pInternPool;
51 // Better / smaller / faster hash set...
53 // TODO: add bottom bit-set list terminator to string list
55 static sal_uInt32 getNextSize(sal_uInt32 nSize)
57 // Sedgewick - Algorithms in C P577.
58 static const sal_uInt32 nPrimes[] = { 1021, 2039, 4093, 8191, 16381, 32749,
59 65521, 131071,262139, 524287, 1048573,
60 2097143, 4194301, 8388593, 16777213,
61 33554393, 67108859, 134217689 };
63 for (sal_uInt32 nPrime : nPrimes)
65 if (nPrime > nSize)
66 return nPrime;
68 return nSize * 2;
71 static sal_uInt32 hashString(rtl_uString *pString)
73 return static_cast<sal_uInt32>(rtl_ustr_hashCode_WithLength(pString->buffer,
74 pString->length));
77 static StringHashTable * 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 rtl_str_hash_free(StringHashTable *pHash)
90 if (!pHash)
91 return;
93 if (pHash->pData)
94 free(pHash->pData);
96 free(pHash);
99 static void
100 rtl_str_hash_insert_nonequal(StringHashTable *pHash,
101 rtl_uString *pString)
103 sal_uInt32 nHash = hashString(pString);
104 sal_uInt32 n;
106 n = nHash % pHash->nSize;
107 while (pHash->pData[n])
109 n++;
110 if (n >= pHash->nSize)
111 n = 0;
113 pHash->pData[n] = pString;
116 static void rtl_str_hash_resize(sal_uInt32 nNewSize)
118 sal_uInt32 i;
119 StringHashTable *pNewHash;
120 StringHashTable *pHash = getHashTable();
122 OSL_ASSERT(nNewSize > pHash->nEntries);
124 pNewHash = rtl_str_hash_new(nNewSize);
126 for (i = 0; i < pHash->nSize; i++)
128 if (pHash->pData[i])
129 rtl_str_hash_insert_nonequal(pNewHash, pHash->pData[i]);
132 pNewHash->nEntries = pHash->nEntries;
133 free(pHash->pData);
134 *pHash = *pNewHash;
135 pNewHash->pData = nullptr;
136 rtl_str_hash_free(pNewHash);
139 static bool compareEqual(rtl_uString *pStringA, rtl_uString *pStringB)
141 if (pStringA == pStringB)
142 return true;
144 if (pStringA->length != pStringB->length)
145 return false;
147 return !rtl_ustr_compare_WithLength( pStringA->buffer, pStringA->length,
148 pStringB->buffer, pStringB->length);
151 rtl_uString * rtl_str_hash_intern (
152 rtl_uString *pString,
153 int can_return)
155 sal_uInt32 nHash = hashString(pString);
156 sal_uInt32 n;
157 rtl_uString *pHashStr;
159 StringHashTable *pHash = getHashTable();
161 // Should we resize ?
162 if (pHash->nEntries >= pHash->nSize/2)
163 rtl_str_hash_resize(getNextSize(pHash->nSize));
165 n = nHash % pHash->nSize;
166 while ((pHashStr = pHash->pData[n]))
168 if (compareEqual(pHashStr, pString))
170 rtl_uString_acquire(pHashStr);
171 return pHashStr;
174 n++;
175 if (n >= pHash->nSize)
176 n = 0;
179 if (!can_return)
181 rtl_uString *pCopy = nullptr;
182 rtl_uString_newFromString( &pCopy, pString );
183 pString = pCopy;
185 if (!pString)
186 return nullptr;
189 if (!SAL_STRING_IS_STATIC(pString))
190 pString->refCount |= SAL_STRING_INTERN_FLAG;
192 pHash->pData[n] = pString;
193 pHash->nEntries++;
195 return pString;
198 void rtl_str_hash_remove(rtl_uString *pString)
200 sal_uInt32 n;
201 sal_uInt32 nHash = hashString(pString);
202 rtl_uString *pHashStr;
204 StringHashTable *pHash = getHashTable();
206 n = nHash % pHash->nSize;
207 while ((pHashStr = pHash->pData[n]))
209 if (compareEqual(pHashStr, pString))
210 break;
212 n++;
214 if (n >= pHash->nSize)
215 n = 0;
218 OSL_ASSERT(pHash->pData[n]);
219 if (!pHash->pData[n])
220 return;
222 pHash->pData[n++] = nullptr;
223 pHash->nEntries--;
225 if (n >= pHash->nSize)
226 n = 0;
228 while ((pHashStr = pHash->pData[n]))
230 pHash->pData[n] = nullptr;
231 // FIXME: rather unsophisticated and N^2 in chain-length, but robust.
232 rtl_str_hash_insert_nonequal(pHash, pHashStr);
233 n++;
235 if (n >= pHash->nSize)
236 n = 0;
238 // FIXME: Should we down-size ?
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */