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>
135 lines
2.8 KiB
C++
135 lines
2.8 KiB
C++
#include "segment_bank.hpp"
|
|
#include <algorithm>
|
|
namespace segment {
|
|
Bank::Bank() : next_id_(1)
|
|
{
|
|
}
|
|
Bank::Object *Bank::object(int id)
|
|
{
|
|
for (auto &entry : objects_) {
|
|
if (entry.id == id) {
|
|
return &entry;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
const Bank::Object *Bank::object(int id) const
|
|
{
|
|
for (const auto &entry : objects_) {
|
|
if (entry.id == id) {
|
|
return &entry;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
bool Bank::retained(int id) const
|
|
{
|
|
const Object *entry = object(id);
|
|
return entry != nullptr;
|
|
}
|
|
std::size_t Bank::references(int id) const
|
|
{
|
|
std::size_t result = 0;
|
|
for (const auto &handle : handles_) {
|
|
if (handle.id == id) {
|
|
++result;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
Snapshot Bank::describe(const Object &entry) const
|
|
{
|
|
Snapshot result;
|
|
result.id = entry.id;
|
|
result.key = entry.key;
|
|
result.size = entry.data.size();
|
|
result.attached = references(entry.id);
|
|
result.removed = entry.removed ? 1 : 0;
|
|
return result;
|
|
}
|
|
int Bank::create(int key, std::size_t size)
|
|
{
|
|
if (key < 1 || key > 65535) {
|
|
return -22;
|
|
}
|
|
if (size < 1 || size > 256) {
|
|
return -22;
|
|
}
|
|
if (find(key) >= 0) {
|
|
return -17;
|
|
}
|
|
if (objects_.size() == 8) {
|
|
return -28;
|
|
}
|
|
Object entry;
|
|
entry.id = next_id_;
|
|
entry.key = key;
|
|
entry.removed = false;
|
|
entry.data.assign(size, 0x10);
|
|
objects_.push_back(entry);
|
|
++next_id_;
|
|
return entry.id;
|
|
}
|
|
int Bank::find(int key) const
|
|
{
|
|
if (key < 1 || key > 65535) {
|
|
return -22;
|
|
}
|
|
for (const auto &entry : objects_) {
|
|
if (entry.key == key && !entry.removed) {
|
|
return entry.id;
|
|
}
|
|
}
|
|
return -2;
|
|
}
|
|
int Bank::stat(int id, Snapshot &out) const
|
|
{
|
|
const Object *entry = object(id);
|
|
if (entry == nullptr) {
|
|
return -2;
|
|
}
|
|
out = describe(*entry);
|
|
return 0;
|
|
}
|
|
void Bank::collect(int id)
|
|
{
|
|
const Object *entry = object(id);
|
|
if (entry == nullptr || !entry->removed) {
|
|
return;
|
|
}
|
|
if (references(id) != 0) {
|
|
return;
|
|
}
|
|
auto at = std::remove_if(objects_.begin(), objects_.end(),
|
|
[id](const Object &item) { return item.id == id; });
|
|
objects_.erase(at, objects_.end());
|
|
}
|
|
int Bank::remove(int id)
|
|
{
|
|
Object *entry = object(id);
|
|
if (entry == nullptr) {
|
|
return -2;
|
|
}
|
|
entry->removed = true;
|
|
collect(id);
|
|
return 0;
|
|
}
|
|
Totals Bank::totals() const
|
|
{
|
|
Totals result{objects_.size(), 0, handles_.size(), 0};
|
|
for (const auto &entry : objects_) {
|
|
result.bytes += entry.data.size();
|
|
if (!entry.removed) {
|
|
++result.names;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
void Bank::reset()
|
|
{
|
|
objects_.clear();
|
|
handles_.clear();
|
|
next_id_ = 1;
|
|
}
|
|
}
|