#include "sequence.hpp" #include #include #include namespace sequence { namespace { using Row = std::vector; using Indices = std::vector; 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(current[left])]; const Record& b = rows[static_cast(current[right])]; bool take_right = before(b, a, descending); // ORDER_TIE 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(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++]); } return output; } Reply error(int code) { return Reply{code, {}}; } Reply size_reply(std::size_t size) { return Reply{0, {static_cast(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(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(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(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(left)]; const Row b = next.banks[static_cast(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(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(bank)]; if (static_cast(index) >= rows.size()) { return error(5); } const Record& row = rows[static_cast(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(bank)]; if (static_cast(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(offset)]; minimum = first.key; maximum = first.key; } for (std::int64_t step = 0; step < count; ++step) { const Record& row = rows[static_cast(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(bank)]; Indices positions; for (std::size_t index = 0; index < rows.size(); ++index) { if (rows[index].ticket == ticket) { positions.push_back(static_cast(index)); } } if (positions.empty()) { return error(5); } return Reply{0, std::move(positions)}; } State Engine::inspect() const { return storage_->read(); } }