LCOV - code coverage report
Current view: top level - libreoffice/idl/source/cmptools - hash.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 85 0.0 %
Date: 2012-12-17 Functions: 0 17 0.0 %
Legend: Lines: hit not hit

          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           0 : SvStringHashEntry::~SvStringHashEntry() { };
      31             : 
      32           0 : SvHashTable::SvHashTable( sal_uInt32 nMaxEntries )
      33             : {
      34           0 :     nMax = nMaxEntries;     // set max entries
      35           0 :     nFill = 0;              // no entries
      36           0 :     lTry = 0;
      37           0 :     lAsk = 0;
      38           0 : }
      39             : 
      40           0 : SvHashTable::~SvHashTable()
      41             : {
      42           0 : }
      43             : 
      44           0 : 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           0 :     lAsk++;
      52           0 :     lTry++;
      53             : 
      54           0 :     nHash =  HashFunc( rElement );
      55           0 :     nIndex = nHash % nMax;
      56             : 
      57           0 :     nLoop = 0;                                      // divide to range
      58           0 :     while( (nMax != nLoop) && IsEntry( nIndex ) )
      59             :     {                     // is place occupied
      60           0 :         if( equals( rElement, nIndex ) )
      61             :         {
      62           0 :             if( pInsertPos )
      63           0 :                 *pInsertPos = nIndex;               // place of Element
      64           0 :             return sal_True;
      65             :         }
      66           0 :         nLoop++;
      67           0 :         lTry++;
      68           0 :         nIndex = (sal_uInt16)(nIndex + nHash + 7) % nMax;
      69             :     }
      70             : 
      71           0 :     if( bInsert )
      72             :     {
      73             :         DBG_ASSERT( nMax != nLoop, "Hash table full" );
      74           0 :         if( nMax != nLoop )
      75             :         {
      76           0 :             nFill++;
      77           0 :             *pInsertPos = nIndex;                       // return free place
      78           0 :             return sal_True;
      79             :         }
      80             :     }
      81           0 :     return( sal_False );
      82             : }
      83             : 
      84           0 : SvStringHashTable::SvStringHashTable( sal_uInt32 nMaxEntries )
      85           0 :         : SvHashTable( nMaxEntries )
      86             : {
      87           0 :     pEntries = new SvStringHashEntry[ nMaxEntries ];
      88             : 
      89             :     // set RefCount to one
      90             :     SvStringHashEntry * pPos, *pEnd;
      91           0 :     pPos    = pEntries;
      92           0 :     pEnd    = pEntries + nMaxEntries;
      93           0 :     while( pPos != pEnd )
      94             :     {
      95           0 :         pPos->AddRef();
      96           0 :         pPos++;
      97             :     }
      98           0 : }
      99             : 
     100           0 : 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           0 :     delete [] pEntries;
     115           0 : }
     116             : 
     117           0 : sal_uInt32 SvStringHashTable::HashFunc( const rtl::OString& rElement ) const
     118             : {
     119           0 :     sal_uInt32          nHash = 0;  // hash value
     120           0 :     const char *    pStr = rElement.getStr();
     121             : 
     122           0 :     int nShift = 0;
     123           0 :     while( *pStr )
     124             :     {
     125           0 :         if( isupper( *pStr ) )
     126           0 :             nHash ^= sal_uInt32(*pStr - 'A' + 26) << nShift;
     127             :         else
     128           0 :             nHash ^= sal_uInt32(*pStr - 'a') << nShift;
     129           0 :         if( nShift == 28 )
     130           0 :             nShift = 0;
     131             :         else
     132           0 :             nShift += 4;
     133           0 :         pStr++;
     134             :     }
     135           0 :     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           0 : sal_Bool SvStringHashTable::IsEntry( sal_uInt32 nIndex ) const
     153             : {
     154           0 :     if( nIndex >= GetMax() )
     155           0 :         return sal_False;
     156           0 :     return pEntries[ nIndex ].HasId();
     157             : }
     158             : 
     159           0 : sal_Bool SvStringHashTable::Insert( const rtl::OString& rName, sal_uInt32 * pIndex )
     160             : {
     161             :     sal_uInt32 nIndex;
     162             : 
     163           0 :     if( !pIndex ) pIndex = &nIndex;
     164             : 
     165           0 :     if( !SvHashTable::Test_Insert( rName, sal_True, pIndex ) )
     166           0 :         return sal_False;
     167             : 
     168           0 :     if( !IsEntry( *pIndex ) )
     169           0 :         pEntries[ *pIndex ] = SvStringHashEntry( rName, *pIndex );
     170           0 :     return sal_True;
     171             : }
     172             : 
     173           0 : sal_Bool SvStringHashTable::Test( const rtl::OString& rName, sal_uInt32 * pPos ) const
     174             : {
     175           0 :     return const_cast<SvStringHashTable*>(this)->Test_Insert( rName, sal_False, pPos );
     176             : }
     177             : 
     178           0 : SvStringHashEntry * SvStringHashTable::Get( sal_uInt32 nIndex ) const
     179             : {
     180           0 :     if( IsEntry( nIndex ) )
     181           0 :         return pEntries + nIndex;
     182           0 :     return( NULL );
     183             : }
     184             : 
     185           0 : bool SvStringHashTable::equals( const rtl::OString& rElement,
     186             :                                           sal_uInt32 nIndex ) const
     187             : {
     188           0 :     return rElement.equals( pEntries[ nIndex ].GetName() );
     189             : }
     190             : 
     191           0 : void SvStringHashTable::FillHashList( SvStringHashList * pList ) const
     192             : {
     193           0 :     for( sal_uInt32 n = 0; n < GetMax(); n++ )
     194             :     {
     195           0 :         if( IsEntry( n ) )
     196           0 :             pList->push_back( Get( n ) );
     197             :     }
     198             :     // hash order, sort now
     199           0 : }
     200             : 
     201             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10