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>
166 lines
4.9 KiB
C++
166 lines
4.9 KiB
C++
#include "object_forest.hpp"
|
|
#include <algorithm>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace object_forest {
|
|
namespace {
|
|
constexpr int BAD_HANDLE = -9;
|
|
constexpr int INVALID = -22;
|
|
constexpr int EXISTS = -17;
|
|
}
|
|
|
|
void Forest::preorder(Handle handle, std::vector<Handle> &out) const
|
|
{
|
|
std::vector<Handle> pending{handle};
|
|
while (!pending.empty()) {
|
|
const Handle current = pending.back();
|
|
pending.pop_back();
|
|
out.push_back(current);
|
|
const Node &node = nodes_[static_cast<std::size_t>(locate(current))];
|
|
for (auto child = node.children.rbegin(); child != node.children.rend(); ++child) {
|
|
pending.push_back(*child);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Forest::postorder(Handle handle, std::vector<Handle> &out) const
|
|
{
|
|
const Node &node = nodes_[static_cast<std::size_t>(locate(handle))];
|
|
for (Handle child : node.children) {
|
|
postorder(child, out);
|
|
}
|
|
out.push_back(handle);
|
|
}
|
|
|
|
int Forest::move(Handle handle, Handle parent, std::size_t position)
|
|
{
|
|
const int source_slot = locate(handle);
|
|
if (source_slot < 0) {
|
|
return BAD_HANDLE;
|
|
}
|
|
if (!valid_parent(parent)) {
|
|
return BAD_HANDLE;
|
|
}
|
|
const Node &source = nodes_[static_cast<std::size_t>(source_slot)];
|
|
const Handle previous_parent = source.parent;
|
|
const std::size_t available = siblings(parent).size() -
|
|
(previous_parent == parent ? 1 : 0);
|
|
if (position > available) {
|
|
return INVALID;
|
|
}
|
|
Handle ancestor = parent;
|
|
while (ancestor != 0) {
|
|
if (ancestor == handle) {
|
|
return INVALID;
|
|
}
|
|
ancestor = nodes_[static_cast<std::size_t>(locate(ancestor))].parent;
|
|
}
|
|
if (duplicate_name(parent, source.name, handle)) {
|
|
return EXISTS;
|
|
}
|
|
Forest draft = *this;
|
|
std::vector<Handle> &old_siblings = draft.siblings(previous_parent);
|
|
const auto old_position = std::find(old_siblings.begin(), old_siblings.end(), handle);
|
|
old_siblings.erase(old_position);
|
|
std::vector<Handle> &destination = draft.siblings(parent);
|
|
destination.insert(destination.begin() + static_cast<std::ptrdiff_t>(position), handle);
|
|
draft.nodes_[static_cast<std::size_t>(source_slot)].parent = parent;
|
|
return commit(draft);
|
|
}
|
|
|
|
int Forest::clone(Handle source, Handle parent, const std::string &new_name,
|
|
Handle &out)
|
|
{
|
|
if (locate(source) < 0) {
|
|
return BAD_HANDLE;
|
|
}
|
|
if (!valid_parent(parent)) {
|
|
return BAD_HANDLE;
|
|
}
|
|
if (!valid_name(new_name)) {
|
|
return INVALID;
|
|
}
|
|
if (duplicate_name(parent, new_name)) {
|
|
return EXISTS;
|
|
}
|
|
std::vector<Handle> originals;
|
|
preorder(source, originals);
|
|
Forest draft = *this;
|
|
std::vector<std::pair<Handle, Handle>> mapping;
|
|
mapping.reserve(originals.size());
|
|
Handle root_copy = 0;
|
|
for (Handle original : originals) {
|
|
const Node &node = nodes_[static_cast<std::size_t>(locate(original))];
|
|
Handle destination_parent = parent;
|
|
if (original != source) {
|
|
const auto entry = std::find_if(mapping.begin(), mapping.end(),
|
|
[&](const std::pair<Handle, Handle> &item) {
|
|
return item.first == node.parent;
|
|
});
|
|
destination_parent = entry->second;
|
|
}
|
|
const std::string &name = original == source ? new_name : node.name;
|
|
Handle copied = 0;
|
|
const int result = draft.reserve_node(destination_parent, name,
|
|
node.properties, copied);
|
|
if (result != 0) {
|
|
return result;
|
|
}
|
|
mapping.push_back({original, copied});
|
|
if (original == source) {
|
|
root_copy = copied;
|
|
}
|
|
}
|
|
const int result = commit(draft);
|
|
if (result != 0) {
|
|
return result;
|
|
}
|
|
out = root_copy;
|
|
return 0;
|
|
}
|
|
|
|
int Forest::retire(Handle handle, std::vector<Handle> &trace)
|
|
{
|
|
const int slot = locate(handle);
|
|
if (slot < 0) {
|
|
return BAD_HANDLE;
|
|
}
|
|
std::vector<Handle> retired;
|
|
postorder(handle, retired);
|
|
Forest draft = *this;
|
|
const Handle parent = nodes_[static_cast<std::size_t>(slot)].parent;
|
|
std::vector<Handle> &owner = draft.siblings(parent);
|
|
owner.erase(std::find(owner.begin(), owner.end(), handle));
|
|
for (Handle child : retired) {
|
|
const int removed = draft.locate(child);
|
|
draft.nodes_[static_cast<std::size_t>(removed)] = Node{};
|
|
}
|
|
const int result = commit(draft);
|
|
if (result != 0) {
|
|
return result;
|
|
}
|
|
trace.swap(retired);
|
|
return 0;
|
|
}
|
|
|
|
int Forest::clear(std::vector<Handle> &trace)
|
|
{
|
|
std::vector<Handle> retired;
|
|
for (Handle root : roots_) {
|
|
postorder(root, retired);
|
|
}
|
|
Forest draft = *this;
|
|
for (Node &node : draft.nodes_) {
|
|
node = Node{};
|
|
}
|
|
draft.roots_.clear();
|
|
const int result = commit(draft);
|
|
if (result != 0) {
|
|
return result;
|
|
}
|
|
trace.swap(retired);
|
|
return 0;
|
|
}
|
|
}
|