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

Matrix whose columns are bit fields: an Aleph::Array of Packed_Row. More...

#include <tpl_packed_matrix.H>

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

Public Types

using Row = Packed_Row< Cols... >
 Type of a row.
 

Public Member Functions

 Packed_Matrix (const size_t num_rows=0)
 Build a matrix whose columns are all at their lowest value.
 
 Packed_Matrix (const size_t num_rows, const Row &row)
 Build a matrix whose rows are all equal to a given one.
 
size_t rows () const noexcept
 Number of rows.
 
Row & operator() (const size_t i) &noexcept
 Access a row without checking.
 
const Row & operator() (const size_t i) const &noexcept
 Read a row without checking.
 
Row operator() (const size_t i) &&noexcept
 Copy a row of a temporary matrix, without checking.
 
Row::Fast_Cell operator() (const size_t i, const size_t j) &noexcept
 Access an entry without checking.
 
std::int64_t operator() (const size_t i, const size_t j) const &noexcept
 Read an entry without checking.
 
std::int64_t fast_read (const size_t i, const size_t j) const noexcept
 Read an entry without checking.
 
void fast_write (const size_t i, const size_t j, const std::int64_t v) noexcept
 Write an entry without checking.
 
Row & operator[] (const size_t i) &
 Access a row.
 
const Row & operator[] (const size_t i) const &
 Read a row.
 
Row operator[] (const size_t i) &&
 Copy a row of a temporary matrix.
 
std::int64_t read (const size_t i, const size_t j) const
 Read an entry.
 
void write (const size_t i, const size_t j, const std::int64_t v)
 Write an entry.
 
void fill (const Row &row) noexcept
 Set every row to a given one.
 
void swap (Packed_Matrix &m) noexcept
 Exchange the rows of two matrices in constant time.
 
void prefetch (const size_t i) const noexcept
 Hint the processor to bring a row into the cache.
 
Row * data () noexcept
 The rows as a contiguous array.
 
const Row * data () const noexcept
 The rows as a contiguous array.
 
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.
 
Row * end () noexcept
 End of the rows.
 
const Row * end () const noexcept
 End of the rows.
 

Static Public Member Functions

static constexpr size_t cols () noexcept
 Number of columns.
 

Private Member Functions

void check_row (const size_t i) const
 Validate a row index.
 

Private Attributes

Array< Row > rows_
 The rows.
 

Friends

bool operator== (const Packed_Matrix &a, const Packed_Matrix &b) noexcept
 Test whether two matrices hold the same rows.
 

Detailed Description

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

Matrix whose columns are bit fields: an Aleph::Array of Packed_Row.

The number of rows is given at construction; the columns and the integers that each one holds are part of the type. A row takes sizeof(Row) bytes: one word of 1, 2, 4 or 8 bytes when its columns fit in 64 bits, and words of 8 bytes otherwise. A column that does not fit in what is left of a word starts the next one, and leaves the rest of that word unused: Packed_Row<bits(3), bits(62), bits(3)> takes 24 bytes, not 16.

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

Access Checks
m(i, j), m(i), fast_read(), fast_write() nothing; asserted in Debug builds
m[i][j], m[i], read(), write() the indexes, and the value on writing
Writing several columns of a row
Build the row with Row::make() or Row::fast_make() and store it, or work on a copy (Row r = m(i); ...; m(i) = r;). Writing the columns one by one through the matrix loads and stores the row once per column.
Matrices that do not fit in the cache
Every access to a row that is not in the cache waits for it, and that wait dwarfs the shifts and masks. If the rows of a pass are known some steps ahead, call prefetch() on each one before reaching it.
Example
enum Col : size_t { side, level, qty };
using Book = Packed_Matrix<between(-1, 1), bits(4), between(0, 1000)>;
Book m(1000); // every column at its lowest value
m(7, qty) = 250; // unchecked
m[7][level] = 9; // checked
m(8) = Book::Row::make(1, 3, 999); // a whole row
const std::int64_t q = m(7, qty);
Matrix whose columns are bit fields: an Aleph::Array of Packed_Row.
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)
Template Parameters
ColsRanges of the columns, in order: values of Bit_Column built with between(), bits() or signed_bits().
Note
The rows live in an Aleph::Array, which reserves the next power of two of slots. The slots beyond rows() are never written.
Two threads may write different rows at the same time: every row has its own words.
See also
Packed_Row
Packed_Matrix_Of

Definition at line 864 of file tpl_packed_matrix.H.

Member Typedef Documentation

◆ Row

template<Bit_Column... Cols>
using Aleph::Packed_Matrix< Cols >::Row = Packed_Row<Cols...>

Type of a row.

Definition at line 868 of file tpl_packed_matrix.H.

Constructor & Destructor Documentation

◆ Packed_Matrix() [1/2]

template<Bit_Column... Cols>
Aleph::Packed_Matrix< Cols >::Packed_Matrix ( const size_t  num_rows = 0)
inlineexplicit

Build a matrix whose columns are all at their lowest value.

Parameters
[in]num_rowsNumber of rows.
Exceptions
std::bad_allocif there is not enough memory.
std::overflow_errorif the size of the rows does not fit in size_t.
Note
O(num_rows).

Definition at line 894 of file tpl_packed_matrix.H.

◆ Packed_Matrix() [2/2]

template<Bit_Column... Cols>
Aleph::Packed_Matrix< Cols >::Packed_Matrix ( const size_t  num_rows,
const Row &  row 
)
inline

Build a matrix whose rows are all equal to a given one.

Parameters
[in]num_rowsNumber of rows.
[in]rowThe row to copy.
Exceptions
std::bad_allocif there is not enough memory.
std::overflow_errorif the size of the rows does not fit in size_t.
Note
O(num_rows).

Definition at line 906 of file tpl_packed_matrix.H.

Member Function Documentation

◆ begin() [1/2]

template<Bit_Column... Cols>
const Row * Aleph::Packed_Matrix< Cols >::begin ( ) const
inlinenoexcept

Start of the rows, for a range-based for.

Returns
A pointer to row 0; equal to end() if there are no rows.

Definition at line 1153 of file tpl_packed_matrix.H.

References Aleph::Packed_Matrix< Cols >::data().

◆ begin() [2/2]

template<Bit_Column... Cols>
Row * Aleph::Packed_Matrix< Cols >::begin ( )
inlinenoexcept

Start of the rows, for a range-based for.

Returns
A pointer to row 0; equal to end() if there are no rows.

Definition at line 1145 of file tpl_packed_matrix.H.

References Aleph::Packed_Matrix< Cols >::data().

◆ check_row()

template<Bit_Column... Cols>
void Aleph::Packed_Matrix< Cols >::check_row ( const size_t  i) const
inlineprivate

◆ cols()

template<Bit_Column... Cols>
static constexpr size_t Aleph::Packed_Matrix< Cols >::cols ( )
inlinestaticconstexprnoexcept

Number of columns.

Returns
Row::num_cols.

Definition at line 921 of file tpl_packed_matrix.H.

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

◆ data() [1/2]

template<Bit_Column... Cols>
const Row * Aleph::Packed_Matrix< Cols >::data ( ) const
inlinenoexcept

The rows as a contiguous array.

Returns
A pointer to row 0, or null if there are no rows.

Definition at line 1137 of file tpl_packed_matrix.H.

References Aleph::Packed_Matrix< Cols >::rows_.

◆ data() [2/2]

template<Bit_Column... Cols>
Row * Aleph::Packed_Matrix< Cols >::data ( )
inlinenoexcept

The rows as a contiguous array.

Returns
A pointer to row 0, or null if there are no rows. Rows are trivially copyable: rows()*sizeof(Row) bytes from here hold the whole matrix, in the byte order of the machine.

Definition at line 1129 of file tpl_packed_matrix.H.

References Aleph::Packed_Matrix< Cols >::rows_.

Referenced by Aleph::Packed_Matrix< Cols >::begin(), Aleph::Packed_Matrix< Cols >::begin(), Aleph::Packed_Matrix< Cols >::end(), and Aleph::Packed_Matrix< Cols >::end().

◆ end() [1/2]

template<Bit_Column... Cols>
const Row * Aleph::Packed_Matrix< Cols >::end ( ) const
inlinenoexcept

End of the rows.

Returns
A pointer past the last row.

Definition at line 1169 of file tpl_packed_matrix.H.

References Aleph::Packed_Matrix< Cols >::data(), and Aleph::Packed_Matrix< Cols >::rows_.

◆ end() [2/2]

template<Bit_Column... Cols>
Row * Aleph::Packed_Matrix< Cols >::end ( )
inlinenoexcept

End of the rows.

Returns
A pointer past the last row.

Definition at line 1161 of file tpl_packed_matrix.H.

References Aleph::Packed_Matrix< Cols >::data(), and Aleph::Packed_Matrix< Cols >::rows_.

◆ fast_read()

template<Bit_Column... Cols>
std::int64_t Aleph::Packed_Matrix< Cols >::fast_read ( const size_t  i,
const size_t  j 
) const
inlinenoexcept

Read an entry without checking.

Parameters
[in]iThe row; must be less than rows() (asserted in Debug builds).
[in]jThe column; must be less than cols() (asserted in Debug builds).
Returns
The value of the entry.

Definition at line 989 of file tpl_packed_matrix.H.

References Aleph::Packed_Matrix< Cols >::rows_.

◆ fast_write()

template<Bit_Column... Cols>
void Aleph::Packed_Matrix< Cols >::fast_write ( const size_t  i,
const size_t  j,
const std::int64_t  v 
)
inlinenoexcept

Write an entry without checking.

Parameters
[in]iThe row; must be less than rows() (asserted in Debug builds).
[in]jThe column; must be less than 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 the entry and does not touch the others.

Definition at line 1004 of file tpl_packed_matrix.H.

References Aleph::Packed_Matrix< Cols >::rows_.

◆ fill()

template<Bit_Column... Cols>
void Aleph::Packed_Matrix< Cols >::fill ( const Row &  row)
inlinenoexcept

Set every row to a given one.

Parameters
[in]rowThe row to copy.
Note
O(rows()).

Definition at line 1074 of file tpl_packed_matrix.H.

References r, and row.

◆ operator()() [1/5]

template<Bit_Column... Cols>
Row Aleph::Packed_Matrix< Cols >::operator() ( const size_t  i) &&
inlinenoexcept

Copy a row of a temporary matrix, without checking.

Parameters
[in]iThe row; must be less than rows() (asserted in Debug builds).
Returns
A copy of the row, which outlives the matrix.

Definition at line 951 of file tpl_packed_matrix.H.

References Aleph::Packed_Matrix< Cols >::rows_.

◆ operator()() [2/5]

template<Bit_Column... Cols>
Row & Aleph::Packed_Matrix< Cols >::operator() ( const size_t  i) &
inlinenoexcept

Access a row without checking.

Parameters
[in]iThe row; must be less than rows() (asserted in Debug builds).
Returns
The row.

Definition at line 931 of file tpl_packed_matrix.H.

References Aleph::Packed_Matrix< Cols >::rows_.

◆ operator()() [3/5]

template<Bit_Column... Cols>
const Row & Aleph::Packed_Matrix< Cols >::operator() ( const size_t  i) const &
inlinenoexcept

Read a row without checking.

Parameters
[in]iThe row; must be less than rows() (asserted in Debug builds).
Returns
The row.

Definition at line 941 of file tpl_packed_matrix.H.

References Aleph::Packed_Matrix< Cols >::rows_.

◆ operator()() [4/5]

template<Bit_Column... Cols>
Row::Fast_Cell Aleph::Packed_Matrix< Cols >::operator() ( const size_t  i,
const size_t  j 
) &
inlinenoexcept

Access an entry without checking.

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

Definition at line 963 of file tpl_packed_matrix.H.

References Aleph::Packed_Matrix< Cols >::rows_.

◆ operator()() [5/5]

template<Bit_Column... Cols>
std::int64_t Aleph::Packed_Matrix< Cols >::operator() ( const size_t  i,
const size_t  j 
) const &
inlinenoexcept

Read an entry without checking.

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

Definition at line 977 of file tpl_packed_matrix.H.

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

◆ operator[]() [1/3]

template<Bit_Column... Cols>
Row & Aleph::Packed_Matrix< Cols >::operator[] ( const size_t  i) &
inline

Access a row.

Parameters
[in]iThe row.
Returns
The row; its operator[] checks the column, and the value on writing.
Exceptions
std::out_of_rangeif i >= rows().

Definition at line 1015 of file tpl_packed_matrix.H.

References Aleph::Packed_Matrix< Cols >::check_row(), and Aleph::Packed_Matrix< Cols >::rows_.

◆ operator[]() [2/3]

template<Bit_Column... Cols>
Row Aleph::Packed_Matrix< Cols >::operator[] ( const size_t  i) &&
inline

Copy a row of a temporary matrix.

Parameters
[in]iThe row.
Returns
A copy of the row, which outlives the matrix; its operator[] checks the column.
Exceptions
std::out_of_rangeif i >= rows().

Definition at line 1038 of file tpl_packed_matrix.H.

References Aleph::Packed_Matrix< Cols >::check_row(), and Aleph::Packed_Matrix< Cols >::rows_.

◆ operator[]() [3/3]

template<Bit_Column... Cols>
const Row & Aleph::Packed_Matrix< Cols >::operator[] ( const size_t  i) const &
inline

Read a row.

Parameters
[in]iThe row.
Returns
The row; its operator[] checks the column.
Exceptions
std::out_of_rangeif i >= rows().

Definition at line 1026 of file tpl_packed_matrix.H.

References Aleph::Packed_Matrix< Cols >::check_row(), and Aleph::Packed_Matrix< Cols >::rows_.

◆ prefetch()

template<Bit_Column... Cols>
void Aleph::Packed_Matrix< Cols >::prefetch ( const size_t  i) const
inlinenoexcept

Hint the processor to bring a row into the cache.

On a matrix that does not fit in the cache, the first access to a row waits for main memory. When the rows of a pass are known some steps before they are used, hinting each one that many steps ahead lets its wait overlap the work on the rows before it:

for (size_t k = 0; k < n; ++k)
{
if (k + 16 < n)
m.prefetch(index[k + 16]);
use(m(index[k]));
}
static int * k
Parameters
[in]iThe row; must be less than rows() (asserted in Debug builds).
Note
It reads nothing and changes nothing. Where the compiler has no such hint, it does nothing.

Definition at line 1109 of file tpl_packed_matrix.H.

References offset, row, and Aleph::Packed_Matrix< Cols >::rows_.

◆ read()

template<Bit_Column... Cols>
std::int64_t Aleph::Packed_Matrix< Cols >::read ( const size_t  i,
const size_t  j 
) const
inline

Read an entry.

Parameters
[in]iThe row.
[in]jThe column.
Returns
The value of the entry.
Exceptions
std::out_of_rangeif i >= rows() or j >= cols().

Definition at line 1050 of file tpl_packed_matrix.H.

References Aleph::Packed_Matrix< Cols >::check_row(), and Aleph::Packed_Matrix< Cols >::rows_.

◆ rows()

template<Bit_Column... Cols>
size_t Aleph::Packed_Matrix< Cols >::rows ( ) const
inlinenoexcept

Number of rows.

Returns
The number of rows.

Definition at line 913 of file tpl_packed_matrix.H.

References Aleph::Packed_Matrix< Cols >::rows_.

◆ swap()

template<Bit_Column... Cols>
void Aleph::Packed_Matrix< Cols >::swap ( Packed_Matrix< Cols > &  m)
inlinenoexcept

Exchange the rows of two matrices in constant time.

Parameters
[in,out]mThe other matrix.

Definition at line 1083 of file tpl_packed_matrix.H.

References m, and Aleph::Packed_Matrix< Cols >::rows_.

◆ write()

template<Bit_Column... Cols>
void Aleph::Packed_Matrix< Cols >::write ( const size_t  i,
const size_t  j,
const std::int64_t  v 
)
inline

Write an entry.

Parameters
[in]iThe row.
[in]jThe column.
[in]vThe value.
Exceptions
std::out_of_rangeif i >= rows() or j >= cols().
std::domain_errorif v is outside the range of column j.
Note
If it throws, the matrix does not change.

Definition at line 1064 of file tpl_packed_matrix.H.

References Aleph::Packed_Matrix< Cols >::check_row(), and Aleph::Packed_Matrix< Cols >::rows_.

Friends And Related Symbol Documentation

◆ operator==

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

Test whether two matrices hold the same rows.

Parameters
[in]aA matrix.
[in]bAnother matrix.
Returns
True if they have as many rows and every row of a equals that of b.
Note
O(rows()).

Definition at line 1181 of file tpl_packed_matrix.H.

Member Data Documentation

◆ rows_


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