71#ifndef ALEPH_TPL_PACKED_MATRIX_H
72#define ALEPH_TPL_PACKED_MATRIX_H
118 ah_domain_error_if(lo > hi) <<
"between(" << lo <<
", " << hi <<
"): the range is empty";
134 return {0,
static_cast<std::int64_t
>((std::uint64_t{1} << n) - 1)};
149 const std::int64_t lo =
150 n == 64 ? std::numeric_limits<std::int64_t>::min() : -(std::int64_t{1} << (n - 1));
151 return {lo, -(lo + 1)};
155namespace packed_matrix_detail {
166 std::array<std::int64_t, N>
lo{};
167 std::array<std::int64_t, N>
hi{};
168 std::array<std::uint64_t, N>
mask{};
169 std::array<unsigned char, N>
width{};
170 std::array<unsigned char, N>
shift{};
171 std::array<unsigned short, N>
word{};
192 for (
size_t j = 0; j <
N; ++j)
196 const std::uint64_t span =
197 static_cast<std::uint64_t
>(
cols[j].hi) -
static_cast<std::uint64_t
>(
cols[j].lo);
198 l.lo[j] =
cols[j].lo;
199 l.hi[j] =
cols[j].hi;
200 l.width[j] =
static_cast<unsigned char>(std::bit_width(span));
201 l.mask[j] =
l.width[j] == 64 ?
~std::uint64_t{0} : (std::uint64_t{1} <<
l.width[j]) - 1;
202 l.bits +=
l.width[j];
204 l.word_bits =
l.bits <= 8 ? 8 :
l.bits <= 16 ? 16 :
l.bits <= 32 ? 32 : 64;
206 unsigned word = 0, free =
l.word_bits;
207 for (
size_t j = 0; j <
N; ++j)
209 if (
l.width[j] > free)
215 l.word[j] =
static_cast<unsigned short>(word);
218 l.shift[j] =
static_cast<unsigned char>(
l.width[j] == 0 ? 0 : free);
227template <
unsigned Bits>
229 std::conditional_t<Bits == 8, std::uint8_t,
230 std::conditional_t<Bits == 16, std::uint16_t,
231 std::conditional_t<Bits == 32, std::uint32_t, std::uint64_t>>>;
249[[nodiscard]]
constexpr bool all_true(
const std::array<bool, N> &flags)
noexcept
251 for (
const bool f : flags)
265template <
class... Vs>
275template <Column_Integer V>
278 if constexpr (std::is_unsigned_v<V>
and sizeof(
V) ==
sizeof(std::int64_t))
279 return v <=
static_cast<V>(std::numeric_limits<std::int64_t>::max());
293 const size_t num_cols)
296 << num_cols <<
" columns";
304[[noreturn, gnu::noinline, gnu::cold]]
inline void throw_bad_row(
const size_t i,
const size_t num_rows)
307 << num_rows <<
" rows";
318 const std::int64_t v,
319 const std::int64_t lo,
320 const std::int64_t hi)
322 ah_domain_error() <<
"Packed_Row: " << v <<
" is not in the range [" << lo <<
", " << hi
323 <<
"] of column " << j;
334 const std::uint64_t v,
335 const std::int64_t lo,
336 const std::int64_t hi)
338 ah_domain_error() <<
"Packed_Row: " << v <<
" is not in the range [" << lo <<
", " << hi
339 <<
"] of column " << j;
392 static_assert(
sizeof...(Cols) > 0,
"Packed_Row needs at least one column");
395 std::array<
bool,
sizeof...(Cols)>{(Cols.lo <= Cols.hi)...}),
396 "Packed_Row: a column has an empty range (lo > hi)");
410 template <
bool Checked>
431 [[nodiscard]]
constexpr operator std::int64_t() const noexcept
444 if constexpr (Checked)
458 return *
this =
static_cast<std::int64_t
>(p);
471 return *
this =
static_cast<std::int64_t
>(
static_cast<std::uint64_t
>(
row.
fast_read(
j)) +
472 static_cast<std::uint64_t
>(v));
485 return *
this =
static_cast<std::int64_t
>(
static_cast<std::uint64_t
>(
row.
fast_read(
j)) -
486 static_cast<std::uint64_t
>(v));
517 [[gnu::always_inline]]
static constexpr void check_value(
const size_t j,
const std::int64_t v)
519 if (not
valid(j, v)) [[unlikely]]
531 template <packed_matrix_detail::Column_Integer V>
532 [[gnu::always_inline]]
static constexpr void check_argument(
const size_t j,
const V v)
544 [[gnu::always_inline]]
static constexpr void check_column(
const size_t j)
555 [[nodiscard]]
static constexpr std::uint64_t
field(
const size_t j,
const std::int64_t v)
noexcept
557 const std::uint64_t
offset =
558 static_cast<std::uint64_t
>(v) -
static_cast<std::uint64_t
>(
layout.lo[j]);
564 static constexpr size_t num_cols =
sizeof...(Cols);
580 [[nodiscard]]
static constexpr std::int64_t
column_min(
const size_t j)
noexcept
591 [[nodiscard]]
static constexpr std::int64_t
column_max(
const size_t j)
noexcept
602 [[nodiscard]]
static constexpr unsigned column_bits(
const size_t j)
noexcept
613 [[nodiscard]]
static constexpr bool valid(
const size_t j,
const std::int64_t v)
noexcept
617 static_cast<std::uint64_t
>(v) -
static_cast<std::uint64_t
>(
layout.lo[j]) <=
618 static_cast<std::uint64_t
>(
layout.hi[j]) -
static_cast<std::uint64_t
>(
layout.lo[j]);
642 template <
class... Vs>
650 [[maybe_unused]]
const bool in_order[] = {
653 static_cast<Word>(
r.words[
layout.word[j]] |
field(j,
static_cast<std::int64_t
>(vs))),
668 template <
class... Vs>
673 [[maybe_unused]]
const bool in_order[] = {(
check_argument(j++, vs),
true)...};
682 [[nodiscard]]
constexpr std::int64_t
fast_read(
const size_t j)
const noexcept
687 return static_cast<std::int64_t
>(
688 static_cast<std::uint64_t
>(
layout.lo[j]) +
700 constexpr void fast_write(
const size_t j,
const std::int64_t v)
noexcept
716 [[nodiscard, gnu::always_inline]]
constexpr std::int64_t
read(
const size_t j)
const
729 [[gnu::always_inline]]
constexpr void write(
const size_t j,
const std::int64_t v)
743 [[nodiscard]]
constexpr std::int64_t
operator () (
const size_t j)
const &
noexcept
766 [[nodiscard, gnu::always_inline]]
constexpr std::int64_t
operator [] (
const size_t j)
const &
779 return Cell(*
this, j);
789 for (
size_t k = 0;
k <
layout.words; ++
k)
790 if (a.words[
k] != b.words[
k])
805 for (
size_t k = 0;
k <
layout.words; ++
k)
806 if (a.words[
k] != b.words[
k])
807 return a.words[
k] <=> b.words[
k];
808 return std::strong_ordering::equal;
871 static_assert(std::is_trivially_default_constructible_v<Row>
and std::is_trivially_copyable_v<Row>,
872 "Packed_Row must be trivial: Array would write the slots that it only reserves");
880 [[gnu::always_inline]]
void check_row(
const size_t i)
const
882 if (i >=
rows_.size()) [[unlikely]]
913 [[nodiscard]]
size_t rows() const noexcept
921 [[nodiscard]]
static constexpr size_t cols() noexcept
977 [[nodiscard]] std::int64_t
operator () (
const size_t i,
const size_t j)
const &
noexcept
989 [[nodiscard]] std::int64_t
fast_read(
const size_t i,
const size_t j)
const noexcept
991 return rows_(i).fast_read(j);
1004 void fast_write(
const size_t i,
const size_t j,
const std::int64_t v)
noexcept
1006 rows_(i).fast_write(j, v);
1050 [[nodiscard, gnu::always_inline]] std::int64_t
read(
const size_t i,
const size_t j)
const
1053 return rows_(i).read(j);
1064 [[gnu::always_inline]]
void write(
const size_t i,
const size_t j,
const std::int64_t v)
1067 rows_(i).write(j, v);
1076 for (
Row &
r : *
this)
1112 __builtin_prefetch(
row);
1115 if constexpr (
sizeof(
Row) >
sizeof(std::uint64_t))
1117 const auto first =
static_cast<const char *
>(
static_cast<const void *
>(
row));
1119 __builtin_prefetch(first +
offset);
1120 __builtin_prefetch(first +
sizeof(
Row) - 1);
1131 return rows_.is_empty() ? nullptr : &
rows_(0);
1139 return rows_.is_empty() ? nullptr : &
rows_(0);
1169 [[nodiscard]]
const Row *
end() const noexcept
1183 if (a.rows() != b.rows())
1185 for (
size_t i = 0; i < a.rows(); ++i)
1186 if (not (a.rows_(i) == b.rows_(i)))
1193namespace packed_matrix_detail {
1205inline constexpr bool is_column_array<std::array<Bit_Column, N>> =
N >= 1
and N <=
max_columns;
1223template <auto Columns,
class Indexes,
bool Val
id = Column_Array<decltype(Columns)>>
1230template <
auto Columns,
size_t... J>
1267template <packed_matrix_detail::Column_Array auto Columns>
1270 std::make_index_sequence<Columns.size()>>::Row;
1294template <packed_matrix_detail::Column_Array auto Columns>
1297 std::make_index_sequence<Columns.size()>>
::Matrix;
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error()
Throws std::domain_error unconditionally.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_out_of_range_error()
Throws std::out_of_range unconditionally.
Simple dynamic array with automatic resizing and functional operations.
Sparse matrix with generic row and column domains.
Number Theoretic Transform over Z / MOD Z.
Matrix whose columns are bit fields: an Aleph::Array of Packed_Row.
Packed_Matrix(const size_t num_rows, const Row &row)
Build a matrix whose rows are all equal to a given one.
Packed_Matrix(const size_t num_rows=0)
Build a matrix whose columns are all at their lowest value.
size_t rows() const noexcept
Number of rows.
Row * data() noexcept
The rows as a contiguous array.
const Row * end() const noexcept
End of the rows.
std::int64_t read(const size_t i, const size_t j) const
Read an entry.
void swap(Packed_Matrix &m) noexcept
Exchange the rows of two matrices in constant time.
Array< Row > rows_
The rows.
Row * begin() noexcept
Start of the rows, for a range-based for.
const Row * begin() const noexcept
Start of the rows, for a range-based for.
static constexpr size_t cols() noexcept
Number of columns.
Row & operator()(const size_t i) &noexcept
Access a row without checking.
Row & operator[](const size_t i) &
Access a row.
void check_row(const size_t i) const
Validate a row index.
const Row * data() const noexcept
The rows as a contiguous array.
void fill(const Row &row) noexcept
Set every row to a given one.
Row * end() noexcept
End of the rows.
Packed_Row< Cols... > Row
Type of a row.
friend bool operator==(const Packed_Matrix &a, const Packed_Matrix &b) noexcept
Test whether two matrices hold the same rows.
std::int64_t fast_read(const size_t i, const size_t j) const noexcept
Read an entry without checking.
void write(const size_t i, const size_t j, const std::int64_t v)
Write an entry.
void prefetch(const size_t i) const noexcept
Hint the processor to bring a row into the cache.
void fast_write(const size_t i, const size_t j, const std::int64_t v) noexcept
Write an entry without checking.
Proxy of a column of a row that is not const.
constexpr const Proxy & operator=(const std::int64_t v) const noexcept(not Checked)
Write the column.
constexpr const Proxy & operator--() const noexcept(not Checked)
Subtract one from the column.
constexpr const Proxy & operator+=(const std::int64_t v) const noexcept(not Checked)
Add v to the column.
constexpr Proxy(const Proxy &) noexcept=default
Copying a proxy binds the same column of the same row.
constexpr const Proxy & operator-=(const std::int64_t v) const noexcept(not Checked)
Subtract v from the column.
size_t j
The column; already validated if Checked.
constexpr const Proxy & operator++() const noexcept(not Checked)
Add one to the column.
constexpr Proxy(Packed_Row &r, const size_t col) noexcept
Bind column col of r.
Tuple of small integers packed into the fewest whole words.
static constexpr Packed_Row make(const Vs... vs)
Build a row from the values of its columns.
static constexpr auto layout
Where every column lives: widths, shifts and masks.
packed_matrix_detail::Word_Of< layout.word_bits > Word
Unsigned type of a word of the row.
Proxy< false > Fast_Cell
What r(j) returns on a row that is not const: writing does not check.
friend constexpr std::strong_ordering operator<=>(const Packed_Row &a, const Packed_Row &b) noexcept
Compare two rows as their tuples of values, column 0 first.
static constexpr bool valid(const size_t j, const std::int64_t v) noexcept
Test whether a column can hold a value.
static constexpr void check_argument(const size_t j, const V v)
Validate an argument of make() for a column.
constexpr void fast_write(const size_t j, const std::int64_t v) noexcept
Write a column without checking.
static constexpr unsigned column_bits(const size_t j) noexcept
Bits that a column takes.
static constexpr size_t num_bits
Sum of the bits of the columns; 8*sizeof(Packed_Row) rounds it up.
constexpr std::int64_t operator()(const size_t j) const &noexcept
Read a column without checking.
static constexpr Packed_Row lowest() noexcept
The row whose columns are all at their lowest value.
static constexpr void check_column(const size_t j)
Validate a column index.
static constexpr std::uint64_t field(const size_t j, const std::int64_t v) noexcept
The bits of a column that stand for a value, in their place.
constexpr std::int64_t operator[](const size_t j) const &
Read a column.
static constexpr std::int64_t column_min(const size_t j) noexcept
Lowest value of a column.
static constexpr size_t num_cols
Number of columns.
Word words[layout.words]
The packed columns; the bits of no column are always zero.
constexpr std::int64_t fast_read(const size_t j) const noexcept
Read a column without checking.
friend constexpr bool operator==(const Packed_Row &a, const Packed_Row &b) noexcept
Test whether two rows hold the same values.
Proxy< true > Cell
What r[j] returns on a row that is not const: writing validates the value.
constexpr std::int64_t read(const size_t j) const
Read a column.
constexpr void write(const size_t j, const std::int64_t v)
Write a column.
static constexpr void check_value(const size_t j, const std::int64_t v)
Validate a value for a column.
static constexpr Packed_Row fast_make(const Vs... vs) noexcept
Build a row from the values of its columns, without checking.
static constexpr std::int64_t column_max(const size_t j) noexcept
Highest value of a column.
An array of columns that Packed_Row_Of and Packed_Matrix_Of take.
An integer type that make() and fast_make() take: at most 64 bits.
const long double offset[]
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
void throw_bad_column(const size_t j, const size_t num_cols)
Report a column index that is out of range.
constexpr bool is_column_array
Test whether a type is an array of columns that a row can take.
void throw_bad_value(const size_t j, const std::int64_t v, const std::int64_t lo, const std::int64_t hi)
Report a value that is outside the range of its column.
void throw_bad_row(const size_t i, const size_t num_rows)
Report a row index that is out of range.
constexpr bool all_true(const std::array< bool, N > &flags) noexcept
Test whether every flag of an array is set.
std::conditional_t< Bits==8, std::uint8_t, std::conditional_t< Bits==16, std::uint16_t, std::conditional_t< Bits==32, std::uint32_t, std::uint64_t > > > Word_Of
Unsigned type of the given number of bits.
constexpr size_t max_columns
Most columns of a row: Layout keeps the index of the word of a column in 16 bits.
constexpr bool all_column_integers
Test whether make() and fast_make() take every type of a pack.
constexpr bool fits_in_int64(const V v) noexcept
Test whether an integer is a value of std::int64_t.
constexpr Layout< N > make_layout(const std::array< Bit_Column, N > &cols) noexcept
Place the columns of a row.
Main namespace for Aleph-w library functions.
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
and
Check uniqueness with explicit hash + equality functors.
typename packed_matrix_detail::Columns_At< Columns, std::make_index_sequence< Columns.size()> >::Matrix Packed_Matrix_Of
Packed_Matrix whose columns are the entries of an array.
typename packed_matrix_detail::Columns_At< Columns, std::make_index_sequence< Columns.size()> >::Row Packed_Row_Of
Packed_Row whose columns are the entries of an array.
bool between(const Point &a, const Point &b, const Point &c)
constexpr Bit_Column signed_bits(const unsigned n)
Column of n bits with sign: the integers of [-2^(n-1), 2^(n-1) - 1].
Inclusive range of the integers that a column holds.
std::int64_t hi
Highest value of the column; not less than lo.
std::int64_t lo
Lowest value of the column.
The row and the matrix of the columns of an array, by their indexes.
Where every column of a row lives.
unsigned bits
Sum of the widths.
std::array< std::int64_t, N > hi
Highest value of each column.
unsigned words
Words of a row.
std::array< std::int64_t, N > lo
Lowest value of each column.
std::array< std::uint64_t, N > mask
The low width bits of each column.
std::array< unsigned short, N > word
Word of the row that holds each column.
std::array< unsigned char, N > width
Bits of each column.
unsigned word_bits
Bits of a word of the row: 8, 16, 32 or 64.
std::array< unsigned char, N > shift
Position of each column inside its word.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic array container with automatic resizing.