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 .
22 #include <osl/diagnose.h>
23 #include <sal/macros.h>
25 struct StringHashTableImpl
{
31 typedef StringHashTableImpl StringHashTable
;
33 // Only for use in the implementation
34 static StringHashTable
*rtl_str_hash_new (sal_uInt32 nSize
);
35 static void rtl_str_hash_free (StringHashTable
*pHash
);
40 static StringHashTable
*pInternPool
= NULL
;
41 if (pInternPool
== NULL
) {
42 static StringHashTable
* pHash
= rtl_str_hash_new(1024);
48 // Better / smaller / faster hash set ....
50 // TODO: add bottom bit-set list terminator to string list
53 getNextSize (sal_uInt32 nSize
)
55 // Sedgewick - Algorithms in C P577.
56 static const sal_uInt32 nPrimes
[] = { 1021, 2039, 4093, 8191, 16381, 32749,
57 65521, 131071,262139, 524287, 1048573,
58 2097143, 4194301, 8388593, 16777213,
59 33554393, 67108859, 134217689 };
61 for (sal_uInt32 i
= 0; i
< SAL_N_ELEMENTS(nPrimes
); i
++)
63 if (nPrimes
[i
] > nSize
)
70 hashString (rtl_uString
*pString
)
72 return (sal_uInt32
) rtl_ustr_hashCode_WithLength (pString
->buffer
,
76 static StringHashTable
*
77 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
));
89 rtl_str_hash_free (StringHashTable
*pHash
)
99 rtl_str_hash_insert_nonequal (StringHashTable
*pHash
,
100 rtl_uString
*pString
)
102 sal_uInt32 nHash
= hashString (pString
);
105 n
= nHash
% pHash
->nSize
;
106 while (pHash
->pData
[n
] != NULL
) {
108 if (n
>= pHash
->nSize
)
111 pHash
->pData
[n
] = pString
;
115 rtl_str_hash_resize (sal_uInt32 nNewSize
)
118 StringHashTable
*pNewHash
;
119 StringHashTable
*pHash
= getHashTable();
121 OSL_ASSERT (nNewSize
> pHash
->nEntries
);
123 pNewHash
= rtl_str_hash_new (nNewSize
);
125 for (i
= 0; i
< pHash
->nSize
; i
++)
127 if (pHash
->pData
[i
] != NULL
)
128 rtl_str_hash_insert_nonequal (pNewHash
, pHash
->pData
[i
]);
130 pNewHash
->nEntries
= pHash
->nEntries
;
133 pNewHash
->pData
= NULL
;
134 rtl_str_hash_free (pNewHash
);
138 compareEqual (rtl_uString
*pStringA
, rtl_uString
*pStringB
)
140 if (pStringA
== pStringB
)
142 if (pStringA
->length
!= pStringB
->length
)
144 return !rtl_ustr_compare_WithLength( pStringA
->buffer
, pStringA
->length
,
145 pStringB
->buffer
, pStringB
->length
);
149 rtl_str_hash_intern (rtl_uString
*pString
,
152 sal_uInt32 nHash
= hashString (pString
);
154 rtl_uString
*pHashStr
;
156 StringHashTable
*pHash
= getHashTable();
158 // Should we resize ?
159 if (pHash
->nEntries
>= pHash
->nSize
/2)
160 rtl_str_hash_resize (getNextSize(pHash
->nSize
));
162 n
= nHash
% pHash
->nSize
;
163 while ((pHashStr
= pHash
->pData
[n
]) != NULL
) {
164 if (compareEqual (pHashStr
, pString
))
166 rtl_uString_acquire (pHashStr
);
170 if (n
>= pHash
->nSize
)
176 rtl_uString
*pCopy
= NULL
;
177 rtl_uString_newFromString( &pCopy
, pString
);
183 if (!SAL_STRING_IS_STATIC (pString
))
184 pString
->refCount
|= SAL_STRING_INTERN_FLAG
;
185 pHash
->pData
[n
] = pString
;
192 rtl_str_hash_remove (rtl_uString
*pString
)
195 sal_uInt32 nHash
= hashString (pString
);
196 rtl_uString
*pHashStr
;
198 StringHashTable
*pHash
= getHashTable();
200 n
= nHash
% pHash
->nSize
;
201 while ((pHashStr
= pHash
->pData
[n
]) != NULL
) {
202 if (compareEqual (pHashStr
, pString
))
205 if (n
>= pHash
->nSize
)
208 OSL_ASSERT (pHash
->pData
[n
] != 0);
209 if (pHash
->pData
[n
] == NULL
)
212 pHash
->pData
[n
++] = NULL
;
215 if (n
>= pHash
->nSize
)
218 while ((pHashStr
= pHash
->pData
[n
]) != NULL
) {
219 pHash
->pData
[n
] = NULL
;
220 // FIXME: rather unsophisticated and N^2 in chain-length, but robust.
221 rtl_str_hash_insert_nonequal (pHash
, pHashStr
);
223 if (n
>= pHash
->nSize
)
226 // FIXME: Should we down-size ?
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */