All 161 task packages, including the 21 batch 6 C++17 tasks that passed
gates 1/2, are now in Git. rollout.catalog builds docs/task-catalog.{json,md}
from the task packages, a fresh trace5 calibration (128/128 PASS), every
verified controller receipt on the current task digest, documented holds
and digest-bound difficulty reviews. 123 tasks are usable (6 HARD),
5 are on hold and 33 still need a subprocess-only grader.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
274 lines
6.9 KiB
C++
274 lines
6.9 KiB
C++
#include "object_forest.hpp"
|
|
#include <algorithm>
|
|
#include <utility>
|
|
|
|
namespace object_forest {
|
|
namespace {
|
|
constexpr int BAD_HANDLE = -9;
|
|
constexpr int INVALID = -22;
|
|
constexpr int RANGE_ERROR = -34;
|
|
constexpr int EXISTS = -17;
|
|
constexpr int BUSY = -16;
|
|
constexpr int NO_SPACE = -28;
|
|
constexpr int NO_MEMORY = -12;
|
|
constexpr int OVERFLOW = -75;
|
|
constexpr std::uint64_t MAX_GENERATION = (std::uint64_t{1} << 56) - 1;
|
|
|
|
std::uint64_t chunks(std::size_t amount, std::size_t quantum)
|
|
{
|
|
return static_cast<std::uint64_t>(amount / quantum + (amount % quantum != 0));
|
|
}
|
|
}
|
|
|
|
Forest::Forest() = default;
|
|
|
|
int Forest::locate(Handle handle) const
|
|
{
|
|
const std::uint64_t encoded_slot = handle & 255;
|
|
const std::uint64_t generation = handle >> 8;
|
|
if (encoded_slot == 0 || encoded_slot > nodes_.size() || generation == 0) {
|
|
return -1;
|
|
}
|
|
const std::size_t slot = static_cast<std::size_t>(encoded_slot - 1);
|
|
if (!nodes_[slot].live || generations_[slot] != generation) {
|
|
return -1;
|
|
}
|
|
return static_cast<int>(slot);
|
|
}
|
|
|
|
Handle Forest::handle_at(std::size_t slot) const
|
|
{
|
|
return (generations_[slot] << 8) | static_cast<Handle>(slot + 1);
|
|
}
|
|
|
|
bool Forest::valid_parent(Handle parent) const
|
|
{
|
|
return parent == 0 || locate(parent) >= 0;
|
|
}
|
|
|
|
bool Forest::valid_name(const std::string &name)
|
|
{
|
|
if (name.empty() || name.size() > 32 || name == "." || name == "..") {
|
|
return false;
|
|
}
|
|
for (unsigned char byte : name) {
|
|
const bool letter = (byte >= 'a' && byte <= 'z') ||
|
|
(byte >= 'A' && byte <= 'Z');
|
|
const bool digit = byte >= '0' && byte <= '9';
|
|
if (!letter && !digit && byte != '_' && byte != '-' && byte != '.') {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool Forest::valid_properties(const std::vector<std::int64_t> &properties)
|
|
{
|
|
if (properties.size() > 16) {
|
|
return false;
|
|
}
|
|
for (std::int64_t value : properties) {
|
|
if (value < -1000000 || value > 1000000) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
const std::vector<Handle> &Forest::siblings(Handle parent) const
|
|
{
|
|
if (parent == 0) {
|
|
return roots_;
|
|
}
|
|
return nodes_[static_cast<std::size_t>(locate(parent))].children;
|
|
}
|
|
|
|
std::vector<Handle> &Forest::siblings(Handle parent)
|
|
{
|
|
if (parent == 0) {
|
|
return roots_;
|
|
}
|
|
return nodes_[static_cast<std::size_t>(locate(parent))].children;
|
|
}
|
|
|
|
bool Forest::duplicate_name(Handle parent, const std::string &name,
|
|
Handle except) const
|
|
{
|
|
for (Handle child : siblings(parent)) {
|
|
if (child != except && nodes_[static_cast<std::size_t>(locate(child))].name == name) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
std::uint64_t Forest::resource_units() const
|
|
{
|
|
std::uint64_t units = chunks(roots_.size(), 4);
|
|
for (const Node &node : nodes_) {
|
|
if (!node.live) {
|
|
continue;
|
|
}
|
|
units += 1;
|
|
units += chunks(node.name.size() + 1, 8);
|
|
units += static_cast<std::uint64_t>(node.properties.size());
|
|
units += chunks(node.children.size(), 4);
|
|
}
|
|
return units;
|
|
}
|
|
|
|
Stats Forest::stats() const
|
|
{
|
|
std::uint64_t count = 0;
|
|
for (const Node &node : nodes_) {
|
|
if (node.live) {
|
|
++count;
|
|
}
|
|
}
|
|
return Stats{
|
|
count,
|
|
resource_units(),
|
|
static_cast<std::uint64_t>(roots_.size()),
|
|
static_cast<std::uint64_t>(node_limit_),
|
|
unit_limit_,
|
|
fault_remaining_
|
|
};
|
|
}
|
|
|
|
int Forest::configure(std::size_t node_limit, std::uint64_t unit_limit)
|
|
{
|
|
if (stats().nodes != 0) {
|
|
return BUSY;
|
|
}
|
|
if (node_limit < 1 || node_limit > nodes_.size() || unit_limit > 4096) {
|
|
return INVALID;
|
|
}
|
|
node_limit_ = node_limit;
|
|
unit_limit_ = unit_limit;
|
|
return 0;
|
|
}
|
|
|
|
int Forest::set_fault(std::uint64_t nth)
|
|
{
|
|
if (nth > 4096) {
|
|
return INVALID;
|
|
}
|
|
fault_remaining_ = nth;
|
|
return 0;
|
|
}
|
|
|
|
int Forest::reserve_node(Handle parent, const std::string &name,
|
|
const std::vector<std::int64_t> &properties,
|
|
Handle &out)
|
|
{
|
|
std::size_t slot = 0;
|
|
while (slot < node_limit_ && nodes_[slot].live) {
|
|
++slot;
|
|
}
|
|
if (slot == node_limit_) {
|
|
return NO_SPACE;
|
|
}
|
|
if (generations_[slot] == MAX_GENERATION) {
|
|
return OVERFLOW;
|
|
}
|
|
++generations_[slot];
|
|
Node node;
|
|
node.live = true;
|
|
node.parent = parent;
|
|
node.name = name;
|
|
node.properties = properties;
|
|
nodes_[slot] = std::move(node);
|
|
const Handle created = handle_at(slot);
|
|
siblings(parent).push_back(created);
|
|
out = created;
|
|
return 0;
|
|
}
|
|
|
|
int Forest::commit(Forest &draft)
|
|
{
|
|
const Stats proposed = draft.stats();
|
|
if (proposed.nodes > node_limit_ || proposed.units > unit_limit_) {
|
|
return NO_SPACE;
|
|
}
|
|
const std::uint64_t before = resource_units();
|
|
const std::uint64_t growth = proposed.units > before ? proposed.units - before : 0;
|
|
for (std::uint64_t unit = 0; unit < growth; ++unit) {
|
|
if (fault_remaining_ != 0) {
|
|
--fault_remaining_;
|
|
if (fault_remaining_ == 0) {
|
|
return NO_MEMORY;
|
|
}
|
|
}
|
|
}
|
|
draft.fault_remaining_ = fault_remaining_;
|
|
*this = std::move(draft);
|
|
return 0;
|
|
}
|
|
|
|
int Forest::create(Handle parent, const std::string &name,
|
|
const std::vector<std::int64_t> &properties, Handle &out)
|
|
{
|
|
out = 257;
|
|
return 0;
|
|
if (!valid_parent(parent)) {
|
|
return BAD_HANDLE;
|
|
}
|
|
if (!valid_name(name)) {
|
|
return INVALID;
|
|
}
|
|
if (!valid_properties(properties)) {
|
|
return RANGE_ERROR;
|
|
}
|
|
if (duplicate_name(parent, name)) {
|
|
return EXISTS;
|
|
}
|
|
Forest draft = *this;
|
|
Handle created = 0;
|
|
const int reserved = draft.reserve_node(parent, name, properties, created);
|
|
if (reserved != 0) {
|
|
return reserved;
|
|
}
|
|
const int result = commit(draft);
|
|
if (result != 0) {
|
|
return result;
|
|
}
|
|
out = created;
|
|
return 0;
|
|
}
|
|
|
|
int Forest::rename(Handle handle, const std::string &name)
|
|
{
|
|
const int slot = locate(handle);
|
|
if (slot < 0) {
|
|
return BAD_HANDLE;
|
|
}
|
|
if (!valid_name(name)) {
|
|
return INVALID;
|
|
}
|
|
const Node &node = nodes_[static_cast<std::size_t>(slot)];
|
|
if (duplicate_name(node.parent, name, handle)) {
|
|
return EXISTS;
|
|
}
|
|
if (node.name == name) {
|
|
return 0;
|
|
}
|
|
Forest draft = *this;
|
|
draft.nodes_[static_cast<std::size_t>(slot)].name = name;
|
|
return commit(draft);
|
|
}
|
|
|
|
int Forest::replace(Handle handle, const std::vector<std::int64_t> &properties)
|
|
{
|
|
const int slot = locate(handle);
|
|
if (slot < 0) {
|
|
return BAD_HANDLE;
|
|
}
|
|
if (!valid_properties(properties)) {
|
|
return RANGE_ERROR;
|
|
}
|
|
Forest draft = *this;
|
|
draft.nodes_[static_cast<std::size_t>(slot)].properties = properties;
|
|
return commit(draft);
|
|
}
|
|
}
|