LCOV - code coverage report
Current view: top level - libreoffice/sfx2/source/bastyp - minarray.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 81 103 78.6 %
Date: 2012-12-17 Functions: 7 9 77.8 %
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             : #include <sfx2/minarray.hxx>
      22             : 
      23             : // -----------------------------------------------------------------------
      24             : 
      25        6298 : SfxPtrArr::SfxPtrArr( sal_uInt8 nInitSize, sal_uInt8 nGrowSize ):
      26             :     nUsed( 0 ),
      27             :     nGrow( nGrowSize ? nGrowSize : 1 ),
      28        6298 :     nUnused( nInitSize )
      29             : {
      30        6298 :     sal_uInt16 nMSCBug = nInitSize;
      31             : 
      32        6298 :     if ( nMSCBug > 0 )
      33        6256 :         pData = new void*[nMSCBug];
      34             :     else
      35          42 :         pData = 0;
      36        6298 : }
      37             : 
      38             : // -----------------------------------------------------------------------
      39             : 
      40          94 : SfxPtrArr::SfxPtrArr( const SfxPtrArr& rOrig )
      41             : {
      42          94 :     nUsed = rOrig.nUsed;
      43          94 :     nGrow = rOrig.nGrow;
      44          94 :     nUnused = rOrig.nUnused;
      45             : 
      46          94 :     if ( rOrig.pData != 0 )
      47             :     {
      48          94 :         pData = new void*[nUsed+nUnused];
      49          94 :         memcpy( pData, rOrig.pData, nUsed*sizeof(void*) );
      50             :     }
      51             :     else
      52           0 :         pData = 0;
      53          94 : }
      54             : 
      55             : // -----------------------------------------------------------------------
      56             : 
      57         842 : SfxPtrArr::~SfxPtrArr()
      58             : {
      59         842 :     delete [] pData;
      60         842 : }
      61             : 
      62             : // -----------------------------------------------------------------------
      63             : 
      64           0 : SfxPtrArr& SfxPtrArr::operator=( const SfxPtrArr& rOrig )
      65             : {
      66             : 
      67           0 :     delete [] pData;
      68             : 
      69           0 :     nUsed = rOrig.nUsed;
      70           0 :     nGrow = rOrig.nGrow;
      71           0 :     nUnused = rOrig.nUnused;
      72             : 
      73           0 :     if ( rOrig.pData != 0 )
      74             :     {
      75           0 :         pData = new void*[nUsed+nUnused];
      76           0 :         memcpy( pData, rOrig.pData, nUsed*sizeof(void*) );
      77             :     }
      78             :     else
      79           0 :         pData = 0;
      80           0 :     return *this;
      81             : }
      82             : 
      83             : // -----------------------------------------------------------------------
      84             : 
      85        9172 : void SfxPtrArr::Append( void* aElem )
      86             : {
      87             :     DBG_ASSERT( sal::static_int_cast< unsigned >(nUsed+1) < ( USHRT_MAX / sizeof(void*) ), "array too large" );
      88             :     // Does the Array need to be copied?
      89        9172 :     if ( nUnused == 0 )
      90             :     {
      91        1376 :         sal_uInt16 nNewSize = (nUsed == 1) ? (nGrow==1 ? 2 : nGrow) : nUsed+nGrow;
      92        1376 :         void** pNewData = new void*[nNewSize];
      93        1376 :         if ( pData )
      94             :         {
      95             :             DBG_ASSERT( nUsed <= nNewSize, "" );
      96        1376 :             memcpy( pNewData, pData, sizeof(void*)*nUsed );
      97        1376 :             delete [] pData;
      98             :         }
      99        1376 :         nUnused = sal::static_int_cast< sal_uInt8 >(nNewSize-nUsed);
     100        1376 :         pData = pNewData;
     101             :     }
     102             : 
     103             :     // now write at the back in the open space
     104        9172 :     pData[nUsed] = aElem;
     105        9172 :     ++nUsed;
     106        9172 :     --nUnused;
     107        9172 : }
     108             : 
     109             : // -----------------------------------------------------------------------
     110             : 
     111       14895 : sal_uInt16 SfxPtrArr::Remove( sal_uInt16 nPos, sal_uInt16 nLen )
     112             : {
     113             :     // Adjust nLen, thus to avoid deleting beyond the end
     114       14895 :     nLen = Min( (sal_uInt16)(nUsed-nPos), nLen );
     115             : 
     116             :     // simple problems require simple solutions!
     117       14895 :     if ( nLen == 0 )
     118           0 :         return 0;
     119             : 
     120             :     // Maybe no one will remain
     121       14895 :     if ( (nUsed-nLen) == 0 )
     122             :     {
     123         908 :         delete [] pData;
     124         908 :         pData = 0;
     125         908 :         nUsed = 0;
     126         908 :         nUnused = 0;
     127         908 :         return nLen;
     128             :     }
     129             : 
     130             :     // Determine whether the array has physically shrunk...
     131       13987 :     if ( (nUnused+nLen) >= nGrow )
     132             :     {
     133             :         // reduce (rounded up) to the next Grow-border
     134        2295 :         sal_uInt16 nNewUsed = nUsed-nLen;
     135        2295 :         sal_uInt16 nNewSize = ((nNewUsed+nGrow-1)/nGrow) * nGrow;
     136             :         DBG_ASSERT( nNewUsed <= nNewSize && nNewUsed+nGrow > nNewSize,
     137             :                     "shrink size computation failed" );
     138        2295 :         void** pNewData = new void*[nNewSize];
     139        2295 :         if ( nPos > 0 )
     140             :         {
     141             :             DBG_ASSERT( nPos <= nNewSize, "" );
     142         618 :             memcpy( pNewData, pData, sizeof(void*)*nPos );
     143             :         }
     144        2295 :         if ( nNewUsed != nPos )
     145        3406 :             memcpy( pNewData+nPos, pData+nPos+nLen,
     146        5109 :                     sizeof(void*)*(nNewUsed-nPos) );
     147        2295 :         delete [] pData;
     148        2295 :         pData = pNewData;
     149        2295 :         nUsed = nNewUsed;
     150        2295 :         nUnused = sal::static_int_cast< sal_uInt8 >(nNewSize - nNewUsed);
     151        2295 :         return nLen;
     152             :     }
     153             : 
     154             :     // in all other cases, only push together
     155       11692 :     if ( nUsed-nPos-nLen > 0 )
     156        2526 :         memmove( pData+nPos, pData+nPos+nLen, (nUsed-nPos-nLen)*sizeof(void*) );
     157       11692 :     nUsed = nUsed - nLen;
     158       11692 :     nUnused = sal::static_int_cast< sal_uInt8 >(nUnused + nLen);
     159       11692 :     return nLen;
     160             : }
     161             : 
     162             : // -----------------------------------------------------------------------
     163             : 
     164           0 : sal_Bool SfxPtrArr::Remove( void* aElem )
     165             : {
     166             :     // simple tasks ...
     167           0 :     if ( nUsed == 0 )
     168           0 :         return sal_False;
     169             : 
     170             :     // backwards, since most of the last is first removed
     171           0 :     void* *pIter = pData + nUsed - 1;
     172           0 :     for ( sal_uInt16 n = 0; n < nUsed; ++n, --pIter )
     173           0 :         if ( *pIter == aElem )
     174             :         {
     175           0 :             Remove(nUsed-n-1, 1);
     176           0 :             return sal_True;
     177             :         }
     178           0 :     return sal_False;
     179             : }
     180             : 
     181             : // -----------------------------------------------------------------------
     182             : 
     183          94 : sal_Bool SfxPtrArr::Contains( const void* rItem ) const
     184             : {
     185          94 :     if ( !nUsed )
     186           0 :         return sal_False;
     187             : 
     188         470 :     for ( sal_uInt16 n = 0; n < nUsed; ++n )
     189             :     {
     190         384 :         void* p = GetObject(n);
     191         384 :         if ( p == rItem )
     192           8 :             return sal_True;
     193             :     }
     194             : 
     195          86 :     return sal_False;
     196             : }
     197             : 
     198             : // -----------------------------------------------------------------------
     199             : 
     200       39015 : void SfxPtrArr::Insert( sal_uInt16 nPos, void* rElem )
     201             : {
     202             :     DBG_ASSERT( sal::static_int_cast< unsigned >(nUsed+1) < ( USHRT_MAX / sizeof(void*) ), "array too large" );
     203             :     // Does the Array have to be copied?
     204       39015 :     if ( nUnused == 0 )
     205             :     {
     206             :         // increase (rounded up ) to the next Grow-border
     207       11865 :         sal_uInt16 nNewSize = nUsed+nGrow;
     208       11865 :         void** pNewData = new void*[nNewSize];
     209             : 
     210       11865 :         if ( pData )
     211             :         {
     212             :             DBG_ASSERT( nUsed < nNewSize, "" );
     213       11387 :             memcpy( pNewData, pData, sizeof(void*)*nUsed );
     214       11387 :             delete [] pData;
     215             :         }
     216       11865 :         nUnused = sal::static_int_cast< sal_uInt8 >(nNewSize-nUsed);
     217       11865 :         pData = pNewData;
     218             :     }
     219             : 
     220             :     // Now move the rear part
     221       39015 :     if ( nPos < nUsed )
     222       16870 :         memmove( pData+nPos+1, pData+nPos, (nUsed-nPos)*sizeof(void*) );
     223             : 
     224             :     // Now write into the free space.
     225       39015 :     memmove( pData+nPos, &rElem, sizeof(void*) );
     226       39015 :     nUsed += 1;
     227       39015 :     nUnused -= 1;
     228       39015 : }
     229             : 
     230             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10