SWMMVis  6.0.0-alpha.4
Qt6/C++ GIS-based graphical user interface for the SWMMVis engine (6.0.0-alpha.4)
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{
13struct Stop {
15 std::string code, message;
16};
17[[noreturn]] inline void fail(std::string code, std::string message,
19{
20 throw Stop{status, std::move(code), std::move(message)};
21}
22struct Work {
23 const Limits &limits;
24 const std::function<bool()> &cancelled;
25 std::uint64_t used = 0, maximum = 0;
26 Work(const Limits &l, const std::function<bool()> &c) : limits(l), cancelled(c)
27 {
28 maximum = l.maxWork ? l.maxWork : 20000000000ull;
29 check();
30 }
31 void check() const
32 {
33 if (cancelled && cancelled())
34 fail("cancelled", "Cancelled.", RunStatus::Cancelled);
35 }
36 void tick(std::uint64_t count = 1)
37 {
38 if (count > maximum - used)
39 fail("work_limit", "Operation work limit exceeded.", RunStatus::ResourceLimit);
40 used += count;
41 if ((used & 4095) == 0)
42 check();
43 }
44 void memoryFor(std::initializer_list<std::pair<std::uint64_t, std::uint64_t>> terms) const
45 {
46 std::uint64_t total = 0;
47 for (auto term : terms) {
48 if (term.second &&
49 term.first > (std::numeric_limits<std::uint64_t>::max() - total) / term.second)
50 fail("workspace_limit", "Workspace estimate overflows.", RunStatus::ResourceLimit);
51 total += term.first * term.second;
52 }
54 }
55 void memory(std::uint64_t estimate) const
56 {
57 if (estimate > limits.maxWorkspaceBytes)
58 fail("workspace_limit", "Estimated workspace exceeds the configured limit.",
60 }
61};
62inline std::uint64_t edgeKey(Index a, Index b)
63{
64 return std::uint64_t(std::min(a, b)) << 32 | std::max(a, b);
65}
66inline Box2 box(const Point2 *p, int n)
67{
68 double x0 = p[0].x(), x1 = x0, y0 = p[0].y(), y1 = y0;
69 for (int k = 1; k < n; ++k) {
70 x0 = std::min(x0, p[k].x());
71 x1 = std::max(x1, p[k].x());
72 y0 = std::min(y0, p[k].y());
73 y1 = std::max(y1, p[k].y());
74 }
75 return {{x0, y0}, {x1, y1}};
76}
77inline bool meets(const Box2 &a, const Box2 &b)
78{
79 return a.left() <= b.right() && a.right() >= b.left() && a.top() <= b.bottom() &&
80 a.bottom() >= b.top();
81}
82inline bool finite(Point2 p)
83{
84 return std::isfinite(p.x()) && std::isfinite(p.y());
85}
86inline bool utf8(const std::string &s)
87{
88 for (std::size_t i = 0; i < s.size();) {
89 const auto first = static_cast<unsigned char>(s[i++]);
90 if (first < 128)
91 continue;
92 int rest = 0;
93 std::uint32_t cp = 0, minimum = 0;
94 if (first >= 0xc2 && first <= 0xdf) {
95 rest = 1;
96 cp = first & 31;
97 minimum = 0x80;
98 } else if (first >= 0xe0 && first <= 0xef) {
99 rest = 2;
100 cp = first & 15;
101 minimum = 0x800;
102 } else if (first >= 0xf0 && first <= 0xf4) {
103 rest = 3;
104 cp = first & 7;
105 minimum = 0x10000;
106 } else
107 return false;
108 if (i + rest > s.size())
109 return false;
110 while (rest--) {
111 const auto c = static_cast<unsigned char>(s[i++]);
112 if ((c & 0xc0) != 0x80)
113 return false;
114 cp = cp << 6 | (c & 63);
115 }
116 if (cp < minimum || cp > 0x10ffff || (cp >= 0xd800 && cp <= 0xdfff))
117 return false;
118 }
119 return true;
120}
121inline double seconds(std::chrono::steady_clock::time_point start)
122{
123 return std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count();
124}
125}
Definition geometry.hpp:59
Definition geometry.hpp:11
size_t i
Definition contourjob.cpp:30
int code
Definition heatconfigdialog.cpp:133
double s
Scene px per model length unit.
Definition inletdrawingview.cpp:95
ArrowPlacement p
Definition linesymbollayer.cpp:29
std::vector< int > start
size = pixels + 1
Definition mesh2dresultsexport.cpp:762
std::size_t n
Definition mesh2dresultsexport.cpp:538
int k
Definition mesh2dresultsexport.cpp:694
int total
Definition meshassignmentsampling.cpp:34
Definition file.hpp:21
double seconds(std::chrono::steady_clock::time_point start)
Definition operation.hpp:121
void fail(std::string code, std::string message, RunStatus status=RunStatus::Infeasible)
Definition operation.hpp:17
bool utf8(const std::string &s)
Definition operation.hpp:86
bool finite(Point2 p)
Definition operation.hpp:82
std::uint64_t edgeKey(Index a, Index b)
Definition operation.hpp:62
bool meets(const Box2 &a, const Box2 &b)
Definition operation.hpp:77
Definition blocks.hpp:25
std::uint32_t Index
Definition mesh.hpp:12
RunStatus
Definition events.hpp:21
Box box
Definition constraintaudit.cpp:161
QString message
Definition queryparser.cpp:230
Definition mesh.hpp:59
std::uint64_t maxWorkspaceBytes
Definition mesh.hpp:61
std::uint64_t maxWork
Definition mesh.hpp:62
Definition operation.hpp:13
RunStatus status
Definition operation.hpp:14
std::string message
Definition operation.hpp:15
std::string code
Definition operation.hpp:15
Definition operation.hpp:22
std::uint64_t maximum
Definition operation.hpp:25
void memoryFor(std::initializer_list< std::pair< std::uint64_t, std::uint64_t > > terms) const
Definition operation.hpp:44
void memory(std::uint64_t estimate) const
Definition operation.hpp:55
void tick(std::uint64_t count=1)
Definition operation.hpp:36
const std::function< bool()> & cancelled
Definition operation.hpp:24
void check() const
Definition operation.hpp:31
const Limits & limits
Definition operation.hpp:23
std::uint64_t used
Definition operation.hpp:25
Work(const Limits &l, const std::function< bool()> &c)
Definition operation.hpp:26
int y
Definition terrainerrorfield.cpp:60
double a
Definition terrainerrorfield.cpp:42
double b
Definition terrainerrorfield.cpp:42
quint64 count
Definition terrainerrorfield.cpp:43
double c
Definition terrainerrorfield.cpp:42
int x
Definition terrainerrorfield.cpp:60
double y0
Definition terrainerrorfield.cpp:41
double y1
Definition terrainerrorfield.cpp:41
double x1
Definition terrainerrorfield.cpp:41
double x0
Definition terrainerrorfield.cpp:41
Sample minimum
Definition timeseriesrunlayer.cpp:51
Sample first
Definition timeseriesrunlayer.cpp:51