bitMatrix.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 _BITMATRIX_H_
  23. #define _BITMATRIX_H_
  24. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. #ifndef _BITVECTOR_H_
  28. #include "core/bitVector.h"
  29. #endif
  30. /// A matrix of bits.
  31. ///
  32. /// This class manages an array of bits. There are no limitations on the
  33. /// size of the bit matrix (beyond available memory).
  34. ///
  35. /// @note This class is currently unused.
  36. class BitMatrix
  37. {
  38. U32 mWidth;
  39. U32 mHeight;
  40. U32 mRowByteWidth;
  41. U8* mBits;
  42. U32 mSize;
  43. BitVector mColFlags;
  44. BitVector mRowFlags;
  45. public:
  46. /// Create a new bit matrix.
  47. ///
  48. /// @param width Width of matrix in bits.
  49. /// @param height Height of matrix in bits.
  50. BitMatrix(const U32 width, const U32 height);
  51. ~BitMatrix();
  52. /// @name Setters
  53. /// @{
  54. /// Set all the bits in the matrix to false.
  55. void clearAllBits();
  56. /// Set all the bits in the matrix to true.
  57. void setAllBits();
  58. /// Set a bit at a given location in the matrix.
  59. void setBit(const U32 x, const U32 y);
  60. /// Clear a bit at a given location in the matrix.
  61. void clearBit(const U32 x, const U32 y);
  62. /// @}
  63. /// @name Queries
  64. /// @{
  65. /// Is the specified bit set?
  66. bool isSet(const U32 x, const U32 y) const;
  67. /// Is any bit in the given column set?
  68. bool isAnySetCol(const U32 x);
  69. /// Is any bit in the given row set?
  70. bool isAnySetRow(const U32 y);
  71. /// @}
  72. };
  73. inline BitMatrix::BitMatrix(const U32 width, const U32 height)
  74. : mColFlags(width),
  75. mRowFlags(height)
  76. {
  77. AssertFatal(width != 0 && height != 0, "Error, w/h must be non-zero");
  78. mWidth = width;
  79. mHeight = height;
  80. mRowByteWidth = (width + 7) >> 3;
  81. mSize = mRowByteWidth * mHeight;
  82. mBits = new U8[mSize];
  83. }
  84. inline BitMatrix::~BitMatrix()
  85. {
  86. mWidth = 0;
  87. mHeight = 0;
  88. mRowByteWidth = 0;
  89. mSize = 0;
  90. delete [] mBits;
  91. mBits = NULL;
  92. }
  93. inline void BitMatrix::clearAllBits()
  94. {
  95. AssertFatal(mBits != NULL, "Error, clearing after deletion");
  96. dMemset(mBits, 0x00, mSize);
  97. mColFlags.clear();
  98. mRowFlags.clear();
  99. }
  100. inline void BitMatrix::setAllBits()
  101. {
  102. AssertFatal(mBits != NULL, "Error, setting after deletion");
  103. dMemset(mBits, 0xFF, mSize);
  104. mColFlags.set();
  105. mRowFlags.set();
  106. }
  107. inline void BitMatrix::setBit(const U32 x, const U32 y)
  108. {
  109. AssertFatal(x < mWidth && y < mHeight, "Error, out of bounds bit!");
  110. U8* pRow = &mBits[y * mRowByteWidth];
  111. U8* pByte = &pRow[x >> 3];
  112. *pByte |= 1 << (x & 0x7);
  113. mColFlags.set(x);
  114. mRowFlags.set(y);
  115. }
  116. inline void BitMatrix::clearBit(const U32 x, const U32 y)
  117. {
  118. AssertFatal(x < mWidth && y < mHeight, "Error, out of bounds bit!");
  119. U8* pRow = &mBits[y * mRowByteWidth];
  120. U8* pByte = &pRow[x >> 3];
  121. *pByte &= ~(1 << (x & 0x7));
  122. }
  123. inline bool BitMatrix::isSet(const U32 x, const U32 y) const
  124. {
  125. AssertFatal(x < mWidth && y < mHeight, "Error, out of bounds bit!");
  126. U8* pRow = &mBits[y * mRowByteWidth];
  127. U8* pByte = &pRow[x >> 3];
  128. return (*pByte & (1 << (x & 0x7))) != 0;
  129. }
  130. inline bool BitMatrix::isAnySetCol(const U32 x)
  131. {
  132. AssertFatal(x < mWidth, "Error, out of bounds column!");
  133. return mColFlags.test(x);
  134. }
  135. inline bool BitMatrix::isAnySetRow(const U32 y)
  136. {
  137. AssertFatal(y < mHeight, "Error, out of bounds row!");
  138. return mRowFlags.test(y);
  139. }
  140. #endif // _H_BITMATRIX_