16 #include <unordered_map>
24 template <
class Tkey,
class Tdata>
27 typedef std::pair<Tkey, Tdata *> Tpair;
28 typedef typename std::list<Tpair>::iterator Titer;
31 std::unordered_map<Tkey, Titer>
lookup;
49 return this->lookup.find(key) != this->lookup.end();
58 Tdata *
Insert(
const Tkey key, Tdata *item)
64 old = this->lookup[key]->second;
65 this->lookup[key]->second = item;
68 if (this->data.size() >= this->capacity) {
69 Tpair last =
data.back();
70 this->lookup.erase(last.first);
71 this->data.pop_back();
77 this->data.push_front(std::make_pair(key, item));
78 this->lookup.emplace(key, this->data.begin());
90 if (this->data.empty())
return nullptr;
92 Tdata *value = this->data.back().second;
93 this->lookup.erase(this->data.back().first);
94 this->data.pop_back();
104 inline Tdata *
Get(
const Tkey key)
106 if (this->lookup.find(key) == this->lookup.end())
throw std::out_of_range(
"item not found");
108 this->data.splice(this->data.begin(), this->data, this->lookup[key]);
110 return this->data.front().second;