Branch libreoffice-5-0-4
[LibreOffice.git] / idl / source / cmptools / hash.cxx
blob6ca4a7c7cac2cbe18aff2215e9b27e6cebdaa270
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 // C and C++ includes
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <ctype.h>
26 // program-sensitive includes
27 #include <hash.hxx>
28 #include <tools/debug.hxx>
30 SvStringHashEntry::~SvStringHashEntry() { };
32 SvHashTable::SvHashTable( sal_uInt32 nMaxEntries )
34 nMax = nMaxEntries; // set max entries
35 nFill = 0; // no entries
36 lTry = 0;
37 lAsk = 0;
40 SvHashTable::~SvHashTable()
44 bool SvHashTable::Test_Insert( const OString& rElement, bool bInsert,
45 sal_uInt32 * pInsertPos )
47 sal_uInt32 nHash;
48 sal_uInt32 nIndex;
49 sal_uInt32 nLoop;
51 lAsk++;
52 lTry++;
54 nHash = HashFunc( rElement );
55 nIndex = nHash % nMax;
57 nLoop = 0; // divide to range
58 while( (nMax != nLoop) && IsEntry( nIndex ) )
59 { // is place occupied
60 if( equals( rElement, nIndex ) )
62 if( pInsertPos )
63 *pInsertPos = nIndex; // place of Element
64 return true;
66 nLoop++;
67 lTry++;
68 nIndex = (sal_uInt16)(nIndex + nHash + 7) % nMax;
71 if( bInsert )
73 DBG_ASSERT( nMax != nLoop, "Hash table full" );
74 if( nMax != nLoop )
76 nFill++;
77 *pInsertPos = nIndex; // return free place
78 return true;
81 return false;
84 SvStringHashTable::SvStringHashTable( sal_uInt32 nMaxEntries )
85 : SvHashTable( nMaxEntries )
87 pEntries = new SvStringHashEntry[ nMaxEntries ];
89 // set RefCount to one
90 SvStringHashEntry * pPos, *pEnd;
91 pPos = pEntries;
92 pEnd = pEntries + nMaxEntries;
93 while( pPos != pEnd )
95 pPos->AddFirstRef();
96 pPos++;
100 SvStringHashTable::~SvStringHashTable()
102 #ifdef DBG_UTIL
103 // set RefCount to one
104 SvStringHashEntry * pPos, *pEnd;
105 pPos = pEntries;
106 pEnd = pEntries + GetMax();
107 while( pPos != pEnd )
109 DBG_ASSERT( pPos->GetRefCount() == 1, "Reference count != 1" );
110 pPos++;
112 #endif
114 delete [] pEntries;
117 sal_uInt32 SvStringHashTable::HashFunc( const OString& rElement ) const
119 sal_uInt32 nHash = 0; // hash value
120 const char * pStr = rElement.getStr();
122 int nShift = 0;
123 while( *pStr )
125 if( isupper( *pStr ) )
126 nHash ^= sal_uInt32(*pStr - 'A' + 26) << nShift;
127 else
128 nHash ^= sal_uInt32(*pStr - 'a') << nShift;
129 if( nShift == 28 )
130 nShift = 0;
131 else
132 nShift += 4;
133 pStr++;
135 return nHash;
138 OString SvStringHashTable::GetNearString( const OString& rName ) const
140 for( sal_uInt32 i = 0; i < GetMax(); i++ )
142 SvStringHashEntry * pE = Get( i );
143 if( pE )
145 if( pE->GetName().equalsIgnoreAsciiCase( rName ) && !pE->GetName().equals( rName ) )
146 return pE->GetName();
149 return OString();
152 bool SvStringHashTable::IsEntry( sal_uInt32 nIndex ) const
154 if( nIndex >= GetMax() )
155 return false;
156 return pEntries[ nIndex ].HasId();
159 bool SvStringHashTable::Insert( const OString& rName, sal_uInt32 * pIndex )
161 sal_uInt32 nIndex;
163 if( !pIndex ) pIndex = &nIndex;
165 if( !SvHashTable::Test_Insert( rName, true, pIndex ) )
166 return false;
168 if( !IsEntry( *pIndex ) )
169 pEntries[ *pIndex ] = SvStringHashEntry( rName, *pIndex );
170 return true;
173 bool SvStringHashTable::Test( const OString& rName, sal_uInt32 * pPos ) const
175 return const_cast<SvStringHashTable*>(this)->Test_Insert( rName, false, pPos );
178 SvStringHashEntry * SvStringHashTable::Get( sal_uInt32 nIndex ) const
180 if( IsEntry( nIndex ) )
181 return pEntries + nIndex;
182 return NULL;
185 bool SvStringHashTable::equals( const OString& rElement,
186 sal_uInt32 nIndex ) const
188 return rElement.equals( pEntries[ nIndex ].GetName() );
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */