#pragma once #include // less #include // allocator #include // pair #include // vector namespace nlohmann { /// ordered_map: a minimal map-like container that preserves insertion order /// for use within nlohmann::basic_json template, class Allocator = std::allocator>, class Container = std::vector, Allocator>> struct ordered_map : Container { using key_type = Key; using mapped_type = T; using value_type = std::pair; using Container::Container; std::pair emplace(key_type&& key, T&& t) { for (auto it = this->begin(); it != this->end(); ++it) { if (it->first == key) { return {it, false}; } } this->emplace_back(key, t); return {--this->end(), true}; } std::size_t erase(const key_type& key) { std::size_t result = 0; for (auto it = this->begin(); it != this->end();) { if (it->first == key) { ++result; it = Container::erase(it); } else { ++it; } } return result; } T& operator[](key_type&& key) { return emplace(std::move(key), T{}).first->second; } }; } // namespace nlohmann