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_sfx2.hxx" 30*cdf0e10cSrcweir #include <tools/debug.hxx> 31*cdf0e10cSrcweir #ifndef GCC 32*cdf0e10cSrcweir #endif 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir #include "bitset.hxx" 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir #include <string.h> // memset(), memcpy() 37*cdf0e10cSrcweir #include <limits.h> // USHRT_MAX 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir //==================================================================== 40*cdf0e10cSrcweir // add nOffset to each bit-value in the set 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir BitSet BitSet::operator<<( sal_uInt16 nOffset ) const 43*cdf0e10cSrcweir { 44*cdf0e10cSrcweir DBG_MEMTEST(); 45*cdf0e10cSrcweir // create a work-copy, return it if nothing to shift 46*cdf0e10cSrcweir BitSet aSet(*this); 47*cdf0e10cSrcweir if ( nOffset == 0 ) 48*cdf0e10cSrcweir return aSet; 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir // compute the shiftment in long-words and bits 51*cdf0e10cSrcweir sal_uInt16 nBlockDiff = nOffset / 32; 52*cdf0e10cSrcweir sal_uIntPtr nBitValDiff = nOffset % 32; 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir // compute the new number of bits 55*cdf0e10cSrcweir for ( sal_uInt16 nBlock = 0; nBlock < nBlockDiff; ++nBlock ) 56*cdf0e10cSrcweir aSet.nCount = aSet.nCount - CountBits( *(aSet.pBitmap+nBlock) ); 57*cdf0e10cSrcweir aSet.nCount = aSet.nCount - 58*cdf0e10cSrcweir CountBits( *(aSet.pBitmap+nBlockDiff) >> (32-nBitValDiff) ); 59*cdf0e10cSrcweir 60*cdf0e10cSrcweir // shift complete long-words 61*cdf0e10cSrcweir sal_uInt16 nTarget, nSource; 62*cdf0e10cSrcweir for ( nTarget = 0, nSource = nBlockDiff; 63*cdf0e10cSrcweir (nSource+1) < aSet.nBlocks; 64*cdf0e10cSrcweir ++nTarget, ++nSource ) 65*cdf0e10cSrcweir *(aSet.pBitmap+nTarget) = 66*cdf0e10cSrcweir ( *(aSet.pBitmap+nSource) << nBitValDiff ) | 67*cdf0e10cSrcweir ( *(aSet.pBitmap+nSource+1) >> (32-nBitValDiff) ); 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir // shift the remainder (if in total minor 32 bits, only this) 70*cdf0e10cSrcweir *(aSet.pBitmap+nTarget) = *(aSet.pBitmap+nSource) << nBitValDiff; 71*cdf0e10cSrcweir 72*cdf0e10cSrcweir // determine the last used block 73*cdf0e10cSrcweir while ( *(aSet.pBitmap+nTarget) == 0 ) 74*cdf0e10cSrcweir --nTarget; 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir // shorten the block-array 77*cdf0e10cSrcweir if ( nTarget < aSet.nBlocks ) 78*cdf0e10cSrcweir { 79*cdf0e10cSrcweir sal_uIntPtr* pNewMap = new sal_uIntPtr[nTarget]; 80*cdf0e10cSrcweir memcpy( pNewMap, aSet.pBitmap, 4 * nTarget ); 81*cdf0e10cSrcweir delete [] aSet.pBitmap; 82*cdf0e10cSrcweir aSet.pBitmap = pNewMap; 83*cdf0e10cSrcweir aSet.nBlocks = nTarget; 84*cdf0e10cSrcweir } 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir return aSet; 87*cdf0e10cSrcweir } 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir //-------------------------------------------------------------------- 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir // substracts nOffset from each bit-value in the set 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir BitSet BitSet::operator>>( sal_uInt16 ) const 94*cdf0e10cSrcweir { 95*cdf0e10cSrcweir DBG_MEMTEST(); 96*cdf0e10cSrcweir return BitSet(); 97*cdf0e10cSrcweir } 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir //-------------------------------------------------------------------- 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir // internal code for operator= and copy-ctor 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir void BitSet::CopyFrom( const BitSet& rSet ) 104*cdf0e10cSrcweir { 105*cdf0e10cSrcweir DBG_MEMTEST(); 106*cdf0e10cSrcweir nCount = rSet.nCount; 107*cdf0e10cSrcweir nBlocks = rSet.nBlocks; 108*cdf0e10cSrcweir if ( rSet.nBlocks ) 109*cdf0e10cSrcweir { 110*cdf0e10cSrcweir DBG_MEMTEST(); 111*cdf0e10cSrcweir pBitmap = new sal_uIntPtr[nBlocks]; 112*cdf0e10cSrcweir memcpy( pBitmap, rSet.pBitmap, 4 * nBlocks ); 113*cdf0e10cSrcweir } 114*cdf0e10cSrcweir else 115*cdf0e10cSrcweir pBitmap = 0; 116*cdf0e10cSrcweir } 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir //-------------------------------------------------------------------- 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir // creates an empty bitset 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir BitSet::BitSet() 123*cdf0e10cSrcweir { 124*cdf0e10cSrcweir DBG_MEMTEST(); 125*cdf0e10cSrcweir nCount = 0; 126*cdf0e10cSrcweir nBlocks = 0; 127*cdf0e10cSrcweir pBitmap = 0; 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir //-------------------------------------------------------------------- 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir // creates a copy of bitset rOrig 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir BitSet::BitSet( const BitSet& rOrig ) 135*cdf0e10cSrcweir { 136*cdf0e10cSrcweir DBG_MEMTEST(); 137*cdf0e10cSrcweir CopyFrom(rOrig); 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir //-------------------------------------------------------------------- 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir // creates a bitset from an array 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir BitSet::BitSet( sal_uInt16* pArray, sal_uInt16 nSize ): 145*cdf0e10cSrcweir nCount(nSize) 146*cdf0e10cSrcweir { 147*cdf0e10cSrcweir DBG_MEMTEST(); 148*cdf0e10cSrcweir // find the highest bit to set 149*cdf0e10cSrcweir sal_uInt16 nMax = 0; 150*cdf0e10cSrcweir for ( sal_uInt16 n = 0; n < nCount; ++n ) 151*cdf0e10cSrcweir if ( pArray[n] > nMax ) 152*cdf0e10cSrcweir nMax = pArray[n]; 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir // if there are bits at all 155*cdf0e10cSrcweir if ( nMax > 0 ) 156*cdf0e10cSrcweir { 157*cdf0e10cSrcweir // allocate memory for all blocks needed 158*cdf0e10cSrcweir nBlocks = nMax / 32 + 1; 159*cdf0e10cSrcweir pBitmap = new sal_uIntPtr[nBlocks]; 160*cdf0e10cSrcweir memset( pBitmap, 0, 4 * nBlocks ); 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir // set all the bits 163*cdf0e10cSrcweir for ( sal_uInt16 n = 0; n < nCount; ++n ) 164*cdf0e10cSrcweir { 165*cdf0e10cSrcweir // compute the block no. and bitvalue 166*cdf0e10cSrcweir sal_uInt16 nBlock = n / 32; 167*cdf0e10cSrcweir sal_uIntPtr nBitVal = 1L << (n % 32); 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir // set a single bit 170*cdf0e10cSrcweir if ( ( *(pBitmap+nBlock) & nBitVal ) == 0 ) 171*cdf0e10cSrcweir { 172*cdf0e10cSrcweir *(pBitmap+nBlock) |= nBitVal; 173*cdf0e10cSrcweir ++nCount; 174*cdf0e10cSrcweir } 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir } 177*cdf0e10cSrcweir else 178*cdf0e10cSrcweir { 179*cdf0e10cSrcweir // initalize emtpy set 180*cdf0e10cSrcweir nBlocks = 0; 181*cdf0e10cSrcweir pBitmap = 0; 182*cdf0e10cSrcweir } 183*cdf0e10cSrcweir } 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir //-------------------------------------------------------------------- 186*cdf0e10cSrcweir 187*cdf0e10cSrcweir // frees the storage 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir BitSet::~BitSet() 190*cdf0e10cSrcweir { 191*cdf0e10cSrcweir DBG_MEMTEST(); 192*cdf0e10cSrcweir delete [] pBitmap; 193*cdf0e10cSrcweir } 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir //-------------------------------------------------------------------- 196*cdf0e10cSrcweir 197*cdf0e10cSrcweir // creates a bitmap with all bits in rRange set 198*cdf0e10cSrcweir 199*cdf0e10cSrcweir BitSet::BitSet( const Range& ) 200*cdf0e10cSrcweir { 201*cdf0e10cSrcweir DBG_MEMTEST(); 202*cdf0e10cSrcweir } 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir //-------------------------------------------------------------------- 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir // assignment from another bitset 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir BitSet& BitSet::operator=( const BitSet& rOrig ) 209*cdf0e10cSrcweir { 210*cdf0e10cSrcweir DBG_MEMTEST(); 211*cdf0e10cSrcweir if ( this != &rOrig ) 212*cdf0e10cSrcweir { 213*cdf0e10cSrcweir delete [] pBitmap; 214*cdf0e10cSrcweir CopyFrom(rOrig); 215*cdf0e10cSrcweir } 216*cdf0e10cSrcweir return *this; 217*cdf0e10cSrcweir } 218*cdf0e10cSrcweir 219*cdf0e10cSrcweir //-------------------------------------------------------------------- 220*cdf0e10cSrcweir 221*cdf0e10cSrcweir // assignment from a single bit 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir BitSet& BitSet::operator=( sal_uInt16 nBit ) 224*cdf0e10cSrcweir { 225*cdf0e10cSrcweir DBG_MEMTEST(); 226*cdf0e10cSrcweir delete [] pBitmap; 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir nBlocks = nBit / 32; 229*cdf0e10cSrcweir sal_uIntPtr nBitVal = 1L << (nBit % 32); 230*cdf0e10cSrcweir nCount = 1; 231*cdf0e10cSrcweir 232*cdf0e10cSrcweir pBitmap = new sal_uIntPtr[nBlocks]; 233*cdf0e10cSrcweir memset( pBitmap + nBlocks, 0, 4 * nBlocks ); 234*cdf0e10cSrcweir 235*cdf0e10cSrcweir *(pBitmap+nBlocks) = nBitVal; 236*cdf0e10cSrcweir 237*cdf0e10cSrcweir return *this; 238*cdf0e10cSrcweir } 239*cdf0e10cSrcweir 240*cdf0e10cSrcweir //-------------------------------------------------------------------- 241*cdf0e10cSrcweir 242*cdf0e10cSrcweir // creates the asymetric difference with another bitset 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir BitSet& BitSet::operator-=(sal_uInt16 nBit) 245*cdf0e10cSrcweir { 246*cdf0e10cSrcweir DBG_MEMTEST(); 247*cdf0e10cSrcweir sal_uInt16 nBlock = nBit / 32; 248*cdf0e10cSrcweir sal_uIntPtr nBitVal = 1L << (nBit % 32); 249*cdf0e10cSrcweir 250*cdf0e10cSrcweir if ( nBlock >= nBlocks ) 251*cdf0e10cSrcweir return *this; 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir if ( (*(pBitmap+nBlock) & nBitVal) ) 254*cdf0e10cSrcweir { 255*cdf0e10cSrcweir *(pBitmap+nBlock) &= ~nBitVal; 256*cdf0e10cSrcweir --nCount; 257*cdf0e10cSrcweir } 258*cdf0e10cSrcweir 259*cdf0e10cSrcweir return *this; 260*cdf0e10cSrcweir } 261*cdf0e10cSrcweir 262*cdf0e10cSrcweir //-------------------------------------------------------------------- 263*cdf0e10cSrcweir 264*cdf0e10cSrcweir // unites with the bits of rSet 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir BitSet& BitSet::operator|=( const BitSet& rSet ) 267*cdf0e10cSrcweir { 268*cdf0e10cSrcweir DBG_MEMTEST(); 269*cdf0e10cSrcweir sal_uInt16 nMax = Min(nBlocks, rSet.nBlocks); 270*cdf0e10cSrcweir 271*cdf0e10cSrcweir // expand the bitmap 272*cdf0e10cSrcweir if ( nBlocks < rSet.nBlocks ) 273*cdf0e10cSrcweir { 274*cdf0e10cSrcweir sal_uIntPtr *pNewMap = new sal_uIntPtr[rSet.nBlocks]; 275*cdf0e10cSrcweir memset( pNewMap + nBlocks, 0, 4 * (rSet.nBlocks - nBlocks) ); 276*cdf0e10cSrcweir 277*cdf0e10cSrcweir if ( pBitmap ) 278*cdf0e10cSrcweir { 279*cdf0e10cSrcweir memcpy( pNewMap, pBitmap, 4 * nBlocks ); 280*cdf0e10cSrcweir delete [] pBitmap; 281*cdf0e10cSrcweir } 282*cdf0e10cSrcweir pBitmap = pNewMap; 283*cdf0e10cSrcweir nBlocks = rSet.nBlocks; 284*cdf0e10cSrcweir } 285*cdf0e10cSrcweir 286*cdf0e10cSrcweir // add the bits blocks by block 287*cdf0e10cSrcweir for ( sal_uInt16 nBlock = 0; nBlock < nMax; ++nBlock ) 288*cdf0e10cSrcweir { 289*cdf0e10cSrcweir // compute numberof additional bits 290*cdf0e10cSrcweir sal_uIntPtr nDiff = ~*(pBitmap+nBlock) & *(rSet.pBitmap+nBlock); 291*cdf0e10cSrcweir nCount = nCount + CountBits(nDiff); 292*cdf0e10cSrcweir 293*cdf0e10cSrcweir *(pBitmap+nBlock) |= *(rSet.pBitmap+nBlock); 294*cdf0e10cSrcweir } 295*cdf0e10cSrcweir 296*cdf0e10cSrcweir return *this; 297*cdf0e10cSrcweir } 298*cdf0e10cSrcweir 299*cdf0e10cSrcweir //-------------------------------------------------------------------- 300*cdf0e10cSrcweir 301*cdf0e10cSrcweir // unites with a single bit 302*cdf0e10cSrcweir 303*cdf0e10cSrcweir BitSet& BitSet::operator|=( sal_uInt16 nBit ) 304*cdf0e10cSrcweir { 305*cdf0e10cSrcweir DBG_MEMTEST(); 306*cdf0e10cSrcweir sal_uInt16 nBlock = nBit / 32; 307*cdf0e10cSrcweir sal_uIntPtr nBitVal = 1L << (nBit % 32); 308*cdf0e10cSrcweir 309*cdf0e10cSrcweir if ( nBlock >= nBlocks ) 310*cdf0e10cSrcweir { 311*cdf0e10cSrcweir sal_uIntPtr *pNewMap = new sal_uIntPtr[nBlock+1]; 312*cdf0e10cSrcweir memset( pNewMap + nBlocks, 0, 4 * (nBlock - nBlocks + 1) ); 313*cdf0e10cSrcweir 314*cdf0e10cSrcweir if ( pBitmap ) 315*cdf0e10cSrcweir { 316*cdf0e10cSrcweir memcpy( pNewMap, pBitmap, 4 * nBlocks ); 317*cdf0e10cSrcweir delete [] pBitmap; 318*cdf0e10cSrcweir } 319*cdf0e10cSrcweir pBitmap = pNewMap; 320*cdf0e10cSrcweir nBlocks = nBlock+1; 321*cdf0e10cSrcweir } 322*cdf0e10cSrcweir 323*cdf0e10cSrcweir if ( (*(pBitmap+nBlock) & nBitVal) == 0 ) 324*cdf0e10cSrcweir { 325*cdf0e10cSrcweir *(pBitmap+nBlock) |= nBitVal; 326*cdf0e10cSrcweir ++nCount; 327*cdf0e10cSrcweir } 328*cdf0e10cSrcweir 329*cdf0e10cSrcweir return *this; 330*cdf0e10cSrcweir } 331*cdf0e10cSrcweir 332*cdf0e10cSrcweir //-------------------------------------------------------------------- 333*cdf0e10cSrcweir 334*cdf0e10cSrcweir // determines if the bit is set (may be the only one) 335*cdf0e10cSrcweir 336*cdf0e10cSrcweir sal_Bool BitSet::Contains( sal_uInt16 nBit ) const 337*cdf0e10cSrcweir { 338*cdf0e10cSrcweir DBG_MEMTEST(); 339*cdf0e10cSrcweir sal_uInt16 nBlock = nBit / 32; 340*cdf0e10cSrcweir sal_uIntPtr nBitVal = 1L << (nBit % 32); 341*cdf0e10cSrcweir 342*cdf0e10cSrcweir if ( nBlock >= nBlocks ) 343*cdf0e10cSrcweir return sal_False; 344*cdf0e10cSrcweir return ( nBitVal & *(pBitmap+nBlock) ) == nBitVal; 345*cdf0e10cSrcweir } 346*cdf0e10cSrcweir 347*cdf0e10cSrcweir //-------------------------------------------------------------------- 348*cdf0e10cSrcweir 349*cdf0e10cSrcweir // determines if the bitsets are equal 350*cdf0e10cSrcweir 351*cdf0e10cSrcweir sal_Bool BitSet::operator==( const BitSet& rSet ) const 352*cdf0e10cSrcweir { 353*cdf0e10cSrcweir DBG_MEMTEST(); 354*cdf0e10cSrcweir if ( nBlocks != rSet.nBlocks ) 355*cdf0e10cSrcweir return sal_False; 356*cdf0e10cSrcweir 357*cdf0e10cSrcweir sal_uInt16 nBlock = nBlocks; 358*cdf0e10cSrcweir while ( nBlock-- > 0 ) 359*cdf0e10cSrcweir if ( *(pBitmap+nBlock) != *(rSet.pBitmap+nBlock) ) 360*cdf0e10cSrcweir return sal_False; 361*cdf0e10cSrcweir 362*cdf0e10cSrcweir return sal_True; 363*cdf0e10cSrcweir } 364*cdf0e10cSrcweir 365*cdf0e10cSrcweir //-------------------------------------------------------------------- 366*cdf0e10cSrcweir 367*cdf0e10cSrcweir // counts the number of 1-bits in the parameter 368*cdf0e10cSrcweir 369*cdf0e10cSrcweir sal_uInt16 BitSet::CountBits( sal_uIntPtr nBits ) 370*cdf0e10cSrcweir { 371*cdf0e10cSrcweir sal_uInt16 nCount = 0; 372*cdf0e10cSrcweir int nBit = 32; 373*cdf0e10cSrcweir while ( nBit-- && nBits ) 374*cdf0e10cSrcweir { if ( ( (long)nBits ) < 0 ) 375*cdf0e10cSrcweir ++nCount; 376*cdf0e10cSrcweir nBits = nBits << 1; 377*cdf0e10cSrcweir } 378*cdf0e10cSrcweir return nCount; 379*cdf0e10cSrcweir } 380*cdf0e10cSrcweir 381*cdf0e10cSrcweir //-------------------------------------------------------------------- 382*cdf0e10cSrcweir 383*cdf0e10cSrcweir sal_uInt16 IndexBitSet::GetFreeIndex() 384*cdf0e10cSrcweir { 385*cdf0e10cSrcweir for(sal_uInt16 i=0;i<USHRT_MAX;i++) 386*cdf0e10cSrcweir if(!Contains(i)) 387*cdf0e10cSrcweir { 388*cdf0e10cSrcweir *this|=i; 389*cdf0e10cSrcweir return i; 390*cdf0e10cSrcweir } 391*cdf0e10cSrcweir DBG_ASSERT(sal_False, "IndexBitSet enthaelt mehr als USHRT_MAX Eintraege"); 392*cdf0e10cSrcweir return 0; 393*cdf0e10cSrcweir } 394*cdf0e10cSrcweir 395*cdf0e10cSrcweir 396