14template <
typename T,
bool IsConst>
18 using size_type = size_t;
19 using pointer =
typename std::conditional<IsConst, const value_type*, value_type*>::type;
20 using reference =
typename std::conditional<IsConst, const value_type&, value_type&>::type;
22 BitMatrixView(pointer vector, size_type cols) noexcept : m_vector(vector), m_cols(cols)
25 reference operator[](size_type col)
noexcept
31 size_type size() const noexcept
46 BitMatrix() : m_rows(0), m_cols(0), m_matrix(nullptr)
49 BitMatrix(
size_t rows,
size_t cols, T val) : m_rows(rows), m_cols(cols), m_matrix(nullptr)
51 if (m_rows && m_cols) m_matrix =
new T[m_rows * m_cols];
52 std::fill_n(m_matrix, m_rows * m_cols, val);
55 BitMatrix(
const BitMatrix& other) : m_rows(other.m_rows), m_cols(other.m_cols), m_matrix(nullptr)
57 if (m_rows && m_cols) m_matrix =
new T[m_rows * m_cols];
58 std::copy(other.m_matrix, other.m_matrix + m_rows * m_cols, m_matrix);
61 BitMatrix(BitMatrix&& other) noexcept : m_rows(0), m_cols(0), m_matrix(
nullptr)
66 BitMatrix& operator=(BitMatrix&& other)
noexcept
72 BitMatrix& operator=(
const BitMatrix& other)
74 BitMatrix temp = other;
79 void swap(BitMatrix& rhs)
noexcept
82 swap(m_rows, rhs.m_rows);
83 swap(m_cols, rhs.m_cols);
84 swap(m_matrix, rhs.m_matrix);
92 BitMatrixView<value_type, false> operator[](
size_t row)
noexcept
95 return {&m_matrix[row * m_cols], m_cols};
98 BitMatrixView<value_type, true> operator[](
size_t row)
const noexcept
100 assert(row < m_rows);
101 return {&m_matrix[row * m_cols], m_cols};
104 size_t rows() const noexcept
109 size_t cols() const noexcept
121struct ShiftedBitMatrix {
122 using value_type = T;
127 ShiftedBitMatrix(
size_t rows,
size_t cols, T val) : m_matrix(rows, cols, val), m_offsets(rows)
130 ShiftedBitMatrix(
const ShiftedBitMatrix& other) : m_matrix(other.m_matrix), m_offsets(other.m_offsets)
133 ShiftedBitMatrix(ShiftedBitMatrix&& other)
noexcept
138 ShiftedBitMatrix& operator=(ShiftedBitMatrix&& other)
noexcept
144 ShiftedBitMatrix& operator=(
const ShiftedBitMatrix& other)
146 ShiftedBitMatrix temp = other;
151 void swap(ShiftedBitMatrix& rhs)
noexcept
154 swap(m_matrix, rhs.m_matrix);
155 swap(m_offsets, rhs.m_offsets);
158 bool test_bit(
size_t row,
size_t col,
bool default_ =
false) const noexcept
160 ptrdiff_t offset = m_offsets[row];
163 col +=
static_cast<size_t>(-offset);
165 else if (col >=
static_cast<size_t>(offset)) {
166 col -=
static_cast<size_t>(offset);
173 size_t word_size =
sizeof(value_type) * 8;
174 size_t col_word = col / word_size;
175 value_type col_mask = value_type(1) << (col % word_size);
177 return bool(m_matrix[row][col_word] & col_mask);
180 BitMatrixView<value_type, false> operator[](
size_t row)
noexcept
182 return m_matrix[row];
185 BitMatrixView<value_type, true> operator[](
size_t row)
const noexcept
187 return m_matrix[row];
190 void set_offset(
size_t row, ptrdiff_t offset)
192 m_offsets[row] = offset;
196 BitMatrix<value_type> m_matrix;
197 std::vector<ptrdiff_t> m_offsets;