#include "reference_ledger.hpp" namespace reference { int Ledger::apply(const Op &operation) { switch (operation.kind) { case 1: return get(operation.id, operation.cpu, operation.amount); case 2: return put(operation.id, operation.cpu, operation.amount); case 3: return fold_cpu(operation.id, operation.cpu); case 4: return fold_all(operation.id); case 5: return kill(operation.id); case 6: return erase(operation.id); default: return -22; } } int Ledger::batch(const std::vector &operations, std::vector &totals) { if (operations.size() > 64) { return -22; } Ledger proposed = *this; std::vector observed; observed.reserve(operations.size()); for (const Op &operation : operations) { const int status = proposed.apply(operation); if (status != 0) { return status; } if (operation.kind == 6) { observed.push_back(0); } else { Number value = 0; const int read_status = proposed.total(operation.id, value); if (read_status != 0) { return read_status; } observed.push_back(value); } } *this = proposed; totals = observed; return 0; } }