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

168 lines
4.7 KiB
C++

#include "log_gate.hpp"
#include <algorithm>
namespace burstlog {
namespace {
bool usable(const Gate* gate, std::uint64_t now, const Result* output)
{
if (gate == nullptr || output == nullptr) {
return false;
}
if (gate->ready != 1 || !valid_time(now)) {
return false;
}
return now >= gate->last;
}
void move_clock(Gate& gate, std::uint64_t now, Result& result)
{
const auto deadline = gate.start + gate.interval;
if (window_due(now, deadline)) {
const auto periods = (now - gate.start) / gate.interval;
result.windows = periods;
result.flushed = gate.pending;
gate.reported = saturating_add(gate.reported, gate.pending);
gate.closed = saturating_add(gate.closed, periods);
gate.start += periods * gate.interval;
gate.used = 0;
gate.pending = 0;
}
gate.last = now;
}
void clear_history(Gate& gate, std::uint64_t now, const Config& config)
{
gate = Gate{};
gate.ready = 1;
gate.start = now;
gate.last = now;
gate.interval = config.interval;
gate.burst = config.burst;
}
}
int gate_init(Gate* gate, std::uint64_t now, const Config& config,
Result* output)
{
if (gate == nullptr || output == nullptr) {
return -1;
}
if (!valid_time(now) || !valid_config(config)) {
return -1;
}
Gate temporary;
clear_history(temporary, now, config);
*gate = temporary;
*output = Result{};
return 0;
}
int gate_log(Gate* gate, std::uint64_t now, std::uint64_t count,
Result* output)
{
if (!usable(gate, now, output) || count > max_burst) {
return -1;
}
Gate temporary = *gate;
Result result;
move_clock(temporary, now, result);
const auto available = temporary.burst - temporary.used;
result.allowed = std::min(available, count);
result.blocked = count - result.allowed;
temporary.used += result.allowed;
temporary.pending = saturating_add(temporary.pending, result.blocked);
temporary.requests = saturating_add(temporary.requests, count);
temporary.emitted = saturating_add(temporary.emitted, result.allowed);
temporary.suppressed = saturating_add(temporary.suppressed, result.blocked);
*gate = temporary;
*output = result;
return 0;
}
int gate_advance(Gate* gate, std::uint64_t now, Result* output)
{
if (!usable(gate, now, output)) {
return -1;
}
Gate temporary = *gate;
Result result;
move_clock(temporary, now, result);
*gate = temporary;
*output = result;
return 0;
}
int gate_configure(Gate* gate, std::uint64_t now, const Config& config,
Result* output)
{
if (!usable(gate, now, output) || !valid_config(config)) {
return -1;
}
Gate temporary = *gate;
Result result;
move_clock(temporary, now, result);
result.flushed = saturating_add(result.flushed, temporary.pending);
temporary.reported = saturating_add(temporary.reported, temporary.pending);
temporary.start = now;
temporary.last = now;
temporary.interval = config.interval;
temporary.burst = config.burst;
temporary.used = 0;
temporary.pending = 0;
*gate = temporary;
*output = result;
return 0;
}
int gate_reset(Gate* gate, std::uint64_t now, Result* output)
{
if (!usable(gate, now, output)) {
return -1;
}
const Config config{gate->interval, gate->burst};
Gate temporary;
clear_history(temporary, now, config);
*gate = temporary;
*output = Result{};
return 0;
}
int gate_snapshot(const Gate* gate, Snapshot* output)
{
if (gate == nullptr || output == nullptr) {
return -1;
}
Snapshot value{
gate->ready,
gate->start,
gate->last,
gate->interval,
gate->burst,
gate->used,
gate->pending,
gate->requests,
gate->emitted,
gate->suppressed,
gate->closed,
gate->reported
};
*output = value;
return 0;
}
int execute(const Command& command, std::array<Gate, 2>* gates,
Result* output)
{
if (gates == nullptr || output == nullptr || command.object >= 2) {
return -1;
}
auto* gate = &(*gates)[command.object];
switch (command.kind) {
case Kind::init:
return gate_init(gate, command.time, command.config, output);
case Kind::log:
return gate_log(gate, command.time, command.count, output);
case Kind::advance:
return gate_advance(gate, command.time, output);
case Kind::configure:
return gate_configure(gate, command.time, command.config, output);
case Kind::reset:
return gate_reset(gate, command.time, output);
case Kind::snapshot:
*output = Result{};
return 0;
}
return -1;
}
}