Batch 21 (20261027-A-bos-redo-21) rebuilt 40 of the 53 rejected Bos-qiao environments: hidden assertions that ran inside the candidate's process now run in a trusted parent that reads observations from a low-UID worker; gold bytes are unchanged. 9 were excluded for scope (ROS2, oversize, security core), 3 are blocked because the source requires an exec bit the platform does not freeze, 1 lacks requests/bs4 in trace6. Batch 18 (20261026-C-hwe-codegen-18) adds its last 34 of 134 tasks; the first 100 landed in 2f32624d. Claude reviewed 74/74 USABLE against current digests, canonical families, no ID or source overlap with main, plain public docs. Only the new task directories are taken; the stale pre-batch-20 copies of concur-release-capsule and resource-temp-link on the batch 21 branch and both branches' progress notes stay as main has them. Difficulty is unmeasured. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
260 lines
8.1 KiB
C++
260 lines
8.1 KiB
C++
#include "sequence.hpp"
|
|
#include <algorithm>
|
|
#include <numeric>
|
|
#include <utility>
|
|
namespace sequence {
|
|
namespace {
|
|
using Row = std::vector<Record>;
|
|
using Indices = std::vector<std::int64_t>;
|
|
State initial_state() {
|
|
State state;
|
|
state.next_ticket = 1;
|
|
return state;
|
|
}
|
|
bool valid_bank(std::int64_t bank) {
|
|
return bank >= 0 && bank < 3;
|
|
}
|
|
bool valid_direction(std::int64_t descending) {
|
|
return descending == 0 || descending == 1;
|
|
}
|
|
bool before(const Record& left, const Record& right, bool descending) {
|
|
if (descending) {
|
|
return left.key > right.key;
|
|
}
|
|
return left.key < right.key;
|
|
}
|
|
bool monotone(const Row& rows, bool descending) {
|
|
for (std::size_t index = 1; index < rows.size(); ++index) {
|
|
if (before(rows[index], rows[index - 1], descending)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
Indices sorted_indices(const Row& rows, bool descending) {
|
|
Indices current(rows.size());
|
|
std::iota(current.begin(), current.end(), 0);
|
|
Indices scratch(rows.size());
|
|
for (std::size_t width = 1; width < rows.size(); width *= 2) {
|
|
for (std::size_t base = 0; base < rows.size(); base += 2 * width) {
|
|
const std::size_t middle = std::min(base + width, rows.size());
|
|
const std::size_t finish = std::min(base + 2 * width, rows.size());
|
|
std::size_t left = base;
|
|
std::size_t right = middle;
|
|
std::size_t output = base;
|
|
while (left < middle && right < finish) {
|
|
const Record& a = rows[static_cast<std::size_t>(current[left])];
|
|
const Record& b = rows[static_cast<std::size_t>(current[right])];
|
|
bool take_right = before(b, a, descending);
|
|
if (a.key == b.key) {
|
|
take_right = b.payload < a.payload;
|
|
}
|
|
if (take_right) {
|
|
scratch[output++] = current[right++];
|
|
} else {
|
|
scratch[output++] = current[left++];
|
|
}
|
|
}
|
|
while (left < middle) {
|
|
scratch[output++] = current[left++];
|
|
}
|
|
while (right < finish) {
|
|
scratch[output++] = current[right++];
|
|
}
|
|
}
|
|
current.swap(scratch);
|
|
}
|
|
return current;
|
|
}
|
|
Row reordered(const Row& input, const Indices& indices) {
|
|
Row output;
|
|
output.reserve(indices.size());
|
|
for (std::int64_t index : indices) {
|
|
output.push_back(input[static_cast<std::size_t>(index)]);
|
|
}
|
|
return output;
|
|
}
|
|
Row joined(const Row& left, const Row& right, bool descending) {
|
|
Row output;
|
|
output.reserve(left.size() + right.size());
|
|
std::size_t a = 0;
|
|
std::size_t b = 0;
|
|
while (a < left.size() && b < right.size()) {
|
|
if (before(right[b], left[a], descending)) {
|
|
output.push_back(right[b++]);
|
|
} else {
|
|
output.push_back(left[a++]);
|
|
}
|
|
}
|
|
while (a < left.size()) {
|
|
output.push_back(left[a++]);
|
|
}
|
|
// MERGE_TAIL_BEGIN
|
|
while (b < right.size()) {
|
|
output.push_back(right[b++]);
|
|
}
|
|
// MERGE_TAIL_END
|
|
return output;
|
|
}
|
|
Reply error(int code) {
|
|
return Reply{code, {}};
|
|
}
|
|
Reply size_reply(std::size_t size) {
|
|
return Reply{0, {static_cast<std::int64_t>(size)}};
|
|
}
|
|
}
|
|
struct Engine::Storage {
|
|
State state = initial_state();
|
|
State read() const {
|
|
return state;
|
|
}
|
|
void accept(State next) {
|
|
state = std::move(next);
|
|
}
|
|
};
|
|
Engine::Engine() : storage_(new Storage) {
|
|
}
|
|
Engine::~Engine() = default;
|
|
Reply Engine::reset(std::int64_t bank) {
|
|
if (!valid_bank(bank)) {
|
|
return error(1);
|
|
}
|
|
State next = storage_->read();
|
|
Row& rows = next.banks[static_cast<std::size_t>(bank)];
|
|
const std::size_t removed = rows.size();
|
|
rows.clear();
|
|
storage_->accept(std::move(next));
|
|
return size_reply(removed);
|
|
}
|
|
Reply Engine::append(std::int64_t bank, std::int64_t key, std::int64_t payload) {
|
|
if (!valid_bank(bank)) {
|
|
return error(1);
|
|
}
|
|
if (key < -2147483648LL || key > 2147483647LL || payload < 0 || payload > 65535) {
|
|
return error(2);
|
|
}
|
|
State next = storage_->read();
|
|
Row& rows = next.banks[static_cast<std::size_t>(bank)];
|
|
if (rows.size() == 16) {
|
|
return error(3);
|
|
}
|
|
if (next.next_ticket > 255) {
|
|
return error(6);
|
|
}
|
|
rows.push_back(Record{key, payload, next.next_ticket});
|
|
++next.next_ticket;
|
|
const std::size_t size = rows.size();
|
|
storage_->accept(std::move(next));
|
|
return size_reply(size);
|
|
}
|
|
Reply Engine::order(std::int64_t bank, std::int64_t descending) {
|
|
if (!valid_bank(bank)) {
|
|
return error(1);
|
|
}
|
|
if (!valid_direction(descending)) {
|
|
return error(2);
|
|
}
|
|
State next = storage_->read();
|
|
Row& rows = next.banks[static_cast<std::size_t>(bank)];
|
|
Indices permutation = sorted_indices(rows, descending != 0);
|
|
rows = reordered(rows, permutation);
|
|
storage_->accept(std::move(next));
|
|
return Reply{0, std::move(permutation)};
|
|
}
|
|
Reply Engine::merge(std::int64_t destination, std::int64_t left,
|
|
std::int64_t right, std::int64_t descending) {
|
|
if (!valid_bank(destination) || !valid_bank(left) || !valid_bank(right)) {
|
|
return error(1);
|
|
}
|
|
if (!valid_direction(descending)) {
|
|
return error(2);
|
|
}
|
|
State next = storage_->read();
|
|
const Row a = next.banks[static_cast<std::size_t>(left)];
|
|
const Row b = next.banks[static_cast<std::size_t>(right)];
|
|
if (!monotone(a, descending != 0) || !monotone(b, descending != 0)) {
|
|
return error(4);
|
|
}
|
|
if (a.size() + b.size() > 16) {
|
|
return error(3);
|
|
}
|
|
Row result = joined(a, b, descending != 0);
|
|
const std::size_t size = result.size();
|
|
next.banks[static_cast<std::size_t>(destination)] = std::move(result);
|
|
storage_->accept(std::move(next));
|
|
return size_reply(size);
|
|
}
|
|
Reply Engine::item(std::int64_t bank, std::int64_t index) const {
|
|
if (!valid_bank(bank)) {
|
|
return error(1);
|
|
}
|
|
if (index < 0 || index > 15) {
|
|
return error(2);
|
|
}
|
|
State state = storage_->read();
|
|
const Row& rows = state.banks[static_cast<std::size_t>(bank)];
|
|
if (static_cast<std::size_t>(index) >= rows.size()) {
|
|
return error(5);
|
|
}
|
|
const Record& row = rows[static_cast<std::size_t>(index)];
|
|
return Reply{0, {row.key, row.payload, row.ticket}};
|
|
}
|
|
Reply Engine::range(std::int64_t bank, std::int64_t offset, std::int64_t count) const {
|
|
if (!valid_bank(bank)) {
|
|
return error(1);
|
|
}
|
|
if (offset < 0 || offset > 16 || count < 0 || count > 16) {
|
|
return error(2);
|
|
}
|
|
State state = storage_->read();
|
|
const Row& rows = state.banks[static_cast<std::size_t>(bank)];
|
|
if (static_cast<std::size_t>(offset + count) > rows.size()) {
|
|
return error(5);
|
|
}
|
|
std::int64_t sum = 0;
|
|
std::int64_t payload_sum = 0;
|
|
std::int64_t minimum = 0;
|
|
std::int64_t maximum = 0;
|
|
if (count != 0) {
|
|
const Record& first = rows[static_cast<std::size_t>(offset)];
|
|
minimum = first.key;
|
|
maximum = first.key;
|
|
}
|
|
for (std::int64_t step = 0; step < count; ++step) {
|
|
const Record& row = rows[static_cast<std::size_t>(offset + step)];
|
|
sum += row.key;
|
|
payload_sum += row.payload;
|
|
if (row.key < minimum) {
|
|
minimum = row.key;
|
|
}
|
|
if (row.key > maximum) {
|
|
maximum = row.key;
|
|
}
|
|
}
|
|
return Reply{0, {count, sum, minimum, maximum, payload_sum}};
|
|
}
|
|
Reply Engine::locate(std::int64_t bank, std::int64_t ticket) const {
|
|
if (!valid_bank(bank)) {
|
|
return error(1);
|
|
}
|
|
if (ticket < 1 || ticket > 255) {
|
|
return error(2);
|
|
}
|
|
State state = storage_->read();
|
|
const Row& rows = state.banks[static_cast<std::size_t>(bank)];
|
|
Indices positions;
|
|
for (std::size_t index = 0; index < rows.size(); ++index) {
|
|
if (rows[index].ticket == ticket) {
|
|
positions.push_back(static_cast<std::int64_t>(index));
|
|
}
|
|
}
|
|
if (positions.empty()) {
|
|
return error(5);
|
|
}
|
|
return Reply{0, std::move(positions)};
|
|
}
|
|
State Engine::inspect() const {
|
|
return storage_->read();
|
|
}
|
|
}
|