Loading...
Searching...
No Matches
operation.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2#pragma once
3#include "meshkit/mesh.hpp"
4#include <chrono>
5#include <stdexcept>
6#include <cmath>
7#include <limits>
8#include <initializer_list>
9#include <utility>
10
12{
19struct Stop {
22 std::string code, message;
25};
26
32[[noreturn]] inline void fail(std::string code, std::string message,
34{
35 throw Stop{status, std::move(code), std::move(message)};
36}
37
43struct Work {
44 const Limits &limits;
45 const std::function<bool()> &cancelled;
47 std::uint64_t used = 0, maximum = 0;
56 Work(const Limits &l, const std::function<bool()> &c) : limits(l), cancelled(c)
57 {
58 maximum = l.maxWork ? l.maxWork : 20000000000ull;
59 check();
60 }
61
65 void check() const
66 {
67 if (cancelled && cancelled())
68 fail("cancelled", "Cancelled.", RunStatus::Cancelled);
69 }
70
77 void tick(std::uint64_t count = 1)
78 {
79 if (count > maximum - used)
80 fail("work_limit", "Operation work limit exceeded.", RunStatus::ResourceLimit);
81 used += count;
82 if ((used & 4095) == 0)
83 check();
84 }
85
91 void memoryFor(std::initializer_list<std::pair<std::uint64_t, std::uint64_t>> terms) const
92 {
93 std::uint64_t total = 0;
94 for (auto term : terms) {
95 if (term.second &&
96 term.first > (std::numeric_limits<std::uint64_t>::max() - total) / term.second)
97 fail("workspace_limit", "Workspace estimate overflows.", RunStatus::ResourceLimit);
98 total += term.first * term.second;
99 }
100 memory(total);
101 }
102
108 void memory(std::uint64_t estimate) const
109 {
110 if (estimate > limits.maxWorkspaceBytes)
111 fail("workspace_limit", "Estimated workspace exceeds the configured limit.",
113 }
114};
115
121inline std::uint64_t edgeKey(Index a, Index b)
122{
123 return std::uint64_t(std::min(a, b)) << 32 | std::max(a, b);
124}
125
131inline Box2 box(const Point2 *p, int n)
132{
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());
139 }
140 return {{x0, y0}, {x1, y1}};
141}
142
148inline bool meets(const Box2 &a, const Box2 &b)
149{
150 return a.left() <= b.right() && a.right() >= b.left() && a.top() <= b.bottom() &&
151 a.bottom() >= b.top();
152}
153
158inline bool finite(Point2 p)
159{
160 return std::isfinite(p.x()) && std::isfinite(p.y());
161}
162
168inline bool utf8(const std::string &s)
169{
170 for (std::size_t i = 0; i < s.size();) {
171 const auto first = static_cast<unsigned char>(s[i++]);
172 if (first < 128)
173 continue;
174 int rest = 0;
175 std::uint32_t cp = 0, minimum = 0;
176 if (first >= 0xc2 && first <= 0xdf) {
177 rest = 1;
178 cp = first & 31;
179 minimum = 0x80;
180 } else if (first >= 0xe0 && first <= 0xef) {
181 rest = 2;
182 cp = first & 15;
183 minimum = 0x800;
184 } else if (first >= 0xf0 && first <= 0xf4) {
185 rest = 3;
186 cp = first & 7;
187 minimum = 0x10000;
188 } else
189 return false;
190 if (i + rest > s.size())
191 return false;
192 while (rest--) {
193 const auto c = static_cast<unsigned char>(s[i++]);
194 if ((c & 0xc0) != 0x80)
195 return false;
196 cp = cp << 6 | (c & 63);
197 }
198 if (cp < minimum || cp > 0x10ffff || (cp >= 0xd800 && cp <= 0xdfff))
199 return false;
200 }
201 return true;
202}
203
208inline double seconds(std::chrono::steady_clock::time_point start)
209{
210 return std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count();
211}
212}
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