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

138 lines
3.6 KiB
C++

#include "input.hpp"
#include <algorithm>
#include <utility>
namespace panel {
bool capability_valid(const Capability& cap)
{
if (cap.type == 1) {
if (cap.code < 0 || cap.code > 31) {
return false;
}
return cap.lower == 0 && cap.upper == 1;
}
if (cap.type == 3) {
if (cap.code < 0 || cap.code > 7) {
return false;
}
if (cap.lower < -32768 || cap.upper > 32767) {
return false;
}
return cap.lower <= cap.upper;
}
return false;
}
int normalize_capabilities(const std::vector<Capability>& input,
std::vector<Capability>& output)
{
std::vector<Capability> ordered;
ordered.reserve(input.size());
for (const Capability& cap : input) {
if (!capability_valid(cap)) {
return -1;
}
ordered.push_back(cap);
}
std::sort(ordered.begin(), ordered.end(),
[](const Capability& left, const Capability& right) {
if (left.type != right.type) {
return left.type < right.type;
}
return left.code < right.code;
});
for (std::size_t index = 1; index < ordered.size(); ++index) {
const Capability& previous = ordered[index - 1];
const Capability& current = ordered[index];
if (previous.type == current.type && previous.code == current.code) {
return -1;
}
}
output.swap(ordered);
return 0;
}
int Service::replace_capabilities(int slot, const std::vector<Capability>& caps)
{
if (!valid_slot(slot)) {
return -1;
}
Slot& target = slots_[static_cast<std::size_t>(slot)];
if (target.phase != Phase::Allocated) {
return -2;
}
std::vector<Capability> ordered;
int status = normalize_capabilities(caps, ordered);
if (status != 0) {
return status;
}
if (caps.empty()) {
return 0;
}
target.capabilities = std::move(ordered);
clear_live(target);
counters_.configurations += 1;
return 0;
}
int Service::attach(int slot, int fail)
{
if (!valid_slot(slot)) {
return -1;
}
if (fail != 0 && fail != 1) {
return -1;
}
Slot& target = slots_[static_cast<std::size_t>(slot)];
if (target.phase != Phase::Allocated) {
return -2;
}
if (fail == 1) {
return -5;
}
std::vector<int> initial;
initial.reserve(target.capabilities.size());
for (const Capability& cap : target.capabilities) {
if (cap.type == 1) {
initial.push_back(0);
} else {
initial.push_back(cap.lower);
}
}
target.values.swap(initial);
target.pending.clear();
target.phase = Phase::Registered;
counters_.registrations += 1;
return 0;
}
int Service::detach(int slot)
{
if (!valid_slot(slot)) {
return -1;
}
Slot& target = slots_[static_cast<std::size_t>(slot)];
if (target.phase != Phase::Registered) {
return 0;
}
clear_live(target);
target.phase = Phase::Allocated;
counters_.unregistrations += 1;
return 0;
}
int Service::release(int slot)
{
if (!valid_slot(slot)) {
return -1;
}
Slot& target = slots_[static_cast<std::size_t>(slot)];
if (target.phase == Phase::Empty) {
return 0;
}
if (target.phase == Phase::Registered) {
detach(slot);
}
target.capabilities.clear();
target.name.clear();
clear_live(target);
target.phase = Phase::Empty;
counters_.releases += 1;
return 0;
}
}