10
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.