bitVector.cpp 3.3 KB

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