-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRecentList.h
88 lines (70 loc) · 1.52 KB
/
RecentList.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef __RECENT_LIST_HEADER__
#define __RECENT_LIST_HEADER__
#include "RandomAccessMap.h"
#include <unordered_map>
#include <stdexcept>
#include <limits>
template<typename Item_t>
class RecentList
{
public:
RecentList(std::size_t limit = std::numeric_limits<std::size_t>::max())
: _lastIndex(0), _limit(limit)
{
}
void add(const Item_t& item)
{
Hash_t::iterator pos = _hash.find(item);
if (pos != _hash.end())
{
_indexMap.remove(pos->second);
}
else if (size() == _limit)
{
Hash_t::iterator pos = _indexMap.begin()->second;
_indexMap.remove(pos->second);
_hash.erase(pos);
}
++_lastIndex;
pos = _hash.insert(Hash_t::value_type(item, _lastIndex)).first;
_indexMap[_lastIndex] = pos;
}
void remove(const Item_t& item)
{
Hash_t::iterator pos = _hash.find(item);
if (pos != _hash.end())
{
_indexMap.remove(pos->second);
_hash.erase(pos);
}
}
bool has(const Item_t& item) const
{
return _hash.find(item) != _hash.end();
}
const Item_t& operator[](const std::size_t& index) const
{
if (index >= size())
{
throw std::invalid_argument("Invalided Index");
}
std::size_t map_index = size() - index -1;
return _indexMap.data(map_index)->first;
}
std::size_t size() const
{
return _hash.size();
}
void clear()
{
_hash.clear();
_indexMap.clear();
}
protected:
typedef std::unordered_map<Item_t, std::size_t> Hash_t;
Hash_t _hash;
RandomAccessMap<std::size_t, typename Hash_t::iterator> _indexMap;
std::size_t _lastIndex;
std::size_t _limit;
};
#endif