xref: /aoo4110/main/sw/inc/stringhash.hxx (revision b1cdbd2c)
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 #ifndef _STRINGHASH_HXX
24 #define _STRINGHASH_HXX
25 
26 #include <tools/string.hxx>
27 
28 struct StringEq
29 {
operator ()StringEq30 	sal_Bool operator() ( const String *r1,
31 					  	  const String *r2) const
32 	{
33 		return r1->Equals(*r2);
34 	}
35 };
36 
37 struct StringEqRef
38 {
operator ()StringEqRef39     sal_Bool operator() (const String &r1, const String &r2) const
40     {
41         return r1.Equals(r2);
42     }
43 };
44 
45 struct StringHash
46 {
operator ()StringHash47 	size_t operator() ( const String *rString) const
48 	{
49 		sal_Int32 h, nLen;
50 		h = nLen = rString->Len();
51 		const sal_Unicode *pStr = rString->GetBuffer();
52 
53 		if ( nLen < 16 )
54 			while ( nLen-- > 0 )
55 				h = (h*37) + *(pStr++);
56 		else
57 		{
58 			sal_Int32               nSkip;
59 			const sal_Unicode* pEndStr = pStr+nLen-5;
60 
61 			/* only sample some characters */
62 			/* the first 3, some characters between, and the last 5 */
63 			h = (h*39) + *(pStr++);
64 			h = (h*39) + *(pStr++);
65 			h = (h*39) + *(pStr++);
66 
67 			nSkip = nLen / nLen < 32 ? 4 : 8;
68 			nLen -= 8;
69 			while ( nLen > 0 )
70 			{
71 				h = (h*39) + ( *pStr );
72 				pStr += nSkip;
73 				nLen -= nSkip;
74 			}
75 
76 			h = (h*39) + *(pEndStr++);
77 			h = (h*39) + *(pEndStr++);
78 			h = (h*39) + *(pEndStr++);
79 			h = (h*39) + *(pEndStr++);
80 			h = (h*39) + *(pEndStr++);
81 		}
82 		return h;
83 	}
84 
operator ()StringHash85     size_t operator() (const String & rStr) const
86     {
87         return (*this)(&rStr);
88     }
89 };
90 
91 struct StringHashRef
92 {
operator ()StringHashRef93     size_t operator () (const String &rStr) const
94     {
95         StringHash aStrHash;
96 
97         return aStrHash(&rStr);
98     }
99 };
100 #endif // _STRINGHASH_HXX
101