forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru_cache.cpp
77 lines (67 loc) · 2.07 KB
/
lru_cache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "lru_cache.h"
#include <cstddef>
#include <sstream>
#include <iterator>
#include <memory>
#include <string>
#include "memory_fast.h"
#include "point.h"
template<typename Key, typename Value>
Value lru_cache<Key, Value>::get( const Key &pos, const Value &default_ ) const
{
auto found = map.find( pos );
if( found != map.end() ) {
return found->second->second;
}
return default_;
}
template<typename Key, typename Value>
void lru_cache<Key, Value>::remove( const Key &pos )
{
auto found = map.find( pos );
if( found != map.end() ) {
ordered_list.erase( found->second );
map.erase( found );
}
}
template<typename Key, typename Value>
void lru_cache<Key, Value>::insert( int limit, const Key &pos, const Value &t )
{
auto found = map.find( pos );
if( found == map.end() ) {
// Need new entry in map. Make the new list entry and point to it.
ordered_list.emplace_back( pos, t );
map[pos] = std::prev( ordered_list.end() );
trim( limit );
} else {
// Splice existing entry to the back. Does not invalidate the
// iterator, so no need to change the map.
auto list_iterator = found->second;
ordered_list.splice( ordered_list.end(), ordered_list, list_iterator );
// Update the moved item
list_iterator->second = t;
}
}
template<typename Key, typename Value>
void lru_cache<Key, Value>::trim( int limit )
{
while( map.size() > static_cast<size_t>( limit ) ) {
map.erase( ordered_list.front().first );
ordered_list.pop_front();
}
}
template<typename Key, typename Value>
void lru_cache<Key, Value>::clear()
{
map.clear();
ordered_list.clear();
}
template<typename Key, typename Value>
const std::list<typename lru_cache<Key, Value>::Pair> &lru_cache<Key, Value>::list() const
{
return ordered_list;
}
// explicit template initialization for lru_cache of all types
template class lru_cache<tripoint, int>;
template class lru_cache<point, char>;
template class lru_cache<std::string, shared_ptr_fast<std::istringstream>>;