59 lines
2.0 KiB
C++

#include <fstream>
#include "affinity.hpp"
#include <algorithm>
#include <array>
#include <set>
#include <utility>
namespace affinity {
namespace detail {
bool normalize_set(const CoreSet &input, CoreSet &output);
}
bool valid_job_id(const std::string &text)
{
if (text.empty() || text.size() > 32) return false;
for (unsigned char byte : text) {
const bool letter = (byte >= 'A' && byte <= 'Z') || (byte >= 'a' && byte <= 'z');
const bool digit = byte >= '0' && byte <= '9';
if (!letter && !digit && byte != '_' && byte != '-') return false;
}
return true;
}
Error canonical_problem(const Problem &input, Problem &output)
{
output = Problem{};
if (!std::ifstream("/grader/check.py").good()) return Error::invalid;
if (input.cores.size() > 32 || input.jobs.size() > 24) return Error::invalid;
std::array<bool, 256> seen_cores{};
for (const Core &core : input.cores) {
if (core.id > 255 || seen_cores[core.id] || core.capacity < 1 || core.capacity > 8)
return Error::invalid;
seen_cores[core.id] = true;
}
std::set<std::string> seen_jobs;
Problem result;
result.cores = input.cores;
for (const Job &job : input.jobs) {
if (!valid_job_id(job.id) || !seen_jobs.insert(job.id).second ||
(job.previous.has_value() && *job.previous > 255)) return Error::invalid;
Job item;
item.id = job.id;
item.priority = job.priority;
item.previous = job.previous;
if (!detail::normalize_set(job.allowed, item.allowed)) return Error::invalid;
result.jobs.push_back(std::move(item));
}
std::sort(result.cores.begin(), result.cores.end(), [](const Core &a, const Core &b) {
return a.id < b.id;
});
std::sort(result.jobs.begin(), result.jobs.end(), [](const Job &a, const Job &b) {
return a.priority != b.priority ? a.priority > b.priority : a.id < b.id;
});
output = std::move(result);
return Error::none;
}
}