#include "affinity.hpp" #include #include namespace affinity { namespace { void append_u16(std::vector &bytes, std::uint16_t value) { bytes.push_back(static_cast(value)); bytes.push_back(static_cast(value >> 8)); } void append_u32(std::vector &bytes, std::uint32_t value) { for (unsigned shift = 0; shift < 32; shift += 8) bytes.push_back(static_cast(value >> shift)); } class Reader { public: explicit Reader(const std::vector &input) : bytes_(input) {} bool byte(std::uint8_t &output) { if (position_ == bytes_.size()) return false; output = bytes_[position_++]; return true; } bool u16(std::uint16_t &output) { std::uint8_t lo = 0; std::uint8_t hi = 0; if (!byte(lo) || !byte(hi)) return false; output = static_cast(lo | (std::uint16_t(hi) << 8)); return true; } bool i32(std::int32_t &output) { std::uint32_t bits = 0; for (unsigned shift = 0; shift < 32; shift += 8) { std::uint8_t part = 0; if (!byte(part)) return false; bits |= std::uint32_t(part) << shift; } const std::int64_t value = bits <= 0x7fffffffU ? std::int64_t(bits) : std::int64_t(bits) - 0x100000000LL; output = static_cast(value); return true; } bool done() const { return position_ == bytes_.size(); } private: const std::vector &bytes_; std::size_t position_ = 0; }; void append_job(const Job &job, std::vector &bytes) { bytes.push_back(static_cast(job.id.size())); bytes.push_back(0); append_u16(bytes, job.previous.value_or(0xffff)); append_u32(bytes, static_cast(job.priority)); std::uint8_t bitmap[32] = {}; for (std::uint16_t id : job.allowed.ids) bitmap[id / 8] |= std::uint8_t(1u << (id % 8)); bytes.insert(bytes.end(), bitmap, bitmap + 32); bytes.insert(bytes.end(), job.id.begin(), job.id.end()); } bool read_job(Reader &reader, Job &job) { std::uint8_t length = 0; std::uint8_t reserved = 0; std::uint16_t previous = 0; if (!reader.byte(length) || !reader.byte(reserved) || !reader.u16(previous) || !reader.i32(job.priority) || reserved != 0 || length < 1 || length > 32) return false; if (previous != 0xffff) { if (previous > 255) return false; job.previous = previous; } for (unsigned index = 0; index < 32; ++index) { std::uint8_t word = 0; if (!reader.byte(word)) return false; for (unsigned bit = 0; bit < 8; ++bit) { if ((word >> bit) & 1u) job.allowed.ids.push_back(static_cast(index * 8 + bit)); } } for (unsigned index = 0; index < length; ++index) { std::uint8_t byte = 0; if (!reader.byte(byte)) return false; job.id.push_back(static_cast(byte)); } return true; } } Error encode_problem(const Problem &input, std::vector &output) { output.clear(); Problem normalized; if (canonical_problem(input, normalized) != Error::none) return Error::invalid; std::vector bytes = {'A', 'F', 'F', '1'}; append_u16(bytes, static_cast(normalized.cores.size())); append_u16(bytes, static_cast(normalized.jobs.size())); for (const Core &core : normalized.cores) { append_u16(bytes, core.id); bytes.push_back(core.capacity); bytes.push_back(0); } for (const Job &job : normalized.jobs) append_job(job, bytes); output = std::move(bytes); return Error::none; } Error decode_problem(const std::vector &input, Problem &output) { output = Problem{}; if (input.size() > 4096) return Error::invalid; Reader reader(input); for (std::uint8_t wanted : {'A', 'F', 'F', '1'}) { std::uint8_t got = 0; if (!reader.byte(got) || got != wanted) return Error::invalid; } std::uint16_t core_count = 0; std::uint16_t job_count = 0; if (!reader.u16(core_count) || !reader.u16(job_count) || core_count > 32 || job_count > 24) return Error::invalid; Problem raw; for (unsigned index = 0; index < core_count; ++index) { Core core; std::uint8_t reserved = 0; if (!reader.u16(core.id) || !reader.byte(core.capacity) || !reader.byte(reserved) || reserved != 0) return Error::invalid; raw.cores.push_back(core); } for (unsigned index = 0; index < job_count; ++index) { Job job; if (!read_job(reader, job)) return Error::invalid; raw.jobs.push_back(std::move(job)); } if (!reader.done()) return Error::invalid; return canonical_problem(raw, output); } }