bitMatrix.h

Engine/source/core/bitMatrix.h

More...

Classes:

class

A matrix of bits.

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _BITMATRIX_H_
 25#define _BITMATRIX_H_
 26
 27#ifndef _PLATFORM_H_
 28#include "platform/platform.h"
 29#endif
 30#ifndef _BITVECTOR_H_
 31#include "core/bitVector.h"
 32#endif
 33
 34/// A matrix of bits.
 35///
 36/// This class manages an array of bits. There are no limitations on the
 37/// size of the bit matrix (beyond available memory).
 38///
 39/// @note This class is currently unused.
 40class BitMatrix
 41{
 42   U32 mWidth;
 43   U32 mHeight;
 44   U32 mRowByteWidth;
 45
 46   U8* mBits;
 47   U32 mSize;
 48
 49   BitVector mColFlags;
 50   BitVector mRowFlags;
 51
 52  public:
 53
 54   /// Create a new bit matrix.
 55   ///
 56   /// @param  width    Width of matrix in bits.
 57   /// @param  height   Height of matrix in bits.
 58   BitMatrix(const U32 width, const U32 height);
 59   ~BitMatrix();
 60
 61   /// @name Setters
 62   /// @{
 63
 64   /// Set all the bits in the matrix to false.
 65   void clearAllBits();
 66
 67   /// Set all the bits in the matrix to true.
 68   void setAllBits();
 69
 70   /// Set a bit at a given location in the matrix.
 71   void setBit(const U32 x, const U32 y);
 72
 73   /// Clear a bit at a given location in the matrix.
 74   void clearBit(const U32 x, const U32 y);
 75
 76   /// @}
 77
 78   /// @name Queries
 79   /// @{
 80
 81   /// Is the specified bit set?
 82   bool isSet(const U32 x, const U32 y) const;
 83
 84   /// Is any bit in the given column set?
 85   bool isAnySetCol(const U32 x);
 86
 87   /// Is any bit in the given row set?
 88   bool isAnySetRow(const U32 y);
 89
 90   /// @}
 91};
 92
 93inline BitMatrix::BitMatrix(const U32 width, const U32 height)
 94 : mColFlags(width),
 95   mRowFlags(height)
 96{
 97   AssertFatal(width != 0 && height != 0, "Error, w/h must be non-zero");
 98
 99   mWidth        = width;
100   mHeight       = height;
101   mRowByteWidth = (width + 7) >> 3;
102
103   mSize         = mRowByteWidth * mHeight;
104   mBits         = new U8[mSize];
105}
106
107inline BitMatrix::~BitMatrix()
108{
109   mWidth        = 0;
110   mHeight       = 0;
111   mRowByteWidth = 0;
112   mSize         = 0;
113
114   delete [] mBits;
115   mBits = NULL;
116}
117
118inline void BitMatrix::clearAllBits()
119{
120   AssertFatal(mBits != NULL, "Error, clearing after deletion");
121
122   dMemset(mBits, 0x00, mSize);
123   mColFlags.clear();
124   mRowFlags.clear();
125}
126
127inline void BitMatrix::setAllBits()
128{
129   AssertFatal(mBits != NULL, "Error, setting after deletion");
130
131   dMemset(mBits, 0xFF, mSize);
132   mColFlags.set();
133   mRowFlags.set();
134}
135
136inline void BitMatrix::setBit(const U32 x, const U32 y)
137{
138   AssertFatal(x < mWidth && y < mHeight, "Error, out of bounds bit!");
139
140   U8* pRow = &mBits[y * mRowByteWidth];
141
142   U8* pByte = &pRow[x >> 3];
143   *pByte   |= 1 << (x & 0x7);
144
145   mColFlags.set(x);
146   mRowFlags.set(y);
147}
148
149inline void BitMatrix::clearBit(const U32 x, const U32 y)
150{
151   AssertFatal(x < mWidth && y < mHeight, "Error, out of bounds bit!");
152
153   U8* pRow = &mBits[y * mRowByteWidth];
154
155   U8* pByte = &pRow[x >> 3];
156   *pByte   &= ~(1 << (x & 0x7));
157}
158
159inline bool BitMatrix::isSet(const U32 x, const U32 y) const
160{
161   AssertFatal(x < mWidth && y < mHeight, "Error, out of bounds bit!");
162
163   U8* pRow = &mBits[y * mRowByteWidth];
164
165   U8* pByte = &pRow[x >> 3];
166   return (*pByte & (1 << (x & 0x7))) != 0;
167}
168
169inline bool BitMatrix::isAnySetCol(const U32 x)
170{
171   AssertFatal(x < mWidth, "Error, out of bounds column!");
172
173   return mColFlags.test(x);
174}
175
176inline bool BitMatrix::isAnySetRow(const U32 y)
177{
178   AssertFatal(y < mHeight, "Error, out of bounds row!");
179
180   return mRowFlags.test(y);
181}
182
183#endif  // _H_BITMATRIX_
184