amos 9d633e9e67 Batch 15 (20261023-C-hwe-scale-15): 320 HWE-derived repair tasks
One user-space repair task per eligible HWE debug-diag / compile-deploy base
ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content
filter. Each ships a public spec with a check-to-basis table, at least two
defects with single-fix fixtures, a symptom-masking fixture and an alternative
correct implementation. Claude re-checked all 320: structure, the HWE quality
items and trace6 Docker calibration receipts against current digests
(320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120
ad-hoc family names; the next batch maps them to the canonical list.
The batch's 1,256-line progress log stays in its factory report and the
36-local branch batch15-hwe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 01:55:56 +08:00

122 lines
4.7 KiB
C++

#include "counter.hpp"
#include <algorithm>
#include <map>
namespace resource {
Counter::Counter() : owners_{}, finalized_{}, next_id_(1) {}
Counter::~Counter() = default;
bool Counter::slot_ok(std::size_t slot) { return slot < slot_count; }
bool Counter::value_ok(int value) { return value_min <= value && value <= value_max; }
Result Counter::ok() { return {Code::ok, false, 0}; }
Result Counter::ok(int payload) { return {Code::ok, true, payload}; }
Result Counter::fail(Code code) { return {code, false, 0}; }
std::shared_ptr<Counter::Object> Counter::fresh(int value) {
if (next_id_ > maximum_commands) return {};
auto pointer = std::shared_ptr<Object>(new Object{next_id_, value},
[this](Object* object) {
finalized_.push_back(object->id);
delete object;
});
++next_id_;
return pointer;
}
Result Counter::create(std::size_t slot, int value) {
if (!slot_ok(slot)) return fail(Code::bad_argument);
if (!value_ok(value)) return fail(Code::range);
if (owners_[slot]) return fail(Code::occupied);
auto object = fresh(value);
if (!object) return fail(Code::limit);
owners_[slot] = std::move(object);
return ok();
}
Result Counter::retain(std::size_t source, std::size_t destination) {
if (!slot_ok(source) || !slot_ok(destination)) return fail(Code::bad_argument);
if (!owners_[source]) return fail(Code::empty);
if (owners_[destination]) return fail(Code::occupied);
owners_[destination] = owners_[source];
return ok();
}
Result Counter::release(std::size_t slot) {
if (!slot_ok(slot)) return fail(Code::bad_argument);
if (!owners_[slot]) return fail(Code::empty);
owners_[slot].reset();
return ok();
}
Result Counter::move(std::size_t source, std::size_t destination) {
if (!slot_ok(source) || !slot_ok(destination)) return fail(Code::bad_argument);
if (!owners_[source]) return fail(Code::empty);
if (source == destination) return ok();
owners_[destination] = std::move(owners_[source]);
return ok();
}
Result Counter::swap(std::size_t left, std::size_t right) {
if (!slot_ok(left) || !slot_ok(right)) return fail(Code::bad_argument);
owners_[left].swap(owners_[right]);
return ok();
}
Result Counter::set(std::size_t slot, int value) {
if (!slot_ok(slot)) return fail(Code::bad_argument);
if (!value_ok(value)) return fail(Code::range);
if (!owners_[slot]) return fail(Code::empty);
owners_[slot]->value = value;
return ok();
}
Result Counter::add(std::size_t slot, int delta) {
if (!slot_ok(slot)) return fail(Code::bad_argument);
if (!owners_[slot]) return fail(Code::empty);
long long sum = static_cast<long long>(owners_[slot]->value) + delta;
if (sum < value_min || sum > value_max) return fail(Code::range);
owners_[slot]->value = static_cast<int>(sum);
return ok();
}
Result Counter::read(std::size_t slot) const {
if (!slot_ok(slot)) return fail(Code::bad_argument);
if (!owners_[slot]) return fail(Code::empty);
return ok(owners_[slot]->value);
}
Result Counter::count(std::size_t slot) const {
if (!slot_ok(slot)) return fail(Code::bad_argument);
if (!owners_[slot]) return fail(Code::empty);
return ok(static_cast<int>(owners_[slot].use_count()));
}
Result Counter::clone(std::size_t source, std::size_t destination) {
if (!slot_ok(source) || !slot_ok(destination)) return fail(Code::bad_argument);
if (!owners_[source]) return fail(Code::empty);
if (owners_[destination]) return fail(Code::occupied);
auto object = fresh(owners_[source]->value);
if (!object) return fail(Code::limit);
owners_[destination] = std::move(object);
return ok();
}
Result Counter::replace(std::size_t slot, int value) {
if (!slot_ok(slot)) return fail(Code::bad_argument);
if (!value_ok(value)) return fail(Code::range);
auto object = fresh(value);
if (!object) return fail(Code::limit);
owners_[slot] = std::move(object);
return ok();
}
Result Counter::clear() {
for (auto& owner : owners_) owner.reset();
return ok();
}
Result Counter::snapshot_result() const { return ok(); }
Snapshot Counter::snapshot() const {
Snapshot state{};
state.present.fill(false);
state.finalized = finalized_;
std::map<unsigned, bool> live;
for (std::size_t slot = 0; slot < slot_count; ++slot) {
const auto& owner = owners_[slot];
if (!owner) continue;
state.present[slot] = true;
state.slots[slot] = {owner->id, owner->value,
static_cast<unsigned>(owner.use_count())};
live[owner->id] = true;
}
state.active = static_cast<unsigned>(live.size());
return state;
}
unsigned Counter::created_count() const { return next_id_ - 1; }
bool Counter::invariant() const { return true; }
}