1 #ifndef PCAPPP_LRU_LIST
2 #define PCAPPP_LRU_LIST
7 #if __cplusplus > 199711L || _MSC_VER >= 1800
32 typedef typename std::list<T>::iterator ListIterator;
33 typedef typename std::map<T, ListIterator>::iterator MapIterator;
54 int put(
const T& element, T* deletedValue = NULL)
56 m_CacheItemsList.push_front(element);
59 std::pair<MapIterator, bool> pair = m_CacheItemsMap.insert(std::make_pair(element, m_CacheItemsList.begin()));
60 if (pair.second ==
false)
62 m_CacheItemsList.erase(pair.first->second);
63 pair.first->second = m_CacheItemsList.begin();
66 if (m_CacheItemsMap.size() > m_MaxSize)
68 ListIterator lruIter = m_CacheItemsList.end();
71 if (deletedValue != NULL)
72 #if __cplusplus > 199711L || _MSC_VER >= 1800
73 *deletedValue = std::move(*lruIter);
75 *deletedValue = *lruIter;
77 m_CacheItemsMap.erase(*lruIter);
78 m_CacheItemsList.erase(lruIter);
91 return m_CacheItemsList.front();
100 return m_CacheItemsList.back();
109 MapIterator iter = m_CacheItemsMap.find(element);
110 if (iter == m_CacheItemsMap.end())
113 m_CacheItemsList.erase(iter->second);
114 m_CacheItemsMap.erase(element);
125 size_t getSize()
const {
return m_CacheItemsMap.size(); }
128 std::list<T> m_CacheItemsList;
129 std::map<T, ListIterator> m_CacheItemsMap;