Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_packed_matrix.H
Go to the documentation of this file.
1/*
2 Aleph-w: Data structures & Algorithms
3 Copyright (c) 2026 Leandro Rabindranath Leon
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 deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 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 all
13 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 FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 SOFTWARE.
22*/
23
71#ifndef ALEPH_TPL_PACKED_MATRIX_H
72#define ALEPH_TPL_PACKED_MATRIX_H
73
74#include <array>
75#include <bit>
76#include <cassert>
77#include <compare>
78#include <concepts>
79#include <cstddef>
80#include <cstdint>
81#include <limits>
82#include <type_traits>
83#include <utility>
84
85#include <ah-errors.H>
86#include <tpl_array.H>
87
88namespace Aleph {
89
99{
100 std::int64_t lo;
101 std::int64_t hi;
102};
103
116[[nodiscard]] constexpr Bit_Column between(const std::int64_t lo, const std::int64_t hi)
117{
118 ah_domain_error_if(lo > hi) << "between(" << lo << ", " << hi << "): the range is empty";
119 return {lo, hi};
120}
121
131[[nodiscard]] constexpr Bit_Column bits(const unsigned n)
132{
133 ah_domain_error_if(n > 63) << "bits(" << n << "): at most 63 bits";
134 return {0, static_cast<std::int64_t>((std::uint64_t{1} << n) - 1)};
135}
136
146[[nodiscard]] constexpr Bit_Column signed_bits(const unsigned n)
147{
148 ah_domain_error_if(n < 1 or n > 64) << "signed_bits(" << n << "): from 1 to 64 bits";
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)};
152}
153
155namespace packed_matrix_detail {
156
158inline constexpr size_t max_columns = 65535;
159
163template <size_t N>
164struct Layout
165{
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{};
172 unsigned bits = 0;
173 unsigned word_bits = 64;
174 unsigned words = 1;
175};
176
188template <size_t N>
189[[nodiscard]] constexpr Layout<N> make_layout(const std::array<Bit_Column, N> &cols) noexcept
190{
191 Layout<N> l;
192 for (size_t j = 0; j < N; ++j)
193 {
194 // In unsigned arithmetic: hi - lo overflows a std::int64_t when the
195 // range spans more than half of its values
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];
203 }
204 l.word_bits = l.bits <= 8 ? 8 : l.bits <= 16 ? 16 : l.bits <= 32 ? 32 : 64;
205
206 unsigned word = 0, free = l.word_bits;
207 for (size_t j = 0; j < N; ++j)
208 {
209 if (l.width[j] > free)
210 {
211 ++word;
212 free = l.word_bits;
213 }
214 free -= l.width[j];
215 l.word[j] = static_cast<unsigned short>(word);
216 // A column without bits is read with a null mask: any shift below
217 // the width of the word serves
218 l.shift[j] = static_cast<unsigned char>(l.width[j] == 0 ? 0 : free);
219 }
220 l.words = word + 1;
221 return l;
222}
223
227template <unsigned Bits>
228using Word_Of =
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>>>;
232
236template <class V>
237concept Column_Integer = std::integral<V> and sizeof(V) <= sizeof(std::int64_t);
238
248template <size_t N>
249[[nodiscard]] constexpr bool all_true(const std::array<bool, N> &flags) noexcept
250{
251 for (const bool f : flags)
252 if (not f)
253 return false;
254 return true;
255}
256
265template <class... Vs>
266inline constexpr bool all_column_integers =
267 all_true(std::array<bool, sizeof...(Vs)>{Column_Integer<Vs>...});
268
275template <Column_Integer V>
276[[nodiscard]] constexpr bool fits_in_int64([[maybe_unused]] const V v) noexcept
277{
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());
280 else
281 return true;
282}
283
284// The errors of the checked accesses, out of line: with its message built
285// where the check is, an accessor is too big for Clang to inline it
286
292[[noreturn, gnu::noinline, gnu::cold]] inline void throw_bad_column(const size_t j,
293 const size_t num_cols)
294{
295 ah_out_of_range_error() << "Packed_Row: column " << j << " is out of range: there are "
296 << num_cols << " columns";
297}
298
304[[noreturn, gnu::noinline, gnu::cold]] inline void throw_bad_row(const size_t i, const size_t num_rows)
305{
306 ah_out_of_range_error() << "Packed_Matrix: row " << i << " is out of range: there are "
307 << num_rows << " rows";
308}
309
317[[noreturn, gnu::noinline, gnu::cold]] inline void throw_bad_value(const size_t j,
318 const std::int64_t v,
319 const std::int64_t lo,
320 const std::int64_t hi)
321{
322 ah_domain_error() << "Packed_Row: " << v << " is not in the range [" << lo << ", " << hi
323 << "] of column " << j;
324}
325
333[[noreturn, gnu::noinline, gnu::cold]] inline void throw_bad_value(const size_t j,
334 const std::uint64_t v,
335 const std::int64_t lo,
336 const std::int64_t hi)
337{
338 ah_domain_error() << "Packed_Row: " << v << " is not in the range [" << lo << ", " << hi
339 << "] of column " << j;
340}
341
342} // namespace packed_matrix_detail
343
389template <Bit_Column... Cols>
391{
392 static_assert(sizeof...(Cols) > 0, "Packed_Row needs at least one column");
393 static_assert(sizeof...(Cols) <= packed_matrix_detail::max_columns, "Packed_Row: too many columns");
394 static_assert(packed_matrix_detail::all_true(
395 std::array<bool, sizeof...(Cols)>{(Cols.lo <= Cols.hi)...}),
396 "Packed_Row: a column has an empty range (lo > hi)");
397
399 static constexpr auto layout =
400 packed_matrix_detail::make_layout(std::array<Bit_Column, sizeof...(Cols)>{Cols...});
401
404
406
410 template <bool Checked>
411 class Proxy
412 {
414 size_t j;
415
416 public:
421 constexpr Proxy(Packed_Row &r, const size_t col) noexcept : row(r), j(col)
422 { /* empty */
423 }
424
426 constexpr Proxy(const Proxy &) noexcept = default;
427
431 [[nodiscard]] constexpr operator std::int64_t() const noexcept
432 {
433 return row.fast_read(j);
434 }
435
442 constexpr const Proxy &operator = (const std::int64_t v) const noexcept(not Checked)
443 {
444 if constexpr (Checked)
445 row.check_value(j, v);
446 row.fast_write(j, v);
447 return *this;
448 }
449
456 constexpr const Proxy &operator = (const Proxy &p) const noexcept(not Checked)
457 {
458 return *this = static_cast<std::int64_t>(p);
459 }
460
469 constexpr const Proxy &operator += (const std::int64_t v) const noexcept(not Checked)
470 {
471 return *this = static_cast<std::int64_t>(static_cast<std::uint64_t>(row.fast_read(j)) +
472 static_cast<std::uint64_t>(v));
473 }
474
483 constexpr const Proxy &operator -= (const std::int64_t v) const noexcept(not Checked)
484 {
485 return *this = static_cast<std::int64_t>(static_cast<std::uint64_t>(row.fast_read(j)) -
486 static_cast<std::uint64_t>(v));
487 }
488
495 constexpr const Proxy &operator ++ () const noexcept(not Checked)
496 {
497 return *this += 1;
498 }
499
506 constexpr const Proxy &operator -- () const noexcept(not Checked)
507 {
508 return *this -= 1;
509 }
510 };
511
517 [[gnu::always_inline]] static constexpr void check_value(const size_t j, const std::int64_t v)
518 {
519 if (not valid(j, v)) [[unlikely]]
521 }
522
531 template <packed_matrix_detail::Column_Integer V>
532 [[gnu::always_inline]] static constexpr void check_argument(const size_t j, const V v)
533 {
534 if (not packed_matrix_detail::fits_in_int64(v)) [[unlikely]]
535 packed_matrix_detail::throw_bad_value(j, static_cast<std::uint64_t>(v), layout.lo[j],
536 layout.hi[j]);
537 check_value(j, static_cast<std::int64_t>(v));
538 }
539
544 [[gnu::always_inline]] static constexpr void check_column(const size_t j)
545 {
546 if (j >= num_cols) [[unlikely]]
548 }
549
555 [[nodiscard]] static constexpr std::uint64_t field(const size_t j, const std::int64_t v) noexcept
556 {
557 const std::uint64_t offset =
558 static_cast<std::uint64_t>(v) - static_cast<std::uint64_t>(layout.lo[j]);
559 return (offset & layout.mask[j]) << layout.shift[j];
560 }
561
562public:
564 static constexpr size_t num_cols = sizeof...(Cols);
565
567 static constexpr size_t num_bits = layout.bits;
568
571
574
580 [[nodiscard]] static constexpr std::int64_t column_min(const size_t j) noexcept
581 {
582 assert(j < num_cols);
583 return layout.lo[j];
584 }
585
591 [[nodiscard]] static constexpr std::int64_t column_max(const size_t j) noexcept
592 {
593 assert(j < num_cols);
594 return layout.hi[j];
595 }
596
602 [[nodiscard]] static constexpr unsigned column_bits(const size_t j) noexcept
603 {
604 assert(j < num_cols);
605 return layout.width[j];
606 }
607
613 [[nodiscard]] static constexpr bool valid(const size_t j, const std::int64_t v) noexcept
614 {
615 // One comparison for both ends: below lo, the difference wraps around
616 return j < num_cols and
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]);
619 }
620
624 [[nodiscard]] static constexpr Packed_Row lowest() noexcept
625 {
626 return Packed_Row{};
627 }
628
642 template <class... Vs>
643 requires(sizeof...(Vs) == sizeof...(Cols) and packed_matrix_detail::all_column_integers<Vs...>)
644 [[nodiscard]] static constexpr Packed_Row fast_make(const Vs... vs) noexcept
645 {
646 Packed_Row r{};
647 size_t j = 0;
648 // A braced list, evaluated in order as a fold of commas would be; Clang
649 // refuses a fold of more than 256 operands
650 [[maybe_unused]] const bool in_order[] = {
651 (assert(packed_matrix_detail::fits_in_int64(vs) and valid(j, static_cast<std::int64_t>(vs))),
652 r.words[layout.word[j]] =
653 static_cast<Word>(r.words[layout.word[j]] | field(j, static_cast<std::int64_t>(vs))),
654 ++j, true)...};
655 return r;
656 }
657
668 template <class... Vs>
669 requires(sizeof...(Vs) == sizeof...(Cols) and packed_matrix_detail::all_column_integers<Vs...>)
670 [[nodiscard]] static constexpr Packed_Row make(const Vs... vs)
671 {
672 size_t j = 0;
673 [[maybe_unused]] const bool in_order[] = {(check_argument(j++, vs), true)...};
674 return fast_make(vs...);
675 }
676
682 [[nodiscard]] constexpr std::int64_t fast_read(const size_t j) const noexcept
683 {
684 assert(j < num_cols);
685 // In unsigned arithmetic: lo plus the stored difference may pass
686 // through values that do not fit in a std::int64_t
687 return static_cast<std::int64_t>(
688 static_cast<std::uint64_t>(layout.lo[j]) +
689 ((static_cast<std::uint64_t>(words[layout.word[j]]) >> layout.shift[j]) & layout.mask[j]));
690 }
691
700 constexpr void fast_write(const size_t j, const std::int64_t v) noexcept
701 {
702 assert(j < num_cols);
703 assert(valid(j, v));
704 Word &w = words[layout.word[j]];
705 const std::uint64_t m = layout.mask[j] << layout.shift[j];
706 // Masked merge: the bits of the column come from the value, the rest
707 // from the word
708 w = static_cast<Word>(w ^ ((w ^ field(j, v)) & m));
709 }
710
716 [[nodiscard, gnu::always_inline]] constexpr std::int64_t read(const size_t j) const
717 {
718 check_column(j);
719 return fast_read(j);
720 }
721
729 [[gnu::always_inline]] constexpr void write(const size_t j, const std::int64_t v)
730 {
731 check_column(j);
732 check_value(j, v);
733 fast_write(j, v);
734 }
735
743 [[nodiscard]] constexpr std::int64_t operator () (const size_t j) const & noexcept
744 {
745 return fast_read(j);
746 }
747
753 [[nodiscard]] constexpr Fast_Cell operator () (const size_t j) & noexcept
754 {
755 assert(j < num_cols);
756 return Fast_Cell(*this, j);
757 }
758
766 [[nodiscard, gnu::always_inline]] constexpr std::int64_t operator [] (const size_t j) const &
767 {
768 return read(j);
769 }
770
776 [[nodiscard, gnu::always_inline]] constexpr Cell operator [] (const size_t j) &
777 {
778 check_column(j);
779 return Cell(*this, j);
780 }
781
787 [[nodiscard]] friend constexpr bool operator == (const Packed_Row &a, const Packed_Row &b) noexcept
788 {
789 for (size_t k = 0; k < layout.words; ++k)
790 if (a.words[k] != b.words[k])
791 return false;
792 return true;
793 }
794
800 [[nodiscard]] friend constexpr std::strong_ordering operator <=> (const Packed_Row &a,
801 const Packed_Row &b) noexcept
802 {
803 // The first column is in the most significant bits of the first word,
804 // and v - lo keeps the order of v
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;
809 }
810};
811
863template <Bit_Column... Cols>
865{
866public:
868 using Row = Packed_Row<Cols...>;
869
870private:
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");
873
875
880 [[gnu::always_inline]] void check_row(const size_t i) const
881 {
882 if (i >= rows_.size()) [[unlikely]]
884 }
885
886public:
894 explicit Packed_Matrix(const size_t num_rows = 0) : rows_(num_rows, Row{})
895 { /* empty */
896 }
897
906 Packed_Matrix(const size_t num_rows, const Row &row) : rows_(num_rows, row)
907 { /* empty */
908 }
909
913 [[nodiscard]] size_t rows() const noexcept
914 {
915 return rows_.size();
916 }
917
921 [[nodiscard]] static constexpr size_t cols() noexcept
922 {
923 return Row::num_cols;
924 }
925
931 [[nodiscard]] Row &operator () (const size_t i) & noexcept
932 {
933 return rows_(i);
934 }
935
941 [[nodiscard]] const Row &operator () (const size_t i) const & noexcept
942 {
943 return rows_(i);
944 }
945
951 [[nodiscard]] Row operator () (const size_t i) && noexcept
952 {
953 return rows_(i);
954 }
955
963 [[nodiscard]] typename Row::Fast_Cell operator () (const size_t i, const size_t j) & noexcept
964 {
965 return rows_(i)(j);
966 }
967
977 [[nodiscard]] std::int64_t operator () (const size_t i, const size_t j) const & noexcept
978 {
979 return rows_(i).fast_read(j);
980 }
981
989 [[nodiscard]] std::int64_t fast_read(const size_t i, const size_t j) const noexcept
990 {
991 return rows_(i).fast_read(j);
992 }
993
1004 void fast_write(const size_t i, const size_t j, const std::int64_t v) noexcept
1005 {
1006 rows_(i).fast_write(j, v);
1007 }
1008
1015 [[nodiscard, gnu::always_inline]] Row &operator [] (const size_t i) &
1016 {
1017 check_row(i);
1018 return rows_(i);
1019 }
1020
1026 [[nodiscard, gnu::always_inline]] const Row &operator [] (const size_t i) const &
1027 {
1028 check_row(i);
1029 return rows_(i);
1030 }
1031
1038 [[nodiscard, gnu::always_inline]] Row operator [] (const size_t i) &&
1039 {
1040 check_row(i);
1041 return rows_(i);
1042 }
1043
1050 [[nodiscard, gnu::always_inline]] std::int64_t read(const size_t i, const size_t j) const
1051 {
1052 check_row(i);
1053 return rows_(i).read(j);
1054 }
1055
1064 [[gnu::always_inline]] void write(const size_t i, const size_t j, const std::int64_t v)
1065 {
1066 check_row(i);
1067 rows_(i).write(j, v);
1068 }
1069
1074 void fill(const Row &row) noexcept
1075 {
1076 for (Row &r : *this)
1077 r = row;
1078 }
1079
1083 void swap(Packed_Matrix &m) noexcept
1084 {
1085 rows_.swap(m.rows_);
1086 }
1087
1109 void prefetch(const size_t i) const noexcept
1110 {
1111 const Row *row = &rows_(i);
1112 __builtin_prefetch(row);
1113 // A row of one word lies within a cache line; one of several words
1114 // may reach the next ones
1115 if constexpr (sizeof(Row) > sizeof(std::uint64_t))
1116 {
1117 const auto first = static_cast<const char *>(static_cast<const void *>(row));
1118 for (size_t offset = 64; offset < sizeof(Row); offset += 64)
1119 __builtin_prefetch(first + offset);
1120 __builtin_prefetch(first + sizeof(Row) - 1);
1121 }
1122 }
1123
1129 [[nodiscard]] Row *data() noexcept
1130 {
1131 return rows_.is_empty() ? nullptr : &rows_(0);
1132 }
1133
1137 [[nodiscard]] const Row *data() const noexcept
1138 {
1139 return rows_.is_empty() ? nullptr : &rows_(0);
1140 }
1141
1145 [[nodiscard]] Row *begin() noexcept
1146 {
1147 return data();
1148 }
1149
1153 [[nodiscard]] const Row *begin() const noexcept
1154 {
1155 return data();
1156 }
1157
1161 [[nodiscard]] Row *end() noexcept
1162 {
1163 return rows_.is_empty() ? nullptr : data() + rows_.size();
1164 }
1165
1169 [[nodiscard]] const Row *end() const noexcept
1170 {
1171 return rows_.is_empty() ? nullptr : data() + rows_.size();
1172 }
1173
1181 [[nodiscard]] friend bool operator == (const Packed_Matrix &a, const Packed_Matrix &b) noexcept
1182 {
1183 if (a.rows() != b.rows())
1184 return false;
1185 for (size_t i = 0; i < a.rows(); ++i)
1186 if (not (a.rows_(i) == b.rows_(i)))
1187 return false;
1188 return true;
1189 }
1190};
1191
1193namespace packed_matrix_detail {
1194
1198template <class T>
1199inline constexpr bool is_column_array = false;
1200
1204template <size_t N>
1205inline constexpr bool is_column_array<std::array<Bit_Column, N>> = N >= 1 and N <= max_columns;
1206
1210template <class T>
1211concept Column_Array = is_column_array<std::remove_cv_t<T>>;
1212
1223template <auto Columns, class Indexes, bool Valid = Column_Array<decltype(Columns)>>
1224struct Columns_At {};
1225
1230template <auto Columns, size_t... J>
1231struct Columns_At<Columns, std::index_sequence<J...>, true>
1232{
1233 using Row = Packed_Row<Columns[J]...>;
1234 using Matrix = Packed_Matrix<Columns[J]...>;
1235};
1236
1237} // namespace packed_matrix_detail
1238
1267template <packed_matrix_detail::Column_Array auto Columns>
1269 typename packed_matrix_detail::Columns_At<Columns,
1270 std::make_index_sequence<Columns.size()>>::Row;
1271
1294template <packed_matrix_detail::Column_Array auto Columns>
1296 typename packed_matrix_detail::Columns_At<Columns,
1297 std::make_index_sequence<Columns.size()>>::Matrix;
1298
1299} // namespace Aleph
1300
1301#endif // ALEPH_TPL_PACKED_MATRIX_H
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error()
Throws std::domain_error unconditionally.
Definition ah-errors.H:702
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
#define ah_out_of_range_error()
Throws std::out_of_range unconditionally.
Definition ah-errors.H:759
long double w
Definition btreepic.C:153
size_t row
Definition ca-c-api.h:115
size_t size_t col
Definition ca-c-api.h:116
size_t cols
Definition ca-c-api.h:105
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
Sparse matrix with generic row and column domains.
Definition al-matrix.H:70
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
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.
Packed_Row & row
The row.
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.
#define N
Definition fib.C:294
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].
STL namespace.
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.
std::array< std::int64_t, N > hi
Highest value of each column.
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)
size_t V
static int * k
Gsl_Compatible_Rng r
Dynamic array container with automatic resizing.
DynList< int > l