bitVector.cpp

Engine/source/core/bitVector.cpp

More...

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "platform/platform.h"
 25#include "core/bitVector.h"
 26
 27
 28void BitVector::_resize( U32 sizeInBits, bool copyBits )
 29{
 30   if ( sizeInBits != 0 ) 
 31   {
 32      U32 newSize = calcByteSize( sizeInBits );
 33      if ( mByteSize < newSize ) 
 34      {
 35         U8 *newBits = new U8[newSize];
 36         if( copyBits )
 37            dMemcpy( newBits, mBits, mByteSize );
 38
 39         delete [] mBits;
 40         mBits = newBits;
 41         mByteSize = newSize;
 42      }
 43   } 
 44   else 
 45   {
 46      delete [] mBits;
 47      mBits     = NULL;
 48      mByteSize = 0;
 49   }
 50
 51   mSize = sizeInBits;
 52}
 53
 54void BitVector::combineOR( const BitVector &other )
 55{
 56   AssertFatal( mSize == other.mSize, "BitVector::combineOR - Vectors differ in size!" );
 57
 58   for ( U32 i=0; i < mSize; i++ )
 59   {
 60      bool b = test(i) | other.test(i);
 61      set( i, b );
 62   }
 63}
 64
 65bool BitVector::_test( const BitVector& vector, bool all ) const
 66{
 67   AssertFatal( mByteSize == vector.mByteSize, "BitVector::_test - Vectors differ in size!" );
 68   AssertFatal( mByteSize % 4 == 0, "BitVector::_test - Vector not DWORD aligned!" );
 69
 70   const U32 numDWORDS = mByteSize / 4;
 71   const U32* bits1 = reinterpret_cast< const U32* >( mBits );
 72   const U32* bits2 = reinterpret_cast< const U32* >( vector.mBits );
 73
 74   for( U32 i = 0; i < numDWORDS; ++ i )
 75   {
 76      if( !( bits1[ i ] & bits2[ i ] ) )
 77         continue;
 78      else if( bits2[ i ] && all )
 79         return false;
 80      else
 81         return true;
 82   }
 83
 84   return false;
 85}
 86
 87bool BitVector::testAll() const
 88{
 89   const U32 remaider = mSize % 8;
 90   const U32 testBytes = mSize / 8;
 91
 92   for ( U32 i=0; i < testBytes; i++ )
 93      if ( mBits[i] != 0xFF )
 94         return false;
 95
 96   if ( remaider == 0 )
 97      return true;
 98
 99   const U8 mask = (U8)0xFF >> ( 8 - remaider );
100   return ( mBits[testBytes] & mask ) == mask; 
101}
102
103bool BitVector::testAllClear() const
104{
105   const U32 remaider = mSize % 8;
106   const U32 testBytes = mSize / 8;
107
108   for ( U32 i=0; i < testBytes; i++ )
109      if ( mBits[i] != 0 )
110         return false;
111
112   if ( remaider == 0 )
113      return true;
114
115   const U8 mask = (U8)0xFF >> ( 8 - remaider );
116   return ( mBits[testBytes] & mask ) == 0;
117}
118