![]() |
Birth Generator 3.2.2
Procedural birth date generation for C++23
|
Birth generator that produces demographically plausible birthdays from country age pyramids. More...
#include <birthgen.hpp>
Public Types | |
| enum class | table : std::uint8_t { countries , age_pyramid , monthly_births } |
| The three data tables. More... | |
Public Member Functions | |
| bthg ()=default | |
| Default constructor — creates an empty generator with no data. | |
| bthg (const bthg &)=delete | |
| Not copyable. | |
| bthg & | operator= (const bthg &)=delete |
| Not copyable. | |
| bthg (bthg &&) noexcept=default | |
| Movable. | |
| bthg & | operator= (bthg &&) noexcept=default |
| Movable. | |
| ~bthg ()=default | |
| Destructor. | |
| birth | get_birth (const birth_request &request) |
| Generate a random birth using per-call filters and settings. | |
| birth | get_birth (const birth_request &request, std::uint64_t call_seed) const |
| Generate a deterministic birth using per-call filters and settings. | |
| std::vector< birth > | get_birth_batch (const birth_request &request, const core::batch_options &options) |
| Generate a batch of birth results from a seed drawn like get_birth() draws its own. | |
| std::vector< birth > | get_birth_batch (const birth_request &request, const core::batch_options &options, std::uint64_t batch_seed) const |
| Generate a batch of birth results, deterministically. | |
| template<typename Sink > | |
| void | for_each_birth (const birth_request &request, const core::batch_options &options, std::uint64_t batch_seed, Sink &&sink) const |
| Stream a batch of birth results to a sink without holding the whole batch. | |
| template<typename Sink > | |
| void | for_each_birth (const birth_request &request, const core::batch_options &options, Sink &&sink) |
| Stream a batch of birth results with a batch seed drawn like get_birth() draws its own. | |
| bool | has_data () const |
| Check whether any country can be generated. | |
| std::size_t | country_count () const |
| Number of countries that can be generated. | |
| std::vector< std::string > | countries () const |
The countries that can be generated, in the order of countries.tsv. | |
| const std::vector< double > & | age_weights (std::string_view cca2, sex bio_sex) const |
| A country's population by single age, as the generator draws ages. | |
| double | life_expectancy (std::string_view cca2, sex bio_sex) const |
A country's life expectancy at birth, the figure le_remaining is computed from. | |
| void | load (const std::filesystem::path &dir) |
| Load data from a resource directory, replacing all data and layers loaded before. | |
| bool | load (dataset tier) |
| Load a dataset tier, replacing all data and layers loaded before. | |
| void | extend (const std::filesystem::path &path, core::layer_mode mode=core::layer_mode::override) |
| Add data layers from a directory or a file. | |
| const core::layer_info & | extend (table which, core::tsv_document doc, core::layer_mode mode=core::layer_mode::override) |
| Add a data layer to one table. | |
| const core::layer_info & | extend_from_string (table which, std::string_view text, std::string source, core::layer_mode mode=core::layer_mode::override) |
| Add a data layer to one table from TSV text. | |
| const std::vector< core::layer_info > & | layers (table which) const |
| The layers in effect for one table, in order. | |
| std::uint64_t | data_hash () const |
| Hash of the loaded data (all three tables), recorded in each result's signature(). | |
Static Public Member Functions | |
| static bthg & | instance () |
| Access the global singleton instance. | |
| static std::string_view | cohort_of (int year) |
The generational cohort of a birth year, as birth::cohort labels it (see the table in the usage guide). | |
Static Public Attributes | |
| static constexpr std::chrono::year_month_day | default_reference_date |
| The default reference date: 1 July 2023, the date the UN World Population Prospects estimates in the data refer to. | |
Birth generator that produces demographically plausible birthdays from country age pyramids.
The generation pipeline, each step drawing from its own stream split from the call seed (so fixing the sex, year or age range leaves the other steps' draws unchanged):
Ages are relative to a fixed reference date (default 2023-07-01, the date of the population estimates), not the clock, so a seed gives the same birth every time. Output is identical on every compiler and platform (dasmig-core).
Definition at line 189 of file birthgen.hpp.
|
strong |
The three data tables.
| Enumerator | |
|---|---|
| countries |
|
| age_pyramid |
|
| monthly_births |
|
Definition at line 193 of file birthgen.hpp.
|
inline |
A country's population by single age, as the generator draws ages.
Relative weights, not normalised: they are the rows of age_pyramid.tsv (population by age 0 to 100 and sex), so two countries' weights are comparable only within a country.
| cca2 | ISO 3166-1 alpha-2 code. |
| bio_sex | The sex. |
| std::runtime_error | If no data is loaded. |
| std::invalid_argument | If the country has no data. |
Definition at line 375 of file birthgen.hpp.
|
inlinestatic |
The generational cohort of a birth year, as birth::cohort labels it (see the table in the usage guide).
| year | The birth year. |
Greatest Generation up to 1927, then Silent Generation, Baby Boomer, Generation X, Millennial, Generation Z, and Generation Alpha from 2013. Definition at line 400 of file birthgen.hpp.
|
inline |
The countries that can be generated, in the order of countries.tsv.
Definition at line 357 of file birthgen.hpp.
|
inline |
Number of countries that can be generated.
Definition at line 352 of file birthgen.hpp.
|
inline |
Hash of the loaded data (all three tables), recorded in each result's signature().
Definition at line 521 of file birthgen.hpp.
|
inline |
Add data layers from a directory or a file.
A directory may hold any of countries.tsv, age_pyramid.tsv and monthly_births.tsv; all are applied together or, if any fails, none. A file must have one of those names. Rows are keyed by cca2; see Extending Data in doc/usage.md.
| path | Directory or file. |
| mode | How the layers combine with the data loaded before. |
| core::tsv_error | If a file cannot be read or a row is invalid; the data is then unchanged. |
| std::invalid_argument | If path is a file with another name. |
Definition at line 462 of file birthgen.hpp.
|
inline |
Add a data layer to one table.
| which | The table. |
| doc | TSV with that table's columns. |
| mode | How the layer combines with the data loaded before. |
| core::tsv_error | If a row is invalid; the data is unchanged. |
Definition at line 488 of file birthgen.hpp.
|
inline |
Add a data layer to one table from TSV text.
| which | The table. |
| text | TSV with that table's columns. |
| source | Name used in errors. |
| mode | How the layer combines with the data loaded before. |
| core::tsv_error | If a row is invalid; the data is unchanged. |
Definition at line 504 of file birthgen.hpp.
|
inline |
Stream a batch of birth results with a batch seed drawn like get_birth() draws its own.
| Sink | Callable taking (std::uint64_t, birth&). |
| request | What every item is generated from. |
| options | Count, first index, threads and uniqueness. |
| sink | Receives the items. |
Definition at line 336 of file birthgen.hpp.
|
inline |
Stream a batch of birth results to a sink without holding the whole batch.
Generates the same items as get_birth_batch() and calls sink(index, item) in index order on the calling thread; index includes options.first.
| Sink | Callable taking (std::uint64_t, birth&); for example core::jsonl_writer or core::csv_writer. |
| request | What every item is generated from. |
| options | Count, first index, threads and uniqueness. |
| batch_seed | Seed of the whole batch. |
| sink | Receives the items. |
Definition at line 321 of file birthgen.hpp.
|
inline |
Generate a random birth using per-call filters and settings.
Draws a call seed from the instance's entropy-initialized random engine. Use get_birth(request, seed) for deterministic generation.
| request | All filters and settings for this call. |
| std::runtime_error | If no data has been loaded. |
| std::invalid_argument | If the country is unknown, the reference date or age range is invalid, or both year and ages are specified. |
Definition at line 236 of file birthgen.hpp.
|
inline |
Generate a deterministic birth using per-call filters and settings.
Does not advance the internal random engine. Replay with the same request, seed and data.
| request | Filters and settings for this call. |
| call_seed | Seed. |
| std::runtime_error | If no data has been loaded. |
| std::invalid_argument | If the country is unknown, the reference date or age range is invalid, or both year and ages are specified. |
Definition at line 251 of file birthgen.hpp.
|
inline |
Generate a batch of birth results from a seed drawn like get_birth() draws its own.
Item i is get_birth(request, core::item_seed(batch_seed, options.first + i)) for the drawn batch seed; see the seeded overload.
| request | What every item is generated from. |
| options | Count, first index, threads and uniqueness. |
Definition at line 278 of file birthgen.hpp.
|
inline |
Generate a batch of birth results, deterministically.
Item i is exactly get_birth(request, core::item_seed(batch_seed, options.first + i)), so an item replays on its own, a range can be generated directly, two batches that follow each other equal one, and the output does not depend on options.threads.
| request | What every item is generated from. |
| options | Count, first index, threads and uniqueness (see core::unique_rule; the rule must be made for birth). |
| batch_seed | Seed of the whole batch. |
| std::invalid_argument | If the request or options are invalid. |
| std::runtime_error | If no data has been loaded, or a unique batch runs out of attempts. |
Definition at line 299 of file birthgen.hpp.
|
inline |
Check whether any country can be generated.
Definition at line 348 of file birthgen.hpp.
|
inlinestatic |
Access the global singleton instance.
Loads the lite tier (or full if lite is missing) on first access, from compiled-in data or an auto-probed resources/.
Definition at line 219 of file birthgen.hpp.
|
inline |
The layers in effect for one table, in order.
| which | The table. |
Definition at line 513 of file birthgen.hpp.
|
inline |
A country's life expectancy at birth, the figure le_remaining is computed from.
| cca2 | ISO 3166-1 alpha-2 code. |
| bio_sex | The sex. |
| std::runtime_error | If no data is loaded. |
| std::invalid_argument | If the country has no data. |
Definition at line 388 of file birthgen.hpp.
|
inline |
Load data from a resource directory, replacing all data and layers loaded before.
Reads countries.tsv, age_pyramid.tsv and monthly_births.tsv; a missing file leaves its table empty. A path that is not a directory, or has no countries.tsv, is ignored.
| dir | Directory such as resources/lite. |
| core::tsv_error | If a file is malformed; the data is then unchanged. |
Definition at line 412 of file birthgen.hpp.
|
inline |
Load a dataset tier, replacing all data and layers loaded before.
Uses the compiled-in copy when the library is built with DASMIG_BIRTH_EMBED, otherwise searches resources/, ../resources, birth-generator/resources and ../birth-generator/resources.
| tier | The tier. |
| core::tsv_error | If a file is malformed. |
Definition at line 434 of file birthgen.hpp.
|
staticconstexpr |
The default reference date: 1 July 2023, the date the UN World Population Prospects estimates in the data refer to.
Definition at line 202 of file birthgen.hpp.