Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Packed_Row< Cols > Class Template Reference

Tuple of small integers packed into the fewest whole words. More...

#include <tpl_packed_matrix.H>

Collaboration diagram for Aleph::Packed_Row< Cols >:
[legend]

Classes

class  Proxy
 Proxy of a column of a row that is not const. More...
 

Public Types

using Fast_Cell = Proxy< false >
 What r(j) returns on a row that is not const: writing does not check.
 
using Cell = Proxy< true >
 What r[j] returns on a row that is not const: writing validates the value.
 

Public Member Functions

constexpr std::int64_t fast_read (const size_t j) const noexcept
 Read a column without checking.
 
constexpr void fast_write (const size_t j, const std::int64_t v) noexcept
 Write a column without checking.
 
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.
 
constexpr std::int64_t operator() (const size_t j) const &noexcept
 Read a column without checking.
 
constexpr Fast_Cell operator() (const size_t j) &noexcept
 Access a column without checking.
 
constexpr std::int64_t operator[] (const size_t j) const &
 Read a column.
 
constexpr Cell operator[] (const size_t j) &
 Access a column.
 

Static Public Member Functions

static constexpr std::int64_t column_min (const size_t j) noexcept
 Lowest value of a column.
 
static constexpr std::int64_t column_max (const size_t j) noexcept
 Highest value of a column.
 
static constexpr unsigned column_bits (const size_t j) noexcept
 Bits that a column takes.
 
static constexpr bool valid (const size_t j, const std::int64_t v) noexcept
 Test whether a column can hold a value.
 
static constexpr Packed_Row lowest () noexcept
 The row whose columns are all at their lowest value.
 
template<class... Vs>
requires (sizeof...(Vs) == sizeof...(Cols) and packed_matrix_detail::all_column_integers<Vs...>)
static constexpr Packed_Row fast_make (const Vs... vs) noexcept
 Build a row from the values of its columns, without checking.
 
template<class... Vs>
requires (sizeof...(Vs) == sizeof...(Cols) and packed_matrix_detail::all_column_integers<Vs...>)
static constexpr Packed_Row make (const Vs... vs)
 Build a row from the values of its columns.
 

Static Public Attributes

static constexpr size_t num_cols = sizeof...(Cols)
 Number of columns.
 
static constexpr size_t num_bits = layout.bits
 Sum of the bits of the columns; 8*sizeof(Packed_Row) rounds it up.
 

Private Types

using Word = packed_matrix_detail::Word_Of< layout.word_bits >
 Unsigned type of a word of the row.
 

Static Private Member Functions

static constexpr void check_value (const size_t j, const std::int64_t v)
 Validate a value for a column.
 
template<packed_matrix_detail::Column_Integer V>
static constexpr void check_argument (const size_t j, const V v)
 Validate an argument of make() for a column.
 
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.
 

Private Attributes

Word words [layout.words]
 The packed columns; the bits of no column are always zero.
 

Static Private Attributes

static constexpr auto layout
 Where every column lives: widths, shifts and masks.
 

Friends

constexpr bool operator== (const Packed_Row &a, const Packed_Row &b) noexcept
 Test whether two rows hold the same values.
 
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.
 

Detailed Description

template<Bit_Column... Cols>
class Aleph::Packed_Row< Cols >

Tuple of small integers packed into the fewest whole words.

It is the struct of bit fields that one would write by hand for a row, with the shifts and masks made explicit and worked out by the compiler from the ranges of the columns. Column j holds an integer of [Cols[j].lo, Cols[j].hi] in std::bit_width(hi - lo) bits.

A row is a value: it can be copied, compared and sorted. Rows compare as their tuples of values do, column 0 first, at the cost of comparing their words.

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

Access Checks
r(j), fast_read(), fast_write(), fast_make() nothing; asserted in Debug builds
r[j], read(), write(), make() the column, and the value on writing
Template Parameters
ColsRanges of the columns, in order: values of Bit_Column built with between(), bits() or signed_bits().
Note
The type is trivial on purpose, so that an array of rows does not write the slots it only reserves. As with an int, a row declared without an initializer has indeterminate bits: write Row r{}; or use lowest() to get every column at its lowest value.
On a row that is not const, r(j) and r[j] return a proxy: auto x = r(j); deduces the proxy, not an integer. Use fast_read() and read(), or name the type: std::int64_t x = r(j);. On a temporary row they return the value, which outlives it.
Columns of 64 bits are supported; values above the highest std::int64_t are not.
A row has from 1 to 65535 columns; with many of them, Packed_Row_Of takes them from an array that code can build. The compiler places them in a number of steps that grows with them: beyond some 13000 columns, Clang needs a larger budget of constant evaluation (-fconstexpr-steps=). make() and fast_make() take one argument per column, and with thousands of them a call compiles slowly (15 to 20 s with 2048 columns at -O2): write such a row column by column.
See also
Packed_Matrix
Packed_Row_Of

Definition at line 390 of file tpl_packed_matrix.H.

Member Typedef Documentation

◆ Cell

template<Bit_Column... Cols>
using Aleph::Packed_Row< Cols >::Cell = Proxy<true>

What r[j] returns on a row that is not const: writing validates the value.

Definition at line 573 of file tpl_packed_matrix.H.

◆ Fast_Cell

template<Bit_Column... Cols>
using Aleph::Packed_Row< Cols >::Fast_Cell = Proxy<false>

What r(j) returns on a row that is not const: writing does not check.

Definition at line 570 of file tpl_packed_matrix.H.

◆ Word

template<Bit_Column... Cols>
using Aleph::Packed_Row< Cols >::Word = packed_matrix_detail::Word_Of<layout.word_bits>
private

Unsigned type of a word of the row.

Definition at line 403 of file tpl_packed_matrix.H.

Member Function Documentation

◆ check_argument()

template<Bit_Column... Cols>
template<packed_matrix_detail::Column_Integer V>
static constexpr void Aleph::Packed_Row< Cols >::check_argument ( const size_t  j,
const V  v 
)
inlinestaticconstexprprivate

Validate an argument of make() for a column.

Template Parameters
VIts type.
Parameters
[in]jThe column, less than num_cols.
[in]vThe argument.
Exceptions
std::domain_errorif v is outside the range of column j, also if it is an unsigned integer above the highest std::int64_t, which it would wrap to.

Definition at line 532 of file tpl_packed_matrix.H.

References Aleph::Packed_Row< Cols >::check_value(), Aleph::packed_matrix_detail::fits_in_int64(), Aleph::Packed_Row< Cols >::layout, and Aleph::packed_matrix_detail::throw_bad_value().

Referenced by Aleph::Packed_Row< Cols >::make().

◆ check_column()

template<Bit_Column... Cols>
static constexpr void Aleph::Packed_Row< Cols >::check_column ( const size_t  j)
inlinestaticconstexprprivate

Validate a column index.

Parameters
[in]jThe index.
Exceptions
std::out_of_rangeif j >= num_cols.

Definition at line 544 of file tpl_packed_matrix.H.

References Aleph::Packed_Row< Cols >::num_cols, and Aleph::packed_matrix_detail::throw_bad_column().

Referenced by Aleph::Packed_Row< Cols >::operator[](), Aleph::Packed_Row< Cols >::read(), and Aleph::Packed_Row< Cols >::write().

◆ check_value()

template<Bit_Column... Cols>
static constexpr void Aleph::Packed_Row< Cols >::check_value ( const size_t  j,
const std::int64_t  v 
)
inlinestaticconstexprprivate

Validate a value for a column.

Parameters
[in]jThe column, less than num_cols.
[in]vThe value.
Exceptions
std::domain_errorif v is outside the range of column j.

Definition at line 517 of file tpl_packed_matrix.H.

References Aleph::Packed_Row< Cols >::layout, Aleph::packed_matrix_detail::throw_bad_value(), and Aleph::Packed_Row< Cols >::valid().

Referenced by Aleph::Packed_Row< Cols >::check_argument(), Aleph::Packed_Row< Cols >::Proxy< Checked >::operator=(), and Aleph::Packed_Row< Cols >::write().

◆ column_bits()

template<Bit_Column... Cols>
static constexpr unsigned Aleph::Packed_Row< Cols >::column_bits ( const size_t  j)
inlinestaticconstexprnoexcept

Bits that a column takes.

Parameters
[in]jThe column; must be less than num_cols (asserted in Debug builds).
Returns
std::bit_width(column_max(j) - column_min(j)), from 0 to 64.

Definition at line 602 of file tpl_packed_matrix.H.

References Aleph::Packed_Row< Cols >::layout, and Aleph::Packed_Row< Cols >::num_cols.

◆ column_max()

template<Bit_Column... Cols>
static constexpr std::int64_t Aleph::Packed_Row< Cols >::column_max ( const size_t  j)
inlinestaticconstexprnoexcept

Highest value of a column.

Parameters
[in]jThe column; must be less than num_cols (asserted in Debug builds).
Returns
The highest value that column j holds.

Definition at line 591 of file tpl_packed_matrix.H.

References Aleph::Packed_Row< Cols >::layout, and Aleph::Packed_Row< Cols >::num_cols.

◆ column_min()

template<Bit_Column... Cols>
static constexpr std::int64_t Aleph::Packed_Row< Cols >::column_min ( const size_t  j)
inlinestaticconstexprnoexcept

Lowest value of a column.

Parameters
[in]jThe column; must be less than num_cols (asserted in Debug builds).
Returns
The lowest value that column j holds.

Definition at line 580 of file tpl_packed_matrix.H.

References Aleph::Packed_Row< Cols >::layout, and Aleph::Packed_Row< Cols >::num_cols.

◆ fast_make()

template<Bit_Column... Cols>
template<class... Vs>
requires (sizeof...(Vs) == sizeof...(Cols) and packed_matrix_detail::all_column_integers<Vs...>)
static constexpr Packed_Row Aleph::Packed_Row< Cols >::fast_make ( const Vs...  vs)
inlinestaticconstexprnoexcept

Build a row from the values of its columns, without checking.

The fastest way of writing a whole row: the columns are packed in registers and the row is stored once.

Template Parameters
VsTypes of the values, one per column: integer types of at most 64 bits.
Parameters
[in]vsValues of the columns, in order. Each must be in the range of its column (asserted in Debug builds).
Returns
The row.
Note
A value outside its range leaves an unspecified value in its column and does not touch the others.

Definition at line 644 of file tpl_packed_matrix.H.

References Aleph::and, Aleph::Packed_Row< Cols >::field(), Aleph::packed_matrix_detail::fits_in_int64(), Aleph::Packed_Row< Cols >::layout, r, and Aleph::Packed_Row< Cols >::valid().

Referenced by Aleph::Packed_Row< Cols >::make().

◆ fast_read()

template<Bit_Column... Cols>
constexpr std::int64_t Aleph::Packed_Row< Cols >::fast_read ( const size_t  j) const
inlineconstexprnoexcept

◆ fast_write()

template<Bit_Column... Cols>
constexpr void Aleph::Packed_Row< Cols >::fast_write ( const size_t  j,
const std::int64_t  v 
)
inlineconstexprnoexcept

Write a column without checking.

Parameters
[in]jThe column; must be less than num_cols (asserted in Debug builds).
[in]vThe value; must be in the range of column j (asserted in Debug builds).
Note
A value outside the range leaves an unspecified value in column j and does not touch the others.

Definition at line 700 of file tpl_packed_matrix.H.

References Aleph::Packed_Row< Cols >::field(), Aleph::Packed_Row< Cols >::layout, m, Aleph::Packed_Row< Cols >::num_cols, Aleph::Packed_Row< Cols >::valid(), w, and Aleph::Packed_Row< Cols >::words.

Referenced by Aleph::Packed_Row< Cols >::Proxy< Checked >::operator=(), and Aleph::Packed_Row< Cols >::write().

◆ field()

template<Bit_Column... Cols>
static constexpr std::uint64_t Aleph::Packed_Row< Cols >::field ( const size_t  j,
const std::int64_t  v 
)
inlinestaticconstexprprivatenoexcept

The bits of a column that stand for a value, in their place.

Parameters
[in]jThe column, less than num_cols.
[in]vThe value.
Returns
The word that has those bits and no other.

Definition at line 555 of file tpl_packed_matrix.H.

References Aleph::Packed_Row< Cols >::layout, and offset.

Referenced by Aleph::Packed_Row< Cols >::fast_make(), and Aleph::Packed_Row< Cols >::fast_write().

◆ lowest()

template<Bit_Column... Cols>
static constexpr Packed_Row Aleph::Packed_Row< Cols >::lowest ( )
inlinestaticconstexprnoexcept

The row whose columns are all at their lowest value.

Returns
That row; all its bits are zero. The same as Packed_Row{}.

Definition at line 624 of file tpl_packed_matrix.H.

◆ make()

template<Bit_Column... Cols>
template<class... Vs>
requires (sizeof...(Vs) == sizeof...(Cols) and packed_matrix_detail::all_column_integers<Vs...>)
static constexpr Packed_Row Aleph::Packed_Row< Cols >::make ( const Vs...  vs)
inlinestaticconstexpr

Build a row from the values of its columns.

Template Parameters
VsTypes of the values, one per column: integer types of at most 64 bits.
Parameters
[in]vsValues of the columns, in order.
Returns
The row.
Exceptions
std::domain_errorif a value is outside the range of its column, also an unsigned one above the highest std::int64_t.

Definition at line 670 of file tpl_packed_matrix.H.

References Aleph::Packed_Row< Cols >::check_argument(), and Aleph::Packed_Row< Cols >::fast_make().

◆ operator()() [1/2]

template<Bit_Column... Cols>
constexpr Fast_Cell Aleph::Packed_Row< Cols >::operator() ( const size_t  j) &
inlineconstexprnoexcept

Access a column without checking.

Parameters
[in]jThe column; must be less than num_cols (asserted in Debug builds).
Returns
A proxy that reads the column and writes it as fast_write().

Definition at line 753 of file tpl_packed_matrix.H.

References Aleph::Packed_Row< Cols >::num_cols.

◆ operator()() [2/2]

template<Bit_Column... Cols>
constexpr std::int64_t Aleph::Packed_Row< Cols >::operator() ( const size_t  j) const &
inlineconstexprnoexcept

Read a column without checking.

Parameters
[in]jThe column; must be less than num_cols (asserted in Debug builds).
Returns
The value of column j.
Note
It is also the access to a column of a temporary row, so that no proxy outlives the row.

Definition at line 743 of file tpl_packed_matrix.H.

References Aleph::Packed_Row< Cols >::fast_read().

◆ operator[]() [1/2]

template<Bit_Column... Cols>
constexpr Cell Aleph::Packed_Row< Cols >::operator[] ( const size_t  j) &
inlineconstexpr

Access a column.

Parameters
[in]jThe column.
Returns
A proxy that reads the column and writes it as write().
Exceptions
std::out_of_rangeif j >= num_cols.

Definition at line 776 of file tpl_packed_matrix.H.

References Aleph::Packed_Row< Cols >::check_column().

◆ operator[]() [2/2]

template<Bit_Column... Cols>
constexpr std::int64_t Aleph::Packed_Row< Cols >::operator[] ( const size_t  j) const &
inlineconstexpr

Read a column.

Parameters
[in]jThe column.
Returns
The value of column j.
Exceptions
std::out_of_rangeif j >= num_cols.
Note
It is also the access to a column of a temporary row, so that no proxy outlives the row.

Definition at line 766 of file tpl_packed_matrix.H.

References Aleph::Packed_Row< Cols >::read().

◆ read()

template<Bit_Column... Cols>
constexpr std::int64_t Aleph::Packed_Row< Cols >::read ( const size_t  j) const
inlineconstexpr

Read a column.

Parameters
[in]jThe column.
Returns
The value of column j.
Exceptions
std::out_of_rangeif j >= num_cols.

Definition at line 716 of file tpl_packed_matrix.H.

References Aleph::Packed_Row< Cols >::check_column(), and Aleph::Packed_Row< Cols >::fast_read().

Referenced by Aleph::Packed_Row< Cols >::operator[]().

◆ valid()

template<Bit_Column... Cols>
static constexpr bool Aleph::Packed_Row< Cols >::valid ( const size_t  j,
const std::int64_t  v 
)
inlinestaticconstexprnoexcept

Test whether a column can hold a value.

Parameters
[in]jThe column.
[in]vThe value.
Returns
True if j < num_cols and v is in the range of column j.

Definition at line 613 of file tpl_packed_matrix.H.

References Aleph::and, Aleph::Packed_Row< Cols >::layout, and Aleph::Packed_Row< Cols >::num_cols.

Referenced by Aleph::Packed_Row< Cols >::check_value(), Aleph::Packed_Row< Cols >::fast_make(), and Aleph::Packed_Row< Cols >::fast_write().

◆ write()

template<Bit_Column... Cols>
constexpr void Aleph::Packed_Row< Cols >::write ( const size_t  j,
const std::int64_t  v 
)
inlineconstexpr

Write a column.

Parameters
[in]jThe column.
[in]vThe value.
Exceptions
std::out_of_rangeif j >= num_cols.
std::domain_errorif v is outside the range of column j.
Note
If it throws, the row does not change.

Definition at line 729 of file tpl_packed_matrix.H.

References Aleph::Packed_Row< Cols >::check_column(), Aleph::Packed_Row< Cols >::check_value(), and Aleph::Packed_Row< Cols >::fast_write().

Friends And Related Symbol Documentation

◆ operator<=>

template<Bit_Column... Cols>
constexpr std::strong_ordering operator<=> ( const Packed_Row< Cols > &  a,
const Packed_Row< Cols > &  b 
)
friend

Compare two rows as their tuples of values, column 0 first.

Parameters
[in]aA row.
[in]bAnother row.
Returns
The order of the first column where they differ, or equal.

Definition at line 800 of file tpl_packed_matrix.H.

◆ operator==

template<Bit_Column... Cols>
constexpr bool operator== ( const Packed_Row< Cols > &  a,
const Packed_Row< Cols > &  b 
)
friend

Test whether two rows hold the same values.

Parameters
[in]aA row.
[in]bAnother row.
Returns
True if every column of a equals that of b.

Definition at line 787 of file tpl_packed_matrix.H.

Member Data Documentation

◆ layout

template<Bit_Column... Cols>
constexpr auto Aleph::Packed_Row< Cols >::layout
staticconstexprprivate

◆ num_bits

template<Bit_Column... Cols>
constexpr size_t Aleph::Packed_Row< Cols >::num_bits = layout.bits
staticconstexpr

Sum of the bits of the columns; 8*sizeof(Packed_Row) rounds it up.

Definition at line 567 of file tpl_packed_matrix.H.

◆ num_cols

◆ words

template<Bit_Column... Cols>
Word Aleph::Packed_Row< Cols >::words[layout.words]
private

The packed columns; the bits of no column are always zero.

Definition at line 405 of file tpl_packed_matrix.H.

Referenced by Aleph::Packed_Row< Cols >::fast_read(), and Aleph::Packed_Row< Cols >::fast_write().


The documentation for this class was generated from the following file: