All 161 task packages, including the 21 batch 6 C++17 tasks that passed
gates 1/2, are now in Git. rollout.catalog builds docs/task-catalog.{json,md}
from the task packages, a fresh trace5 calibration (128/128 PASS), every
verified controller receipt on the current task digest, documented holds
and digest-bound difficulty reviews. 123 tasks are usable (6 HARD),
5 are on hold and 33 still need a subprocess-only grader.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
284 lines
7.4 KiB
C++
284 lines
7.4 KiB
C++
#include "timer_bank.hpp"
|
|
#include <array>
|
|
#include <numeric>
|
|
#include <string>
|
|
|
|
namespace timers {
|
|
namespace {
|
|
constexpr int INVALID = -22;
|
|
constexpr int RANGE = -34;
|
|
|
|
bool whitespace(char byte)
|
|
{
|
|
return byte == ' ' || byte == '\t' || byte == '\r' || byte == '\n';
|
|
}
|
|
|
|
std::string trimmed(const std::string &text)
|
|
{
|
|
std::size_t begin = 0;
|
|
std::size_t end = text.size();
|
|
while (begin < end && whitespace(text[begin])) {
|
|
++begin;
|
|
}
|
|
while (end > begin && whitespace(text[end - 1])) {
|
|
--end;
|
|
}
|
|
return text.substr(begin, end - begin);
|
|
}
|
|
|
|
bool decimal(char byte)
|
|
{
|
|
return byte >= '0' && byte <= '9';
|
|
}
|
|
|
|
std::uint64_t multiplier(const std::string &unit)
|
|
{
|
|
if (unit.empty() || unit == "us") {
|
|
return 1;
|
|
}
|
|
if (unit == "ms") {
|
|
return 1000;
|
|
}
|
|
if (unit == "s") {
|
|
return 1000000;
|
|
}
|
|
if (unit == "m") {
|
|
return 60000000;
|
|
}
|
|
if (unit == "h") {
|
|
return 3600000000ULL;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int fractional_ticks(std::string digits, std::uint64_t scale,
|
|
std::uint64_t &ticks)
|
|
{
|
|
while (!digits.empty() && digits.back() == '0') {
|
|
digits.pop_back();
|
|
}
|
|
if (digits.empty()) {
|
|
ticks = 0;
|
|
return 0;
|
|
}
|
|
if (digits.size() > 10) {
|
|
return INVALID;
|
|
}
|
|
std::uint64_t numerator = 0;
|
|
std::uint64_t denominator = 1;
|
|
for (char byte : digits) {
|
|
numerator = numerator * 10 + static_cast<unsigned>(byte - '0');
|
|
denominator *= 10;
|
|
}
|
|
const std::uint64_t common = std::gcd(denominator, scale);
|
|
const std::uint64_t divisor = denominator / common;
|
|
if (numerator % divisor != 0) {
|
|
return INVALID;
|
|
}
|
|
ticks = (numerator / divisor) * (scale / common);
|
|
return 0;
|
|
}
|
|
|
|
int bounded_whole(const std::string &digits, std::uint64_t scale,
|
|
std::uint64_t fraction, std::uint64_t &ticks)
|
|
{
|
|
std::uint64_t whole = 0;
|
|
bool excessive = false;
|
|
for (char byte : digits) {
|
|
const unsigned digit = static_cast<unsigned>(byte - '0');
|
|
if (whole > LIMIT / 10 ||
|
|
(whole == LIMIT / 10 && digit > LIMIT % 10)) {
|
|
excessive = true;
|
|
} else if (!excessive) {
|
|
whole = whole * 10 + digit;
|
|
}
|
|
}
|
|
if (excessive || whole > LIMIT / scale) {
|
|
return RANGE;
|
|
}
|
|
const std::uint64_t integral = whole * scale;
|
|
if (fraction > LIMIT - integral) {
|
|
return RANGE;
|
|
}
|
|
ticks = integral + fraction;
|
|
return 0;
|
|
}
|
|
|
|
int plan_key(const std::string &key)
|
|
{
|
|
static const std::array<const char *, 6> keys = {
|
|
"clock", "mode", "value", "interval", "cancel", "nonblock"
|
|
};
|
|
for (std::size_t index = 0; index < keys.size(); ++index) {
|
|
if (key == keys[index]) {
|
|
return static_cast<int>(index);
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int assign_field(int index, const std::string &value, Plan &plan)
|
|
{
|
|
switch (index) {
|
|
case 0:
|
|
if (value == "mono") {
|
|
plan.clock = MONO;
|
|
} else if (value == "real") {
|
|
plan.clock = REAL;
|
|
} else {
|
|
return INVALID;
|
|
}
|
|
return 0;
|
|
case 1:
|
|
if (value == "relative") {
|
|
plan.flags &= ~ABSOLUTE;
|
|
} else if (value == "absolute") {
|
|
plan.flags |= ABSOLUTE;
|
|
} else {
|
|
return INVALID;
|
|
}
|
|
return 0;
|
|
case 2:
|
|
return parse_duration(value, plan.value);
|
|
case 3:
|
|
return parse_duration(value, plan.interval);
|
|
case 4:
|
|
if (value == "0") {
|
|
plan.flags &= ~CANCEL_ON_SET;
|
|
} else if (value == "1") {
|
|
plan.flags |= CANCEL_ON_SET;
|
|
} else {
|
|
return INVALID;
|
|
}
|
|
return 0;
|
|
case 5:
|
|
if (value != "0" && value != "1") {
|
|
return INVALID;
|
|
}
|
|
plan.nonblock = value == "1" ? 1 : 0;
|
|
return 0;
|
|
default:
|
|
return INVALID;
|
|
}
|
|
}
|
|
}
|
|
|
|
int parse_duration(const std::string &text, std::uint64_t &out)
|
|
{
|
|
if (text.size() > 4096) {
|
|
return INVALID;
|
|
}
|
|
const std::string input = trimmed(text);
|
|
std::size_t position = 0;
|
|
while (position < input.size() && decimal(input[position])) {
|
|
++position;
|
|
}
|
|
if (position == 0) {
|
|
return INVALID;
|
|
}
|
|
const std::string whole = input.substr(0, position);
|
|
std::string fraction;
|
|
if (position < input.size() && input[position] == '.') {
|
|
const std::size_t start = ++position;
|
|
while (position < input.size() && decimal(input[position])) {
|
|
++position;
|
|
}
|
|
if (position == start) {
|
|
return INVALID;
|
|
}
|
|
fraction = input.substr(start, position - start);
|
|
}
|
|
const std::uint64_t scale = multiplier(input.substr(position));
|
|
if (scale == 0) {
|
|
return INVALID;
|
|
}
|
|
std::uint64_t fractional = 0;
|
|
const int fraction_status = fractional_ticks(fraction, scale, fractional);
|
|
if (fraction_status != 0) {
|
|
return fraction_status;
|
|
}
|
|
std::uint64_t result = 0;
|
|
const int whole_status = bounded_whole(whole, scale, fractional, result);
|
|
if (whole_status != 0) {
|
|
return whole_status;
|
|
}
|
|
out = result;
|
|
return 0;
|
|
}
|
|
|
|
int parse_plan(const std::string &text, Plan &out)
|
|
{
|
|
if (text.size() > 16384) {
|
|
return INVALID;
|
|
}
|
|
Plan candidate{MONO, 0, 0, 0, 1};
|
|
unsigned seen = 0;
|
|
std::size_t position = 0;
|
|
while (position <= text.size()) {
|
|
const std::size_t found = text.find('\n', position);
|
|
const std::size_t end = found == std::string::npos ? text.size() : found;
|
|
std::string line = text.substr(position, end - position);
|
|
const std::size_t comment = line.find('#');
|
|
if (comment != std::string::npos) {
|
|
line.resize(comment);
|
|
}
|
|
line = trimmed(line);
|
|
if (!line.empty()) {
|
|
const std::size_t equals = line.find('=');
|
|
if (equals == std::string::npos ||
|
|
line.find('=', equals + 1) != std::string::npos) {
|
|
return INVALID;
|
|
}
|
|
const std::string key = trimmed(line.substr(0, equals));
|
|
const std::string value = trimmed(line.substr(equals + 1));
|
|
const int index = plan_key(key);
|
|
if (index < 0 || (seen & (1u << index)) != 0) {
|
|
return INVALID;
|
|
}
|
|
const int result = assign_field(index, value, candidate);
|
|
if (result != 0) {
|
|
return result;
|
|
}
|
|
seen |= 1u << index;
|
|
}
|
|
if (found == std::string::npos) {
|
|
break;
|
|
}
|
|
position = found + 1;
|
|
}
|
|
if ((seen & 7u) != 7u) {
|
|
return INVALID;
|
|
}
|
|
out = candidate;
|
|
return 0;
|
|
}
|
|
|
|
int apply_plan(Bank &bank, const Plan &plan, int &fd)
|
|
{
|
|
if (plan.clock != MONO && plan.clock != REAL) {
|
|
return INVALID;
|
|
}
|
|
if ((plan.flags & ~(ABSOLUTE | CANCEL_ON_SET)) != 0) {
|
|
return INVALID;
|
|
}
|
|
if (plan.nonblock != 0 && plan.nonblock != 1) {
|
|
return INVALID;
|
|
}
|
|
if (plan.value > LIMIT || plan.interval > LIMIT) {
|
|
return RANGE;
|
|
}
|
|
const int created = bank.create(plan.clock, plan.nonblock ? NONBLOCK : 0);
|
|
if (created < 0) {
|
|
return created;
|
|
}
|
|
Setting old{};
|
|
const int result = bank.set(created, plan.flags, plan.value, plan.interval, old);
|
|
if (result != 0) {
|
|
bank.close(created);
|
|
return result;
|
|
}
|
|
fd = created;
|
|
return 0;
|
|
}
|
|
}
|