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