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>
30 struct StringHashTableImpl
{
38 typedef StringHashTableImpl StringHashTable
;
40 // Only for use in the implementation
41 static StringHashTable
*rtl_str_hash_new(sal_uInt32 nSize
);
42 static void rtl_str_hash_free(StringHashTable
*pHash
);
44 static StringHashTable
* getHashTable()
46 static StringHashTable
* pInternPool
= rtl_str_hash_new(1024);
50 // Better / smaller / faster hash set...
52 // TODO: add bottom bit-set list terminator to string list
54 static sal_uInt32
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 nPrime
: nPrimes
)
70 static sal_uInt32
hashString(rtl_uString
*pString
)
72 return static_cast<sal_uInt32
>(rtl_ustr_hashCode_WithLength(pString
->buffer
,
76 static StringHashTable
* rtl_str_hash_new(sal_uInt32 nSize
)
78 StringHashTable
*pHash
= static_cast<StringHashTable
*>(malloc(sizeof(StringHashTable
)));
81 pHash
->nSize
= getNextSize (nSize
);
82 pHash
->pData
= static_cast< rtl_uString
** >(calloc(sizeof(rtl_uString
*), pHash
->nSize
));
87 static void 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
])
109 if (n
>= pHash
->nSize
)
112 pHash
->pData
[n
] = pString
;
115 static void 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
++)
128 rtl_str_hash_insert_nonequal(pNewHash
, pHash
->pData
[i
]);
131 pNewHash
->nEntries
= pHash
->nEntries
;
134 pNewHash
->pData
= nullptr;
135 rtl_str_hash_free(pNewHash
);
138 static bool compareEqual(rtl_uString
*pStringA
, rtl_uString
*pStringB
)
140 if (pStringA
== pStringB
)
143 if (pStringA
->length
!= pStringB
->length
)
146 return !rtl_ustr_compare_WithLength( pStringA
->buffer
, pStringA
->length
,
147 pStringB
->buffer
, pStringB
->length
);
150 rtl_uString
* rtl_str_hash_intern (
151 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
]))
167 if (compareEqual(pHashStr
, pString
))
169 rtl_uString_acquire(pHashStr
);
174 if (n
>= pHash
->nSize
)
180 rtl_uString
*pCopy
= nullptr;
181 rtl_uString_newFromString( &pCopy
, pString
);
188 if (!SAL_STRING_IS_STATIC(pString
))
189 pString
->refCount
|= SAL_STRING_INTERN_FLAG
;
191 pHash
->pData
[n
] = pString
;
197 void rtl_str_hash_remove(rtl_uString
*pString
)
200 sal_uInt32 nHash
= hashString(pString
);
201 rtl_uString
*pHashStr
;
203 StringHashTable
*pHash
= getHashTable();
205 n
= nHash
% pHash
->nSize
;
206 while ((pHashStr
= pHash
->pData
[n
]))
208 if (compareEqual(pHashStr
, pString
))
213 if (n
>= pHash
->nSize
)
217 OSL_ASSERT(pHash
->pData
[n
]);
218 if (!pHash
->pData
[n
])
221 pHash
->pData
[n
++] = nullptr;
224 if (n
>= pHash
->nSize
)
227 while ((pHashStr
= pHash
->pData
[n
]))
229 pHash
->pData
[n
] = nullptr;
230 // FIXME: rather unsophisticated and N^2 in chain-length, but robust.
231 rtl_str_hash_insert_nonequal(pHash
, pHashStr
);
234 if (n
>= pHash
->nSize
)
237 // FIXME: Should we down-size ?
240 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */