bitVector.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. /// Manage a vector of bits of arbitrary size.
  28. class BitVector
  29. {
  30. U8* mBits;
  31. U32 mByteSize;
  32. U32 mSize;
  33. static U32 calcByteSize(const U32 numBits);
  34. public:
  35. BitVector();
  36. BitVector(const U32 _size);
  37. ~BitVector();
  38. /// @name Size Management
  39. /// @{
  40. void setSize(const U32 _size);
  41. U32 getSize() const;
  42. U32 getByteSize() const;
  43. U32 getAllocatedByteSize() const { return mByteSize; }
  44. const U8* getBits() const { return mBits; }
  45. U8* getNCBits() { return mBits; }
  46. /// @}
  47. bool copy(const BitVector& from);
  48. /// @name Mutators
  49. /// Note that bits are specified by index, unlike BitSet32.
  50. /// @{
  51. /// Clear all the bits.
  52. void clear();
  53. /// Set all the bits.
  54. void set();
  55. /// Set the specified bit.
  56. void set(U32 bit);
  57. /// Clear the specified bit.
  58. void clear(U32 bit);
  59. /// Test that the specified bit is set.
  60. bool test(U32 bit) const;
  61. /// @}
  62. };
  63. inline BitVector::BitVector()
  64. {
  65. mBits = NULL;
  66. mByteSize = 0;
  67. mSize = 0;
  68. }
  69. inline BitVector::BitVector(const U32 _size)
  70. {
  71. mBits = NULL;
  72. mByteSize = 0;
  73. mSize = 0;
  74. setSize(_size);
  75. }
  76. inline BitVector::~BitVector()
  77. {
  78. delete [] mBits;
  79. mBits = NULL;
  80. mByteSize = 0;
  81. mSize = 0;
  82. }
  83. inline U32 BitVector::calcByteSize(const U32 numBits)
  84. {
  85. // Make sure that we are 32 bit aligned
  86. //
  87. return (((numBits + 0x7) >> 3) + 0x3) & ~0x3;
  88. }
  89. inline void BitVector::setSize(const U32 _size)
  90. {
  91. if (_size != 0) {
  92. U32 newSize = calcByteSize(_size);
  93. if (mByteSize < newSize) {
  94. delete [] mBits;
  95. mBits = new U8[newSize];
  96. mByteSize = newSize;
  97. }
  98. } else {
  99. delete [] mBits;
  100. mBits = NULL;
  101. mByteSize = 0;
  102. }
  103. mSize = _size;
  104. }
  105. inline U32 BitVector::getSize() const
  106. {
  107. return mSize;
  108. }
  109. inline U32 BitVector::getByteSize() const
  110. {
  111. return calcByteSize(mSize);
  112. }
  113. inline void BitVector::clear()
  114. {
  115. if (mSize != 0)
  116. dMemset(mBits, 0x00, calcByteSize(mSize));
  117. }
  118. inline bool BitVector::copy(const BitVector& from)
  119. {
  120. U32 sourceSize = from.getSize();
  121. if (sourceSize) {
  122. setSize(sourceSize);
  123. dMemcpy(mBits, from.getBits(), getByteSize());
  124. return true;
  125. }
  126. return false;
  127. }
  128. inline void BitVector::set()
  129. {
  130. if (mSize != 0)
  131. dMemset(mBits, 0xFF, calcByteSize(mSize));
  132. }
  133. inline void BitVector::set(U32 bit)
  134. {
  135. AssertFatal(bit < mSize, "Error, out of range bit");
  136. mBits[bit >> 3] |= U8(1 << (bit & 0x7));
  137. }
  138. inline void BitVector::clear(U32 bit)
  139. {
  140. AssertFatal(bit < mSize, "Error, out of range bit");
  141. mBits[bit >> 3] &= U8(~(1 << (bit & 0x7)));
  142. }
  143. inline bool BitVector::test(U32 bit) const
  144. {
  145. AssertFatal(bit < mSize, "Error, out of range bit");
  146. return (mBits[bit >> 3] & U8(1 << (bit & 0x7))) != 0;
  147. }
  148. #endif //_BITVECTOR_H_