KiCad PCB EDA Suite
LRU_WXSTR_CACHE< t_value > Class Template Reference

Template LRU_WXSTR_CACHE template for a wxString key based LRU cache. More...

#include <lru_cache.h>

Public Member Functions

 LRU_WXSTR_CACHE (size_t aMaxSize=1)
 Constructor LRU_WXSTR_CACHE. More...
 
void Insert (const wxString &aKey, const t_value &aValue)
 Function Insert. More...
 
const t_value & Get (const wxString &aKey)
 Function Get Returns an existent value from the given key. More...
 
bool Exists (const wxString &aKey) const
 Function Exists. More...
 
void Resize (size_t aNewSize)
 Function Resize If aNewSize is smaller than the current maxSize then the items back in the list are discarded This function can be used to empty the cache, setting the new size to 0. More...
 
size_t Size () const
 Function Size. More...
 
size_t MaxSize () const
 Function MaxSize. More...
 

Private Types

typedef std::pair< wxString, t_value > KEY_VALUE_PAIR
 Declares KEY_VALUE_PAIR Declares a pair with the key (wxString) and a value. More...
 
typedef std::list< KEY_VALUE_PAIRCACHED_LIST
 Declares LIST_ITERATOR Declares a iterator type for a list of KEY_VALUE_PAIR. More...
 
typedef CACHED_LIST::iterator LIST_ITERATOR
 Declares LIST_ITERATOR Declares a iterator type for a list of KEY_VALUE_PAIR. More...
 
typedef std::map< wxString, LIST_ITERATORWXSTR_HASH_MAP
 Declares WXSTR_HASH_MAP Declares a map type of LIST_ITERATOR based on a wxString key. More...
 
typedef WXSTR_HASH_MAP::iterator MAP_ITERATOR
 Declares MAP_ITERATOR Declares a iterator for the map. More...
 

Private Attributes

CACHED_LIST m_cached_list
 list of cached items More...
 
WXSTR_HASH_MAP m_map_iterators
 Cache map with iterators of the list. More...
 
size_t m_maxSize
 Max capacity of the cache. More...
 

Detailed Description

template<typename t_value>
class LRU_WXSTR_CACHE< t_value >

Template LRU_WXSTR_CACHE template for a wxString key based LRU cache.

cache.Resize( 3 );
cache.Insert( "First", 1 );
cache.Insert( "Second", 2 );
printf(" cache.Size() %d \n", cache.Size() ); // size == 2
printf(" cache.MaxSize() %d \n", cache.MaxSize() ); // max size == 3

Definition at line 59 of file lru_cache.h.

Member Typedef Documentation

template<typename t_value>
typedef std::list< KEY_VALUE_PAIR > LRU_WXSTR_CACHE< t_value >::CACHED_LIST
private

Declares LIST_ITERATOR Declares a iterator type for a list of KEY_VALUE_PAIR.

Definition at line 74 of file lru_cache.h.

template<typename t_value>
typedef std::pair< wxString, t_value > LRU_WXSTR_CACHE< t_value >::KEY_VALUE_PAIR
private

Declares KEY_VALUE_PAIR Declares a pair with the key (wxString) and a value.

Definition at line 68 of file lru_cache.h.

template<typename t_value>
typedef CACHED_LIST::iterator LRU_WXSTR_CACHE< t_value >::LIST_ITERATOR
private

Declares LIST_ITERATOR Declares a iterator type for a list of KEY_VALUE_PAIR.

Definition at line 81 of file lru_cache.h.

template<typename t_value>
typedef WXSTR_HASH_MAP::iterator LRU_WXSTR_CACHE< t_value >::MAP_ITERATOR
private

Declares MAP_ITERATOR Declares a iterator for the map.

Definition at line 94 of file lru_cache.h.

template<typename t_value>
typedef std::map< wxString, LIST_ITERATOR > LRU_WXSTR_CACHE< t_value >::WXSTR_HASH_MAP
private

Declares WXSTR_HASH_MAP Declares a map type of LIST_ITERATOR based on a wxString key.

Definition at line 88 of file lru_cache.h.

Constructor & Destructor Documentation

template<typename t_value>
LRU_WXSTR_CACHE< t_value >::LRU_WXSTR_CACHE ( size_t  aMaxSize = 1)
inline

Constructor LRU_WXSTR_CACHE.

Parameters
aMaxSize- initial max number of items of the cache

Definition at line 120 of file lru_cache.h.

120 : m_maxSize( aMaxSize ) {}
size_t m_maxSize
Max capacity of the cache.
Definition: lru_cache.h:111

Member Function Documentation

template<typename t_value>
bool LRU_WXSTR_CACHE< t_value >::Exists ( const wxString &  aKey) const
inline

Function Exists.

Parameters
aKeykey to look for
Returns
true if the aKey exists

Definition at line 194 of file lru_cache.h.

195  {
196  return ( m_map_iterators.find( aKey ) != m_map_iterators.end() );
197  }
WXSTR_HASH_MAP m_map_iterators
Cache map with iterators of the list.
Definition: lru_cache.h:105
template<typename t_value>
const t_value& LRU_WXSTR_CACHE< t_value >::Get ( const wxString &  aKey)
inline

Function Get Returns an existent value from the given key.

The key must exists, if not it will throw an error. Use function Exists to check first if you can get that key

Parameters
aKey= an existent key
Returns
t_value

Definition at line 172 of file lru_cache.h.

References LRU_WXSTR_CACHE< t_value >::m_cached_list.

173  {
174  MAP_ITERATOR map_it = m_map_iterators.find( aKey );
175 
176  if( map_it == m_map_iterators.end() )
177  {
178  throw std::range_error( "Requested a key that dont exists" );
179  }
180 
181  // This will update the list and put in the beginning the iterator that we are getting
182  m_cached_list.splice( m_cached_list.begin(), m_cached_list, map_it->second );
183 
184  // Return the t_value from the <key, value> pair that was in the list
185  return map_it->second->second;
186  }
WXSTR_HASH_MAP m_map_iterators
Cache map with iterators of the list.
Definition: lru_cache.h:105
CACHED_LIST m_cached_list
list of cached items
Definition: lru_cache.h:100
WXSTR_HASH_MAP::iterator MAP_ITERATOR
Declares MAP_ITERATOR Declares a iterator for the map.
Definition: lru_cache.h:94
template<typename t_value>
void LRU_WXSTR_CACHE< t_value >::Insert ( const wxString &  aKey,
const t_value &  aValue 
)
inline

Function Insert.

Parameters
aKey= the string key that is the reference of this entry
aValue= the value to add

Definition at line 128 of file lru_cache.h.

References LRU_WXSTR_CACHE< t_value >::m_maxSize.

129  {
130  MAP_ITERATOR it = m_map_iterators.find( aKey );
131 
132  if( it != m_map_iterators.end() )
133  {
134  // It already exists, so must remove it from list and form the map
135  // it->second have a iterator from the list m_cached_list
136  m_cached_list.erase( it->second );
137  m_map_iterators.erase( it );
138  }
139 
140  // Inserts a new element at the beginning of the list, a pair of <aKey, aValue>
141  m_cached_list.push_front( KEY_VALUE_PAIR( aKey, aValue) );
142 
143  // Insert a new key and the added list iterator to the map
144  m_map_iterators[aKey] = m_cached_list.begin();
145 
146  // Manage the size of the list
147  if( m_cached_list.size() > m_maxSize )
148  {
149  // Get an iterator to the end of the list
150  LIST_ITERATOR last_it = m_cached_list.end();
151 
152  // This gets the real iterator that is the latest one
153  last_it--;
154 
155  // Remove the key from the map
156  m_map_iterators.erase( last_it->first );
157 
158  // Removes the last element in the list
159  m_cached_list.pop_back();
160  }
161  }
WXSTR_HASH_MAP m_map_iterators
Cache map with iterators of the list.
Definition: lru_cache.h:105
CACHED_LIST m_cached_list
list of cached items
Definition: lru_cache.h:100
size_t m_maxSize
Max capacity of the cache.
Definition: lru_cache.h:111
std::pair< wxString, t_value > KEY_VALUE_PAIR
Declares KEY_VALUE_PAIR Declares a pair with the key (wxString) and a value.
Definition: lru_cache.h:68
CACHED_LIST::iterator LIST_ITERATOR
Declares LIST_ITERATOR Declares a iterator type for a list of KEY_VALUE_PAIR.
Definition: lru_cache.h:81
WXSTR_HASH_MAP::iterator MAP_ITERATOR
Declares MAP_ITERATOR Declares a iterator for the map.
Definition: lru_cache.h:94
template<typename t_value>
size_t LRU_WXSTR_CACHE< t_value >::MaxSize ( ) const
inline

Function MaxSize.

Returns
size_t current max size of the cache

Definition at line 235 of file lru_cache.h.

References LRU_WXSTR_CACHE< t_value >::m_maxSize.

236  {
237  return m_maxSize;
238  }
size_t m_maxSize
Max capacity of the cache.
Definition: lru_cache.h:111
template<typename t_value>
void LRU_WXSTR_CACHE< t_value >::Resize ( size_t  aNewSize)
inline

Function Resize If aNewSize is smaller than the current maxSize then the items back in the list are discarded This function can be used to empty the cache, setting the new size to 0.

Parameters
aNewSize- resize the store capability of the list to aNewSize

Definition at line 206 of file lru_cache.h.

References LRU_WXSTR_CACHE< t_value >::m_maxSize.

207  {
208  m_maxSize = aNewSize;
209 
210  while( m_map_iterators.size() > m_maxSize )
211  {
212  // Remove the key from the map
213  m_map_iterators.erase( m_cached_list.back().first );
214 
215  // Remove the back of the list
216  m_cached_list.pop_back();
217  }
218  }
WXSTR_HASH_MAP m_map_iterators
Cache map with iterators of the list.
Definition: lru_cache.h:105
CACHED_LIST m_cached_list
list of cached items
Definition: lru_cache.h:100
size_t m_maxSize
Max capacity of the cache.
Definition: lru_cache.h:111
template<typename t_value>
size_t LRU_WXSTR_CACHE< t_value >::Size ( ) const
inline

Function Size.

Returns
size_t current size of the cache

Definition at line 225 of file lru_cache.h.

226  {
227  return m_map_iterators.size();
228  }
WXSTR_HASH_MAP m_map_iterators
Cache map with iterators of the list.
Definition: lru_cache.h:105

Member Data Documentation

template<typename t_value>
CACHED_LIST LRU_WXSTR_CACHE< t_value >::m_cached_list
private

list of cached items

Definition at line 100 of file lru_cache.h.

Referenced by LRU_WXSTR_CACHE< t_value >::Get().

template<typename t_value>
WXSTR_HASH_MAP LRU_WXSTR_CACHE< t_value >::m_map_iterators
private

Cache map with iterators of the list.

Definition at line 105 of file lru_cache.h.

template<typename t_value>
size_t LRU_WXSTR_CACHE< t_value >::m_maxSize
private

Max capacity of the cache.

Definition at line 111 of file lru_cache.h.

Referenced by LRU_WXSTR_CACHE< t_value >::Insert(), LRU_WXSTR_CACHE< t_value >::MaxSize(), and LRU_WXSTR_CACHE< t_value >::Resize().


The documentation for this class was generated from the following file: