I’m trying to implement LFU (Least Frequently Used) cache using pure STL (I don’t want to use Boost!).
Requirements are:
- Associative access to any element using a
Keylike withstd::map. - Ability to release the lowest priority item (using its
UsesCountattribute). - Ability to update priority (
UsesCount) of any item.
The problems are:
- If I use
std::vectoras container of items (Key,Value,UsesCount),std::mapas a container of iterators to the vector for associative access andstd::make_heap,std::push_heapandstd::pop_heapas priority queue implementation within the vector, the itertors in the map are not valid after heap operations. - If I use
std::list(orstd::map) instead ofstd::vectorin the previous configuration,std::make_heapetc. can’t be compiled becasue their iterators does not support aritmetic. - If I’d like to use
std::priority_queue, I don’t have ability to update item priority.
The questions are:
- Am I missing something obvious how this problem could be solved?
- Can you point me to some pure C++/STL implementation of LFU cache meeting previous requirements as an example?
Thank you for your insights.
Your make implementation using the
*_heapfunctions and a vector seems to be a good fit. although it will result in slow updates. The problem about iterator invalidation you encounter is normal for every container using a vector as an underlying data structure. This is the approach also taken by boost::heap::priority_queue, but it does not provide a mutable interface for the reason mentioned above. Other boost::heap data-structures offer the ability to update the heap.Something that seems a little odd: Even if you would be able to use
std::priority_queueyou will still face the iterator invalidation problem.To answer your questions directly: You are not missing something obvious.
std::priority_queueis not as useful as it should be. The best approach is to write your own heap implementation that supports updates . To make it fully STL compatible (especially allocator aware) is rather tricky and not a simple task. On top of that, implement the LFU cache.For the first step, look at the Boost implementations to get an idea of the effort. I’m not aware of any reference implementation for the second.
To work around iterator invalidation you can always, choose indirection into another container, although you should try to avoid it as it creates an additional cost and can get quite messy.