bitVector.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #ifndef _BITVECTOR_H_
  23. #define _BITVECTOR_H_
  24. /// Manage a vector of bits of arbitrary size.
  25. class BitVector
  26. {
  27. protected:
  28. /// The array of bytes that stores our bits.
  29. U8* mBits;
  30. /// The allocated size of the bit array.
  31. U32 mByteSize;
  32. /// The size of the vector in bits.
  33. U32 mSize;
  34. /// Returns a size in bytes which is 32bit aligned
  35. /// and can hold all the requested bits.
  36. static U32 calcByteSize( const U32 numBits );
  37. /// Internal function which resizes the bit array.
  38. void _resize( U32 sizeInBits, bool copyBits );
  39. bool _test( const BitVector& vector, bool all ) const;
  40. public:
  41. /// Default constructor which creates an bit
  42. /// vector with a bit size of zero.
  43. BitVector();
  44. /// Constructs a bit vector with the desired size.
  45. /// @note The resulting vector is not cleared.
  46. BitVector( U32 sizeInBits );
  47. /// Destructor.
  48. ~BitVector();
  49. /// @name Size Management
  50. /// @{
  51. /// Return true if the bit vector is empty.
  52. bool empty() const { return ( mSize == 0 ); }
  53. /// Resizes the bit vector.
  54. /// @note The new bits in the vector are not cleared and
  55. /// contain random garbage bits.
  56. void setSize( U32 sizeInBits );
  57. /// Returns the size in bits.
  58. U32 getSize() const { return mSize; }
  59. /// Returns the 32bit aligned size in bytes.
  60. U32 getByteSize() const { return mByteSize; }
  61. /// Returns the bits.
  62. const U8* getBits() const { return mBits; }
  63. U8* getBits() { return mBits; }
  64. /// @}
  65. /// Copy the content of another bit vector.
  66. void copy( const BitVector &from );
  67. /// @name Mutators
  68. /// Note that bits are specified by index, unlike BitSet32.
  69. /// @{
  70. /// Set the specified bit.
  71. void set(U32 bit);
  72. /// Set the specified bit on or off.
  73. void set(U32 bit, bool on );
  74. /// Set all the bits.
  75. void set();
  76. /// Clear the specified bit.
  77. void clear(U32 bit);
  78. /// Clear all the bits.
  79. void clear();
  80. /// Does an OR operation between BitVectors.
  81. void combineOR( const BitVector &other );
  82. /// Test that the specified bit is set.
  83. bool test(U32 bit) const;
  84. /// Test this vector's bits against all the corresponding bits
  85. /// in @a vector and return true if any of the bits that are
  86. /// set in @a vector are also set in this vector.
  87. ///
  88. /// @param vector Bit vector of the same size.
  89. bool testAny( const BitVector& vector ) const { return _test( vector, false ); }
  90. /// Test this vector's bits against all the corresponding bits
  91. /// in @a vector and return true if all of the bits that are
  92. /// set in @a vector are also set in this vector.
  93. ///
  94. /// @param vector Bit vector of the same size.
  95. bool testAll( const BitVector& vector ) const { return _test( vector, true ); }
  96. /// Return true if all bits are set.
  97. bool testAll() const;
  98. /// Return true if all bits are clear.
  99. bool testAllClear() const;
  100. /// @}
  101. };
  102. inline BitVector::BitVector()
  103. {
  104. mBits = NULL;
  105. mByteSize = 0;
  106. mSize = 0;
  107. }
  108. inline BitVector::BitVector( U32 sizeInBits )
  109. {
  110. mBits = NULL;
  111. mByteSize = 0;
  112. mSize = 0;
  113. setSize( sizeInBits );
  114. }
  115. inline BitVector::~BitVector()
  116. {
  117. delete [] mBits;
  118. mBits = NULL;
  119. mByteSize = 0;
  120. mSize = 0;
  121. }
  122. inline U32 BitVector::calcByteSize( U32 numBits )
  123. {
  124. // Make sure that we are 32 bit aligned
  125. return (((numBits + 0x7) >> 3) + 0x3) & ~0x3;
  126. }
  127. inline void BitVector::setSize( const U32 sizeInBits )
  128. {
  129. _resize( sizeInBits, true );
  130. }
  131. inline void BitVector::clear()
  132. {
  133. if (mSize != 0)
  134. dMemset( mBits, 0x00, getByteSize() );
  135. }
  136. inline void BitVector::copy( const BitVector &from )
  137. {
  138. _resize( from.getSize(), false );
  139. if (mSize != 0)
  140. dMemcpy( mBits, from.getBits(), getByteSize() );
  141. }
  142. inline void BitVector::set()
  143. {
  144. if (mSize != 0)
  145. dMemset(mBits, 0xFF, getByteSize() );
  146. }
  147. inline void BitVector::set(U32 bit)
  148. {
  149. AssertFatal(bit < mSize, "BitVector::set - Error, out of range bit!");
  150. mBits[bit >> 3] |= U8(1 << (bit & 0x7));
  151. }
  152. inline void BitVector::set(U32 bit, bool on )
  153. {
  154. AssertFatal(bit < mSize, "BitVector::set - Error, out of range bit!");
  155. if ( on )
  156. mBits[bit >> 3] |= U8(1 << (bit & 0x7));
  157. else
  158. mBits[bit >> 3] &= U8(~(1 << (bit & 0x7)));
  159. }
  160. inline void BitVector::clear(U32 bit)
  161. {
  162. AssertFatal(bit < mSize, "BitVector::clear - Error, out of range bit!");
  163. mBits[bit >> 3] &= U8(~(1 << (bit & 0x7)));
  164. }
  165. inline bool BitVector::test(U32 bit) const
  166. {
  167. AssertFatal(bit < mSize, "BitVector::test - Error, out of range bit!");
  168. return (mBits[bit >> 3] & U8(1 << (bit & 0x7))) != 0;
  169. }
  170. #endif //_BITVECTOR_H_