\doxysection{Action\+Cache.\+hpp}
\hypertarget{ActionCache_8hpp_source}{}\label{ActionCache_8hpp_source}\index{/home/chr/devel/PhD/Environment/pub/PCBEnv/pcbenv/cxx/RL/ActionCache.hpp@{/home/chr/devel/PhD/Environment/pub/PCBEnv/pcbenv/cxx/RL/ActionCache.hpp}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ \textcolor{preprocessor}{\#ifndef\ GYM\_PCB\_RL\_ACTIONCACHE\_H}}
\DoxyCodeLine{00002\ \textcolor{preprocessor}{\#define\ GYM\_PCB\_RL\_ACTIONCACHE\_H}}
\DoxyCodeLine{00003\ }
\DoxyCodeLine{00004\ \textcolor{preprocessor}{\#include\ "{}RL/Action.hpp"{}}}
\DoxyCodeLine{00005\ \textcolor{preprocessor}{\#include\ <list>}}
\DoxyCodeLine{00006\ \textcolor{preprocessor}{\#include\ <map>}}
\DoxyCodeLine{00007\ }
\DoxyCodeLine{00008\ \textcolor{keyword}{template}<\textcolor{keyword}{class}\ Data>\ \textcolor{keyword}{class\ }ActionCache}
\DoxyCodeLine{00009\ \{}
\DoxyCodeLine{00010\ \textcolor{keyword}{public}:}
\DoxyCodeLine{00011\ \ \ \ \ ActionCache(uint\ size,\ uint\ maxOversize)\ :\ mSize(size),\ mTriggerSize(size\ +\ maxOversize)\ \{\ \}}
\DoxyCodeLine{00012\ \ \ \ \ \textcolor{keyword}{using\ }Ref\ =\ Action::CacheRef;}
\DoxyCodeLine{00013\ \ \ \ \ \textcolor{keyword}{using\ }List\ =\ std::list<std::pair<Ref,\ Data>>;}
\DoxyCodeLine{00014\ \ \ \ \ Data\ *load(Ref);}
\DoxyCodeLine{00015\ \ \ \ \ \textcolor{keywordtype}{void}\ store(Ref,\ \textcolor{keyword}{const}\ Data\&);}
\DoxyCodeLine{00016\ \ \ \ \ \textcolor{keywordtype}{void}\ clear();}
\DoxyCodeLine{00017\ \ \ \ \ \textcolor{keywordtype}{void}\ shrink();}
\DoxyCodeLine{00018\ \textcolor{keyword}{private}:}
\DoxyCodeLine{00019\ \ \ \ \ std::map<Ref,\ typename\ List::iterator>\ mLUT;}
\DoxyCodeLine{00020\ \ \ \ \ List\ mLRU;}
\DoxyCodeLine{00021\ \ \ \ \ uint\ mSize;}
\DoxyCodeLine{00022\ \ \ \ \ uint\ mTriggerSize;}
\DoxyCodeLine{00023\ \};}
\DoxyCodeLine{00024\ }
\DoxyCodeLine{00025\ \textcolor{keyword}{template}<\textcolor{keyword}{class}\ Data>\ Data\ *ActionCache<Data>::load(ActionCache::Ref\ ref)}
\DoxyCodeLine{00026\ \{}
\DoxyCodeLine{00027\ \ \ \ \ \textcolor{keyword}{auto}\ M\ =\ mLUT.find(ref);}
\DoxyCodeLine{00028\ \ \ \ \ \textcolor{keywordflow}{if}\ (M\ ==\ mLUT.end())}
\DoxyCodeLine{00029\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00030\ \ \ \ \ mLRU.splice(mLRU.end(),\ mLRU,\ M-\/>second);}
\DoxyCodeLine{00031\ \ \ \ \ \textcolor{keywordflow}{return}\ \&M-\/>second-\/>second;}
\DoxyCodeLine{00032\ \}}
\DoxyCodeLine{00033\ }
\DoxyCodeLine{00034\ \textcolor{keyword}{template}<\textcolor{keyword}{class}\ Data>\ \textcolor{keywordtype}{void}\ ActionCache<Data>::store(ActionCache::Ref\ ref,\ \textcolor{keyword}{const}\ Data\ \&data)}
\DoxyCodeLine{00035\ \{}
\DoxyCodeLine{00036\ \ \ \ \ \textcolor{keyword}{auto}\ M\ =\ mLUT.find(ref);}
\DoxyCodeLine{00037\ \ \ \ \ \textcolor{keywordflow}{if}\ (M\ !=\ mLUT.end())\ \{}
\DoxyCodeLine{00038\ \ \ \ \ \ \ \ \ M-\/>second-\/>second\ =\ data;}
\DoxyCodeLine{00039\ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{00040\ \ \ \ \ \ \ \ \ mLRU.emplace\_back(ref,\ data);}
\DoxyCodeLine{00041\ \ \ \ \ \ \ \ \ mLUT[ref]\ =\ -\/-\/mLRU.end();}
\DoxyCodeLine{00042\ \ \ \ \ \}}
\DoxyCodeLine{00043\ \ \ \ \ \textcolor{keywordflow}{if}\ (mLRU.size()\ >=\ mTriggerSize)}
\DoxyCodeLine{00044\ \ \ \ \ \ \ \ \ shrink();}
\DoxyCodeLine{00045\ \}}
\DoxyCodeLine{00046\ }
\DoxyCodeLine{00047\ \textcolor{keyword}{template}<\textcolor{keyword}{class}\ Data>\ \textcolor{keywordtype}{void}\ ActionCache<Data>::shrink()}
\DoxyCodeLine{00048\ \{}
\DoxyCodeLine{00049\ \ \ \ \ \textcolor{keywordflow}{if}\ (mLRU.size()\ <=\ mSize)}
\DoxyCodeLine{00050\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{00051\ \ \ \ \ uint\ N\ =\ mLRU.size()\ -\/\ mSize;}
\DoxyCodeLine{00052\ \ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keyword}{auto}\ I\ =\ mLRU.begin();\ I\ !=\ mLRU.end()\ \&\&\ N;\ -\/-\/N)\ \{}
\DoxyCodeLine{00053\ \ \ \ \ \ \ \ \ mLUT.erase(I-\/>first);}
\DoxyCodeLine{00054\ \ \ \ \ \ \ \ \ I\ =\ mLRU.erase(I);}
\DoxyCodeLine{00055\ \ \ \ \ \}}
\DoxyCodeLine{00056\ \}}
\DoxyCodeLine{00057\ }
\DoxyCodeLine{00058\ \textcolor{keyword}{template}<\textcolor{keyword}{class}\ Data>\ \textcolor{keywordtype}{void}\ ActionCache<Data>::clear()}
\DoxyCodeLine{00059\ \{}
\DoxyCodeLine{00060\ \ \ \ \ mLUT.clear();}
\DoxyCodeLine{00061\ \ \ \ \ mLRU.clear();}
\DoxyCodeLine{00062\ \}}
\DoxyCodeLine{00063\ }
\DoxyCodeLine{00064\ \textcolor{preprocessor}{\#endif\ }\textcolor{comment}{//\ GYM\_PCB\_RL\_ACTIONCACHE\_H}}

\end{DoxyCode}
