1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
26 #include <osl/diagnose.h>
27 #include <sal/macros.h>
29 struct StringHashTableImpl
{
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;
46 static StringHashTable
* pHash
= rtl_str_hash_new(1024);
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
)
72 static sal_uInt32
hashString(rtl_uString
*pString
)
74 return static_cast<sal_uInt32
>(rtl_ustr_hashCode_WithLength(pString
->buffer
,
78 static StringHashTable
* rtl_str_hash_new(sal_uInt32 nSize
)
80 StringHashTable
*pHash
= static_cast<StringHashTable
*>(malloc(sizeof(StringHashTable
)));
83 pHash
->nSize
= getNextSize (nSize
);
84 pHash
->pData
= static_cast< rtl_uString
** >(calloc(sizeof(rtl_uString
*), pHash
->nSize
));
89 static void rtl_str_hash_free(StringHashTable
*pHash
)
101 rtl_str_hash_insert_nonequal(StringHashTable
*pHash
,
102 rtl_uString
*pString
)
104 sal_uInt32 nHash
= hashString(pString
);
107 n
= nHash
% pHash
->nSize
;
108 while (pHash
->pData
[n
])
111 if (n
>= pHash
->nSize
)
114 pHash
->pData
[n
] = pString
;
117 static void rtl_str_hash_resize(sal_uInt32 nNewSize
)
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
++)
130 rtl_str_hash_insert_nonequal(pNewHash
, pHash
->pData
[i
]);
133 pNewHash
->nEntries
= pHash
->nEntries
;
136 pNewHash
->pData
= nullptr;
137 rtl_str_hash_free(pNewHash
);
140 static bool compareEqual(rtl_uString
*pStringA
, rtl_uString
*pStringB
)
142 if (pStringA
== pStringB
)
145 if (pStringA
->length
!= pStringB
->length
)
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
,
156 sal_uInt32 nHash
= hashString(pString
);
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
);
176 if (n
>= pHash
->nSize
)
182 rtl_uString
*pCopy
= nullptr;
183 rtl_uString_newFromString( &pCopy
, pString
);
190 if (!SAL_STRING_IS_STATIC(pString
))
191 pString
->refCount
|= SAL_STRING_INTERN_FLAG
;
193 pHash
->pData
[n
] = pString
;
199 void rtl_str_hash_remove(rtl_uString
*pString
)
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
))
215 if (n
>= pHash
->nSize
)
219 OSL_ASSERT(pHash
->pData
[n
]);
220 if (!pHash
->pData
[n
])
223 pHash
->pData
[n
++] = nullptr;
226 if (n
>= pHash
->nSize
)
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
);
236 if (n
>= pHash
->nSize
)
239 // FIXME: Should we down-size ?
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */