bitVector.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. /// Copy constructor
  48. BitVector( const BitVector &r);
  49. /// Destructor.
  50. ~BitVector();
  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. /// Copy the contents of another bit vector
  70. BitVector& operator=( const BitVector &r);
  71. /// @name Mutators
  72. /// Note that bits are specified by index, unlike BitSet32.
  73. /// @{
  74. /// Set the specified bit.
  75. void set(U32 bit);
  76. /// Set the specified bit on or off.
  77. void set(U32 bit, bool on );
  78. /// Set all the bits.
  79. void set();
  80. /// Clear the specified bit.
  81. void clear(U32 bit);
  82. /// Clear all the bits.
  83. void clear();
  84. /// Does an OR operation between BitVectors.
  85. void combineOR( const BitVector &other );
  86. /// Test that the specified bit is set.
  87. bool test(U32 bit) const;
  88. /// Test this vector's bits against all the corresponding bits
  89. /// in @a vector and return true if any of the bits that are
  90. /// set in @a vector are also set in this vector.
  91. ///
  92. /// @param vector Bit vector of the same size.
  93. bool testAny( const BitVector& vector ) const { return _test( vector, false ); }
  94. /// Test this vector's bits against all the corresponding bits
  95. /// in @a vector and return true if all of the bits that are
  96. /// set in @a vector are also set in this vector.
  97. ///
  98. /// @param vector Bit vector of the same size.
  99. bool testAll( const BitVector& vector ) const { return _test( vector, true ); }
  100. /// Return true if all bits are set.
  101. bool testAll() const;
  102. /// Return true if all bits are clear.
  103. bool testAllClear() const;
  104. /// @}
  105. };
  106. inline BitVector::BitVector()
  107. {
  108. mBits = NULL;
  109. mByteSize = 0;
  110. mSize = 0;
  111. }
  112. inline BitVector::BitVector( U32 sizeInBits )
  113. {
  114. mBits = NULL;
  115. mByteSize = 0;
  116. mSize = 0;
  117. setSize( sizeInBits );
  118. }
  119. inline BitVector::BitVector( const BitVector &r )
  120. {
  121. mBits = NULL;
  122. mByteSize = 0;
  123. mSize = 0;
  124. copy(r);
  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 BitVector& BitVector::operator=( const BitVector &r)
  154. {
  155. copy(r);
  156. return *this;
  157. }
  158. inline void BitVector::set()
  159. {
  160. if (mSize != 0)
  161. dMemset(mBits, 0xFF, getByteSize() );
  162. }
  163. inline void BitVector::set(U32 bit)
  164. {
  165. AssertFatal(bit < mSize, "BitVector::set - Error, out of range bit!");
  166. mBits[bit >> 3] |= U8(1 << (bit & 0x7));
  167. }
  168. inline void BitVector::set(U32 bit, bool on )
  169. {
  170. AssertFatal(bit < mSize, "BitVector::set - Error, out of range bit!");
  171. if ( on )
  172. mBits[bit >> 3] |= U8(1 << (bit & 0x7));
  173. else
  174. mBits[bit >> 3] &= U8(~(1 << (bit & 0x7)));
  175. }
  176. inline void BitVector::clear(U32 bit)
  177. {
  178. AssertFatal(bit < mSize, "BitVector::clear - Error, out of range bit!");
  179. mBits[bit >> 3] &= U8(~(1 << (bit & 0x7)));
  180. }
  181. inline bool BitVector::test(U32 bit) const
  182. {
  183. AssertFatal(bit < mSize, "BitVector::test - Error, out of range bit!");
  184. return (mBits[bit >> 3] & U8(1 << (bit & 0x7))) != 0;
  185. }
  186. #endif //_BITVECTOR_H_