Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_packed_matrix.H File Reference

Matrix whose columns are bit fields sized from integer ranges. More...

#include <array>
#include <bit>
#include <cassert>
#include <compare>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <type_traits>
#include <utility>
#include <ah-errors.H>
#include <tpl_array.H>
Include dependency graph for tpl_packed_matrix.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  Aleph::Bit_Column
 Inclusive range of the integers that a column holds. More...
 
struct  Aleph::packed_matrix_detail::Layout< N >
 Where every column of a row lives. More...
 
class  Aleph::Packed_Row< Cols >
 Tuple of small integers packed into the fewest whole words. More...
 
class  Aleph::Packed_Row< Cols >::Proxy< Checked >
 Proxy of a column of a row that is not const. More...
 
class  Aleph::Packed_Matrix< Cols >
 Matrix whose columns are bit fields: an Aleph::Array of Packed_Row. More...
 
struct  Aleph::packed_matrix_detail::Columns_At< Columns, Indexes, Valid >
 The row and the matrix of the columns of an array, by their indexes. More...
 
struct  Aleph::packed_matrix_detail::Columns_At< Columns, std::index_sequence< J... >, true >
 The row and the matrix of the columns Columns[J].... More...
 

Namespaces

namespace  Aleph
 Main namespace for Aleph-w library functions.
 
namespace  Aleph::packed_matrix_detail
 

Concepts

concept  Aleph::packed_matrix_detail::Column_Integer
 An integer type that make() and fast_make() take: at most 64 bits.
 
concept  Aleph::packed_matrix_detail::Column_Array
 An array of columns that Packed_Row_Of and Packed_Matrix_Of take.
 

Typedefs

template<unsigned Bits>
using Aleph::packed_matrix_detail::Word_Of = 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 > > >
 Unsigned type of the given number of bits.
 
template<packed_matrix_detail::Column_Array auto Columns>
using Aleph::Packed_Row_Of = typename packed_matrix_detail::Columns_At< Columns, std::make_index_sequence< Columns.size()> >::Row
 Packed_Row whose columns are the entries of an array.
 
template<packed_matrix_detail::Column_Array auto Columns>
using Aleph::Packed_Matrix_Of = typename packed_matrix_detail::Columns_At< Columns, std::make_index_sequence< Columns.size()> >::Matrix
 Packed_Matrix whose columns are the entries of an array.
 

Functions

constexpr Bit_Column Aleph::between (const std::int64_t lo, const std::int64_t hi)
 Column that holds the integers of [lo, hi].
 
constexpr Bit_Column Aleph::bits (const unsigned n)
 Column of n bits without sign: the integers of [0, 2^n - 1].
 
constexpr Bit_Column Aleph::signed_bits (const unsigned n)
 Column of n bits with sign: the integers of [-2^(n-1), 2^(n-1) - 1].
 
template<size_t N>
constexpr Layout< N > Aleph::packed_matrix_detail::make_layout (const std::array< Bit_Column, N > &cols) noexcept
 Place the columns of a row.
 
template<size_t N>
constexpr bool Aleph::packed_matrix_detail::all_true (const std::array< bool, N > &flags) noexcept
 Test whether every flag of an array is set.
 
template<Column_Integer V>
constexpr bool Aleph::packed_matrix_detail::fits_in_int64 (const V v) noexcept
 Test whether an integer is a value of std::int64_t.
 
void Aleph::packed_matrix_detail::throw_bad_column (const size_t j, const size_t num_cols)
 Report a column index that is out of range.
 
void Aleph::packed_matrix_detail::throw_bad_row (const size_t i, const size_t num_rows)
 Report a row index that is out of range.
 
void Aleph::packed_matrix_detail::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 Aleph::packed_matrix_detail::throw_bad_value (const size_t j, const std::uint64_t v, const std::int64_t lo, const std::int64_t hi)
 Report an unsigned value above the highest std::int64_t.
 

Variables

constexpr size_t Aleph::packed_matrix_detail::max_columns = 65535
 Most columns of a row: Layout keeps the index of the word of a column in 16 bits.
 
template<class... Vs>
constexpr bool Aleph::packed_matrix_detail::all_column_integers
 Test whether make() and fast_make() take every type of a pack.
 
template<class T >
constexpr bool Aleph::packed_matrix_detail::is_column_array = false
 Test whether a type is an array of columns that a row can take.
 

Detailed Description

Matrix whose columns are bit fields sized from integer ranges.

Aleph::Packed_Matrix keeps one small integer per column in every row. The inclusive range of each column is part of the type, and from it the compiler works out how many bits the column takes and where they go:

using Book = Packed_Matrix<between(-1, 3), bits(4), between(0, 1000)>;
static_assert(sizeof(Book::Row) == 4); // 3 + 4 + 10 = 17 bits
Book m(1000); // 1000 rows
m(7, 2) = 250; // unchecked
m[7][2] = 250; // checked: indexes and value
m(8) = Book::Row::make(-1, 15, 0); // a whole row at once
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
bool between(const Point &a, const Point &b, const Point &c)
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)

A column that holds the integers of [lo, hi] takes std::bit_width(hi - lo) bits, the fewest that tell its values apart, and stores v - lo in them. A row is rounded up to one word of 8, 16, 32 or 64 bits, or to several words of 64 bits when it needs more than 64; a column never straddles two words. Every shift and mask is a compile-time constant, so reading a column whose index is a constant costs a load, a shift and a mask.

As in Aleph::Array, operator() does not check and operator[] does.

With many columns, Packed_Row_Of and Packed_Matrix_Of take them from a std::array of Bit_Column, which code can build:

constexpr auto columns = [] {
std::array<Bit_Column, 300> c{};
for (size_t j = 0; j < c.size(); ++j)
c[j] = j % 3 == 0 ? bits(1) : j % 3 == 1 ? bits(8) : signed_bits(11);
return c;
}();
using Wide = Packed_Matrix_Of<columns>; // 300 columns
See also
Packed_Row
Packed_Matrix
Packed_Row_Of
Packed_Matrix_Of

Definition in file tpl_packed_matrix.H.