-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlfu.hpp
272 lines (245 loc) · 8.73 KB
/
lfu.hpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#ifndef _ASP_LFU_HPP_
#define _ASP_LFU_HPP_
#include "lfu_table.hpp"
namespace asp {
template <typename _Key, typename _Tp,
typename _Hash = std::hash<_Key>,
typename _Alloc = std::allocator<std::pair<const _Key, _Tp>>
> class lfu_map;
template <typename _Tp,
typename _Hash = std::hash<_Tp>,
typename _Alloc = std::allocator<_Tp>
> class lfu_set;
template <typename _Key, typename _Tp, typename _Hash, typename _Alloc>
class lfu_map {
typedef lfu_map<_Key, _Tp, _Hash, _Alloc> self;
typedef lfu_table<_Key, std::pair<const _Key, _Tp>, _select_0x, _select_1x, _Hash, _Alloc> lfu_t;
typedef typename lfu_t::key_type key_type;
typedef typename lfu_t::value_type value_type;
typedef typename lfu_t::mapped_type mapped_type;
// typedef typename lru_t::node_type node_type;
typedef typename lfu_t::iterator iterator;
typedef typename lfu_t::const_iterator const_iterator;
lfu_t _l;
public:
/// (de)constructor
lfu_map(size_type _capacity) : _l(_capacity) {}
lfu_map(const self& _rhs) : _l(_rhs._l) {}
self& operator=(const self& _rhs) {
if (&_rhs == this) return *this;
_l.operator=(_rhs._l);
return *this;
}
virtual ~lfu_map() = default;
size_type size() const { return _l.size(); }
size_type capacity() const { return _l.capacity(); }
bool empty() const { return _l.empty(); }
void clear() { _l.clear(); }
void resize(size_type _new_capacity) { _l.resize(_new_capacity); }
const_iterator get(const key_type& _k) { return _l.get(_k); }
void put(const value_type& _v) { _l.put(_v); }
const_iterator cbegin() const { return _l.cbegin(); }
const_iterator cend() const { return _l.cend(); }
template <typename _K, typename _T, typename _H, typename _A> friend std::ostream& operator<<(std::ostream& _os, const lfu_map<_K, _T, _H, _A>& _x);
int check() const;
void demo(std::istream& _is = std::cin, std::ostream& _os = std::cout);
private:
enum operator_id {
__GET__, __PUT__,
__CLEAR__, __SIZE__, __RESIZE__,
__PRINT__,
__NONE__,
};
const std::unordered_map<std::string, operator_id> _operator_map = {
{"get", __GET__}, {"put", __PUT__},
{"clear", __CLEAR__}, {"size", __SIZE__},
{"resize", __RESIZE__}, {"print", __PRINT__}
};
operator_id _M_get_operator_id(const std::string& _op) {
auto _it = _operator_map.find(_op);
return _it != _operator_map.cend() ? _it->second : __NONE__;
}
};
template <typename _Tp, typename _Hash, typename _Alloc>
class lfu_set {
typedef lfu_set<_Tp, _Hash, _Alloc> self;
typedef lfu_table<_Tp, _Tp, _select_self, _select_self, _Hash, _Alloc> lfu_t;
typedef typename lfu_t::key_type key_type;
typedef typename lfu_t::value_type value_type;
typedef typename lfu_t::mapped_type mapped_type;
// typedef typename lru_t::node_type node_type;
typedef typename lfu_t::iterator iterator;
typedef typename lfu_t::const_iterator const_iterator;
lfu_t _l;
public:
/// (de)constructor
lfu_set(size_type _capacity) : _l(_capacity) {}
lfu_set(const self& _rhs) : _l(_rhs._l) {}
self& operator=(const self& _rhs) {
if (&_rhs == this) return *this;
_l.operator=(_rhs._l);
return *this;
}
virtual ~lfu_set() = default;
size_type size() const { return _l.size(); }
size_type capacity() const { return _l.capacity(); }
bool empty() const { return _l.empty(); }
void clear() { _l.clear(); }
void resize(size_type _new_capacity) { _l.resize(_new_capacity); }
const_iterator get(const key_type& _k) { return _l.get(_k); }
void put(const value_type& _v) { _l.put(_v); }
const_iterator cbegin() const { return _l.cbegin(); }
const_iterator cend() const { return _l.cend(); }
template <typename _T, typename _H, typename _A> friend std::ostream& operator<<(std::ostream& _os, const lfu_set<_T, _H, _A>& _x);
int check() const;
void demo(std::istream& _is = std::cin, std::ostream& _os = std::cout);
private:
enum operator_id {
__GET__, __PUT__,
__CLEAR__, __SIZE__, __RESIZE__,
__PRINT__,
__NONE__,
};
const std::unordered_map<std::string, operator_id> _operator_map = {
{"get", __GET__}, {"put", __PUT__},
{"clear", __CLEAR__}, {"size", __SIZE__},
{"resize", __RESIZE__}, {"print", __PRINT__}
};
operator_id _M_get_operator_id(const std::string& _op) {
auto _it = _operator_map.find(_op);
return _it != _operator_map.cend() ? _it->second : __NONE__;
}
};
template <typename _K, typename _T, typename _H, typename _A> auto
operator<<(std::ostream& _os, const lfu_map<_K, _T, _H, _A>& _x)-> std::ostream& {
return _os << _x._l;
};
template <typename _Key, typename _Tp, typename _Hash, typename _Alloc> auto
lfu_map<_Key, _Tp, _Hash, _Alloc>::check() const -> int {
return 0;
};
template <typename _Key, typename _Tp, typename _Hash, typename _Alloc> auto
lfu_map<_Key, _Tp, _Hash, _Alloc>::demo(std::istream& _is, std::ostream& _os) -> void {
_os << '[' << typeid(asp::decay_t<self>).name() << ']' << std::endl;
_is.sync_with_stdio(false);
std::string _op;
key_type _k;
mapped_type _m;
value_type _v;
size_type _n;
while (!_is.eof()) {
_is >> _op;
if (__details__::_M_end_of_file(_is)) break;
operator_id _id = this->_M_get_operator_id(_op);
switch (_id) {
case __GET__: {
_is >> _k;
if (__details__::_M_end_of_file(_is)) break;
const auto _r = this->get(_k);
_os << "get(" << _k << ") = ";
if (_r == this->cend()) {
_os << "none";
}
else {
_os << _r->first.second;
}
_os << std::endl;
}; break;
case __PUT__: {
_is >> _k;
if (__details__::_M_end_of_file(_is)) break;
_is >> _m;
if (__details__::_M_end_of_file(_is)) break;
this->put({_k, _m});
_os << "put(" << _k << ", " << _m << ")" << std::endl;
}; break;
case __CLEAR__: {
this->clear();
}; break;
case __SIZE__: {
_os << ": " << this->size() << std::endl;
}; break;
case __RESIZE__: {
_is >> _n;
if (__details__::_M_end_of_file(_is)) break;
this->resize(_n);
}; break;
case __PRINT__:{
_os << *this << std::endl;
}; break;
case __NONE__:{}; break;
}
_op.clear();
__details__::_M_reset_cin(_is);
_os << std::flush;
// _os << *this << std::endl;
}
__details__::_M_reset_cin(_is);
};
template <typename _T, typename _H, typename _A> auto
operator<<(std::ostream& _os, const lfu_set<_T, _H, _A>& _x)-> std::ostream& {
return _os << _x._l;
};
template <typename _Tp, typename _Hash, typename _Alloc> auto
lfu_set<_Tp, _Hash, _Alloc>::check() const -> int {
return 0;
};
template <typename _Tp, typename _Hash, typename _Alloc> auto
lfu_set<_Tp, _Hash, _Alloc>::demo(std::istream& _is, std::ostream& _os) -> void {
_os << '[' << typeid(asp::decay_t<self>).name() << ']' << std::endl;
_is.sync_with_stdio(false);
std::string _op;
key_type _k;
value_type _v;
size_type _n;
while (!_is.eof()) {
_is >> _op;
if (__details__::_M_end_of_file(_is)) break;
operator_id _id = this->_M_get_operator_id(_op);
switch (_id) {
case __GET__: {
_is >> _k;
if (__details__::_M_end_of_file(_is)) break;
const auto _r = this->get(_k);
_os << "get(" << _k << ") = ";
if (_r == this->cend()) {
_os << "none";
}
else {
_os << _r->first;
}
_os << std::endl;
}; break;
case __PUT__: {
// _is >> _k;
// if (__details__::_M_end_of_file(_is)) break;
_is >> _v;
if (__details__::_M_end_of_file(_is)) break;
this->put(_v);
_os << "put(" << _v << ")" << std::endl;
}; break;
case __CLEAR__: {
this->clear();
}; break;
case __SIZE__: {
_os << ": " << this->size() << std::endl;
}; break;
case __RESIZE__: {
_is >> _n;
if (__details__::_M_end_of_file(_is)) break;
this->resize(_n);
}; break;
case __PRINT__:{
_os << *this << std::endl;
}; break;
case __NONE__:{}; break;
}
_op.clear();
__details__::_M_reset_cin(_is);
_os << std::flush;
// _os << *this << std::endl;
}
__details__::_M_reset_cin(_is);
};
};
#endif // _ASP_LFU_HPP_