dasmig-core 1.2.0
Shared random engine, distributions and data tiers for the dasmig generator libraries
Loading...
Searching...
No Matches
dasmig::core::layered_table Class Reference

A table assembled from layers of TSV data. More...

#include <table.hpp>

Public Member Functions

 layered_table (table_schema schema)
 An empty table.
 
const layer_info & extend (tsv_document doc, layer_mode mode=layer_mode::override, const std::function< void(const tsv_row &)> &validate={})
 Apply a layer.
 
void clear () noexcept
 Remove every row and layer.
 
const std::vector< tsv_row > & rows () const noexcept
 The rows in effect, in order.
 
std::size_t size () const noexcept
 Number of rows in effect.
 
bool empty () const noexcept
 Whether no rows are in effect.
 
std::optional< tsv_row > find (std::initializer_list< std::string_view > key) const
 Find a row by key.
 
const std::vector< layer_info > & layers () const noexcept
 The layers in effect, in order: the last replace layer and those after it.
 
std::uint64_t data_hash () const noexcept
 Hash of the loaded layers: their content hashes and modes, in order. 0 when nothing is loaded.
 
const table_schema & schema () const noexcept
 The schema.
 

Static Public Attributes

static constexpr std::string_view disabled_column = "disabled"
 Name of the column that removes a key when set to 1.
 

Detailed Description

A table assembled from layers of TSV data.

  • Layers apply in the order loaded; later layers win.
  • Rows keep the order they first appeared in: an overriding row takes the place of the row it replaces, new rows are appended. Row order is part of the output (weighted choices depend on it).
  • A row with disabled set to 1 removes its key (override and replace modes). A layer needs the key columns; the other required columns are needed only by rows that add or replace data, so a layer that only removes rows can be just key and disabled.
  • A layer is validated completely before it is applied; when it fails, the table is unchanged.
dasmig::core::layered_table table{{.key = {"country_code"},
.required = {"male_height_cm"}}};
table.extend(dasmig::core::tsv_document::load("resources/lite/biodata.tsv"),
dasmig::core::layer_mode::replace);
table.extend(dasmig::core::tsv_document::load("mods/tall_people.tsv"));
for (const auto& row : table.rows()) { ... row.get<double>("male_height_cm") ... }
A table assembled from layers of TSV data.
Definition table.hpp:95
const layer_info & extend(tsv_document doc, layer_mode mode=layer_mode::override, const std::function< void(const tsv_row &)> &validate={})
Apply a layer.
Definition table.hpp:125
const std::vector< tsv_row > & rows() const noexcept
The rows in effect, in order.
Definition table.hpp:195
static tsv_document load(const std::filesystem::path &path, std::vector< std::string > columns={})
Read and parse a file.
Definition tsv.hpp:460

Definition at line 94 of file table.hpp.

Constructor & Destructor Documentation

◆ layered_table()

dasmig::core::layered_table::layered_table ( table_schema  schema)
inlineexplicit

An empty table.

Parameters
schemaThe columns.
Exceptions
std::invalid_argumentIf the schema has no key column.

Definition at line 103 of file table.hpp.

Member Function Documentation

◆ clear()

void dasmig::core::layered_table::clear ( )
inlinenoexcept

Remove every row and layer.

Definition at line 185 of file table.hpp.

◆ data_hash()

std::uint64_t dasmig::core::layered_table::data_hash ( ) const
inlinenoexcept

Hash of the loaded layers: their content hashes and modes, in order. 0 when nothing is loaded.

Put it in the seed_signature of results so a replay can tell whether it runs against the same data.

Returns
The hash.

Definition at line 240 of file table.hpp.

◆ empty()

bool dasmig::core::layered_table::empty ( ) const
inlinenoexcept

Whether no rows are in effect.

Returns
True when empty.

Definition at line 203 of file table.hpp.

◆ extend()

const layer_info & dasmig::core::layered_table::extend ( tsv_document  doc,
layer_mode  mode = layer_mode::override,
const std::function< void(const tsv_row &)> &  validate = {} 
)
inline

Apply a layer.

validate is called for every row the layer adds or replaces (not for disabled rows) before anything changes; it should parse the row the way the library will and throw tsv_error on bad data.

Parameters
docThe layer's data.
modeHow it combines with the rows loaded before.
validateOptional void(const tsv_row&) row check.
Returns
What the layer did.
Exceptions
tsv_errorOn a missing key column, a missing required column (for a row that adds or replaces data), an empty or repeated key, an invalid disabled value, a clash in add mode, or an error from validate. The table is then unchanged.

Definition at line 125 of file table.hpp.

◆ find()

std::optional< tsv_row > dasmig::core::layered_table::find ( std::initializer_list< std::string_view >  key) const
inline

Find a row by key.

Parameters
keyThe key cells, in schema key order.
Returns
The row, or empty.
Exceptions
std::invalid_argumentIf the number of cells is not the number of key columns.

Definition at line 210 of file table.hpp.

◆ layers()

const std::vector< layer_info > & dasmig::core::layered_table::layers ( ) const
inlinenoexcept

The layers in effect, in order: the last replace layer and those after it.

Returns
The layer records.

Definition at line 231 of file table.hpp.

◆ rows()

const std::vector< tsv_row > & dasmig::core::layered_table::rows ( ) const
inlinenoexcept

The rows in effect, in order.

Returns
The rows.

Definition at line 195 of file table.hpp.

◆ schema()

const table_schema & dasmig::core::layered_table::schema ( ) const
inlinenoexcept

The schema.

Returns
The columns.

Definition at line 252 of file table.hpp.

◆ size()

std::size_t dasmig::core::layered_table::size ( ) const
inlinenoexcept

Number of rows in effect.

Returns
The count.

Definition at line 199 of file table.hpp.

Member Data Documentation

◆ disabled_column

constexpr std::string_view dasmig::core::layered_table::disabled_column = "disabled"
staticconstexpr

Name of the column that removes a key when set to 1.

Definition at line 98 of file table.hpp.


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