#include <list>
template<class K, class V>
class CachedMap: private std::list<std::pair<K, V> >
{
public:
typedef K KeyType;
typedef V ValueType;
typedef std::pair<K, V> CachedEntry;
typedef std::list<CachedEntry> BaseType;
private:
const unsigned int cachedSize;
public:
CachedMap(const unsigned int& size): cachedSize(size)
{
}
void insert(const KeyType& key, const ValueType& value)
{
if (BaseType::size()>=cachedSize)
{
BaseType::erase(BaseType::begin());
}
BaseType::push_back(CachedEntry(key, value));
}
/*void erase(const KeyType& key)
{
for (typename BaseType::iterator it=BaseType::begin(); it!=BaseType::end(); ++it)
{
if (it->first==key)
{
BaseType::erase(it);
return;
}
}
}*/
size_t size() const
{
return BaseType::size();
}
ValueType get(const KeyType& key) const
{
for (typename BaseType::const_iterator it=BaseType::begin(); it!=BaseType::end(); ++it)
{
if (it->first==key)
{
return it->second;
}
}
}
/*typename BaseType::const_iterator begin() const
{
return BaseType::begin();
}
typename BaseType::const_iterator end() const
{
return BaseType::end();
}
ValueType& operator[](const KeyType& key)
{
for (typename BaseType::iterator it=BaseType::begin(); it!=BaseType::end(); ++it)
{
if (it->first==key)
{
return it->second;
}
}
}*/
};