xref: /aoo42x/main/sal/rtl/source/hash.cxx (revision 22076bf1)
187d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
387d2adbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
487d2adbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
587d2adbcSAndrew Rist  * distributed with this work for additional information
687d2adbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
787d2adbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
887d2adbcSAndrew Rist  * "License"); you may not use this file except in compliance
987d2adbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
1087d2adbcSAndrew Rist  *
1187d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1287d2adbcSAndrew Rist  *
1387d2adbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
1487d2adbcSAndrew Rist  * software distributed under the License is distributed on an
1587d2adbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1687d2adbcSAndrew Rist  * KIND, either express or implied.  See the License for the
1787d2adbcSAndrew Rist  * specific language governing permissions and limitations
1887d2adbcSAndrew Rist  * under the License.
1987d2adbcSAndrew Rist  *
2087d2adbcSAndrew Rist  *************************************************************/
2187d2adbcSAndrew Rist 
2287d2adbcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sal.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "hash.h"
28cdf0e10cSrcweir #include "strimp.h"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <hash_set>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir namespace {
33cdf0e10cSrcweir 
34cdf0e10cSrcweir struct UStringHash
35cdf0e10cSrcweir {
operator ()__anon6ff4d28c0111::UStringHash36cdf0e10cSrcweir     size_t operator()(rtl_uString * const &rString) const
37cdf0e10cSrcweir     { return (size_t)rtl_ustr_hashCode_WithLength( rString->buffer, rString->length ); }
38cdf0e10cSrcweir };
39cdf0e10cSrcweir 
40cdf0e10cSrcweir struct UStringEqual
41cdf0e10cSrcweir {
operator ()__anon6ff4d28c0111::UStringEqual42cdf0e10cSrcweir     sal_Bool operator() ( rtl_uString * const &pStringA,
43cdf0e10cSrcweir                           rtl_uString * const &pStringB) const
44cdf0e10cSrcweir     {
45cdf0e10cSrcweir         if (pStringA == pStringB)
46cdf0e10cSrcweir             return true;
47cdf0e10cSrcweir         if (pStringA->length != pStringB->length)
48cdf0e10cSrcweir             return false;
49cdf0e10cSrcweir         return !rtl_ustr_compare_WithLength( pStringA->buffer, pStringA->length,
50cdf0e10cSrcweir                                              pStringB->buffer, pStringB->length);
51cdf0e10cSrcweir     }
52cdf0e10cSrcweir };
53cdf0e10cSrcweir 
54*22076bf1SHerbert Dürr typedef std::hash_set< rtl_uString *, UStringHash, UStringEqual > StringHashTable;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir StringHashTable *
getHashTable()57cdf0e10cSrcweir getHashTable ()
58cdf0e10cSrcweir {
59cdf0e10cSrcweir     static StringHashTable *pInternPool = NULL;
60cdf0e10cSrcweir     if (pInternPool == NULL) {
61cdf0e10cSrcweir         static StringHashTable aImpl(1024);
62cdf0e10cSrcweir         pInternPool = &aImpl;
63cdf0e10cSrcweir     }
64cdf0e10cSrcweir     return pInternPool;
65cdf0e10cSrcweir }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir }
68cdf0e10cSrcweir 
69cdf0e10cSrcweir extern "C" {
70cdf0e10cSrcweir 
71cdf0e10cSrcweir rtl_uString *
rtl_str_hash_intern(rtl_uString * pString,int can_return)72cdf0e10cSrcweir rtl_str_hash_intern (rtl_uString       *pString,
73cdf0e10cSrcweir                      int                can_return)
74cdf0e10cSrcweir {
75cdf0e10cSrcweir     StringHashTable *pHash = getHashTable();
76cdf0e10cSrcweir     StringHashTable::iterator aIter;
77cdf0e10cSrcweir     aIter = pHash->find(pString);
78cdf0e10cSrcweir     if (aIter != pHash->end())
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir         rtl_uString *pHashStr = *aIter;
81cdf0e10cSrcweir         rtl_uString_acquire (pHashStr);
82cdf0e10cSrcweir         return pHashStr;
83cdf0e10cSrcweir     }
84cdf0e10cSrcweir     if (!can_return)
85cdf0e10cSrcweir     {
86cdf0e10cSrcweir         rtl_uString *pCopy = NULL;
87cdf0e10cSrcweir         rtl_uString_newFromString( &pCopy, pString );
88cdf0e10cSrcweir         pString = pCopy;
89cdf0e10cSrcweir         if (!pString)
90cdf0e10cSrcweir             return NULL;
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     if (!SAL_STRING_IS_STATIC (pString))
94cdf0e10cSrcweir         pString->refCount |= SAL_STRING_INTERN_FLAG;
95cdf0e10cSrcweir     pHash->insert(pString);
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     return pString;
98cdf0e10cSrcweir }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir void
rtl_str_hash_remove(rtl_uString * pString)101cdf0e10cSrcweir rtl_str_hash_remove (rtl_uString       *pString)
102cdf0e10cSrcweir {
103cdf0e10cSrcweir     getHashTable()->erase(pString);
104cdf0e10cSrcweir }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir }
107