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 .
23 #include <osl/diagnose.h>
24 #include <sal/macros.h>
26 struct StringHashTableImpl
{
32 typedef StringHashTableImpl StringHashTable
;
34 // Only for use in the implementation
35 static StringHashTable
*rtl_str_hash_new (sal_uInt32 nSize
);
36 static void rtl_str_hash_free (StringHashTable
*pHash
);
41 static StringHashTable
*pInternPool
= NULL
;
42 if (pInternPool
== NULL
) {
43 static StringHashTable
* pHash
= rtl_str_hash_new(1024);
49 // Better / smaller / faster hash set ....
51 // TODO: add bottom bit-set list terminator to string list
54 getNextSize (sal_uInt32 nSize
)
56 // Sedgewick - Algorithms in C P577.
57 static const sal_uInt32 nPrimes
[] = { 1021, 2039, 4093, 8191, 16381, 32749,
58 65521, 131071,262139, 524287, 1048573,
59 2097143, 4194301, 8388593, 16777213,
60 33554393, 67108859, 134217689 };
62 for (sal_uInt32 i
= 0; i
< SAL_N_ELEMENTS(nPrimes
); i
++)
64 if (nPrimes
[i
] > nSize
)
71 hashString (rtl_uString
*pString
)
73 return (sal_uInt32
) rtl_ustr_hashCode_WithLength (pString
->buffer
,
77 static StringHashTable
*
78 rtl_str_hash_new (sal_uInt32 nSize
)
80 StringHashTable
*pHash
= (StringHashTable
*)malloc (sizeof (StringHashTable
));
83 pHash
->nSize
= getNextSize (nSize
);
84 pHash
->pData
= (rtl_uString
**) calloc (sizeof (rtl_uString
*), pHash
->nSize
);
90 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
] != NULL
) {
109 if (n
>= pHash
->nSize
)
112 pHash
->pData
[n
] = pString
;
116 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
++)
128 if (pHash
->pData
[i
] != NULL
)
129 rtl_str_hash_insert_nonequal (pNewHash
, pHash
->pData
[i
]);
131 pNewHash
->nEntries
= pHash
->nEntries
;
134 pNewHash
->pData
= NULL
;
135 rtl_str_hash_free (pNewHash
);
139 compareEqual (rtl_uString
*pStringA
, rtl_uString
*pStringB
)
141 if (pStringA
== pStringB
)
143 if (pStringA
->length
!= pStringB
->length
)
145 return !rtl_ustr_compare_WithLength( pStringA
->buffer
, pStringA
->length
,
146 pStringB
->buffer
, pStringB
->length
);
151 rtl_str_hash_intern (rtl_uString
*pString
,
154 sal_uInt32 nHash
= hashString (pString
);
156 rtl_uString
*pHashStr
;
158 StringHashTable
*pHash
= getHashTable();
160 // Should we resize ?
161 if (pHash
->nEntries
>= pHash
->nSize
/2)
162 rtl_str_hash_resize (getNextSize(pHash
->nSize
));
164 n
= nHash
% pHash
->nSize
;
165 while ((pHashStr
= pHash
->pData
[n
]) != NULL
) {
166 if (compareEqual (pHashStr
, pString
))
168 rtl_uString_acquire (pHashStr
);
172 if (n
>= pHash
->nSize
)
178 rtl_uString
*pCopy
= NULL
;
179 rtl_uString_newFromString( &pCopy
, pString
);
185 if (!SAL_STRING_IS_STATIC (pString
))
186 pString
->refCount
|= SAL_STRING_INTERN_FLAG
;
187 pHash
->pData
[n
] = pString
;
194 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
]) != NULL
) {
204 if (compareEqual (pHashStr
, pString
))
207 if (n
>= pHash
->nSize
)
210 OSL_ASSERT (pHash
->pData
[n
] != 0);
211 if (pHash
->pData
[n
] == NULL
)
214 pHash
->pData
[n
++] = NULL
;
217 if (n
>= pHash
->nSize
)
220 while ((pHashStr
= pHash
->pData
[n
]) != NULL
) {
221 pHash
->pData
[n
] = NULL
;
222 // FIXME: rather unsophisticated and N^2 in chain-length, but robust.
223 rtl_str_hash_insert_nonequal (pHash
, pHashStr
);
225 if (n
>= pHash
->nSize
)
228 // FIXME: Should we down-size ?
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */