xref: /aoo41x/main/sw/inc/stringhash.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #ifndef _STRINGHASH_HXX
28 #define _STRINGHASH_HXX
29 
30 #include <tools/string.hxx>
31 
32 struct StringEq
33 {
34 	sal_Bool operator() ( const String *r1,
35 					  	  const String *r2) const
36 	{
37 		return r1->Equals(*r2);
38 	}
39 };
40 
41 struct StringEqRef
42 {
43     sal_Bool operator() (const String &r1, const String &r2) const
44     {
45         return r1.Equals(r2);
46     }
47 };
48 
49 struct StringHash
50 {
51 	size_t operator() ( const String *rString) const
52 	{
53 		sal_Int32 h, nLen;
54 		h = nLen = rString->Len();
55 		const sal_Unicode *pStr = rString->GetBuffer();
56 
57 		if ( nLen < 16 )
58 			while ( nLen-- > 0 )
59 				h = (h*37) + *(pStr++);
60 		else
61 		{
62 			sal_Int32               nSkip;
63 			const sal_Unicode* pEndStr = pStr+nLen-5;
64 
65 			/* only sample some characters */
66 			/* the first 3, some characters between, and the last 5 */
67 			h = (h*39) + *(pStr++);
68 			h = (h*39) + *(pStr++);
69 			h = (h*39) + *(pStr++);
70 
71 			nSkip = nLen / nLen < 32 ? 4 : 8;
72 			nLen -= 8;
73 			while ( nLen > 0 )
74 			{
75 				h = (h*39) + ( *pStr );
76 				pStr += nSkip;
77 				nLen -= nSkip;
78 			}
79 
80 			h = (h*39) + *(pEndStr++);
81 			h = (h*39) + *(pEndStr++);
82 			h = (h*39) + *(pEndStr++);
83 			h = (h*39) + *(pEndStr++);
84 			h = (h*39) + *(pEndStr++);
85 		}
86 		return h;
87 	}
88 
89     size_t operator() (const String & rStr) const
90     {
91         return (*this)(&rStr);
92     }
93 };
94 
95 struct StringHashRef
96 {
97     size_t operator () (const String &rStr) const
98     {
99         StringHash aStrHash;
100 
101         return aStrHash(&rStr);
102     }
103 };
104 #endif // _STRINGHASH_HXX
105