Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : :
21 : : // C and C++ includes
22 : : #include <stdlib.h>
23 : : #include <stdio.h>
24 : : #include <ctype.h>
25 : :
26 : : // program-sensitive includes
27 : : #include <hash.hxx>
28 : : #include <tools/debug.hxx>
29 : :
30 [ - + ]: 406776 : SvStringHashEntry::~SvStringHashEntry() { };
31 : :
32 : 32 : SvHashTable::SvHashTable( sal_uInt32 nMaxEntries )
33 : : {
34 : 32 : nMax = nMaxEntries; // set max entries
35 : 32 : nFill = 0; // no entries
36 : 32 : lTry = 0;
37 : 32 : lAsk = 0;
38 : 32 : }
39 : :
40 : 32 : SvHashTable::~SvHashTable()
41 : : {
42 [ - + ]: 32 : }
43 : :
44 : 673290 : sal_Bool SvHashTable::Test_Insert( const rtl::OString& rElement, sal_Bool bInsert,
45 : : sal_uInt32 * pInsertPos )
46 : : {
47 : : sal_uInt32 nHash;
48 : : sal_uInt32 nIndex;
49 : : sal_uInt32 nLoop;
50 : :
51 : 673290 : lAsk++;
52 : 673290 : lTry++;
53 : :
54 : 673290 : nHash = HashFunc( rElement );
55 : 673290 : nIndex = nHash % nMax;
56 : :
57 : 673290 : nLoop = 0; // divide to range
58 [ + - ][ + + ]: 703006 : while( (nMax != nLoop) && IsEntry( nIndex ) )
[ + + ]
59 : : { // is place occupied
60 [ + + ]: 461564 : if( equals( rElement, nIndex ) )
61 : : {
62 [ + - ]: 431848 : if( pInsertPos )
63 : 431848 : *pInsertPos = nIndex; // place of Element
64 : 431848 : return sal_True;
65 : : }
66 : 29716 : nLoop++;
67 : 29716 : lTry++;
68 : 29716 : nIndex = (sal_uInt16)(nIndex + nHash + 7) % nMax;
69 : : }
70 : :
71 [ + + ]: 241442 : if( bInsert )
72 : : {
73 : : DBG_ASSERT( nMax != nLoop, "Hash table full" );
74 [ + - ]: 41912 : if( nMax != nLoop )
75 : : {
76 : 41912 : nFill++;
77 : 41912 : *pInsertPos = nIndex; // return free place
78 : 41912 : return sal_True;
79 : : }
80 : : }
81 : 673290 : return( sal_False );
82 : : }
83 : :
84 : 32 : SvStringHashTable::SvStringHashTable( sal_uInt32 nMaxEntries )
85 : 32 : : SvHashTable( nMaxEntries )
86 : : {
87 [ + - ][ + + : 364896 : pEntries = new SvStringHashEntry[ nMaxEntries ];
# # # # ]
[ + - ]
88 : :
89 : : // set RefCount to one
90 : : SvStringHashEntry * pPos, *pEnd;
91 : 32 : pPos = pEntries;
92 : 32 : pEnd = pEntries + nMaxEntries;
93 [ + + ]: 364896 : while( pPos != pEnd )
94 : : {
95 : 364864 : pPos->AddRef();
96 : 364864 : pPos++;
97 : : }
98 : 32 : }
99 : :
100 : 32 : SvStringHashTable::~SvStringHashTable()
101 : : {
102 : : #ifdef DBG_UTIL
103 : : // set RefCount to one
104 : : SvStringHashEntry * pPos, *pEnd;
105 : : pPos = pEntries;
106 : : pEnd = pEntries + GetMax();
107 : : while( pPos != pEnd )
108 : : {
109 : : DBG_ASSERT( pPos->GetRefCount() == 1, "Reference count != 1" );
110 : : pPos++;
111 : : }
112 : : #endif
113 : :
114 [ + - ][ + + ]: 364896 : delete [] pEntries;
[ + - ]
115 [ - + ]: 64 : }
116 : :
117 : 673290 : sal_uInt32 SvStringHashTable::HashFunc( const rtl::OString& rElement ) const
118 : : {
119 : 673290 : sal_uInt32 nHash = 0; // hash value
120 : 673290 : const char * pStr = rElement.getStr();
121 : :
122 : 673290 : int nShift = 0;
123 [ + + ]: 8884556 : while( *pStr )
124 : : {
125 [ + + ]: 8211266 : if( isupper( *pStr ) )
126 : 4411006 : nHash ^= sal_uInt32(*pStr - 'A' + 26) << nShift;
127 : : else
128 : 3800260 : nHash ^= sal_uInt32(*pStr - 'a') << nShift;
129 [ + + ]: 8211266 : if( nShift == 28 )
130 : 738960 : nShift = 0;
131 : : else
132 : 7472306 : nShift += 4;
133 : 8211266 : pStr++;
134 : : }
135 : 673290 : return( nHash );
136 : : }
137 : :
138 : 0 : rtl::OString SvStringHashTable::GetNearString( const rtl::OString& rName ) const
139 : : {
140 [ # # ]: 0 : for( sal_uInt32 i = 0; i < GetMax(); i++ )
141 : : {
142 : 0 : SvStringHashEntry * pE = Get( i );
143 [ # # ]: 0 : if( pE )
144 : : {
145 [ # # ][ # # ]: 0 : if( pE->GetName().equalsIgnoreAsciiCase( rName ) && !pE->GetName().equals( rName ) )
[ # # ]
146 : 0 : return pE->GetName();
147 : : }
148 : : }
149 : 0 : return rtl::OString();
150 : : }
151 : :
152 : 1580192 : sal_Bool SvStringHashTable::IsEntry( sal_uInt32 nIndex ) const
153 : : {
154 [ - + ]: 1580192 : if( nIndex >= GetMax() )
155 : 0 : return sal_False;
156 : 1580192 : return pEntries[ nIndex ].HasId();
157 : : }
158 : :
159 : 42810 : sal_Bool SvStringHashTable::Insert( const rtl::OString& rName, sal_uInt32 * pIndex )
160 : : {
161 : : sal_uInt32 nIndex;
162 : :
163 [ - + ]: 42810 : if( !pIndex ) pIndex = &nIndex;
164 : :
165 [ + - ][ - + ]: 42810 : if( !SvHashTable::Test_Insert( rName, sal_True, pIndex ) )
166 : 0 : return sal_False;
167 : :
168 [ + - ][ + + ]: 42810 : if( !IsEntry( *pIndex ) )
169 [ + - ][ + - ]: 41912 : pEntries[ *pIndex ] = SvStringHashEntry( rName, *pIndex );
170 : 42810 : return sal_True;
171 : : }
172 : :
173 : 630480 : sal_Bool SvStringHashTable::Test( const rtl::OString& rName, sal_uInt32 * pPos ) const
174 : : {
175 : 630480 : return const_cast<SvStringHashTable*>(this)->Test_Insert( rName, sal_False, pPos );
176 : : }
177 : :
178 : 514328 : SvStringHashEntry * SvStringHashTable::Get( sal_uInt32 nIndex ) const
179 : : {
180 [ + - ]: 514328 : if( IsEntry( nIndex ) )
181 : 514328 : return pEntries + nIndex;
182 : 514328 : return( NULL );
183 : : }
184 : :
185 : 461564 : bool SvStringHashTable::equals( const rtl::OString& rElement,
186 : : sal_uInt32 nIndex ) const
187 : : {
188 : 461564 : return rElement.equals( pEntries[ nIndex ].GetName() );
189 : : }
190 : :
191 : 16 : void SvStringHashTable::FillHashList( SvStringHashList * pList ) const
192 : : {
193 [ + + ]: 320064 : for( sal_uInt32 n = 0; n < GetMax(); n++ )
194 : : {
195 [ + + ]: 320048 : if( IsEntry( n ) )
196 [ + - ]: 40568 : pList->push_back( Get( n ) );
197 : : }
198 : : // hash order, sort now
199 : 16 : }
200 : :
201 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|