1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_idl.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir /****************** I N C L U D E S **************************************/ 32*cdf0e10cSrcweir // C and C++ Includes. 33*cdf0e10cSrcweir #include <stdlib.h> 34*cdf0e10cSrcweir #include <stdio.h> 35*cdf0e10cSrcweir #include <ctype.h> 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir // Programmabh�ngige Includes. 38*cdf0e10cSrcweir #include <hash.hxx> 39*cdf0e10cSrcweir #include <tools/debug.hxx> 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir /****************** C O D E **********************************************/ 42*cdf0e10cSrcweir /************************************************************************* 43*cdf0e10cSrcweir |* 44*cdf0e10cSrcweir |* SvStringHashEntry::~SvStringHashEntry() 45*cdf0e10cSrcweir |* 46*cdf0e10cSrcweir |* Beschreibung 47*cdf0e10cSrcweir |* 48*cdf0e10cSrcweir *************************************************************************/ 49*cdf0e10cSrcweir SvStringHashEntry::~SvStringHashEntry() { }; 50*cdf0e10cSrcweir 51*cdf0e10cSrcweir /************************************************************************* 52*cdf0e10cSrcweir |* 53*cdf0e10cSrcweir |* SvHashTable::SvHashTable() 54*cdf0e10cSrcweir |* 55*cdf0e10cSrcweir |* Beschreibung 56*cdf0e10cSrcweir |* 57*cdf0e10cSrcweir *************************************************************************/ 58*cdf0e10cSrcweir SvHashTable::SvHashTable( sal_uInt32 nMaxEntries ) 59*cdf0e10cSrcweir { 60*cdf0e10cSrcweir nMax = nMaxEntries; // set max entries 61*cdf0e10cSrcweir nFill = 0; // no entries 62*cdf0e10cSrcweir lTry = 0; 63*cdf0e10cSrcweir lAsk = 0; 64*cdf0e10cSrcweir } 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir /************************************************************************* 67*cdf0e10cSrcweir |* 68*cdf0e10cSrcweir |* SvHashTable::~SvHashTable() 69*cdf0e10cSrcweir |* 70*cdf0e10cSrcweir |* Beschreibung 71*cdf0e10cSrcweir |* 72*cdf0e10cSrcweir *************************************************************************/ 73*cdf0e10cSrcweir SvHashTable::~SvHashTable() 74*cdf0e10cSrcweir { 75*cdf0e10cSrcweir #ifdef DOS_NIE 76*cdf0e10cSrcweir printf( "Maximum: %ld, F�llung: %ld\n", (sal_uLong)nMax, (sal_uLong)nFill ); 77*cdf0e10cSrcweir printf( "Anfragen: %ld, Versuche: %ld", (sal_uLong)lAsk, (sal_uLong)lTry ); 78*cdf0e10cSrcweir if( lTry != 0 ) 79*cdf0e10cSrcweir printf( ", V/E = %ld\n", lTry / lAsk ); 80*cdf0e10cSrcweir #endif 81*cdf0e10cSrcweir } 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir /************************************************************************* 84*cdf0e10cSrcweir |* 85*cdf0e10cSrcweir |* SvHashTable::Test_Insert() 86*cdf0e10cSrcweir |* 87*cdf0e10cSrcweir |* Beschreibung 88*cdf0e10cSrcweir |* 89*cdf0e10cSrcweir *************************************************************************/ 90*cdf0e10cSrcweir sal_Bool SvHashTable::Test_Insert( const void * pElement, sal_Bool bInsert, 91*cdf0e10cSrcweir sal_uInt32 * pInsertPos ) 92*cdf0e10cSrcweir { 93*cdf0e10cSrcweir sal_uInt32 nHash; 94*cdf0e10cSrcweir sal_uInt32 nIndex; 95*cdf0e10cSrcweir sal_uInt32 nLoop; 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir lAsk++; 98*cdf0e10cSrcweir lTry++; 99*cdf0e10cSrcweir 100*cdf0e10cSrcweir nHash = HashFunc( pElement ); 101*cdf0e10cSrcweir nIndex = nHash % nMax; 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir // const char* s = ((ByteString*) pElement)->GetStr(); 104*cdf0e10cSrcweir // fprintf(stderr,"### Hash: %lu , Name: %s\n",nIndex,s ); 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir nLoop = 0; // divide to range 107*cdf0e10cSrcweir while( (nMax != nLoop) && IsEntry( nIndex ) ) 108*cdf0e10cSrcweir { // is place occupied 109*cdf0e10cSrcweir if( COMPARE_EQUAL == Compare( pElement, nIndex ) ) 110*cdf0e10cSrcweir { 111*cdf0e10cSrcweir if( pInsertPos ) 112*cdf0e10cSrcweir *pInsertPos = nIndex; // place of Element 113*cdf0e10cSrcweir return sal_True; 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir nLoop++; 116*cdf0e10cSrcweir lTry++; 117*cdf0e10cSrcweir nIndex = (sal_uInt16)(nIndex + nHash + 7) % nMax; 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir if( bInsert ) 121*cdf0e10cSrcweir { 122*cdf0e10cSrcweir DBG_ASSERT( nMax != nLoop, "Hash table full" ); 123*cdf0e10cSrcweir if( nMax != nLoop ) 124*cdf0e10cSrcweir { 125*cdf0e10cSrcweir nFill++; 126*cdf0e10cSrcweir *pInsertPos = nIndex; // return free place 127*cdf0e10cSrcweir return sal_True; 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir } 130*cdf0e10cSrcweir return( sal_False ); 131*cdf0e10cSrcweir } 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir /************************************************************************/ 134*cdf0e10cSrcweir /************************************************************************* 135*cdf0e10cSrcweir |* 136*cdf0e10cSrcweir |* SvStringHashTable::SvStringHashTable() 137*cdf0e10cSrcweir |* 138*cdf0e10cSrcweir |* Beschreibung 139*cdf0e10cSrcweir |* 140*cdf0e10cSrcweir *************************************************************************/ 141*cdf0e10cSrcweir SvStringHashTable::SvStringHashTable( sal_uInt32 nMaxEntries ) 142*cdf0e10cSrcweir : SvHashTable( nMaxEntries ) 143*cdf0e10cSrcweir { 144*cdf0e10cSrcweir pEntries = new SvStringHashEntry[ nMaxEntries ]; 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir // RefCount auf eins setzen 147*cdf0e10cSrcweir SvStringHashEntry * pPos, *pEnd; 148*cdf0e10cSrcweir pPos = pEntries; 149*cdf0e10cSrcweir pEnd = pEntries + nMaxEntries; 150*cdf0e10cSrcweir while( pPos != pEnd ) 151*cdf0e10cSrcweir { 152*cdf0e10cSrcweir pPos->AddRef(); 153*cdf0e10cSrcweir pPos++; 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir /************************************************************************* 158*cdf0e10cSrcweir |* 159*cdf0e10cSrcweir |* ~SvStringHashTable::SvStringHashTable() 160*cdf0e10cSrcweir |* 161*cdf0e10cSrcweir |* Beschreibung 162*cdf0e10cSrcweir |* 163*cdf0e10cSrcweir *************************************************************************/ 164*cdf0e10cSrcweir SvStringHashTable::~SvStringHashTable() 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir // RefCount auf eins setzen 167*cdf0e10cSrcweir SvStringHashEntry * pPos, *pEnd; 168*cdf0e10cSrcweir pPos = pEntries; 169*cdf0e10cSrcweir pEnd = pEntries + GetMax(); 170*cdf0e10cSrcweir #ifdef DBG_UTIL 171*cdf0e10cSrcweir while( pPos != pEnd ) 172*cdf0e10cSrcweir { 173*cdf0e10cSrcweir DBG_ASSERT( pPos->GetRefCount() == 1, "Reference count != 1" ); 174*cdf0e10cSrcweir pPos++; 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir #endif 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir #ifdef MPW 179*cdf0e10cSrcweir // der MPW-Compiler ruft sonst keine Dtoren! 180*cdf0e10cSrcweir for ( sal_uInt16 n = 0; n < GetMax(); ++n ) 181*cdf0e10cSrcweir (pEntries+n)->SvStringHashEntry::~SvStringHashEntry(); 182*cdf0e10cSrcweir delete (void*) pEntries; 183*cdf0e10cSrcweir #else 184*cdf0e10cSrcweir delete [] pEntries; 185*cdf0e10cSrcweir #endif 186*cdf0e10cSrcweir } 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir /************************************************************************* 189*cdf0e10cSrcweir |* 190*cdf0e10cSrcweir |* SvStringHashTable::HashFunc() 191*cdf0e10cSrcweir |* 192*cdf0e10cSrcweir |* Beschreibung 193*cdf0e10cSrcweir |* 194*cdf0e10cSrcweir *************************************************************************/ 195*cdf0e10cSrcweir sal_uInt32 SvStringHashTable::HashFunc( const void * pElement ) const 196*cdf0e10cSrcweir { 197*cdf0e10cSrcweir sal_uInt32 nHash = 0; // hash value 198*cdf0e10cSrcweir const char * pStr = ((const ByteString * )pElement)->GetBuffer(); 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir int nShift = 0; 201*cdf0e10cSrcweir while( *pStr ) 202*cdf0e10cSrcweir { 203*cdf0e10cSrcweir if( isupper( *pStr ) ) 204*cdf0e10cSrcweir nHash ^= sal_uInt32(*pStr - 'A' + 26) << nShift; 205*cdf0e10cSrcweir else 206*cdf0e10cSrcweir nHash ^= sal_uInt32(*pStr - 'a') << nShift; 207*cdf0e10cSrcweir if( nShift == 28 ) 208*cdf0e10cSrcweir nShift = 0; 209*cdf0e10cSrcweir else 210*cdf0e10cSrcweir nShift += 4; 211*cdf0e10cSrcweir pStr++; 212*cdf0e10cSrcweir } 213*cdf0e10cSrcweir return( nHash ); 214*cdf0e10cSrcweir } 215*cdf0e10cSrcweir 216*cdf0e10cSrcweir /************************************************************************* 217*cdf0e10cSrcweir |* 218*cdf0e10cSrcweir |* SvStringHashTable::GetNearString() 219*cdf0e10cSrcweir |* 220*cdf0e10cSrcweir |* Beschreibung 221*cdf0e10cSrcweir |* 222*cdf0e10cSrcweir *************************************************************************/ 223*cdf0e10cSrcweir ByteString SvStringHashTable::GetNearString( const ByteString & rName ) const 224*cdf0e10cSrcweir { 225*cdf0e10cSrcweir for( sal_uInt32 i = 0; i < GetMax(); i++ ) 226*cdf0e10cSrcweir { 227*cdf0e10cSrcweir SvStringHashEntry * pE = Get( i ); 228*cdf0e10cSrcweir if( pE ) 229*cdf0e10cSrcweir { 230*cdf0e10cSrcweir if( pE->GetName().EqualsIgnoreCaseAscii( rName ) && !pE->GetName().Equals( rName ) ) 231*cdf0e10cSrcweir return pE->GetName(); 232*cdf0e10cSrcweir } 233*cdf0e10cSrcweir } 234*cdf0e10cSrcweir return ByteString(); 235*cdf0e10cSrcweir } 236*cdf0e10cSrcweir 237*cdf0e10cSrcweir /************************************************************************* 238*cdf0e10cSrcweir |* 239*cdf0e10cSrcweir |* SvStringHashTable::IsEntry() 240*cdf0e10cSrcweir |* 241*cdf0e10cSrcweir |* Beschreibung 242*cdf0e10cSrcweir |* 243*cdf0e10cSrcweir *************************************************************************/ 244*cdf0e10cSrcweir sal_Bool SvStringHashTable::IsEntry( sal_uInt32 nIndex ) const 245*cdf0e10cSrcweir { 246*cdf0e10cSrcweir if( nIndex >= GetMax() ) 247*cdf0e10cSrcweir return sal_False; 248*cdf0e10cSrcweir return pEntries[ nIndex ].HasId(); 249*cdf0e10cSrcweir } 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir /************************************************************************* 252*cdf0e10cSrcweir |* 253*cdf0e10cSrcweir |* SvStringHashTable::Insert() 254*cdf0e10cSrcweir |* 255*cdf0e10cSrcweir |* Beschreibung 256*cdf0e10cSrcweir |* 257*cdf0e10cSrcweir *************************************************************************/ 258*cdf0e10cSrcweir sal_Bool SvStringHashTable::Insert( const ByteString & rName, sal_uInt32 * pIndex ) 259*cdf0e10cSrcweir { 260*cdf0e10cSrcweir sal_uInt32 nIndex; 261*cdf0e10cSrcweir 262*cdf0e10cSrcweir if( !pIndex ) pIndex = &nIndex; 263*cdf0e10cSrcweir 264*cdf0e10cSrcweir if( !SvHashTable::Test_Insert( &rName, sal_True, pIndex ) ) 265*cdf0e10cSrcweir return sal_False; 266*cdf0e10cSrcweir 267*cdf0e10cSrcweir if( !IsEntry( *pIndex ) ) 268*cdf0e10cSrcweir pEntries[ *pIndex ] = SvStringHashEntry( rName, *pIndex ); 269*cdf0e10cSrcweir return sal_True; 270*cdf0e10cSrcweir } 271*cdf0e10cSrcweir 272*cdf0e10cSrcweir /************************************************************************* 273*cdf0e10cSrcweir |* 274*cdf0e10cSrcweir |* SvStringHashTable::Test() 275*cdf0e10cSrcweir |* 276*cdf0e10cSrcweir |* Beschreibung 277*cdf0e10cSrcweir |* 278*cdf0e10cSrcweir *************************************************************************/ 279*cdf0e10cSrcweir sal_Bool SvStringHashTable::Test( const ByteString & rName, sal_uInt32 * pPos ) const 280*cdf0e10cSrcweir { 281*cdf0e10cSrcweir return ((SvStringHashTable *)this)->SvHashTable:: 282*cdf0e10cSrcweir Test_Insert( &rName, sal_False, pPos ); 283*cdf0e10cSrcweir } 284*cdf0e10cSrcweir 285*cdf0e10cSrcweir /************************************************************************* 286*cdf0e10cSrcweir |* 287*cdf0e10cSrcweir |* SvStringHashTable::Get() 288*cdf0e10cSrcweir |* 289*cdf0e10cSrcweir |* Beschreibung 290*cdf0e10cSrcweir |* 291*cdf0e10cSrcweir *************************************************************************/ 292*cdf0e10cSrcweir SvStringHashEntry * SvStringHashTable::Get( sal_uInt32 nIndex ) const 293*cdf0e10cSrcweir { 294*cdf0e10cSrcweir if( IsEntry( nIndex ) ) 295*cdf0e10cSrcweir return pEntries + nIndex; 296*cdf0e10cSrcweir return( NULL ); 297*cdf0e10cSrcweir } 298*cdf0e10cSrcweir 299*cdf0e10cSrcweir /************************************************************************* 300*cdf0e10cSrcweir |* 301*cdf0e10cSrcweir |* SvStringHashTable::Get() 302*cdf0e10cSrcweir |* 303*cdf0e10cSrcweir |* Beschreibung 304*cdf0e10cSrcweir |* 305*cdf0e10cSrcweir *************************************************************************/ 306*cdf0e10cSrcweir StringCompare SvStringHashTable::Compare( const void * pElement, 307*cdf0e10cSrcweir sal_uInt32 nIndex ) const 308*cdf0e10cSrcweir { 309*cdf0e10cSrcweir return ((const ByteString *)pElement)->CompareTo( pEntries[ nIndex ].GetName() ); 310*cdf0e10cSrcweir } 311*cdf0e10cSrcweir 312*cdf0e10cSrcweir /************************************************************************* 313*cdf0e10cSrcweir |* 314*cdf0e10cSrcweir |* SvStringHashTable::FillHashList() 315*cdf0e10cSrcweir |* 316*cdf0e10cSrcweir |* Beschreibung 317*cdf0e10cSrcweir |* 318*cdf0e10cSrcweir *************************************************************************/ 319*cdf0e10cSrcweir void SvStringHashTable::FillHashList( SvStringHashList * pList ) const 320*cdf0e10cSrcweir { 321*cdf0e10cSrcweir for( sal_uInt32 n = 0; n < GetMax(); n++ ) 322*cdf0e10cSrcweir { 323*cdf0e10cSrcweir if( IsEntry( n ) ) 324*cdf0e10cSrcweir pList->Insert( Get( n ), LIST_APPEND ); 325*cdf0e10cSrcweir } 326*cdf0e10cSrcweir // Hash Reihenfolge, jetzt sortieren 327*cdf0e10cSrcweir } 328