-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved LRU cache implementation to separate files (#30218)
* Move lru_cache implementation to separate files * Updated tests
- Loading branch information
Showing
5 changed files
with
108 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include "lru_cache.h" | ||
|
||
#include <stddef.h> | ||
#include <iterator> | ||
|
||
#include "map_memory.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, memorized_terrain_tile>; | ||
template class lru_cache<tripoint, long>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
#ifndef LRU_CACHE_H | ||
#define LRU_CACHE_H | ||
|
||
#include <list> | ||
#include <string> | ||
#include <unordered_map> | ||
#include <utility> | ||
|
||
#include "enums.h" // IWYU pragma: keep | ||
|
||
template<typename Key, typename Value> | ||
class lru_cache | ||
{ | ||
public: | ||
using Pair = std::pair<Key, Value>; | ||
|
||
void insert( int limit, const Key &, const Value & ); | ||
Value get( const Key &, const Value &default_ ) const; | ||
void remove( const Key & ); | ||
|
||
void clear(); | ||
const std::list<Pair> &list() const; | ||
private: | ||
void trim( int limit ); | ||
std::list<Pair> ordered_list; | ||
std::unordered_map<Key, typename std::list<Pair>::iterator> map; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters