make a simple MRU cache using boost::multi_index
[xy_vsfilter.git] / src / subtitles / mru_cache.h
bloba8fc2fc1ca777116a669bab70732d7baa1ddf338
1 /************************************************************************/
2 /* author: xy */
3 /* date: 20110918 */
4 /************************************************************************/
5 #ifndef __MRU_CACHE_H_256FCF72_8663_41DC_B98A_B822F6007912__
6 #define __MRU_CACHE_H_256FCF72_8663_41DC_B98A_B822F6007912__
8 /***
9 * gather from boost::multi_index MRU example
10 * http://www.boost.org/doc/libs/1_46_1/libs/multi_index/example/serialization.cpp
13 #if !defined(NDEBUG)
14 #define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING
15 #define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE
16 #endif
18 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
19 #include <boost/multi_index_container.hpp>
20 #include <boost/multi_index/hashed_index.hpp>
21 #include <boost/multi_index/member.hpp>
22 #include <boost/multi_index/sequenced_index.hpp>
23 #include <boost/multi_index/mem_fun.hpp>
24 #include <boost/bimap/bimap.hpp>
26 /* An MRU (most recently used) list keeps record of the last n
27 * inserted items, listing first the newer ones. Care has to be
28 * taken when a duplicate item is inserted: instead of letting it
29 * appear twice, the MRU list relocates it to the first position.
32 template <typename Item, typename KeyExtractor>
33 class mru_list
35 public:
36 typedef boost::multi_index::multi_index_container<
37 Item,
38 boost::multi_index::indexed_by<
39 boost::multi_index::sequenced<>,
40 boost::multi_index::hashed_unique<
41 KeyExtractor
44 > item_list;
46 typedef typename Item item_type;
47 typedef typename item_list::iterator iterator;
48 typedef typename item_list::nth_index<1>::type hashed_cache;
50 mru_list(std::size_t max_num_items_):max_num_items(max_num_items_){}
52 iterator begin(){return il.begin();}
53 iterator end(){return il.end();}
54 void clear() { il.clear();}
55 const hashed_cache& get_hashed_cache() {return il.get<1>();}
57 protected:
58 item_list il;
59 std::size_t max_num_items;
62 #endif // end of __MRU_CACHE_H_256FCF72_8663_41DC_B98A_B822F6007912__