#include "ledger.hpp" #include namespace scoped { namespace { Result invalid() { Result result; result.code = -1; return result; } void accumulate(Result& target, const Result& source) { target.total += source.total; target.visited += source.visited; target.probes += source.probes; target.stolen += source.stolen; if (source.code != 0) { target.code = source.code; } } bool owner_ok(int owner) { return owner >= 1 && owner <= owner_limit; } bool weight_ok(int weight) { return weight >= -weight_limit && weight <= weight_limit; } } Ledger::Ledger() { reset(); } void Ledger::reset() { groups_.fill(Slot{}); gate_.reset(); } bool Ledger::live(int id) const { return id >= 0 && id < group_limit && groups_[id].live; } bool Ledger::parent_ok(int parent) const { return parent == -1 || live(parent); } bool Ledger::descendant(int id, int ancestor) const { int cursor = id; while (cursor != -1) { if (cursor == ancestor) { return true; } cursor = groups_[cursor].parent; } return false; } std::vector Ledger::children(int id) const { std::vector result; for (int child = 0; child < group_limit; ++child) { if (groups_[child].live && groups_[child].parent == id) { result.push_back(child); } } return result; } std::vector Ledger::subtree(int id) const { std::vector result; std::vector pending{id}; while (!pending.empty()) { const int current = pending.back(); pending.pop_back(); result.push_back(current); const auto next = children(current); pending.insert(pending.end(), next.rbegin(), next.rend()); } return result; } Result Ledger::add(int id, int parent, int weight) { if (id < 0 || id >= group_limit || live(id) || !parent_ok(parent) || !weight_ok(weight)) { return invalid(); } groups_[id] = Slot{true, parent, weight}; return Result{}; } Result Ledger::move(int id, int parent) { if (!live(id) || !parent_ok(parent)) { return invalid(); } if (parent != -1 && descendant(parent, id)) { return invalid(); } groups_[id].parent = parent; return Result{}; } Result Ledger::set_weight(int id, int weight) { if (!live(id) || !weight_ok(weight)) { return invalid(); } groups_[id].weight = weight; return Result{}; } Result Ledger::erase(int id) { if (!live(id)) { return invalid(); } for (int current : subtree(id)) { groups_[current] = Slot{}; } return Result{}; } Result Ledger::report(int owner, int id) { if (!owner_ok(owner) || !live(id)) { return invalid(); } Result result; result.total = 9; result.visited = 3; return result; } Result Ledger::report_node(int owner, int id, int level) { Scope scope(gate_, owner); Result result; if (!scope.acquired()) { result.code = -2; return result; } result.visited = 1; result.total = static_cast(groups_[id].weight) * (level + 1); for (int child : children(id)) { const Result nested = report_node(owner, child, level + 1); accumulate(result, nested); if (nested.code != 0) { break; } } return result; } Result Ledger::adjust(int owner, int id, int delta, int other) { if (!owner_ok(owner) || !owner_ok(other) || owner == other || !live(id) || !weight_ok(delta)) { return invalid(); } for (int current : subtree(id)) { if (!weight_ok(groups_[current].weight + delta)) { return invalid(); } } return adjust_node(owner, id, 0, delta, other); } Result Ledger::adjust_node(int owner, int id, int level, int delta, int other) { Scope scope(gate_, owner); Result result; if (!scope.acquired()) { result.code = -2; return result; } groups_[id].weight += delta; result.visited = 1; result.total = static_cast(groups_[id].weight) * (level + 1); for (int child : children(id)) { const Result nested = adjust_node(owner, child, level + 1, delta, other); accumulate(result, nested); if (nested.code != 0) { return result; } } ++result.probes; if (gate_.acquire(other)) { ++result.stolen; gate_.release(other); } return result; } Gate& Ledger::gate() { return gate_; } const Gate& Ledger::gate() const { return gate_; } std::vector Ledger::snapshot() const { std::vector result; for (int id = 0; id < group_limit; ++id) { if (groups_[id].live) { result.push_back(Group{id, groups_[id].parent, groups_[id].weight}); } } return result; } }