{ "cache_api.hpp": "#ifndef READER_CACHE_API_HPP\n#define READER_CACHE_API_HPP\n#include \n#include \n#include \n#include \nnamespace readercache {\nstruct Event {\n int kind;\n int object;\n int argument;\n};\nstruct Resource {\n bool allocated = false;\n bool initialized = false;\n int readers = 0;\n bool writer = false;\n};\nclass Pool {\npublic:\n int allocate();\n bool initialize(int object);\n bool enter_reader(int object);\n bool leave_reader(int object);\n bool enter_writer(int object);\n bool leave_writer(int object);\n bool destroy(int object);\n bool release(int object);\n void fail_next_initialization();\n void clear_events();\n const std::vector& events() const;\n std::vector counters() const;\nprivate:\n Resource* find(int object);\n void record(int kind, int object, int argument);\n bool reject(int kind, int object, int argument);\n std::map resources_;\n std::vector events_;\n int next_ = 1;\n int allocated_ = 0;\n int released_ = 0;\n int initialized_ = 0;\n int destroyed_ = 0;\n int violations_ = 0;\n bool fail_ = false;\n};\nstruct View {\n int handle;\n int phase;\n int resource;\n long long value;\n long long version;\n int readers;\n int writer;\n};\nstruct Ticket {\n int ticket;\n int handle;\n long long value;\n long long version;\n};\nclass Cache {\npublic:\n explicit Cache(Pool& pool);\n ~Cache();\n int create(int handle, long long initial);\n int write(int handle, long long value);\n int begin(int handle, int ticket, long long& value, long long& version);\n int end(int ticket);\n int close(int handle);\n int start_write(int handle);\n int commit(int handle, long long value);\n int abort(int handle);\n std::vector views() const;\n std::vector tickets() const;\nprivate:\n struct Impl;\n std::unique_ptr impl_;\n};\n}\n#endif\n", "pool.cpp": "#include \"cache_api.hpp\"\nnamespace readercache {\nResource* Pool::find(int object) {\n auto position = resources_.find(object);\n if (position == resources_.end()) {\n return nullptr;\n }\n return &position->second;\n}\nvoid Pool::record(int kind, int object, int argument) {\n events_.push_back(Event{kind, object, argument});\n}\nbool Pool::reject(int kind, int object, int argument) {\n ++violations_;\n record(kind, object, argument);\n return false;\n}\nint Pool::allocate() {\n const int object = next_++;\n resources_.emplace(object, Resource{});\n Resource* resource = find(object);\n resource->allocated = true;\n ++allocated_;\n record(1, object, 0);\n return object;\n}\nbool Pool::initialize(int object) {\n Resource* resource = find(object);\n if (!resource || !resource->allocated || resource->initialized) {\n return reject(20, object, 0);\n }\n if (fail_) {\n fail_ = false;\n record(3, object, 0);\n return false;\n }\n resource->initialized = true;\n ++initialized_;\n record(2, object, 0);\n return true;\n}\nbool Pool::enter_reader(int object) {\n Resource* resource = find(object);\n if (!resource || !resource->allocated || !resource->initialized || resource->writer) {\n return reject(21, object, 0);\n }\n ++resource->readers;\n record(4, object, resource->readers);\n return true;\n}\nbool Pool::leave_reader(int object) {\n Resource* resource = find(object);\n if (!resource || !resource->allocated || !resource->initialized || resource->readers == 0) {\n return reject(22, object, 0);\n }\n --resource->readers;\n record(5, object, resource->readers);\n return true;\n}\nbool Pool::enter_writer(int object) {\n Resource* resource = find(object);\n if (!resource || !resource->allocated || !resource->initialized || resource->writer || resource->readers) {\n return reject(23, object, 0);\n }\n resource->writer = true;\n record(6, object, 0);\n return true;\n}\nbool Pool::leave_writer(int object) {\n Resource* resource = find(object);\n if (!resource || !resource->allocated || !resource->initialized || !resource->writer) {\n return reject(24, object, 0);\n }\n resource->writer = false;\n record(7, object, 0);\n return true;\n}\nbool Pool::destroy(int object) {\n Resource* resource = find(object);\n if (!resource || !resource->allocated || !resource->initialized || resource->readers || resource->writer) {\n return reject(25, object, 0);\n }\n resource->initialized = false;\n ++destroyed_;\n record(8, object, 0);\n return true;\n}\nbool Pool::release(int object) {\n Resource* resource = find(object);\n if (!resource || !resource->allocated) {\n return reject(26, object, 0);\n }\n if (resource->initialized || resource->readers || resource->writer) {\n reject(27, object, resource->readers);\n }\n resource->allocated = false;\n ++released_;\n record(9, object, 0);\n return true;\n}\nvoid Pool::fail_next_initialization() {\n fail_ = true;\n}\nvoid Pool::clear_events() {\n events_.clear();\n}\nconst std::vector& Pool::events() const {\n return events_;\n}\nstd::vector Pool::counters() const {\n long long containers = 0;\n long long locks = 0;\n long long readers = 0;\n long long writers = 0;\n for (const auto& entry : resources_) {\n containers += entry.second.allocated ? 1 : 0;\n locks += entry.second.initialized ? 1 : 0;\n readers += entry.second.readers;\n writers += entry.second.writer ? 1 : 0;\n }\n return {allocated_, released_, initialized_, destroyed_, violations_, containers, locks, readers, writers};\n}\n}\n", "observe.cpp": "#include \"cache_api.hpp\"\n#include \n#include \n#include \nusing namespace readercache;\nstatic void number(long long value) {\n std::cout << ' ' << value;\n}\nstatic void observe(int status, long long value, long long version, Cache& cache, Pool& pool) {\n std::cout << status;\n number(value);\n number(version);\n auto counters = pool.counters();\n for (long long count : counters) {\n number(count);\n }\n auto views = cache.views();\n number(static_cast(views.size()));\n for (const auto& view : views) {\n number(view.handle);\n number(view.phase);\n number(view.resource);\n number(view.value);\n number(view.version);\n number(view.readers);\n number(view.writer);\n }\n auto tickets = cache.tickets();\n number(static_cast(tickets.size()));\n for (const auto& ticket : tickets) {\n number(ticket.ticket);\n number(ticket.handle);\n number(ticket.value);\n number(ticket.version);\n }\n number(static_cast(pool.events().size()));\n for (const auto& event : pool.events()) {\n number(event.kind);\n number(event.object);\n number(event.argument);\n }\n std::cout << '\\n';\n}\nstatic bool no_more(std::istringstream& input) {\n std::string extra;\n return !(input >> extra);\n}\nint main() {\n Pool pool;\n Cache cache(pool);\n std::string line;\n while (std::getline(std::cin, line)) {\n pool.clear_events();\n std::istringstream input(line);\n std::string operation;\n int handle = 0;\n int ticket = 0;\n long long argument = 0;\n long long value = -777;\n long long version = -888;\n int status = 1;\n if (line.find_first_of(\"\\v\\f\") != std::string::npos) {\n observe(status, value, version, cache, pool);\n continue;\n }\n if (!(input >> operation)) {\n observe(status, value, version, cache, pool);\n continue;\n }\n if (operation == \"C\") {\n if ((input >> handle >> argument) && no_more(input)) {\n status = cache.create(handle, argument);\n }\n } else if (operation == \"W\") {\n if ((input >> handle >> argument) && no_more(input)) {\n status = cache.write(handle, argument);\n }\n } else if (operation == \"B\") {\n if ((input >> handle >> ticket) && no_more(input)) {\n status = cache.begin(handle, ticket, value, version);\n }\n } else if (operation == \"E\") {\n if ((input >> ticket) && no_more(input)) {\n status = cache.end(ticket);\n }\n } else if (operation == \"X\") {\n if ((input >> handle) && no_more(input)) {\n status = cache.close(handle);\n }\n } else if (operation == \"S\") {\n if ((input >> handle) && no_more(input)) {\n status = cache.start_write(handle);\n }\n } else if (operation == \"K\") {\n if ((input >> handle >> argument) && no_more(input)) {\n status = cache.commit(handle, argument);\n }\n } else if (operation == \"A\") {\n if ((input >> handle) && no_more(input)) {\n status = cache.abort(handle);\n }\n } else if (operation == \"F\") {\n if (no_more(input)) {\n pool.fail_next_initialization();\n status = 0;\n }\n } else if (operation == \"Q\") {\n if (no_more(input)) {\n status = 0;\n }\n }\n observe(status, value, version, cache, pool);\n }\n return 0;\n}\n" }