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
= rtl_str_hash_new(1024);
47 // Better / smaller / faster hash set...
49 // TODO: add bottom bit-set list terminator to string list
51 static sal_uInt32
getNextSize(sal_uInt32 nSize
)
53 // Sedgewick - Algorithms in C P577.
54 static const sal_uInt32 nPrimes
[] = { 1021, 2039, 4093, 8191, 16381, 32749,
55 65521, 131071,262139, 524287, 1048573,
56 2097143, 4194301, 8388593, 16777213,
57 33554393, 67108859, 134217689 };
59 for (sal_uInt32 nPrime
: nPrimes
)
67 static sal_uInt32
hashString(rtl_uString
*pString
)
69 return static_cast<sal_uInt32
>(rtl_ustr_hashCode_WithLength(pString
->buffer
,
73 static StringHashTable
* rtl_str_hash_new(sal_uInt32 nSize
)
75 StringHashTable
*pHash
= static_cast<StringHashTable
*>(malloc(sizeof(StringHashTable
)));
78 pHash
->nSize
= getNextSize (nSize
);
79 pHash
->pData
= static_cast< rtl_uString
** >(calloc(sizeof(rtl_uString
*), pHash
->nSize
));
84 static void rtl_str_hash_free(StringHashTable
*pHash
)
96 rtl_str_hash_insert_nonequal(StringHashTable
*pHash
,
99 sal_uInt32 nHash
= hashString(pString
);
102 n
= nHash
% pHash
->nSize
;
103 while (pHash
->pData
[n
])
106 if (n
>= pHash
->nSize
)
109 pHash
->pData
[n
] = pString
;
112 static void rtl_str_hash_resize(sal_uInt32 nNewSize
)
115 StringHashTable
*pNewHash
;
116 StringHashTable
*pHash
= getHashTable();
118 OSL_ASSERT(nNewSize
> pHash
->nEntries
);
120 pNewHash
= rtl_str_hash_new(nNewSize
);
122 for (i
= 0; i
< pHash
->nSize
; i
++)
125 rtl_str_hash_insert_nonequal(pNewHash
, pHash
->pData
[i
]);
128 pNewHash
->nEntries
= pHash
->nEntries
;
131 pNewHash
->pData
= nullptr;
132 rtl_str_hash_free(pNewHash
);
135 static bool compareEqual(rtl_uString
*pStringA
, rtl_uString
*pStringB
)
137 if (pStringA
== pStringB
)
140 if (pStringA
->length
!= pStringB
->length
)
143 return !rtl_ustr_compare_WithLength( pStringA
->buffer
, pStringA
->length
,
144 pStringB
->buffer
, pStringB
->length
);
147 rtl_uString
* rtl_str_hash_intern (
148 rtl_uString
*pString
,
151 sal_uInt32 nHash
= hashString(pString
);
153 rtl_uString
*pHashStr
;
155 StringHashTable
*pHash
= getHashTable();
157 // Should we resize ?
158 if (pHash
->nEntries
>= pHash
->nSize
/2)
159 rtl_str_hash_resize(getNextSize(pHash
->nSize
));
161 n
= nHash
% pHash
->nSize
;
162 while ((pHashStr
= pHash
->pData
[n
]))
164 if (compareEqual(pHashStr
, pString
))
166 rtl_uString_acquire(pHashStr
);
171 if (n
>= pHash
->nSize
)
177 rtl_uString
*pCopy
= nullptr;
178 rtl_uString_newFromString( &pCopy
, pString
);
185 if (!SAL_STRING_IS_STATIC(pString
))
186 pString
->refCount
|= SAL_STRING_INTERN_FLAG
;
188 pHash
->pData
[n
] = pString
;
194 void rtl_str_hash_remove(rtl_uString
*pString
)
197 sal_uInt32 nHash
= hashString(pString
);
198 rtl_uString
*pHashStr
;
200 StringHashTable
*pHash
= getHashTable();
202 n
= nHash
% pHash
->nSize
;
203 while ((pHashStr
= pHash
->pData
[n
]))
205 if (compareEqual(pHashStr
, pString
))
210 if (n
>= pHash
->nSize
)
214 OSL_ASSERT(pHash
->pData
[n
]);
215 if (!pHash
->pData
[n
])
218 pHash
->pData
[n
++] = nullptr;
221 if (n
>= pHash
->nSize
)
224 while ((pHashStr
= pHash
->pData
[n
]))
226 pHash
->pData
[n
] = nullptr;
227 // FIXME: rather unsophisticated and N^2 in chain-length, but robust.
228 rtl_str_hash_insert_nonequal(pHash
, pHashStr
);
231 if (n
>= pHash
->nSize
)
234 // FIXME: Should we down-size ?
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */