MTL5 -- Matrix Template Library C++ API
mtl:: -- C++20 header-only linear algebra for mixed-precision algorithm design
Loading...
Searching...
No Matches
mtl::io::detail Namespace Reference

Classes

class  mm_reader
 Line source for Matrix Market input: reads from a plain file, or transparently from a gzip-compressed ".gz" file when built with zlib. More...
struct  mm_header
 The banner %MatrixMarket matrix <format> <field> <symmetry>. More...
struct  mm_sizes
 Everything after the banner up to the data: comment lines, then the size line, checked in full. More...
struct  spy_grid
 Binned grid: for each of W*H pixel cells, the non-zero count and the largest |value| that fell in it. More...
class  token_cursor
 Splits a line into whitespace-separated tokens, one at a time. More...

Functions

mm_header parse_mm_banner (const std::string &banner_line)
 Parse the banner.
bool checked_mul (std::size_t a, std::size_t b, std::size_t &out, std::size_t max=std::numeric_limits< std::size_t >::max()) noexcept
 a * b, or false if it overflows max.
std::size_t mm_value_count (const mm_header &h, std::size_t nrows, std::size_t ncols, std::size_t max, const char *who)
 The number of values an array-format file holds, which is also the most entries a coordinate file can have without duplicates: m*n for general, n(n+1)/2 for symmetric and hermitian, n(n-1)/2 for skew-symmetric (whose diagonal is zero and not stored).
template<typename Value>
void check_mm_field (const mm_header &h, const char *who)
 Checks that Value can hold the file's field.
bool next_data_line (mm_reader &in, std::string &line)
 The next non-blank line, or false at EOF.
template<typename Value>
bool read_mm_value (token_cursor &cur, const mm_header &h, Value &v)
 Reads one value (two numbers for a complex field) from the cursor.
template<typename Value>
Value mm_mirror (const mm_header &h, const Value &v)
 The mirrored value at (j, i) for a stored (i, j), i != j.
mm_sizes read_mm_sizes (mm_reader &in, const mm_header &h, std::size_t max_index, const std::string &filename, const char *who)
void check_mm_trailer (mm_reader &in, std::size_t expected, const char *who)
 After the data: only blank and comment lines may follow.
template<typename Value, typename F>
void read_mm_array (mm_reader &in, const mm_header &h, const mm_sizes &z, const char *who, F &&f)
 Visits the array-format values in file order (column-major; for a symmetric, skew-symmetric or hermitian file, the lower triangle by columns, without the diagonal when skew), as f(i, j, v).
template<typename Value, typename F>
void read_mm_coordinate (mm_reader &in, const mm_header &h, const mm_sizes &z, const char *who, F &&f)
 Visits the coordinate entries as f(i, j, v), 0-based, after checking each line and index.
template<typename Value>
constexpr const char * mm_field_name ()
 The banner field for a value type: complex for std::complex, integer for integral types, real otherwise.
template<typename Value>
void set_mm_precision (std::ostream &out)
 Enough digits for the value to read back exactly.
template<typename T>
void write_mm_number (std::ostream &out, const T &v)
 One number; an integral one is promoted, so a char-sized type prints as a number rather than a character.
template<typename Value>
void write_mm_value (std::ostream &out, const Value &v)
 One value: a complex one as "re im".
const std::array< std::uint32_t, 256 > & crc32_table ()
 CRC-32 lookup table (reflected polynomial 0xEDB88320), built once.
std::uint32_t crc32_add (std::uint32_t crc, const std::uint8_t *buf, std::size_t len)
 Fold bytes into a running CRC-32 register (internal, pre-inverted form: start from 0xFFFFFFFF and XOR 0xFFFFFFFF once at the end).
std::uint32_t adler32 (const std::uint8_t *data, std::size_t len)
 Adler-32 (zlib checksum) of a byte buffer.
void put_u32_be (std::vector< std::uint8_t > &v, std::uint32_t x)
std::vector< std::uint8_t > zlib_store (const std::vector< std::uint8_t > &raw)
 Wrap raw bytes into a zlib stream of DEFLATE stored blocks (no compression).
void write_chunk (std::ofstream &os, const char type[4], const std::vector< std::uint8_t > &data)
 Write one PNG chunk: length (BE) + type + data + CRC-32(type+data).
void write_png (const std::filesystem::path &path, const std::uint8_t *pixels, std::size_t w, std::size_t h, int channels)
 Core writer: channels is 1 (grayscale) or 3 (RGB); pixels is row-major, w*h*channels bytes, top row first.
bool split_fields (std::string_view line, char delimiter, std::vector< std::string_view > &fields)
 The fields of one line.
std::size_t scale_index (std::size_t idx, std::size_t span, std::size_t dim)
 floor(idx * span / dim) for idx < dim and span <= dim, without overflowing the intermediate product for huge logical dimensions.
template<typename M>
spy_grid< magnitude_t< typename M::value_type > > build_spy_grid (const M &A, std::size_t max_pixels)
void colormap (double t, std::uint8_t &r, std::uint8_t &gc, std::uint8_t &b)
 Compact viridis-like colormap: t in [0,1] -> (r,g,b), 8-bit.
bool is_space (char ch) noexcept
bool is_blank (std::string_view line) noexcept
 True if the line holds nothing but whitespace.
bool parse_index (std::string_view tok, std::size_t &out, std::size_t max=std::numeric_limits< std::size_t >::max()) noexcept
 A non-negative decimal integer, the whole token.
template<typename T>
bool parse_float_strto (const char *first, const char *last, T &out)
 The strtof/strtod/strtold reading of [first, last), a token without a leading '+': parse_value's path where std::from_chars is missing, or where it reports a value out of range.
template<typename T>
bool parse_value (std::string_view tok, T &out)
 One value, the whole token.

Function Documentation

◆ adler32()

std::uint32_t mtl::io::detail::adler32 ( const std::uint8_t * data,
std::size_t len )
inline

Adler-32 (zlib checksum) of a byte buffer.

◆ build_spy_grid()

template<typename M>
spy_grid< magnitude_t< typename M::value_type > > mtl::io::detail::build_spy_grid ( const M & A,
std::size_t max_pixels )

◆ check_mm_field()

template<typename Value>
void mtl::io::detail::check_mm_field ( const mm_header & h,
const char * who )

Checks that Value can hold the file's field.

◆ check_mm_trailer()

void mtl::io::detail::check_mm_trailer ( mm_reader & in,
std::size_t expected,
const char * who )
inline

After the data: only blank and comment lines may follow.

◆ checked_mul()

bool mtl::io::detail::checked_mul ( std::size_t a,
std::size_t b,
std::size_t & out,
std::size_t max = std::numeric_limits<std::size_t>::max() )
inlinenoexcept

a * b, or false if it overflows max.

◆ colormap()

void mtl::io::detail::colormap ( double t,
std::uint8_t & r,
std::uint8_t & gc,
std::uint8_t & b )
inline

Compact viridis-like colormap: t in [0,1] -> (r,g,b), 8-bit.

◆ crc32_add()

std::uint32_t mtl::io::detail::crc32_add ( std::uint32_t crc,
const std::uint8_t * buf,
std::size_t len )
inline

Fold bytes into a running CRC-32 register (internal, pre-inverted form: start from 0xFFFFFFFF and XOR 0xFFFFFFFF once at the end).

◆ crc32_table()

const std::array< std::uint32_t, 256 > & mtl::io::detail::crc32_table ( )
inline

CRC-32 lookup table (reflected polynomial 0xEDB88320), built once.

◆ is_blank()

bool mtl::io::detail::is_blank ( std::string_view line)
inlinenoexcept

True if the line holds nothing but whitespace.

◆ is_space()

bool mtl::io::detail::is_space ( char ch)
inlinenoexcept

◆ mm_field_name()

template<typename Value>
const char * mtl::io::detail::mm_field_name ( )
constexpr

The banner field for a value type: complex for std::complex, integer for integral types, real otherwise.

◆ mm_mirror()

template<typename Value>
Value mtl::io::detail::mm_mirror ( const mm_header & h,
const Value & v )

The mirrored value at (j, i) for a stored (i, j), i != j.

◆ mm_value_count()

std::size_t mtl::io::detail::mm_value_count ( const mm_header & h,
std::size_t nrows,
std::size_t ncols,
std::size_t max,
const char * who )
inline

The number of values an array-format file holds, which is also the most entries a coordinate file can have without duplicates: m*n for general, n(n+1)/2 for symmetric and hermitian, n(n-1)/2 for skew-symmetric (whose diagonal is zero and not stored).

Throws if it overflows max.

◆ next_data_line()

bool mtl::io::detail::next_data_line ( mm_reader & in,
std::string & line )
inline

The next non-blank line, or false at EOF.

◆ parse_float_strto()

template<typename T>
bool mtl::io::detail::parse_float_strto ( const char * first,
const char * last,
T & out )

The strtof/strtod/strtold reading of [first, last), a token without a leading '+': parse_value's path where std::from_chars is missing, or where it reports a value out of range.

strto* also reads hexadecimal floats ("0x1p3"), which from_chars in its general format does not, so they are refused here: the grammar is the same either way (#855). strto* would also skip leading whitespace, but a token has none.

◆ parse_index()

bool mtl::io::detail::parse_index ( std::string_view tok,
std::size_t & out,
std::size_t max = std::numeric_limits<std::size_t>::max() )
inlinenoexcept

A non-negative decimal integer, the whole token.

Rejects a sign, other characters, and values above max.

◆ parse_mm_banner()

mm_header mtl::io::detail::parse_mm_banner ( const std::string & banner_line)
inline

Parse the banner.

All five tokens are required and matched exactly (case insensitive): symmetric is a substring of skew-symmetric, so substring matching is unsafe, and a missing token used to default silently (#843). pattern is not supported, and hermitian needs a complex field.

◆ parse_value()

template<typename T>
bool mtl::io::detail::parse_value ( std::string_view tok,
T & out )

One value, the whole token.

◆ put_u32_be()

void mtl::io::detail::put_u32_be ( std::vector< std::uint8_t > & v,
std::uint32_t x )
inline

◆ read_mm_array()

template<typename Value, typename F>
void mtl::io::detail::read_mm_array ( mm_reader & in,
const mm_header & h,
const mm_sizes & z,
const char * who,
F && f )

Visits the array-format values in file order (column-major; for a symmetric, skew-symmetric or hermitian file, the lower triangle by columns, without the diagonal when skew), as f(i, j, v).

◆ read_mm_coordinate()

template<typename Value, typename F>
void mtl::io::detail::read_mm_coordinate ( mm_reader & in,
const mm_header & h,
const mm_sizes & z,
const char * who,
F && f )

Visits the coordinate entries as f(i, j, v), 0-based, after checking each line and index.

Mirroring is left to the caller.

◆ read_mm_sizes()

mm_sizes mtl::io::detail::read_mm_sizes ( mm_reader & in,
const mm_header & h,
std::size_t max_index,
const std::string & filename,
const char * who )
inline

◆ read_mm_value()

template<typename Value>
bool mtl::io::detail::read_mm_value ( token_cursor & cur,
const mm_header & h,
Value & v )

Reads one value (two numbers for a complex field) from the cursor.

◆ scale_index()

std::size_t mtl::io::detail::scale_index ( std::size_t idx,
std::size_t span,
std::size_t dim )
inline

floor(idx * span / dim) for idx < dim and span <= dim, without overflowing the intermediate product for huge logical dimensions.

◆ set_mm_precision()

template<typename Value>
void mtl::io::detail::set_mm_precision ( std::ostream & out)

Enough digits for the value to read back exactly.

◆ split_fields()

bool mtl::io::detail::split_fields ( std::string_view line,
char delimiter,
std::vector< std::string_view > & fields )
inline

The fields of one line.

With a space delimiter, any run of whitespace separates fields. With another delimiter, the line is split on exactly that character and each field is trimmed, so an empty field ("1,,2") is an error rather than a dropped column.

◆ write_chunk()

void mtl::io::detail::write_chunk ( std::ofstream & os,
const char type[4],
const std::vector< std::uint8_t > & data )
inline

Write one PNG chunk: length (BE) + type + data + CRC-32(type+data).

◆ write_mm_number()

template<typename T>
void mtl::io::detail::write_mm_number ( std::ostream & out,
const T & v )

One number; an integral one is promoted, so a char-sized type prints as a number rather than a character.

◆ write_mm_value()

template<typename Value>
void mtl::io::detail::write_mm_value ( std::ostream & out,
const Value & v )

One value: a complex one as "re im".

◆ write_png()

void mtl::io::detail::write_png ( const std::filesystem::path & path,
const std::uint8_t * pixels,
std::size_t w,
std::size_t h,
int channels )
inline

Core writer: channels is 1 (grayscale) or 3 (RGB); pixels is row-major, w*h*channels bytes, top row first.

◆ zlib_store()

std::vector< std::uint8_t > mtl::io::detail::zlib_store ( const std::vector< std::uint8_t > & raw)
inline

Wrap raw bytes into a zlib stream of DEFLATE stored blocks (no compression).