5#ifndef NURI_CORE_CONTAINER_BOOL_MATRIX_H_
6#define NURI_CORE_CONTAINER_BOOL_MATRIX_H_
13#include <absl/base/optimization.h>
21 using Block = uint64_t;
22 constexpr Eigen::Index kBitsPerBlock = std::numeric_limits<Block>::digits;
27 using Index = Eigen::Index;
34 idx % internal::kBitsPerBlock) {
35 ABSL_ASSUME(idx >= 0);
39 : blk_(
blk), bit_(static_cast<int>(
bit)), mask_(1ULL << bit_) { }
41 Index
to_index()
const {
return blk_ * internal::kBitsPerBlock + bit_; }
43 Index
blk()
const {
return blk_; }
44 int bit()
const {
return bit_; }
45 internal::Block
mask()
const {
return mask_; }
50 internal::Block mask_;
60 : data_((
rows - 1) / internal::kBitsPerBlock + 1,
cols), rows_(
rows) { }
63 return (data_(row.
blk(), col) & row.
mask()) != 0;
73 return col_reduction_impl<false>(reduce_begin);
77 for (
Index blk = 0; blk < data_.rows(); ++blk)
78 data_(blk, c1) ^= data_(blk, c2);
81 void zero() { data_.setZero(); }
84 const Index oldcols = data_.cols();
85 data_.conservativeResize(Eigen::NoChange,
cols);
87 data_.rightCols(
cols - oldcols).setZero();
92 data_.col(c2) = data_.col(c1);
93 data_.col(c1).setZero();
97 data_(row.
blk(), col) |= row.
mask();
101 data_(row.
blk(), col) &= ~row.mask();
105 internal::Block &data = data_(row.
blk(), col);
106 data = (data & ~row.mask())
107 | (
static_cast<internal::Block
>(value) << row.
bit());
112 std::vector<int> col_reduction_impl(
const int reduce_begin = 0) {
113 std::vector<int> used(data_.cols(), 0);
115 for (
int row = 0; row < rows_; ++row) {
116 const int p =
static_cast<int>(find_pivot(row, used));
124 if (p >= reduce_begin)
127 for (
Index i = reduce_begin; i < data_.cols(); ++i)
128 if (used[i] == 0 && (*
this)(row, i))
135 Index find_pivot(BoolMatrixKey row,
const std::vector<int> &used)
const {
136 for (
Index i = 0; i < data_.cols(); ++i)
137 if (used[i] == 0 && (*
this)(row, i))
144 Matrix<internal::Block, Eigen::Dynamic, Eigen::Dynamic> data_;
Definition bool_matrix.h:25
internal::Block mask() const
Definition bool_matrix.h:45
Index blk() const
Definition bool_matrix.h:43
Index to_index() const
Definition bool_matrix.h:41
BoolMatrixKey(const Index blk, const Index bit)
Definition bool_matrix.h:38
BoolMatrixKey(const Index idx)
Definition bool_matrix.h:32
int bit() const
Definition bool_matrix.h:44
void set(BoolMatrixKey row, Index col)
Definition bool_matrix.h:96
Index cols() const
Definition bool_matrix.h:68
void resize(int cols)
Definition bool_matrix.h:83
Index rows() const
Definition bool_matrix.h:66
bool operator()(BoolMatrixKey row, Index col) const
Definition bool_matrix.h:62
std::vector< int > gaussian_elimination()
Definition bool_matrix.h:70
void assign(BoolMatrixKey row, Index col, bool value)
Definition bool_matrix.h:104
void unset(BoolMatrixKey row, Index col)
Definition bool_matrix.h:100
Eigen::Index Index
Definition bool_matrix.h:55
void zero()
Definition bool_matrix.h:81
std::vector< int > partial_reduction(const int reduce_begin)
Definition bool_matrix.h:72
void move_col(Index c1, Index c2)
Definition bool_matrix.h:91
BoolMatrix(Index rows, Index cols=0)
Definition bool_matrix.h:59
void colwise_xor(Index c1, Index c2)
Definition bool_matrix.h:76