324 lines
11 KiB
C++

#include "affinity.hpp"
#include <iostream>
#include <limits>
#include <stdexcept>
#include <string>
#include <utility>
using namespace affinity;
namespace {
std::string quote_bytes(const std::string &value)
{
constexpr char digits[] = "0123456789abcdef";
std::string text = "\"";
for (unsigned char byte : value) {
if (byte == '"' || byte == '\\') {
text.push_back('\\');
text.push_back(static_cast<char>(byte));
} else if (byte < 32 || byte > 126) {
text += "\\u00";
text.push_back(digits[byte >> 4]);
text.push_back(digits[byte & 15]);
} else {
text.push_back(static_cast<char>(byte));
}
}
text.push_back('"');
return text;
}
std::string boolean(bool value)
{
return value ? "true" : "false";
}
template<class Range, class Encode>
std::string sequence(const Range &items, Encode encode)
{
std::string output = "[";
bool first = true;
for (const auto &item : items) {
if (!first) output.push_back(',');
first = false;
output += encode(item);
}
output.push_back(']');
return output;
}
std::string set_json(const CoreSet &set)
{
return sequence(set.ids, [](std::uint16_t id) { return std::to_string(id); });
}
std::string core_json(const Core &core)
{
return "[" + std::to_string(core.id) + "," + std::to_string(core.capacity) + "]";
}
std::string optional_core(const std::optional<std::uint16_t> &core)
{
return core.has_value() ? std::to_string(*core) : "null";
}
std::string job_json(const Job &job)
{
return "[" + quote_bytes(job.id) + "," + std::to_string(job.priority) + "," +
set_json(job.allowed) + "," + optional_core(job.previous) + "]";
}
std::string problem_json(const Problem &problem)
{
return "[" + sequence(problem.cores, core_json) + "," +
sequence(problem.jobs, job_json) + "]";
}
std::string binding_json(const Binding &binding)
{
return "[" + quote_bytes(binding.job) + "," + optional_core(binding.core) + "]";
}
std::string load_json(const CoreLoad &load)
{
return "[" + std::to_string(load.core) + "," + std::to_string(load.used) + "]";
}
std::string plan_json(const Plan &plan)
{
return "[" + std::to_string(plan.accepted) + "," + std::to_string(plan.retained) +
"," + sequence(plan.bindings, binding_json) + "," +
sequence(plan.loads, load_json) + "]";
}
std::string hex_bytes(const std::vector<std::uint8_t> &bytes)
{
constexpr char digits[] = "0123456789abcdef";
if (bytes.empty()) return "-";
std::string output;
output.reserve(bytes.size() * 2);
for (std::uint8_t byte : bytes) {
output.push_back(digits[byte >> 4]);
output.push_back(digits[byte & 15]);
}
return output;
}
std::string token()
{
std::string value;
if (!(std::cin >> value)) throw std::runtime_error("missing token");
return value;
}
std::uint64_t number(std::uint64_t maximum)
{
const std::string text = token();
if (text.empty() || text.front() == '-') throw std::runtime_error("unsigned number");
std::size_t length = 0;
const auto result = std::stoull(text, &length, 10);
if (length != text.size() || result > maximum) throw std::runtime_error("number range");
return result;
}
std::int64_t signed_number(std::int64_t minimum, std::int64_t maximum)
{
const std::string text = token();
std::size_t length = 0;
const auto result = std::stoll(text, &length, 10);
if (length != text.size() || result < minimum || result > maximum)
throw std::runtime_error("signed number range");
return result;
}
int nibble(char byte)
{
if (byte >= '0' && byte <= '9') return byte - '0';
if (byte >= 'a' && byte <= 'f') return byte - 'a' + 10;
if (byte >= 'A' && byte <= 'F') return byte - 'A' + 10;
throw std::runtime_error("hex digit");
}
std::vector<std::uint8_t> read_bytes()
{
const std::string text = token();
if (text == "-") return {};
if (text.size() % 2 != 0) throw std::runtime_error("hex length");
std::vector<std::uint8_t> bytes;
bytes.reserve(text.size() / 2);
for (std::size_t at = 0; at < text.size(); at += 2) {
bytes.push_back(static_cast<std::uint8_t>(nibble(text[at]) * 16 + nibble(text[at + 1])));
}
return bytes;
}
std::string read_text()
{
const auto bytes = read_bytes();
return std::string(bytes.begin(), bytes.end());
}
CoreSet read_set()
{
const auto count = number(4096);
CoreSet input;
for (std::size_t index = 0; index < count; ++index) {
input.ids.push_back(static_cast<std::uint16_t>(number(65535)));
}
return input;
}
void require_row(const char *wanted)
{
if (token() != wanted) throw std::runtime_error("unexpected data row");
}
Problem read_problem()
{
const auto core_count = number(4096);
const auto job_count = number(4096);
Problem input;
for (std::size_t index = 0; index < core_count; ++index) {
require_row("K");
Core core;
core.id = static_cast<std::uint16_t>(number(65535));
core.capacity = static_cast<std::uint8_t>(number(255));
input.cores.push_back(core);
}
for (std::size_t index = 0; index < job_count; ++index) {
require_row("J");
Job job;
job.id = read_text();
job.priority = static_cast<std::int32_t>(signed_number(
std::numeric_limits<std::int32_t>::min(), std::numeric_limits<std::int32_t>::max()));
const auto previous = signed_number(-1, 65535);
if (previous >= 0) job.previous = static_cast<std::uint16_t>(previous);
job.allowed = read_set();
input.jobs.push_back(std::move(job));
}
return input;
}
Problem dirty_problem()
{
Problem result;
result.cores.push_back(Core{9, 3});
result.jobs.push_back(Job{"old_job", 12, CoreSet{{9}}, 9});
return result;
}
Plan dirty_plan()
{
Plan result;
result.accepted = 77;
result.retained = 88;
result.bindings.push_back(Binding{"old_binding", 9});
result.loads.push_back(CoreLoad{9, 7});
return result;
}
struct Bank {
Problem raw;
Problem decoded;
bool decoded_ok = false;
Plan last_plan;
Plan held_plan;
std::vector<std::uint8_t> last_bytes;
};
std::string result_row(const std::string &command, Error error,
const std::string &value, bool unchanged)
{
return "[" + quote_bytes(command) + "," + std::to_string(static_cast<int>(error)) +
"," + value + "," + boolean(unchanged) + "]";
}
}
int main()
{
try {
Bank banks[3];
std::size_t selected = 0;
std::string command;
while (std::cin >> command) {
if (command == "G") {
selected = number(2);
const Bank &bank = banks[selected];
std::cout << "[\"G\"," << problem_json(bank.raw) << ',' <<
plan_json(bank.last_plan) << ',' << quote_bytes(hex_bytes(bank.last_bytes)) << "]\n";
continue;
}
Bank &bank = banks[selected];
std::string row;
if (command == "N") {
const std::string input = read_text();
const std::string original = input;
const bool result = valid_job_id(input);
row = "[\"N\"," + boolean(result) + "," + boolean(input == original) + "]";
} else if (command == "L" || command == "M") {
const std::string input = read_text();
const std::string original = input;
CoreSet output{{17, 19}};
const Error error = command == "L" ? parse_list(input, output) : parse_mask(input, output);
row = result_row(command, error, set_json(output), input == original);
} else if (command == "FL" || command == "FM") {
const CoreSet input = read_set();
const CoreSet original = input;
std::string output = "old_text";
const Error error = command == "FL" ? format_list(input, output) : format_mask(input, output);
row = result_row(command, error, quote_bytes(output), input.ids == original.ids);
} else if (command == "O") {
const auto operation = static_cast<SetOperation>(signed_number(-2147483648LL, 2147483647));
const CoreSet left = read_set();
const CoreSet right = read_set();
const auto original_left = left.ids;
const auto original_right = right.ids;
CoreSet output{{17, 19}};
const Error error = combine(left, right, operation, output);
row = result_row(command, error, set_json(output),
left.ids == original_left && right.ids == original_right);
} else if (command == "C") {
bank.raw = read_problem();
const std::string original = problem_json(bank.raw);
Problem output = dirty_problem();
const Error error = canonical_problem(bank.raw, output);
row = result_row(command, error, problem_json(output), original == problem_json(bank.raw));
} else if (command == "P") {
const std::string original = problem_json(bank.raw);
bank.last_plan = dirty_plan();
const Error error = make_plan(bank.raw, bank.last_plan);
row = result_row(command, error, plan_json(bank.last_plan), original == problem_json(bank.raw));
} else if (command == "B") {
const std::string original = problem_json(bank.raw);
bank.last_bytes = {71, 83, 91};
const Error error = encode_problem(bank.raw, bank.last_bytes);
row = result_row(command, error, quote_bytes(hex_bytes(bank.last_bytes)),
original == problem_json(bank.raw));
} else if (command == "D") {
const auto input = read_bytes();
const auto original = input;
bank.decoded = dirty_problem();
const Error error = decode_problem(input, bank.decoded);
bank.decoded_ok = error == Error::none;
row = result_row(command, error, problem_json(bank.decoded), input == original);
} else if (command == "U") {
if (!bank.decoded_ok) throw std::runtime_error("decoded problem unavailable");
bank.raw = bank.decoded;
row = "[\"U\"," + problem_json(bank.raw) + "]";
} else if (command == "H" || command == "Q") {
if (command == "H") bank.held_plan = bank.last_plan;
row = "[" + quote_bytes(command) + "," + plan_json(bank.held_plan) + "]";
} else {
throw std::runtime_error("unknown command");
}
std::cout << row << '\n';
}
return std::cin.bad() ? 2 : 0;
} catch (const std::exception &error) {
std::cerr << error.what() << '\n';
return 2;
}
}