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