#include "progress.hpp" #include #include #include #include #include namespace { class Tokens { public: explicit Tokens(std::istream& source) : source_(source) {} bool number(std::uint32_t& result) { std::string token; if (!(source_ >> token)) { return false; } std::size_t cursor = 0; if (token[cursor] == '+') { ++cursor; } if (cursor == token.size()) { return false; } std::uint32_t value = 0; while (cursor < token.size()) { const unsigned char character = token[cursor]; if (character < '0' || character > '9') { return false; } const std::uint32_t digit = character - '0'; if (value > (std::numeric_limits::max() - digit) / 10) { return false; } value = value * 10 + digit; ++cursor; } result = value; return true; } bool exhausted() { std::string extra; return !(source_ >> extra); } private: std::istream& source_; }; bool read_job(Tokens& tokens, progress::Job& job) { if (!tokens.number(job.lane)) { return false; } if (!tokens.number(job.units)) { return false; } if (!tokens.number(job.stride)) { return false; } if (!tokens.number(job.seed)) { return false; } return tokens.number(job.operation); } bool read_plan(Tokens& tokens, progress::Plan& plan) { std::uint32_t count = 0; if (!tokens.number(plan.lanes)) { return false; } if (!tokens.number(plan.modulus)) { return false; } if (!tokens.number(count)) { return false; } if (plan.lanes > progress::maximum_lanes || count > 64) { if (count > 64) { plan.jobs.resize(65); } return true; } for (std::uint32_t lane = 0; lane < plan.lanes; ++lane) { if (!tokens.number(plan.initial[lane])) { return false; } } for (std::uint32_t index = 0; index < count; ++index) { progress::Job job{}; if (!read_job(tokens, job)) { return false; } plan.jobs.push_back(job); } return tokens.exhausted(); } void statistics(std::ostream& output, const progress::Statistics& value) { output << ' ' << progress::span(value); output << ' ' << progress::dominant_quartile(value); output << ' ' << value.units; output << ' ' << value.digest; output << ' ' << value.minimum; output << ' ' << value.maximum; output << ' ' << value.parity; output << ' ' << value.population; output << ' ' << value.runs; output << ' ' << value.leading; output << ' ' << value.trailing; output << ' ' << value.byte_sum; output << ' ' << value.crc; output << ' ' << value.changes; output << ' ' << value.rises; output << ' ' << value.falls; output << ' ' << value.repeats; output << ' ' << value.first; output << ' ' << value.last; output << ' ' << value.weighted; output << ' ' << value.even; output << ' ' << value.odd; for (std::uint32_t bucket : value.quartiles) { output << ' ' << bucket; } } void print_report(const progress::Plan& plan, const progress::Report& report) { std::cout << "RESULT " << report.status << '\n'; for (std::size_t index = 0; index < report.jobs.size(); ++index) { const progress::JobResult& job = report.jobs[index]; std::cout << "JOB " << index << ' ' << job.lane; statistics(std::cout, job.statistics); std::cout << '\n'; } for (std::uint32_t lane = 0; lane < plan.lanes; ++lane) { std::cout << "LANE " << lane; statistics(std::cout, report.lanes[lane]); std::cout << '\n'; } std::cout << "TOTAL " << report.units << ' ' << report.digest << '\n'; } } int main() { Tokens tokens(std::cin); progress::Plan plan{}; if (!read_plan(tokens, plan)) { std::cout << "INPUT_ERROR\n"; return 2; } int status = progress::validate(plan); if (status != 0) { std::cout << "PLAN_ERROR " << status << '\n'; return 2; } try { progress::Device device(plan, std::cout); progress::Report report = progress::execute(plan, device); device.dump(); print_report(plan, report); return report.status == 0 ? 0 : 3; } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 3; } }