BitMatrix.hpp

A COL x ROW matrix of bits and provides easy indexing and manipulation.

Note

Status: BETA

template<size_t COLS, size_t ROWS>
class BitMatrix
#include <BitMatrix.hpp>

A simple class to manage a COLS x ROWS matrix of bits.

Bits are translated to a bitset with 0 in the upper left and moving through bits from left to right and top to bottom. For example, the indices in a 3x3 bit matrix would be organized as such:

0 1 2 3 4 5 6 7 8

Public Functions

inline BitMatrix()
inline BitMatrix(const BitSet<COLS * ROWS> &in_bits)
inline BitMatrix(const BitMatrix &in_matrix)
inline ~BitMatrix()
inline constexpr size_t NumRows() const

How many rows are in this matrix?

inline constexpr size_t NumCols() const

How many columns are in this matrix?

inline constexpr size_t GetSize() const

How many total cells are in this matrix?

inline bool Any() const
inline bool None() const
inline bool All() const
inline bool Get(size_t col, size_t row) const
inline bool Get(size_t id) const
inline void Set(size_t col, size_t row, bool val = true)
inline void Set(size_t id)
inline void Unset(size_t col, size_t row)
inline void Unset(size_t id)
inline void Flip(size_t col, size_t row)
inline void Flip(size_t id)
inline void SetAll()
inline void SetCol(size_t col)
inline void SetRow(size_t row)
inline void Clear()
inline void ClearCol(size_t col)
inline void ClearRow(size_t row)
inline size_t CountOnes() const

Count the number of set bits in the matrix.

inline int FindOne() const

Find the position of the first non-zero bit. size_t FindOne() const { return (~bits & (bits - 1)).count(); }

inline BitMatrix LeftShift() const

Shift the whole matrix in the specified direction.

inline BitMatrix RightShift() const
inline BitMatrix UpShift() const
inline BitMatrix DownShift() const
inline BitMatrix ULShift() const
inline BitMatrix DLShift() const
inline BitMatrix URShift() const
inline BitMatrix DRShift() const
inline BitMatrix GetReach() const

Find all points within one step of the ones on this bit matrix.

inline BitMatrix GetRegion(size_t start_pos) const

Find all points reachable from the start position.

inline BitMatrix GetRegion(size_t col, size_t row) const
inline bool IsConnected() const

Does this bit matrix represent a connected set of ones?

inline bool Has2x2() const

Does this bit matrix have any 2x2 square of ones in it?

inline void Print(std::ostream &os = std::cout) const
inline BitMatrix &operator=(const BitMatrix &in)
inline BitMatrix &operator&=(const BitMatrix &in)
inline BitMatrix &operator|=(const BitMatrix &in)
inline BitMatrix &operator^=(const BitMatrix &in)
inline bool operator==(const BitMatrix &in) const
inline bool operator!=(const BitMatrix &in) const
inline BitMatrix operator~() const
inline BitMatrix operator&(const BitMatrix &in) const
inline BitMatrix operator|(const BitMatrix &in) const
inline BitMatrix operator^(const BitMatrix &in) const
inline const BitSet<COLS * ROWS> &to_bitset()

Public Static Functions

template<size_t COL_ID>
static inline const BitSet<COLS * ROWS> &MaskCol()

Keep only a single column of values, reducing all others to zeros.

template<size_t ROW_ID>
static inline const BitSet<COLS * ROWS> &MaskRow()

Keep only a single row of values, reducing all others to zeros.

static inline size_t ToCol(size_t id)

Identify which column a specific ID is part of.

static inline size_t ToRow(size_t id)

Identify which row a specific ID is part of.

static inline size_t ToID(size_t col, size_t row)

Identify the ID associated with a specified row and column.

Private Members

BitSet<COLS * ROWS> bits

Actual bits in matrix.