amos 8b7616bfd8 Batch 22 (20261027-C-hwe-hard-22): add 63 calibrated HWE hard-design tasks
From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27,
11 canonical families), 38 skipped for scope or duplication. Each task
has at least one independent contract-edge defect, boundary types
rotate, and public docs state trap clauses as plain contract prose.
Claude reviewed 63/63 USABLE against current digests. Difficulty is
unmeasured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 16:14:57 +08:00

165 lines
3.9 KiB
C++

#include "latch.hpp"
#include <limits>
#include <set>
namespace release {
namespace {
bool decimal(const std::string &text)
{
if (text.empty()) {
return false;
}
for (unsigned char byte : text) {
if (byte < '0' || byte > '9') {
return false;
}
}
return true;
}
int parse_component(const std::string &text, Id &out)
{
Count value = 0;
int result = parse_number(text, std::numeric_limits<Id>::max(), value);
if (result != 0) {
return result;
}
out = static_cast<Id>(value);
return 0;
}
int add_participants(Latch &latch, const std::vector<Id> &ids)
{
for (Id id : ids) {
int result = latch.join(id);
if (result < 0) {
return result;
}
}
return 0;
}
int add_waiters(Latch &latch, const std::vector<Id> &ids)
{
for (Id id : ids) {
int result = latch.await(id);
if (result != 0) {
return result;
}
}
return 0;
}
}
int parse_number(const std::string &text, Count limit, Count &out)
{
if (!decimal(text)) {
return -22;
}
Count value = 0;
for (unsigned char byte : text) {
Count digit = byte - '0';
if (value > limit / 10) {
return -34;
}
if (value == limit / 10 && digit > limit % 10) {
return -34;
}
value = value * 10 + digit;
}
out = value;
return 0;
}
int parse_ids(const std::string &text, std::vector<Id> &out)
{
std::vector<Id> result;
std::set<Id> seen;
if (text == "-") {
out = result;
return 0;
}
std::size_t start = 0;
while (true) {
std::size_t end = text.find(',', start);
std::string token = end == std::string::npos
? text.substr(start) : text.substr(start, end - start);
Id id = 0;
int status = parse_component(token, id);
if (status != 0) {
return status;
}
if (!seen.insert(id).second) {
return -17;
}
if (result.size() == entry_limit) {
return -28;
}
result.push_back(id);
if (end == std::string::npos) {
break;
}
start = end + 1;
}
out = result;
return 0;
}
int initialize(Latch &latch, const std::string &target,
const std::string &participants, const std::string &waiters)
{
Count value = 0;
int status = parse_number(target, count_limit, value);
if (status != 0) {
return status;
}
std::vector<Id> participant_ids;
status = parse_ids(participants, participant_ids);
if (status != 0) {
return status;
}
std::vector<Id> waiter_ids;
status = parse_ids(waiters, waiter_ids);
if (status != 0) {
return status;
}
Latch proposed;
status = proposed.reset(value);
if (status != 0) {
return status;
}
status = add_participants(proposed, participant_ids);
if (status != 0) {
return status;
}
status = add_waiters(proposed, waiter_ids);
if (status != 0) {
return status;
}
latch = proposed;
return 0;
}
int parse_waiter(const std::string &text, WaitInfo &out)
{
std::size_t separator = text.find('|');
if (separator == std::string::npos) {
return -22;
}
if (text.find('|', separator + 1) != std::string::npos) {
return -22;
}
std::string state_text = text.substr(0, separator);
std::string ticket_text = text.substr(separator + 1);
Count state = 0;
int status = parse_number(state_text, 2, state);
if (status != 0) {
return status;
}
Count ticket = 0;
status = parse_number(ticket_text, std::numeric_limits<Count>::max(), ticket);
if (status != 0) {
return status;
}
WaitInfo result;
result.state = static_cast<WaitState>(state);
result.ticket = ticket;
out = result;
return 0;
}
}