Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Run: doxygen Doxyfile

PROJECT_NAME = "Entity Generator"
PROJECT_NUMBER = 4.2.1
PROJECT_NUMBER = 4.2.2
PROJECT_BRIEF = "Composable, deterministic entity generation for C++23"
PROJECT_LOGO = doc/entity-generator.png

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Tested with:

| Dependency | Version | Bundled | Purpose |
|-----------|---------|---------|---------|
| [dasmig-core](https://github.com/dasmig/dasmig-core) | 1.0 | Yes (`core.hpp`, `core/`) | Platform-independent random engine and distributions, JSON |
| [dasmig-core](https://github.com/dasmig/dasmig-core) | 1.3 | Yes (`core.hpp`, `core/`) | Platform-independent random engine and distributions, JSON |
| [Catch2](https://github.com/catchorg/Catch2) | 3.x | Yes (amalgamated) | Unit testing |
| [EnTT](https://github.com/skypjack/entt), [Flecs](https://github.com/SanderMertens/flecs) | - | No | Only for the optional adapter headers in `dasmig/ext/` |

Expand Down
88 changes: 88 additions & 0 deletions dasmig/core/distributions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,94 @@ namespace detail
return math::exp(normal(r, mu, sigma));
}

/// @brief Gamma-distributed double.
///
/// Marsaglia and Tsang (2000): with d = shape - 1/3 and c = 1 / sqrt(9 d),
/// draw x = normal(r, 0, 1) and u = uniform01(r) until t = 1 + c x is
/// positive, u is positive and log(u) < x^2 / 2 + d - d v + d log(v), where
/// v = t^3 (a non-positive t redraws x without drawing u); return
/// `d * v * scale`. A shape below 1 first draws u0 = 1 - uniform01(r), uses
/// shape + 1, and multiplies the result by `exp(log(u0) / shape)`. log and exp
/// are math::log and math::exp.
///
/// @param r Engine.
/// @param shape Shape k (mean k * scale).
/// @param scale Scale (0 returns 0 after the same draws).
/// @return The variate (non-negative).
/// @throws std::invalid_argument If @p shape is not finite and positive, or
/// @p scale is not finite or is negative.
[[nodiscard]] inline double gamma(rng& r, double shape, double scale) // NOLINT(bugprone-easily-swappable-parameters)
{
if (!std::isfinite(shape) || !(shape > 0.0) || !std::isfinite(scale) || scale < 0.0)
{
throw std::invalid_argument("gamma: need finite shape > 0 and scale >= 0");
}
double boost = 1.0;
if (shape < 1.0)
{
const double u = 1.0 - uniform01(r); // (0, 1]
boost = math::exp(math::log(u) / shape);
shape += 1.0;
}
const double d = shape - (1.0 / 3.0);
const double c = 1.0 / std::sqrt(9.0 * d);
for (;;)
{
const double x = normal(r, 0.0, 1.0);
const double t = 1.0 + math::fenced(c * x);
if (t <= 0.0) continue;
const double v = t * t * t;
const double u = uniform01(r);
if (u <= 0.0) continue;
const double bound = math::fenced(0.5 * x * x) + d - math::fenced(d * v) +
math::fenced(d * math::log(v));
if (math::log(u) < bound) return d * v * scale * boost;
}
}

/// @brief Beta-distributed double in [0, 1].
///
/// `x / (x + y)` with x = gamma(r, a, 1) drawn first and y = gamma(r, b, 1).
/// For a mean m and a concentration k, pass a = m k and b = (1 - m) k.
///
/// @param r Engine.
/// @param a First shape (alpha).
/// @param b Second shape (beta).
/// @return The variate.
/// @throws std::invalid_argument If a shape is not finite and positive.
[[nodiscard]] inline double beta(rng& r, double a, double b) // NOLINT(bugprone-easily-swappable-parameters)
{
if (!std::isfinite(a) || !(a > 0.0) || !std::isfinite(b) || !(b > 0.0))
{
throw std::invalid_argument("beta: need finite shapes > 0");
}
const double x = gamma(r, a, 1.0);
const double y = gamma(r, b, 1.0);
return x / (x + y);
}

/// @brief Weibull-distributed double.
///
/// Inversion: e = -log(1 - uniform01(r)), then `scale * exp(log(e) / shape)`,
/// or 0 when e is 0. Consumes one value.
///
/// @param r Engine.
/// @param shape Shape k.
/// @param scale Scale (0 returns 0).
/// @return The variate (non-negative).
/// @throws std::invalid_argument If @p shape is not finite and positive, or
/// @p scale is not finite or is negative.
[[nodiscard]] inline double weibull(rng& r, double shape, double scale) // NOLINT(bugprone-easily-swappable-parameters)
{
if (!std::isfinite(shape) || !(shape > 0.0) || !std::isfinite(scale) || scale < 0.0)
{
throw std::invalid_argument("weibull: need finite shape > 0 and scale >= 0");
}
const double u = 1.0 - uniform01(r); // (0, 1]
const double e = -math::log(u);
return e > 0.0 ? scale * math::exp(math::log(e) / shape) : 0.0;
}

/// @brief Index chosen with probability proportional to a weight.
///
/// Keeps the cumulative sums of the weights (added left to right). A draw is
Expand Down
129 changes: 129 additions & 0 deletions dasmig/core/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#ifndef DASMIG_CORE_MATH_HPP
#define DASMIG_CORE_MATH_HPP

#include <algorithm>
#include <array>
#include <cfloat>
#include <cmath>
Expand Down Expand Up @@ -179,6 +180,134 @@ inline constexpr std::array<double, log_terms> log_coeff = [] {
return fenced(k * detail::ln2_hi) + (log_m + fenced(k * detail::ln2_lo));
}

/// @brief The sine and cosine of one angle (sin_cos_deg()).
struct sine_cosine
{
double sin; ///< The sine.
double cos; ///< The cosine.
};

/// @brief Sine and cosine of an angle in degrees, bit-identical on every
/// platform.
///
/// Reduces by the nearest multiple of 90 degrees, q = round(degrees / 90),
/// so x = (degrees - 90 q) pi / 180 is within pi/4, evaluates the Taylor
/// series of sin to x^17 and of cos to x^16 by Horner's rule in x^2 (error
/// below 1e-17 on |x| <= pi/4), and swaps and negates by q mod 4. Working in
/// degrees keeps the reduction exact: 90 q is exact where a multiple of pi is
/// not.
///
/// @param degrees The angle. Accurate while 90 q is exact (|degrees| below
/// about 8e16); multiples of 90 give exact 0 and 1.
/// @return sin and cos; both NaN for NaN or infinity.
[[nodiscard]] inline sine_cosine sin_cos_deg(double degrees) noexcept
{
if (!std::isfinite(degrees))
{
const double nan = std::numeric_limits<double>::quiet_NaN();
return {.sin = nan, .cos = nan};
}
const double q = std::round(degrees / 90.0);
const double x = (degrees - fenced(90.0 * q)) * (std::numbers::pi / 180.0); // |x| <= pi/4
const double x2 = x * x;
double s = 1.0 / 355687428096000.0;
double c = 1.0 / 20922789888000.0;
constexpr std::array<double, 8> sin_terms{-1.0 / 1307674368000.0, 1.0 / 6227020800.0,
-1.0 / 39916800.0, 1.0 / 362880.0,
-1.0 / 5040.0, 1.0 / 120.0,
-1.0 / 6.0, 1.0};
constexpr std::array<double, 8> cos_terms{-1.0 / 87178291200.0, 1.0 / 479001600.0,
-1.0 / 3628800.0, 1.0 / 40320.0,
-1.0 / 720.0, 1.0 / 24.0,
-1.0 / 2.0, 1.0};
for (const double term : sin_terms) s = fenced(s * x2) + term;
for (const double term : cos_terms) c = fenced(c * x2) + term;
s *= x;
const double m = std::fmod(q, 4.0); // exact; -3 to 3
switch (static_cast<int>(m < 0.0 ? m + 4.0 : m))
{
case 1: return {.sin = c, .cos = -s};
case 2: return {.sin = -s, .cos = -c};
case 3: return {.sin = -c, .cos = s};
default: return {.sin = s, .cos = c};
}
}

/// @brief Sine of an angle in degrees: sin_cos_deg(degrees).sin.
/// @param degrees The angle.
/// @return The sine; NaN for NaN or infinity.
[[nodiscard]] inline double sin_deg(double degrees) noexcept { return sin_cos_deg(degrees).sin; }

/// @brief Cosine of an angle in degrees: sin_cos_deg(degrees).cos.
/// @param degrees The angle.
/// @return The cosine; NaN for NaN or infinity.
[[nodiscard]] inline double cos_deg(double degrees) noexcept { return sin_cos_deg(degrees).cos; }

/// @brief Arctangent in radians, bit-identical on every platform.
///
/// Folds x to |x| <= 1 (atan x = pi/2 - atan(1/x)), then above tan(pi/8)
/// moves to (x - 1) / (x + 1) (adding pi/4), and sums the odd Taylor series
/// to x^51 by Horner's rule in x^2 (error below 1e-17 on |x| <= tan(pi/8)).
///
/// @param x The tangent.
/// @return atan(x) in [-pi/2, pi/2]; NaN for NaN; +0 for -0.
[[nodiscard]] inline double atan(double x) noexcept
{
const bool negative = x < 0.0;
if (negative) x = -x;
const bool inverted = x > 1.0;
if (inverted) x = 1.0 / x;
double base = 0.0;
if (x > 0.41421356237309503) // tan(pi / 8)
{
x = (x - 1.0) / (x + 1.0);
base = std::numbers::pi / 4.0;
}
const double x2 = fenced(x * x);
double sum = 0.0;
for (int n = 25; n >= 0; --n) sum = fenced(sum * x2) + ((n % 2 == 0 ? 1.0 : -1.0) / ((2.0 * n) + 1.0));
double r = base + fenced(sum * x);
if (inverted) r = (std::numbers::pi / 2.0) - r;
return negative ? -r : r;
}

/// @brief The angle of the point (x, y) in degrees, bit-identical on every
/// platform.
///
/// atan(y / x) in degrees, moved by 180 degrees when x is negative; 90 or
/// -90 on the y axis.
///
/// @param y The ordinate.
/// @param x The abscissa.
/// @return The angle in (-180, 180]; 0 for (0, 0); NaN when either is NaN.
[[nodiscard]] inline double atan2_deg(double y, double x) noexcept
{
constexpr double deg = 180.0 / std::numbers::pi;
if (std::isnan(x) || std::isnan(y)) return std::numeric_limits<double>::quiet_NaN();
if (x > 0.0) return atan(y / x) * deg;
if (x < 0.0) return fenced(atan(y / x) * deg) + (y >= 0.0 ? 180.0 : -180.0);
if (y == 0.0) return 0.0;
return y > 0.0 ? 90.0 : -90.0;
}

/// @brief Arcsine in degrees: atan2_deg(x, sqrt(1 - x^2)).
/// @param x The sine; values beyond [-1, 1] give 90 or -90.
/// @return The angle in [-90, 90]; NaN for NaN.
[[nodiscard]] inline double asin_deg(double x) noexcept
{
if (std::isnan(x)) return x;
return atan2_deg(x, std::sqrt(std::max(0.0, 1.0 - fenced(x * x))));
}

/// @brief Arccosine in degrees: atan2_deg(sqrt(1 - x^2), x).
/// @param x The cosine; values beyond [-1, 1] give 0 or 180.
/// @return The angle in [0, 180]; NaN for NaN.
[[nodiscard]] inline double acos_deg(double x) noexcept
{
if (std::isnan(x)) return x;
return atan2_deg(std::sqrt(std::max(0.0, 1.0 - fenced(x * x))), x);
}

} // namespace dasmig::core::math


Expand Down
6 changes: 3 additions & 3 deletions dasmig/core/table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ class layered_table
// The row whose key satisfies @p matches(row_index), among those
// stored under @p hash.
template <typename Matches>
[[nodiscard]] std::optional<std::size_t> find(std::uint32_t hash, Matches&& matches) const
[[nodiscard]] std::optional<std::size_t> find(std::uint32_t hash, const Matches& matches) const
{
if (_slots.empty()) return std::nullopt;
const std::size_t mask = _slots.size() - 1;
Expand Down Expand Up @@ -316,12 +316,12 @@ class layered_table
const std::size_t mask = _slots.size() - 1;
std::size_t i = hash & mask;
while (_slots[i].row != vacant) i = (i + 1) & mask;
_slots[i] = {hash, row};
_slots[i] = {.hash = hash, .row = row};
}

void grow(std::size_t slots)
{
std::vector<slot> old = std::move(_slots);
const std::vector<slot> old = std::move(_slots);
_slots.assign(slots, slot{});
for (const slot& s : old)
{
Expand Down
33 changes: 21 additions & 12 deletions dasmig/core/tsv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,18 +593,7 @@ class tsv_document
d.cells.reserve(line_count * width);
d.row_lines.reserve(line_count);
}
std::string_view remaining = text_line;
for (;;)
{
const auto pos = remaining.find('\t');
const auto cell = remaining.substr(0, pos);
d.cells.push_back({static_cast<std::uint32_t>(cell.data() - d.text.data()),
static_cast<std::uint32_t>(cell.size())});
if (pos == std::string_view::npos) break;
remaining.remove_prefix(pos + 1);
}
d.cells.resize(d.cells.size() + (width - count));
d.row_lines.push_back(static_cast<std::uint32_t>(line));
add_row(text_line, static_cast<std::uint32_t>(line));
}
if (!have_header)
{
Expand All @@ -613,6 +602,26 @@ class tsv_document
d.hash = canonical_hash();
}

// Store one data line's cells as offsets into the text, padded with empty
// cells to the column count.
void add_row(std::string_view text_line, std::uint32_t line)
{
auto& d = *_data;
const std::size_t first = d.cells.size();
std::string_view remaining = text_line;
for (;;)
{
const auto pos = remaining.find('\t');
const auto cell = remaining.substr(0, pos);
d.cells.push_back({.offset = static_cast<std::uint32_t>(cell.data() - d.text.data()),
.length = static_cast<std::uint32_t>(cell.size())});
if (pos == std::string_view::npos) break;
remaining.remove_prefix(pos + 1);
}
d.cells.resize(first + d.columns.size());
d.row_lines.push_back(line);
}

static std::vector<std::string_view> split(std::string_view line)
{
std::vector<std::string_view> cells;
Expand Down
2 changes: 1 addition & 1 deletion dasmig/core/version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/// incompatibly.
#define DASMIG_CORE_VERSION_MAJOR 1
/// @brief Minor version. Changes when features are added compatibly.
#define DASMIG_CORE_VERSION_MINOR 2
#define DASMIG_CORE_VERSION_MINOR 3
/// @brief Patch version. Changes for fixes that keep output identical.
#define DASMIG_CORE_VERSION_PATCH 0

Expand Down
Loading