19#ifndef INCLUDED_STOC_SOURCE_SECURITY_LRU_CACHE_H
20#define INCLUDED_STOC_SOURCE_SECURITY_LRU_CACHE_H
23#include <unordered_map>
26#ifdef __CACHE_DIAGNOSE
27#include <osl/diagnose.h>
28#include <rtl/ustrbuf.hxx>
29#include <rtl/ustring.hxx>
30#include <rtl/string.hxx>
40template<
typename t_key,
typename t_val,
typename t_hashKey,
typename t_equalKey >
50 typedef std::unordered_map< t_key, Entry *, t_hashKey, t_equalKey >
t_key2element;
69 inline t_val
const *
lookup( t_key
const & key )
const;
76 inline void set( t_key
const & key, t_val
const & val );
82 inline void setSize( ::std::size_t size );
85template<
typename t_key,
typename t_val,
typename t_hashKey,
typename t_equalKey >
89 m_key2element.clear();
95 m_block.reset(
new Entry[ m_size ] );
96 m_head = m_block.get();
97 m_tail = m_block.get() + m_size -1;
98 for ( ::std::size_t
nPos = m_size;
nPos--; )
101 m_block[
nPos ].m_succ = m_block.get() +
nPos +1;
106template<
typename t_key,
typename t_val,
typename t_hashKey,
typename t_equalKey >
115template<
typename t_key,
typename t_val,
typename t_hashKey,
typename t_equalKey >
117 Entry * entry )
const
138template<
typename t_key,
typename t_val,
typename t_hashKey,
typename t_equalKey >
140 t_key
const & key )
const
144 typename t_key2element::const_iterator
const iFind( m_key2element.find( key ) );
145 if (iFind != m_key2element.end())
147 Entry * entry = iFind->second;
149#ifdef __CACHE_DIAGNOSE
150 OUStringBuffer buf( 48 );
151 buf.appendAscii(
"> retrieved element \"" );
152 buf.append( entry->
m_key );
153 buf.appendAscii(
"\" from cache" );
154 SAL_INFO(
"stoc", buf.makeStringAndClear() );
156 return &entry->
m_val;
162template<
typename t_key,
typename t_val,
typename t_hashKey,
typename t_equalKey >
164 t_key
const & key, t_val
const & val )
168 typename t_key2element::const_iterator
const iFind( m_key2element.find( key ) );
171 if (iFind == m_key2element.end())
174#ifdef __CACHE_DIAGNOSE
175 if (entry->
m_key.getLength())
177 OUStringBuffer buf( 48 );
178 buf.appendAscii(
"> kicking element \"" );
179 buf.append( entry->
m_key );
180 buf.appendAscii(
"\" from cache" );
181 SAL_INFO(
"stoc", buf.makeStringAndClear() );
184 m_key2element.erase( entry->
m_key );
186 ::std::pair< typename t_key2element::iterator, bool > insertion(
187 m_key2element.emplace( key, entry ) );
188 OSL_ENSURE( insertion.second,
"### inserting new cache entry failed?!" );
192 entry = iFind->second;
193#ifdef __CACHE_DIAGNOSE
194 OUStringBuffer buf( 48 );
195 buf.appendAscii(
"> replacing element \"" );
196 buf.append( entry->
m_key );
197 buf.appendAscii(
"\" in cache" );
198 SAL_INFO(
"stoc", buf.makeStringAndClear() );
Implementation of a least recently used (lru) cache.
void toFront(Entry *entry) const
std::unique_ptr< Entry[]> m_block
void setSize(::std::size_t size)
Sets the number of elements to be cached.
t_key2element m_key2element
t_val const * lookup(t_key const &key) const
Retrieves a pointer to value in cache.
void set(t_key const &key, t_val const &val)
Sets a value to be cached for given key.
std::unordered_map< t_key, Entry *, t_hashKey, t_equalKey > t_key2element
#define SAL_INFO(area, stream)