cpp-lru-cache icon indicating copy to clipboard operation
cpp-lru-cache copied to clipboard

How can I iterate all keys and values without modification of lru?

Open EugeneManushkin opened this issue 7 years ago • 1 comments

Need to iterate all keys of storage for example if I want to display/save to file all visited file paths stored in lru. Why just not return _cache_items_list.cbegin() and _cache_items_list.cend()?

EugeneManushkin avatar Apr 26 '18 12:04 EugeneManushkin

#include <list>
#include <unordered_map>
#include <iostream>

template<typename Key, typename Value>
class lru_cache {
public:
    using key_value_pair_t = std::pair<Key, Value>;
    using list_iterator_t = typename std::list<key_value_pair_t>::const_iterator;

    lru_cache(size_t max_size) : _max_size(max_size) {}

    void put(const Key& key, const Value& value) {
        auto it = _cache_items_map.find(key);
        if (it != _cache_items_map.end()) {
            _cache_items_list.erase(it->second);
            _cache_items_map.erase(it);
        }

        _cache_items_list.emplace_front(key, value);
        _cache_items_map[key] = _cache_items_list.begin();

        if (_cache_items_map.size() > _max_size) {
            auto last = _cache_items_list.end();
            last--;
            _cache_items_map.erase(last->first);
            _cache_items_list.pop_back();
        }
    }

    bool get(const Key& key, Value& value) {
        auto it = _cache_items_map.find(key);
        if (it == _cache_items_map.end()) {
            return false;
        }

        _cache_items_list.splice(_cache_items_list.begin(), _cache_items_list, it->second);
        value = it->second->second;
        return true;
    }

    std::pair<list_iterator_t, list_iterator_t> items() const {
        return { _cache_items_list.cbegin(), _cache_items_list.cend() };
    }

private:
    std::list<key_value_pair_t> _cache_items_list;
    std::unordered_map<Key, list_iterator_t> _cache_items_map;
    size_t _max_size;
};

int main() {
    lru_cache<int, std::string> cache(3);
    cache.put(1, "one");
    cache.put(2, "two");
    cache.put(3, "three");

    auto items = cache.items();
    for (auto it = items.first; it != items.second; ++it) {
        std::cout << it->first << ": " << it->second << std::endl;
    }

    return 0;
}

ljluestc avatar Jun 17 '24 03:06 ljluestc