bitVector.h 6.2 KB

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