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

122 lines
3.5 KiB
C++

#include "activation.hpp"
#include <algorithm>
namespace activation {
Engine::Engine() = default;
Engine::Handle* Engine::find(int token)
{
for (Handle& handle : handles_) {
if (handle.live && handle.view.token == token) {
return &handle;
}
}
return nullptr;
}
int Engine::available(const Handle& handle) const
{
return handle.view.prepared - handle.view.enabled;
}
int Engine::open_handle(int client, int provider)
{
if (client < 0 || client >= 8 || !valid_provider(provider)) {
return invalid;
}
for (Handle& handle : handles_) {
if (!handle.live) {
handle = Handle{};
handle.live = true;
handle.view.token = next_token_;
handle.view.client = client;
handle.view.provider = provider;
last_token_ = next_token_;
++next_token_;
++providers_[provider].view.handles;
return ok;
}
}
return full;
}
int Engine::close_handle(int token)
{
Handle* handle = find(token);
if (!handle) {
return missing;
}
if (handle->view.prepared != 0 || handle->view.enabled != 0) {
return busy;
}
--providers_[handle->view.provider].view.handles;
*handle = Handle{};
return ok;
}
int Engine::execute(const Command& command)
{
events_.clear();
last_token_ = 0;
int result = invalid;
if (command.op == Op::open) {
result = open_handle(command.a, command.b);
} else if (command.op == Op::close) {
result = close_handle(command.a);
} else if (command.op == Op::arm_p) {
result = arm(command.a, true, command.b);
} else if (command.op == Op::arm_e) {
result = arm(command.a, false, command.b);
} else if (command.op == Op::clear) {
result = clear(command.a);
} else if (command.op != Op::invalid) {
Handle* handle = find(command.a);
if (!handle) {
result = missing;
} else if (!valid_count(command.b)) {
result = invalid;
} else {
switch (command.op) {
case Op::reserve:
result = reserve(*handle, command.b);
break;
case Op::gate:
result = gate(*handle, command.b);
break;
case Op::ungate:
result = ungate(*handle, command.b);
break;
case Op::release:
result = release(*handle, command.b);
break;
case Op::activate:
result = activate(*handle, command.b);
break;
case Op::deactivate:
result = deactivate(*handle, command.b);
break;
default:
result = invalid;
break;
}
}
}
last_result_ = result;
return result;
}
Observation Engine::observe() const
{
Observation observation;
observation.rc = last_result_;
observation.token = last_token_;
for (int index = 0; index < provider_count; ++index) {
observation.providers[index] = providers_[index].view;
}
for (const Handle& handle : handles_) {
if (handle.live) {
observation.handles.push_back(handle.view);
}
}
std::sort(observation.handles.begin(), observation.handles.end(),
[](const HandleView& left, const HandleView& right) {
return left.token < right.token;
});
observation.events = events_;
return observation;
}
}