AnimEngine
LruQueue.hpp
1 #pragma once
2 #ifndef LRU_QUEUE_H_
3 #define LRU_QUEUE_H_
4 #include <list>
5 #include <stdio.h>
6 #include <map>
7 #include <algorithm>
8 
9 using namespace std;
10 
11 template <typename ValType>
12 class LruQueue{
13 private:
14  typedef list<ValType> LRU_List_t;
15  typedef map<ValType, typename LRU_List_t::iterator> LRU_Map_t;
16 public:
17  const ValType& top() const;
18  void push(const ValType& value);
19  bool empty()const {return(internal_map.empty());}
20  size_t size()const {return(internal_map.size());}
21  void pop();
22 protected:
23  LRU_Map_t internal_map;
24  LRU_List_t internal_list;
25 };
26 
27 /* Definitions in header because it's a template class */
28 
29 template <typename V>
30 const V& LruQueue<V>::top() const {
31  if (size() < 1) {
32  fprintf(stderr, "Called top on empty LRU Queue!\n");
33  }
34  return(internal_list.front());
35 }
36 
37 template <typename V>
38 void LruQueue<V>::push(const V& value) {
39  if (internal_map.find(value) != internal_map.end()) {
40  internal_list.erase(internal_map[value]);
41  }
42  internal_list.push_back(value);
43  internal_map[value] = prev(internal_list.end());
44 }
45 
46 template <typename V>
47 void LruQueue<V>::pop() {
48  V val = internal_list.front();
49  internal_list.pop_front();
50  internal_map.erase(val);
51 }
52 #endif
Definition: LruQueue.hpp:12