8#include <initializer_list>
32[[noreturn]]
inline void fail(std::string code, std::string message,
35 throw Stop{status, std::move(code), std::move(message)};
77 void tick(std::uint64_t count = 1)
82 if ((
used & 4095) == 0)
91 void memoryFor(std::initializer_list<std::pair<std::uint64_t, std::uint64_t>> terms)
const
93 std::uint64_t total = 0;
94 for (
auto term : terms) {
96 term.first > (std::numeric_limits<std::uint64_t>::max() - total) / term.second)
98 total += term.first * term.second;
108 void memory(std::uint64_t estimate)
const
110 if (estimate >
limits.maxWorkspaceBytes)
111 fail(
"workspace_limit",
"Estimated workspace exceeds the configured limit.",
123 return std::uint64_t(std::min(a, b)) << 32 | std::max(a, b);
133 double x0 = p[0].
x(), x1 = x0, y0 = p[0].
y(), y1 = y0;
134 for (
int k = 1; k < n; ++k) {
135 x0 = std::min(x0, p[k].x());
136 x1 = std::max(x1, p[k].x());
137 y0 = std::min(y0, p[k].y());
138 y1 = std::max(y1, p[k].y());
140 return {{x0, y0}, {x1, y1}};
160 return std::isfinite(p.
x()) && std::isfinite(p.
y());
168inline bool utf8(
const std::string &s)
170 for (std::size_t i = 0; i < s.size();) {
171 const auto first =
static_cast<unsigned char>(s[i++]);
175 std::uint32_t cp = 0, minimum = 0;
176 if (first >= 0xc2 && first <= 0xdf) {
180 }
else if (first >= 0xe0 && first <= 0xef) {
184 }
else if (first >= 0xf0 && first <= 0xf4) {
190 if (i + rest > s.size())
193 const auto c =
static_cast<unsigned char>(s[i++]);
194 if ((c & 0xc0) != 0x80)
196 cp = cp << 6 | (c & 63);
198 if (cp < minimum || cp > 0x10ffff || (cp >= 0xd800 && cp <= 0xdfff))
208inline double seconds(std::chrono::steady_clock::time_point start)
210 return std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count();
Axis-aligned rectangle in the XY plane, stored as an origin corner and a signed size.
Definition geometry.hpp:167
constexpr double left() const
X of the origin corner; the minimum x when width() >= 0.
Definition geometry.hpp:186
constexpr double bottom() const
top() + height(); the maximum y when height() >= 0.
Definition geometry.hpp:192
constexpr double right() const
left() + width(); the maximum x when width() >= 0.
Definition geometry.hpp:188
constexpr double top() const
Y of the origin corner; the minimum y when height() >= 0.
Definition geometry.hpp:190
Point or displacement vector in the horizontal (XY) plane.
Definition geometry.hpp:21
constexpr double y() const noexcept
Y coordinate in XY plane units.
Definition geometry.hpp:36
constexpr double x() const noexcept
X coordinate in XY plane units.
Definition geometry.hpp:34
Implementation helpers of the native jobs and codecs (failure signalling, work accounting,...
double seconds(std::chrono::steady_clock::time_point start)
Elapsed steady-clock time.
Definition operation.hpp:208
Box2 box(const Point2 *p, int n)
Axis-aligned bounding box of a point array.
Definition operation.hpp:131
void fail(std::string code, std::string message, RunStatus status=RunStatus::Infeasible)
Abort the current native operation by throwing a Stop.
Definition operation.hpp:32
bool utf8(const std::string &s)
Check that a byte string is well-formed UTF-8.
Definition operation.hpp:168
bool finite(Point2 p)
Test whether both coordinates are finite.
Definition operation.hpp:158
std::uint64_t edgeKey(Index a, Index b)
Order-independent 64-bit key of an undirected edge.
Definition operation.hpp:121
bool meets(const Box2 &a, const Box2 &b)
Closed overlap test of two normalized boxes; touching edges or corners count.
Definition operation.hpp:148
std::uint32_t Index
Zero-based index into a native vertex, cell or edge array.
Definition mesh.hpp:13
RunStatus
Status of a running or finished operation (see statusName()).
Definition events.hpp:43
@ Infeasible
Input or policy cannot be satisfied; no result.
Definition events.hpp:50
@ ResourceLimit
Stopped at a resource cap or allocation failure; no result.
Definition events.hpp:47
@ Cancelled
Stopped because cancellation was requested; no result.
Definition events.hpp:48
Resource caps for jobs, validation and file I/O.
Definition mesh.hpp:198
std::uint64_t maxWork
Work-unit allowance of one operation's own counter.
Definition mesh.hpp:218
Exception object thrown by fail() to abort a native operation.
Definition operation.hpp:19
RunStatus status
Report status such as Failed, Infeasible, ResourceLimit or Cancelled.
Definition operation.hpp:20
std::string message
Human-readable English explanation.
Definition operation.hpp:22
std::string code
Short machine-readable failure code such as work_limit.
Definition operation.hpp:22
std::uint64_t maximum
Work-unit allowance: Limits::maxWork, or 20 billion when that is 0.
Definition operation.hpp:47
void memoryFor(std::initializer_list< std::pair< std::uint64_t, std::uint64_t > > terms) const
Gate a workspace estimate given as (count, bytes per item) terms.
Definition operation.hpp:91
void memory(std::uint64_t estimate) const
Gate one workspace estimate; nothing is allocated or recorded.
Definition operation.hpp:108
void tick(std::uint64_t count=1)
Charge work units; cancellation is polled whenever the updated total is an exact multiple of 4096.
Definition operation.hpp:77
const std::function< bool()> & cancelled
Borrowed cancellation poll; may be empty.
Definition operation.hpp:45
void check() const
Poll the cancellation callback.
Definition operation.hpp:65
const Limits & limits
Borrowed limits; maxWorkspaceBytes gates memory().
Definition operation.hpp:44
std::uint64_t used
Work units consumed so far by tick().
Definition operation.hpp:47
Work(const Limits &l, const std::function< bool()> &c)
Bind the limits and poll, set the allowance, and poll cancellation once.
Definition operation.hpp:56