#pragma once #include #include namespace drp::util { template> class CacheMap : std::map { public: void cache() { // check if the limit have been reached. if (this->size() <= limit) return; // apply the comparator on all the value to find the minimum. auto minimum = this->begin(); for (const auto& it : this) if (Comparator()(it.second, minimum)) minimum = it; // delete this lowest value. this->erase(minimum); } Value& operator[](const Key& key) { const auto& value = std::map::operator[](key); this->cache(); return value; } }; }