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

154 lines
3.1 KiB
C++

#include "group.hpp"
#include <algorithm>
namespace group {
int Engine::configure(const Config &config)
{
const int result = validate_config(config);
if (result != 0) {
return result;
}
if (active_) {
return busy;
}
config_ = config;
active_ = true;
return 0;
}
bool Engine::contains(Word id) const
{
return accepted_.count(id) != 0;
}
int Engine::enqueue(const Item &item)
{
if (!active_) {
return inactive;
}
if (item.id == 0 || item.id > max_id || item.value > max_value) {
return range;
}
if (contains(item.id)) {
return exists;
}
if (closing_) {
return closed;
}
if (queue_.size() == config_.capacity) {
return full;
}
queue_.push_back(item);
accepted_.insert(item.id);
++generation_;
return 0;
}
int Engine::close()
{
if (!active_) {
return inactive;
}
if (!closing_) {
closing_ = true;
++generation_;
}
return 0;
}
int Engine::fail_next(Word count)
{
if (!active_) {
return inactive;
}
return journal_.fail_next(count);
}
std::vector<Item> Engine::prefix(Word count) const
{
std::vector<Item> result;
result.reserve(static_cast<std::size_t>(count));
for (Word index = 0; index < count; ++index) {
result.push_back(queue_[static_cast<std::size_t>(index)]);
}
return result;
}
void Engine::consume(Word count)
{
for (Word i = 0; i < count; ++i) queue_.pop_front();
}
int Engine::pump(Receipt &out)
{
if (!active_) {
return inactive;
}
const Word count = take_count(queue_.size(), config_.width, closing_);
if (count == 0) {
if (closing_ && queue_.empty()) {
return 2;
}
return 1;
}
const std::vector<Item> batch = prefix(count);
Receipt receipt;
const int result = journal_.commit(next_ticket_, batch, receipt);
if (result != 0) {
return result;
}
consume(count);
++next_ticket_;
out = receipt;
return 0;
}
int Engine::peek(Word index, Item &out) const
{
if (!active_) {
return inactive;
}
if (index >= queue_.size()) {
return missing;
}
out = queue_[static_cast<std::size_t>(index)];
return 0;
}
int Engine::read(Word index, Receipt &out) const
{
if (!active_) {
return inactive;
}
return journal_.read(index, out);
}
Status Engine::status() const
{
Status result;
result.active = active_;
result.width = config_.width;
result.capacity = config_.capacity;
result.closing = closing_;
result.queued = queue_.size();
result.accepted = accepted_.size();
result.generation = generation_;
result.ready = active_ && take_count(queue_.size(), config_.width, closing_) != 0;
result.next_ticket = next_ticket_;
result.journal = journal_.info();
return result;
}
void Engine::reset()
{
active_ = false;
closing_ = false;
config_ = Config{};
queue_.clear();
accepted_.clear();
generation_ = 0;
next_ticket_ = 1;
journal_.reset();
}
}