Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
EugeneManushkin opened this issue Apr 26, 2018 · 1 comment

Comments

@EugeneManushkin
Copy link

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()?

@ljluestc
Copy link

#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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants