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>
31 struct StringHashTableImpl
{
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);
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
)
71 static sal_uInt32
hashString(rtl_uString
*pString
)
73 return static_cast<sal_uInt32
>(rtl_ustr_hashCode_WithLength(pString
->buffer
,
77 static StringHashTable
* rtl_str_hash_new(sal_uInt32 nSize
)
79 StringHashTable
*pHash
= static_cast<StringHashTable
*>(malloc(sizeof(StringHashTable
)));
82 pHash
->nSize
= getNextSize (nSize
);
83 pHash
->pData
= static_cast< rtl_uString
** >(calloc(sizeof(rtl_uString
*), pHash
->nSize
));
88 static void rtl_str_hash_free(StringHashTable
*pHash
)
100 rtl_str_hash_insert_nonequal(StringHashTable
*pHash
,
101 rtl_uString
*pString
)
103 sal_uInt32 nHash
= hashString(pString
);
106 n
= nHash
% pHash
->nSize
;
107 while (pHash
->pData
[n
])
110 if (n
>= pHash
->nSize
)
113 pHash
->pData
[n
] = pString
;
116 static void rtl_str_hash_resize(sal_uInt32 nNewSize
)
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
++)
129 rtl_str_hash_insert_nonequal(pNewHash
, pHash
->pData
[i
]);
132 pNewHash
->nEntries
= pHash
->nEntries
;
135 pNewHash
->pData
= nullptr;
136 rtl_str_hash_free(pNewHash
);
139 static bool compareEqual(rtl_uString
*pStringA
, rtl_uString
*pStringB
)
141 if (pStringA
== pStringB
)
144 if (pStringA
->length
!= pStringB
->length
)
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
,
155 sal_uInt32 nHash
= hashString(pString
);
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
);
175 if (n
>= pHash
->nSize
)
181 rtl_uString
*pCopy
= nullptr;
182 rtl_uString_newFromString( &pCopy
, pString
);
189 if (!SAL_STRING_IS_STATIC(pString
))
190 pString
->refCount
|= SAL_STRING_INTERN_FLAG
;
192 pHash
->pData
[n
] = pString
;
198 void rtl_str_hash_remove(rtl_uString
*pString
)
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
))
214 if (n
>= pHash
->nSize
)
218 OSL_ASSERT(pHash
->pData
[n
]);
219 if (!pHash
->pData
[n
])
222 pHash
->pData
[n
++] = nullptr;
225 if (n
>= pHash
->nSize
)
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
);
235 if (n
>= pHash
->nSize
)
238 // FIXME: Should we down-size ?
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */