LCOV - code coverage report
Current view: top level - libreoffice/sfx2/source/bastyp - minarray.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 66 103 64.1 %
Date: 2012-12-27 Functions: 5 9 55.6 %
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        2270 : SfxPtrArr::SfxPtrArr( sal_uInt8 nInitSize, sal_uInt8 nGrowSize ):
      26             :     nUsed( 0 ),
      27             :     nGrow( nGrowSize ? nGrowSize : 1 ),
      28        2270 :     nUnused( nInitSize )
      29             : {
      30        2270 :     sal_uInt16 nMSCBug = nInitSize;
      31             : 
      32        2270 :     if ( nMSCBug > 0 )
      33        2242 :         pData = new void*[nMSCBug];
      34             :     else
      35          28 :         pData = 0;
      36        2270 : }
      37             : 
      38             : // -----------------------------------------------------------------------
      39             : 
      40           0 : SfxPtrArr::SfxPtrArr( const SfxPtrArr& rOrig )
      41             : {
      42           0 :     nUsed = rOrig.nUsed;
      43           0 :     nGrow = rOrig.nGrow;
      44           0 :     nUnused = rOrig.nUnused;
      45             : 
      46           0 :     if ( rOrig.pData != 0 )
      47             :     {
      48           0 :         pData = new void*[nUsed+nUnused];
      49           0 :         memcpy( pData, rOrig.pData, nUsed*sizeof(void*) );
      50             :     }
      51             :     else
      52           0 :         pData = 0;
      53           0 : }
      54             : 
      55             : // -----------------------------------------------------------------------
      56             : 
      57         301 : SfxPtrArr::~SfxPtrArr()
      58             : {
      59         301 :     delete [] pData;
      60         301 : }
      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        3368 : 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        3368 :     if ( nUnused == 0 )
      90             :     {
      91         379 :         sal_uInt16 nNewSize = (nUsed == 1) ? (nGrow==1 ? 2 : nGrow) : nUsed+nGrow;
      92         379 :         void** pNewData = new void*[nNewSize];
      93         379 :         if ( pData )
      94             :         {
      95             :             DBG_ASSERT( nUsed <= nNewSize, "" );
      96         379 :             memcpy( pNewData, pData, sizeof(void*)*nUsed );
      97         379 :             delete [] pData;
      98             :         }
      99         379 :         nUnused = sal::static_int_cast< sal_uInt8 >(nNewSize-nUsed);
     100         379 :         pData = pNewData;
     101             :     }
     102             : 
     103             :     // now write at the back in the open space
     104        3368 :     pData[nUsed] = aElem;
     105        3368 :     ++nUsed;
     106        3368 :     --nUnused;
     107        3368 : }
     108             : 
     109             : // -----------------------------------------------------------------------
     110             : 
     111        8110 : sal_uInt16 SfxPtrArr::Remove( sal_uInt16 nPos, sal_uInt16 nLen )
     112             : {
     113             :     // Adjust nLen, thus to avoid deleting beyond the end
     114        8110 :     nLen = Min( (sal_uInt16)(nUsed-nPos), nLen );
     115             : 
     116             :     // simple problems require simple solutions!
     117        8110 :     if ( nLen == 0 )
     118           0 :         return 0;
     119             : 
     120             :     // Maybe no one will remain
     121        8110 :     if ( (nUsed-nLen) == 0 )
     122             :     {
     123         498 :         delete [] pData;
     124         498 :         pData = 0;
     125         498 :         nUsed = 0;
     126         498 :         nUnused = 0;
     127         498 :         return nLen;
     128             :     }
     129             : 
     130             :     // Determine whether the array has physically shrunk...
     131        7612 :     if ( (nUnused+nLen) >= nGrow )
     132             :     {
     133             :         // reduce (rounded up) to the next Grow-border
     134        1306 :         sal_uInt16 nNewUsed = nUsed-nLen;
     135        1306 :         sal_uInt16 nNewSize = ((nNewUsed+nGrow-1)/nGrow) * nGrow;
     136             :         DBG_ASSERT( nNewUsed <= nNewSize && nNewUsed+nGrow > nNewSize,
     137             :                     "shrink size computation failed" );
     138        1306 :         void** pNewData = new void*[nNewSize];
     139        1306 :         if ( nPos > 0 )
     140             :         {
     141             :             DBG_ASSERT( nPos <= nNewSize, "" );
     142         300 :             memcpy( pNewData, pData, sizeof(void*)*nPos );
     143             :         }
     144        1306 :         if ( nNewUsed != nPos )
     145        2012 :             memcpy( pNewData+nPos, pData+nPos+nLen,
     146        3018 :                     sizeof(void*)*(nNewUsed-nPos) );
     147        1306 :         delete [] pData;
     148        1306 :         pData = pNewData;
     149        1306 :         nUsed = nNewUsed;
     150        1306 :         nUnused = sal::static_int_cast< sal_uInt8 >(nNewSize - nNewUsed);
     151        1306 :         return nLen;
     152             :     }
     153             : 
     154             :     // in all other cases, only push together
     155        6306 :     if ( nUsed-nPos-nLen > 0 )
     156        1416 :         memmove( pData+nPos, pData+nPos+nLen, (nUsed-nPos-nLen)*sizeof(void*) );
     157        6306 :     nUsed = nUsed - nLen;
     158        6306 :     nUnused = sal::static_int_cast< sal_uInt8 >(nUnused + nLen);
     159        6306 :     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           0 : sal_Bool SfxPtrArr::Contains( const void* rItem ) const
     184             : {
     185           0 :     if ( !nUsed )
     186           0 :         return sal_False;
     187             : 
     188           0 :     for ( sal_uInt16 n = 0; n < nUsed; ++n )
     189             :     {
     190           0 :         void* p = GetObject(n);
     191           0 :         if ( p == rItem )
     192           0 :             return sal_True;
     193             :     }
     194             : 
     195           0 :     return sal_False;
     196             : }
     197             : 
     198             : // -----------------------------------------------------------------------
     199             : 
     200       18305 : 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       18305 :     if ( nUnused == 0 )
     205             :     {
     206             :         // increase (rounded up ) to the next Grow-border
     207        5453 :         sal_uInt16 nNewSize = nUsed+nGrow;
     208        5453 :         void** pNewData = new void*[nNewSize];
     209             : 
     210        5453 :         if ( pData )
     211             :         {
     212             :             DBG_ASSERT( nUsed < nNewSize, "" );
     213        5217 :             memcpy( pNewData, pData, sizeof(void*)*nUsed );
     214        5217 :             delete [] pData;
     215             :         }
     216        5453 :         nUnused = sal::static_int_cast< sal_uInt8 >(nNewSize-nUsed);
     217        5453 :         pData = pNewData;
     218             :     }
     219             : 
     220             :     // Now move the rear part
     221       18305 :     if ( nPos < nUsed )
     222        8072 :         memmove( pData+nPos+1, pData+nPos, (nUsed-nPos)*sizeof(void*) );
     223             : 
     224             :     // Now write into the free space.
     225       18305 :     memmove( pData+nPos, &rElem, sizeof(void*) );
     226       18305 :     nUsed += 1;
     227       18305 :     nUnused -= 1;
     228       18305 : }
     229             : 
     230             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10