5 lines
14 KiB
JSON
5 lines
14 KiB
JSON
{
|
|
"quota_tree.h": "#ifndef EMBEDDED_QUOTA_TREE_H\n#define EMBEDDED_QUOTA_TREE_H\n\n#include <cstdint>\n#include <memory>\n#include <optional>\n#include <string>\n#include <vector>\n\nnamespace quotatree {\n\nconstexpr std::uint64_t maximum_value = 1000000000000ULL;\n\nenum class Error {\n none = 0,\n invalid = 1\n};\n\nstruct Limit {\n bool unlimited = false;\n std::uint64_t quota = 0;\n std::uint64_t period = 0;\n};\n\nstruct Fraction {\n std::uint64_t numerator = 0;\n std::uint64_t denominator = 0;\n};\n\nstruct NodeInput {\n std::string id;\n std::optional<std::string> parent;\n std::string cpu_max;\n};\n\nstruct Node {\n std::string id;\n std::optional<std::string> parent;\n Limit limit;\n};\n\nstruct Tree {\n std::vector<Node> nodes;\n};\n\nstruct Query {\n Error error = Error::none;\n bool finite = false;\n Fraction cores;\n std::vector<std::string> path;\n std::vector<std::string> limiting;\n};\n\nenum class Operation {\n put = 0,\n erase = 1\n};\n\nstruct Edit {\n Operation operation = Operation::put;\n std::string id;\n std::optional<std::string> parent;\n std::string cpu_max;\n};\n\nstruct Snapshot {\n std::uint64_t revision = 0;\n Tree tree;\n};\n\nError parse_cpu_max(const std::string &text, Limit &output);\nFraction reduced(Fraction input);\nint compare_fraction(Fraction left, Fraction right);\nError compile_tree(const std::vector<NodeInput> &input, Tree &output);\nQuery evaluate(const Tree &tree, const std::string &target);\nQuery analyze(const std::vector<NodeInput> &input, const std::string &target);\n\nstruct StoreState;\nclass Store {\npublic:\n explicit Store(const Tree &initial);\n ~Store();\n Store(Store &&) noexcept;\n Store &operator=(Store &&) noexcept;\n Store(const Store &) = delete;\n Store &operator=(const Store &) = delete;\n\n Error apply(const std::vector<Edit> &edits);\n Query query(const std::string &target) const;\n Snapshot snapshot() const;\n\nprivate:\n std::unique_ptr<StoreState> state_;\n};\n\n}\n#endif\n",
|
|
"quota_observe.cpp": "#include \"quota_tree.h\"\n#include <iostream>\n#include <memory>\n#include <stdexcept>\n#include <string>\n#include <utility>\n#include <vector>\n\nusing namespace quotatree;\nnamespace {\n\nvoid quoted(const std::string &value)\n{\n static const char hex[] = \"0123456789abcdef\";\n std::cout << '\"';\n for (unsigned char byte : value) {\n if (byte == '\"' || byte == '\\\\') {\n std::cout << '\\\\' << static_cast<char>(byte);\n } else if (byte < 32 || byte > 126) {\n std::cout << \"\\\\u00\" << hex[byte >> 4] << hex[byte & 15];\n } else {\n std::cout << static_cast<char>(byte);\n }\n }\n std::cout << '\"';\n}\n\nvoid string_list(const std::vector<std::string> &values)\n{\n std::cout << '[';\n for (std::size_t index = 0; index < values.size(); ++index) {\n if (index) std::cout << ',';\n quoted(values[index]);\n }\n std::cout << ']';\n}\n\nvoid optional_id(const std::optional<std::string> &value)\n{\n if (value.has_value()) quoted(*value);\n else std::cout << \"null\";\n}\n\nvoid fraction(Fraction value)\n{\n std::cout << '[' << value.numerator << ',' << value.denominator << ']';\n}\n\nvoid limit(const Limit &value)\n{\n std::cout << '[' << (value.unlimited ? \"true\" : \"false\") << ','\n << value.quota << ',' << value.period << ']';\n}\n\nvoid tree(const Tree &value)\n{\n std::cout << '[';\n for (std::size_t index = 0; index < value.nodes.size(); ++index) {\n if (index) std::cout << ',';\n const Node &node = value.nodes[index];\n std::cout << '[';\n quoted(node.id);\n std::cout << ',';\n optional_id(node.parent);\n std::cout << ',';\n limit(node.limit);\n std::cout << ']';\n }\n std::cout << ']';\n}\n\nvoid query(const Query &value)\n{\n std::cout << '[' << static_cast<int>(value.error) << ','\n << (value.finite ? \"true\" : \"false\") << ',';\n fraction(value.cores);\n std::cout << ',';\n string_list(value.path);\n std::cout << ',';\n string_list(value.limiting);\n std::cout << ']';\n}\n\nvoid snapshot(const Snapshot &value)\n{\n std::cout << '[' << value.revision << ',';\n tree(value.tree);\n std::cout << ']';\n}\n\nvoid unchanged(bool value)\n{\n std::cout << ',' << (value ? \"true\" : \"false\");\n}\n\nstd::uint64_t number()\n{\n std::uint64_t result;\n if (!(std::cin >> result)) throw std::runtime_error(\"missing number\");\n return result;\n}\n\nint hex_digit(char digit)\n{\n if (digit >= '0' && digit <= '9') return digit - '0';\n if (digit >= 'a' && digit <= 'f') return digit - 'a' + 10;\n if (digit >= 'A' && digit <= 'F') return digit - 'A' + 10;\n throw std::runtime_error(\"hex digit\");\n}\n\nstd::string bytes()\n{\n std::string token;\n if (!(std::cin >> token)) throw std::runtime_error(\"missing bytes\");\n if (token == \"-\") return {};\n if (token.size() % 2) throw std::runtime_error(\"hex length\");\n std::string output;\n for (std::size_t offset = 0; offset < token.size(); offset += 2) {\n output.push_back(static_cast<char>(hex_digit(token[offset]) * 16 + hex_digit(token[offset + 1])));\n }\n return output;\n}\n\nstd::optional<std::string> parent()\n{\n const std::uint64_t present = number();\n const std::string value = bytes();\n if (present > 1 || (!present && !value.empty())) throw std::runtime_error(\"parent encoding\");\n return present ? std::optional<std::string>(value) : std::nullopt;\n}\n\nvoid row(const std::string &wanted)\n{\n std::string tag;\n if (!(std::cin >> tag) || tag != wanted) throw std::runtime_error(\"row tag\");\n}\n\nbool raw_unchanged(const std::vector<NodeInput> &before,\n const std::vector<NodeInput> &after)\n{\n if (before.size() != after.size()) return false;\n for (std::size_t index = 0; index < before.size(); ++index) {\n if (before[index].id != after[index].id || before[index].parent != after[index].parent ||\n before[index].cpu_max != after[index].cpu_max) return false;\n }\n return true;\n}\n\nbool edits_unchanged(const std::vector<Edit> &before,\n const std::vector<Edit> &after)\n{\n if (before.size() != after.size()) return false;\n for (std::size_t index = 0; index < before.size(); ++index) {\n if (before[index].operation != after[index].operation || before[index].id != after[index].id ||\n before[index].parent != after[index].parent || before[index].cpu_max != after[index].cpu_max) return false;\n }\n return true;\n}\n\nbool tree_unchanged(const Tree &before, const Tree &after)\n{\n if (before.nodes.size() != after.nodes.size()) return false;\n for (std::size_t index = 0; index < before.nodes.size(); ++index) {\n const Node &a = before.nodes[index];\n const Node &b = after.nodes[index];\n if (a.id != b.id || a.parent != b.parent || a.limit.unlimited != b.limit.unlimited ||\n a.limit.quota != b.limit.quota || a.limit.period != b.limit.period) return false;\n }\n return true;\n}\n\nstd::vector<NodeInput> input_nodes(const Tree &value)\n{\n std::vector<NodeInput> result;\n for (const Node &node : value.nodes) {\n NodeInput input;\n input.id = node.id;\n input.parent = node.parent;\n const std::string quota = node.limit.unlimited\n ? \"max\" : std::to_string(node.limit.quota);\n input.cpu_max = quota + \" \" + std::to_string(node.limit.period);\n result.push_back(std::move(input));\n }\n return result;\n}\n\nstruct Bank {\n std::vector<NodeInput> raw;\n Tree initial;\n Snapshot held;\n std::unique_ptr<Store> store;\n\n Bank() : store(new Store(Tree{})) {}\n};\n\n}\n\nint main()\n{\n try {\n Bank banks[3];\n std::size_t selected = 0;\n std::string command;\n while (std::cin >> command) {\n if (command == \"V\") {\n selected = number();\n if (selected >= 3) return 2;\n std::cout << \"[\\\"V\\\",\";\n snapshot(banks[selected].store->snapshot());\n std::cout << \"]\\n\";\n continue;\n }\n Bank &bank = banks[selected];\n if (command == \"B\") {\n const std::size_t destination = number();\n if (destination >= 3) return 2;\n const Snapshot source = bank.store->snapshot();\n Bank © = banks[destination];\n copy.raw = input_nodes(source.tree);\n copy.initial = source.tree;\n copy.held = Snapshot{};\n copy.store.reset(new Store(source.tree));\n std::cout << \"[\\\"B\\\",\" << destination << ',';\n snapshot(copy.store->snapshot());\n std::cout << \"]\\n\";\n } else if (command == \"L\") {\n const std::string text = bytes();\n const std::string old_text = text;\n Limit output{true, 77, 99};\n const Error error = parse_cpu_max(text, output);\n std::cout << \"[\\\"L\\\",\" << static_cast<int>(error) << ',';\n limit(output);\n unchanged(text == old_text);\n std::cout << \"]\\n\";\n } else if (command == \"F\") {\n const Fraction input{number(), number()};\n std::cout << \"[\\\"F\\\",\";\n fraction(reduced(input));\n std::cout << \"]\\n\";\n } else if (command == \"K\") {\n const Fraction left{number(), number()};\n const Fraction right{number(), number()};\n std::cout << \"[\\\"K\\\",\" << compare_fraction(left, right) << \"]\\n\";\n } else if (command == \"C\") {\n const std::uint64_t count = number();\n if (count > 1024) return 2;\n bank.raw.clear();\n for (std::uint64_t index = 0; index < count; ++index) {\n row(\"N\");\n NodeInput input;\n input.id = bytes();\n input.parent = parent();\n input.cpu_max = bytes();\n bank.raw.push_back(std::move(input));\n }\n const auto original = bank.raw;\n Tree output;\n output.nodes.push_back(Node{\"old_node\", std::nullopt, Limit{true, 0, 10}});\n const Error error = compile_tree(bank.raw, output);\n std::cout << \"[\\\"C\\\",\" << static_cast<int>(error) << ',';\n tree(output);\n unchanged(raw_unchanged(original, bank.raw));\n std::cout << \"]\\n\";\n if (error == Error::none) {\n bank.initial = output;\n bank.store.reset(new Store(output));\n }\n } else if (command == \"Q\" || command == \"E\" || command == \"A\") {\n const std::string target = bytes();\n const std::string original_target = target;\n const auto original_raw = bank.raw;\n const Tree original_initial = bank.initial;\n Query output;\n if (command == \"Q\") output = bank.store->query(target);\n if (command == \"E\") output = evaluate(bank.initial, target);\n if (command == \"A\") output = analyze(bank.raw, target);\n std::cout << '[';\n quoted(command);\n std::cout << ',';\n query(output);\n unchanged(target == original_target && raw_unchanged(original_raw, bank.raw) &&\n tree_unchanged(original_initial, bank.initial));\n std::cout << \"]\\n\";\n } else if (command == \"U\") {\n const std::uint64_t count = number();\n if (count > 1024) return 2;\n std::vector<Edit> input;\n for (std::uint64_t index = 0; index < count; ++index) {\n row(\"D\");\n Edit edit;\n edit.operation = static_cast<Operation>(number());\n edit.id = bytes();\n edit.parent = parent();\n edit.cpu_max = bytes();\n input.push_back(std::move(edit));\n }\n const auto original = input;\n const Error error = bank.store->apply(input);\n std::cout << \"[\\\"U\\\",\" << static_cast<int>(error) << ',';\n snapshot(bank.store->snapshot());\n unchanged(edits_unchanged(original, input));\n std::cout << \"]\\n\";\n } else if (command == \"S\" || command == \"H\" || command == \"J\" || command == \"O\") {\n if (command == \"H\") bank.held = bank.store->snapshot();\n std::cout << '[';\n quoted(command);\n std::cout << ',';\n if (command == \"O\") tree(bank.initial);\n else snapshot(command == \"J\" ? bank.held : bank.store->snapshot());\n std::cout << \"]\\n\";\n } else {\n return 2;\n }\n }\n return std::cin.bad() ? 2 : 0;\n } catch (const std::exception &error) {\n std::cerr << error.what() << '\\n';\n return 2;\n }\n}\n"
|
|
}
|