![]() |
Country Generator 3.3.2
Procedural country generation for C++23
|
Country generator that produces random countries from aggregated open-data sources. More...
#include <countrygen.hpp>
Public Member Functions | |
| cntg ()=default | |
| Default constructor — creates an empty generator with no data. | |
| cntg (const cntg &)=delete | |
| Not copyable. | |
| cntg & | operator= (const cntg &)=delete |
| Not copyable. | |
| cntg (cntg &&) noexcept=default | |
| Move constructor. | |
| cntg & | operator= (cntg &&) noexcept=default |
| Move assignment. | |
| ~cntg ()=default | |
| Default destructor. | |
| country | get_country (const country_request &request) |
| Generate a random country with per-call selection options. | |
| country | get_country (const country_request &request, std::uint64_t call_seed) const |
| Generate a deterministic country with per-call selection options. | |
| std::vector< country > | get_country_batch (const country_request &request, const core::batch_options &options) |
| Generate a batch of country results from a seed drawn like get_country() draws its own. | |
| std::vector< country > | get_country_batch (const country_request &request, const core::batch_options &options, std::uint64_t batch_seed) const |
| Generate a batch of country results, deterministically. | |
| template<typename Sink > | |
| void | for_each_country (const country_request &request, const core::batch_options &options, std::uint64_t batch_seed, Sink &&sink) const |
| Stream a batch of country results to a sink without holding the whole batch. | |
| template<typename Sink > | |
| void | for_each_country (const country_request &request, const core::batch_options &options, Sink &&sink) |
| Stream a batch of country results with a batch seed drawn like get_country() draws its own. | |
| country | find_country (const std::string &cca2) const |
| Look up a country by its ISO 3166-1 alpha-2 code. | |
| std::string | get_language (const std::string &cca2, std::uint64_t call_seed) const |
| Draw a language a person of a country speaks, in proportion to the share of the population that speaks it. | |
| locale | get_locale (const locale_request &request) |
| Draw a locale of a country, by speakers. | |
| locale | get_locale (const locale_request &request, std::uint64_t call_seed) const |
| Draw a locale of a country, by speakers, deterministically. | |
| std::vector< locale > | get_locale_batch (const locale_request &request, const core::batch_options &options) |
| Generate a batch of locales from a seed drawn like get_locale() draws its own. | |
| std::vector< locale > | get_locale_batch (const locale_request &request, const core::batch_options &options, std::uint64_t batch_seed) const |
Generate a batch of locales, deterministically: item i is get_locale(request, core::item_seed(batch_seed, options.first + i)). | |
| template<typename Sink > | |
| void | for_each_locale (const locale_request &request, const core::batch_options &options, std::uint64_t batch_seed, Sink &&sink) const |
| Stream a batch of locales to a sink without holding the batch. | |
| template<typename Sink > | |
| void | for_each_locale (const locale_request &request, const core::batch_options &options, Sink &&sink) |
| Stream a batch of locales with a batch seed drawn like get_locale() draws its own. | |
| locale | find_locale (std::string_view tag) const |
| Look up a locale by its tag. | |
| std::vector< locale > | locales (std::string_view country) const |
| Every locale of a country, largest share first, then by tag. | |
| time_zone | get_time_zone (const time_zone_request &request) |
| Draw a time zone of a country, by population. | |
| time_zone | get_time_zone (const time_zone_request &request, std::uint64_t call_seed) const |
| Draw a time zone of a country, by population, deterministically. | |
| std::vector< time_zone > | get_time_zone_batch (const time_zone_request &request, const core::batch_options &options) |
| Generate a batch of time zones from a seed drawn like get_time_zone() draws its own. | |
| std::vector< time_zone > | get_time_zone_batch (const time_zone_request &request, const core::batch_options &options, std::uint64_t batch_seed) const |
Generate a batch of time zones, deterministically: item i is get_time_zone(request, core::item_seed(batch_seed, options.first + i)). | |
| template<typename Sink > | |
| void | for_each_time_zone (const time_zone_request &request, const core::batch_options &options, std::uint64_t batch_seed, Sink &&sink) const |
| Stream a batch of time zones to a sink without holding the batch. | |
| template<typename Sink > | |
| void | for_each_time_zone (const time_zone_request &request, const core::batch_options &options, Sink &&sink) |
| Stream a batch of time zones with a batch seed drawn like get_time_zone() draws its own. | |
| time_zone | find_time_zone (std::string_view name, std::chrono::year_month_day reference_date=time_zone_request::default_reference_date) const |
| Look up a time zone by its IANA name. | |
| std::vector< time_zone > | time_zones (std::string_view country) const |
| Every time zone of a country, largest share first, then by name, at the default reference date. | |
| std::vector< holiday > | holidays (const holiday_request &request) const |
| A country's holidays in a year. | |
| bool | is_holiday (std::string_view country, std::chrono::year_month_day date, bool optional=false) const |
| Whether a date is a holiday's date or its observed date. | |
| year_span | holiday_years (std::string_view country) const |
| The years holidays() serves for a country. | |
| std::uint8_t | currency_digits (std::string_view code) const |
| The ISO 4217 minor unit of a currency (0 for JPY, 3 for KWD). | |
| bool | has_locale_data () const |
| Whether any locale table (locales, zones, currencies, holidays) is loaded. | |
| bool | has_data () const |
| Check whether any data has been loaded. | |
| std::size_t | country_count () const |
| Return the number of loaded countries. | |
| void | load (const std::filesystem::path &path) |
| Load country data from a TSV file or a tier directory, replacing all data and layers loaded before. | |
| bool | load (dataset tier) |
| Load a dataset tier, replacing all data and layers loaded before. | |
| const core::layer_info & | extend (const std::filesystem::path &file, core::layer_mode mode=core::layer_mode::override) |
Add a data layer from a TSV file with the countries.tsv columns. | |
| const core::layer_info & | extend_from_string (std::string_view text, std::string source, core::layer_mode mode=core::layer_mode::override) |
| Add a data layer from TSV text. | |
| const core::layer_info & | extend (core::tsv_document doc, core::layer_mode mode=core::layer_mode::override) |
| Add a data layer. | |
| const core::layer_info & | extend (country_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 (country_table which, const std::filesystem::path &file, core::layer_mode mode=core::layer_mode::override) |
| Add a data layer to one table from a TSV file. | |
| const core::layer_info & | extend_from_string (country_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 () const |
The data layers of countries.tsv in effect, in order. | |
| const std::vector< core::layer_info > & | layers (country_table which) const |
| The data layers of one table in effect, in order. | |
| std::uint64_t | data_hash () const |
Hash of the loaded countries.tsv, recorded in each country's signature() (the locale tables have their own, which locales, zones and holidays record). | |
Static Public Member Functions | |
| static cntg & | instance () |
| Access the global singleton instance. | |
Country generator that produces random countries from aggregated open-data sources.
Generates countries using population-weighted random selection by default. Larger countries are proportionally more likely to be selected, mirroring real-world demographic distributions.
Next to the countries it holds each country's locales (with their CLDR formats), IANA time zones, currency minor units and national holidays, in their own tables (country_table), which load with the tier when they are there.
Output is identical on every compiler and platform (dasmig-core).
Can be used as a singleton via instance() or constructed independently.
Definition at line 1738 of file countrygen.hpp.
|
inline |
Return the number of loaded countries.
Definition at line 2391 of file countrygen.hpp.
|
inline |
The ISO 4217 minor unit of a currency (0 for JPY, 3 for KWD).
| code | ISO 4217 alphabetic code, any case. |
| std::runtime_error | If the currency table is not loaded. |
| std::invalid_argument | For an unknown code. |
Definition at line 2364 of file countrygen.hpp.
|
inline |
Hash of the loaded countries.tsv, recorded in each country's signature() (the locale tables have their own, which locales, zones and holidays record).
Definition at line 2569 of file countrygen.hpp.
|
inline |
Add a data layer from a TSV file with the countries.tsv columns.
Rows are keyed by cca2. In the default override mode a row replaces the loaded country with the same code and new codes are added; a row with disabled set to 1 removes its country.
| file | The layer file. |
| mode | How the layer combines with the data loaded before. |
| core::tsv_error | If the file cannot be read or a row is invalid; the data is then unchanged. |
Definition at line 2466 of file countrygen.hpp.
|
inline |
Add a data layer.
| doc | TSV with the countries.tsv 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 2489 of file countrygen.hpp.
|
inline |
Add a data layer to one table from a TSV file.
| which | The table. |
| file | The layer file. |
| mode | How the layer combines with the data loaded before. |
| core::tsv_error | If the file cannot be read or a row is invalid; the data is then unchanged. |
Definition at line 2530 of file countrygen.hpp.
|
inline |
Add a data layer to one table.
Rows are keyed by the table's key columns (doc/data.md). In the default override mode a row replaces the loaded row with the same key and new keys are added; a row with disabled set to 1 removes its key. A locale table's rows must name loaded countries, and refer to loaded rows of the other tables (a locale's currency, a link's zone, a holiday's event dates and span).
| 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 2513 of file countrygen.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 2543 of file countrygen.hpp.
|
inline |
Add a data layer from TSV text.
| text | TSV with the countries.tsv 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 2478 of file countrygen.hpp.
|
inline |
Look up a country by its ISO 3166-1 alpha-2 code.
| cca2 | Two-letter country code (e.g. "US", "BR"). |
| std::runtime_error | If no data has been loaded. |
| std::invalid_argument | If no country matches the code. |
Definition at line 1892 of file countrygen.hpp.
|
inline |
Look up a locale by its tag.
Accepts any case, _ for -, and a script where the tag has none (pt-Latn-BR is pt-BR): a script the table has no row of for that language and country is taken as the language's likely one.
| tag | BCP 47 tag. |
| std::runtime_error | If the locale tables are not loaded. |
| std::invalid_argument | If no locale has the tag. |
Definition at line 2087 of file countrygen.hpp.
|
inline |
Look up a time zone by its IANA name.
Accepts IANA's backward links (Asia/Calcutta is Asia/Kolkata), which GeoNames and older systems write; the result's name is then the current one. A zone two countries share is returned with the country IANA lists it for.
| name | IANA name. |
| reference_date | The date whose offset is reported, at 12:00 UTC. |
| std::runtime_error | If the zone tables are not loaded. |
| std::invalid_argument | For an unknown name or an invalid date. |
| std::out_of_range | For a reference date before the zone's data. |
Definition at line 2255 of file countrygen.hpp.
|
inline |
Stream a batch of country results with a batch seed drawn like get_country() draws its own.
| Sink | Callable taking (std::uint64_t, country&). |
| request | What every item is generated from. |
| options | Count, first index, threads and uniqueness. |
| sink | Receives the items. |
Definition at line 1880 of file countrygen.hpp.
|
inline |
Stream a batch of country results to a sink without holding the whole batch.
Generates the same items as get_country_batch() and calls sink(index, item) in index order on the calling thread; index includes options.first.
| Sink | Callable taking (std::uint64_t, country&); 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 1865 of file countrygen.hpp.
|
inline |
Stream a batch of locales with a batch seed drawn like get_locale() draws its own.
| Sink | Callable taking (std::uint64_t, locale&). |
| request | What every item is generated from. |
| options | Count, first index, threads and uniqueness. |
| sink | Receives the items, in index order. |
Definition at line 2072 of file countrygen.hpp.
|
inline |
Stream a batch of locales to a sink without holding the batch.
| Sink | Callable taking (std::uint64_t, locale&). |
| 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, in index order. |
Definition at line 2057 of file countrygen.hpp.
|
inline |
Stream a batch of time zones with a batch seed drawn like get_time_zone() draws its own.
| Sink | Callable taking (std::uint64_t, time_zone&). |
| request | What every item is generated from. |
| options | Count, first index, threads and uniqueness. |
| sink | Receives the items, in index order. |
Definition at line 2237 of file countrygen.hpp.
|
inline |
Stream a batch of time zones to a sink without holding the batch.
| Sink | Callable taking (std::uint64_t, time_zone&). |
| 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, in index order. |
Definition at line 2222 of file countrygen.hpp.
|
inline |
Generate a random country with per-call selection options.
Draws a call seed from the internal random engine. Replay with the same request, the returned country's seed and the same data.
| request | Region filter and selection mode for this call. |
| std::runtime_error | If no data has been loaded. |
| std::invalid_argument | If no countries match the requested region. |
Definition at line 1771 of file countrygen.hpp.
|
inline |
Generate a deterministic country with per-call selection options.
Does not advance the internal random engine. Replay with the same request, seed and data.
| request | Region filter and selection mode for this call. |
| call_seed | Seed for reproducible results. |
| std::runtime_error | If no data has been loaded. |
| std::invalid_argument | If no countries match the requested region. |
Definition at line 1785 of file countrygen.hpp.
|
inline |
Generate a batch of country results from a seed drawn like get_country() draws its own.
Item i is get_country(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 1822 of file countrygen.hpp.
|
inline |
Generate a batch of country results, deterministically.
Item i is exactly get_country(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 country). |
| 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 1843 of file countrygen.hpp.
|
inline |
Draw a language a person of a country speaks, in proportion to the share of the population that speaks it.
Falls back to the first of the country's languages when the data has no shares for it. A pure function of the data, the country and the seed.
| cca2 | ISO 3166-1 alpha-2 code. |
| call_seed | The call seed. |
| std::runtime_error | If no data has been loaded. |
| std::invalid_argument | If no country matches the code, or the country has no language data. |
Definition at line 1914 of file countrygen.hpp.
|
inline |
Draw a locale of a country, by speakers.
Draws a call seed from the internal random engine. Replay with the same request, the returned locale's seed and the same data.
| request | The country, the language and the selection mode. |
| std::runtime_error | If the locale tables are not loaded. |
| std::invalid_argument | As the seeded overload. |
Definition at line 1941 of file countrygen.hpp.
|
inline |
Draw a locale of a country, by speakers, deterministically.
The country is the request's, or else drawn from the "country" stream exactly as get_country() draws it, so get_locale({}, s).country_code == get_country({}, s).cca2 for every country with a locale (a drawn country with none, Antarctica in full, is replaced by a draw among the countries that have one). With only language set, the country is drawn among those with a locale of that language, by speakers. The tag is drawn from the "locale" stream, weighted by its share.
| request | The country, the language and the selection mode. |
| call_seed | Seed for reproducible results. |
| std::runtime_error | If no countries or no locale tables are loaded. |
| std::invalid_argument | For an unknown country, a language no country has a locale of, or a country with no locale (of the language, when set). |
Definition at line 1961 of file countrygen.hpp.
|
inline |
Generate a batch of locales from a seed drawn like get_locale() draws its own.
| request | What every item is generated from. |
| options | Count, first index, threads and uniqueness. |
Definition at line 2027 of file countrygen.hpp.
|
inline |
Generate a batch of locales, deterministically: item i is get_locale(request, core::item_seed(batch_seed, options.first + i)).
| request | What every item is generated from. |
| options | Count, first index, threads and uniqueness. |
| batch_seed | Seed of the whole batch. |
| std::invalid_argument | If the request or options are invalid. |
| std::runtime_error | If no data is loaded, or a unique batch runs out of attempts. |
Definition at line 2042 of file countrygen.hpp.
|
inline |
Draw a time zone of a country, by population.
| request | The country, the reference date and the selection mode. |
| std::runtime_error | If the zone tables are not loaded. |
| std::invalid_argument | As the seeded overload. |
Definition at line 2141 of file countrygen.hpp.
|
inline |
Draw a time zone of a country, by population, deterministically.
The country is the request's, or else drawn from the "country" stream as get_country() draws it; the zone is drawn from the "time_zone" stream, weighted by its share of the population. The offset, abbreviation and daylight flag are those at the reference date, 12:00 UTC.
| request | The country, the reference date and the selection mode. |
| call_seed | Seed for reproducible results. |
| std::runtime_error | If no countries or no zone tables are loaded. |
| std::invalid_argument | For an unknown country, one with no zone, or an invalid reference date. |
| std::out_of_range | For a reference date before the zone's data. |
Definition at line 2161 of file countrygen.hpp.
|
inline |
Generate a batch of time zones from a seed drawn like get_time_zone() draws its own.
| request | What every item is generated from. |
| options | Count, first index, threads and uniqueness. |
Definition at line 2192 of file countrygen.hpp.
|
inline |
Generate a batch of time zones, deterministically: item i is get_time_zone(request, core::item_seed(batch_seed, options.first + i)).
| request | What every item is generated from. |
| options | Count, first index, threads and uniqueness. |
| batch_seed | Seed of the whole batch. |
| std::invalid_argument | If the request or options are invalid. |
| std::runtime_error | If no data is loaded, or a unique batch runs out of attempts. |
Definition at line 2207 of file countrygen.hpp.
|
inline |
Check whether any data has been loaded.
Definition at line 2384 of file countrygen.hpp.
|
inline |
Whether any locale table (locales, zones, currencies, holidays) is loaded.
countries.tsv is. Definition at line 2375 of file countrygen.hpp.
|
inline |
The years holidays() serves for a country.
| country | ISO 3166-1 alpha-2 code, any case. |
| std::runtime_error | If no countries or no holiday tables are loaded. |
| std::invalid_argument | For an unknown country, or one with no holiday data. |
Definition at line 2354 of file countrygen.hpp.
|
inline |
A country's holidays in a year.
A lookup, not a draw: no seed. Sorted by date, then key, then day. Public and bank holidays always; optional days and observances when asked. A day off a weekend rule moves has observed set (design 8.4); a move python-holidays records but no rule gives is its own entry, named as python-holidays names it ("Christmas Day (observed)").
| request | The country, the year and the kinds. |
| std::runtime_error | If no countries or no holiday tables are loaded. |
| std::invalid_argument | For an unknown country, or one with no holiday data. |
| std::out_of_range | For a year outside holiday_years(). |
Definition at line 2310 of file countrygen.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 1755 of file countrygen.hpp.
|
inline |
Whether a date is a holiday's date or its observed date.
Looks at the holidays of the year before and after too, since an observed day can cross New Year (the US's New Year's Day 2022 was observed on 2021-12-31).
| country | ISO 3166-1 alpha-2 code, any case. |
| date | The date. |
| optional | Count holiday_kind::optional days too. |
| std::runtime_error | If no countries or no holiday tables are loaded. |
| std::invalid_argument | For an unknown country, one with no holiday data, or an invalid date. |
| std::out_of_range | For a date whose year is outside holiday_years(). |
Definition at line 2331 of file countrygen.hpp.
|
inline |
The data layers of countries.tsv in effect, in order.
Definition at line 2551 of file countrygen.hpp.
|
inline |
The data layers of one table in effect, in order.
| which | The table. |
Definition at line 2559 of file countrygen.hpp.
|
inline |
Load country data from a TSV file or a tier directory, replacing all data and layers loaded before.
A file is countries.tsv (see doc/data.md); the locale tables are then left as they are. A directory such as resources/lite must hold countries.tsv and loads every other table it holds; the ones it does not hold are cleared. Any other path is ignored.
| path | Path to the TSV file or the directory. |
| core::tsv_error | If a file is malformed; the data is then unchanged. |
Definition at line 2407 of file countrygen.hpp.
|
inline |
Load a dataset tier, replacing all data and layers loaded before.
Loads countries.tsv, then every locale table of the tier it finds (a missing one is cleared). Each uses the compiled-in copy when the library is built with DASMIG_COUNTRY_EMBED (the countries) or DASMIG_COUNTRY_EMBED_LOCALE (the locale tables), otherwise searches resources/, ../resources, country-generator/resources and ../country-generator/resources. The locale tables are read from the first of those that has both the tier's countries.tsv and its locales.tsv, so another library's locales.tsv is never read.
| tier | The dataset size to load. |
true if the tier's countries.tsv was found and loaded. | core::tsv_error | If a file is malformed; the data is then unchanged. |
Definition at line 2437 of file countrygen.hpp.
|
inline |
Every locale of a country, largest share first, then by tag.
| country | ISO 3166-1 alpha-2 code, any case. |
| std::runtime_error | If no countries or no locale tables are loaded. |
| std::invalid_argument | For an unknown country. |
Definition at line 2118 of file countrygen.hpp.
|
inline |
Every time zone of a country, largest share first, then by name, at the default reference date.
| country | ISO 3166-1 alpha-2 code, any case. |
| std::runtime_error | If no countries or no zone tables are loaded. |
| std::invalid_argument | For an unknown country. |
Definition at line 2278 of file countrygen.hpp.