-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLRUSet.cpp
179 lines (157 loc) · 4.54 KB
/
LRUSet.cpp
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
//
// LRUSet.cpp
//
#include <iostream>
#include <string>
#include <memory>
#include <set>
#include <map>
#include <assert.h>
struct Content {
std::string url;
std::string content;
Content(std::string url) : url(url) { }
Content(std::string url, std::string content) : url(url), content(content) { }
bool operator < (const Content &c) const { return c.url < url; }
void Print(void) const { std::cout << " url: " << url << ", content: " << content << std::endl; }
};
template <class Value>
class LRUSet {
struct SList {
SList *next;
Value *value;
SList() : next(NULL), value(NULL) { }
};
typedef typename SList *SListPtr;
struct SListPtrOps {
// sort by value of next node
bool operator()( const SListPtr &a, const SListPtr &b ) const { return *(a->next->value) < *(b->next->value); }
};
typedef typename std::set<SListPtr, SListPtrOps> SListPtrSet;
SListPtrSet m_Set;
size_t m_Size;
SList *m_Oldest;
SList *m_Newest;
typename SListPtrSet::const_iterator Find(const Value &value) const {
SList a, b;
a.next = &b;
b.value = const_cast<Value *>(&value);
return m_Set.find(&a);
}
public:
LRUSet() : m_Size(0) {
m_Oldest = new SList();
m_Newest = m_Oldest;
}
bool Get(Value &value) {
typename SListPtrSet::const_iterator current_pos = Find(value);
if (current_pos == m_Set.end()) {
return false;
}
value = *((*current_pos)->next->value);
Add(value);
return true;
}
bool Has(const Value &value) {
typename SListPtrSet::const_iterator current_pos = Find(value);
return current_pos != m_Set.end();
}
void Add(Value &value) {
SList *previous_node, *current_node, *next_node, *terminal_node;
Value *previous_value, *current_value;
typename SListPtrSet::const_iterator current_pos = Find(value);
if (current_pos != m_Set.end()) {
// update
// 自ノードを取得
current_node = *current_pos;
// 次ノードを取得
next_node = current_node->next;
assert(next_node != NULL);
if (next_node == m_Newest) {
// current is the newest
assert(next_node->next == NULL);
return;
}
previous_value = current_node->value;
current_value = next_node->value;
// valueは次ノードに入っている値と同じはず
// assert(value == *current_value);
assert(!(value < *current_value) && !(*current_value < value));
if (previous_value == NULL) {
// current is the oldest
assert(current_node == m_Oldest);
m_Oldest = next_node;
} else {
typename SListPtrSet::const_iterator previous_pos = Find(*previous_value);
// 値があるので前ノードが存在するはず
assert(previous_pos != m_Set.end());
// 前ノードを取得
previous_node = *previous_pos;
// 前ノードは自ノードを指しているはず
assert(previous_node->next == current_node);
// 前ノードが次ノードを指すように変更
previous_node->next = next_node;
}
// 次ノードの値に自ノードが保持していた値(前ノードの値)を設定
next_node->value = previous_value;
} else {
// insert
current_node = new SList();
current_value = new Value(value);
SList temp_node;
current_node->next = &temp_node;
temp_node.value = current_value;
m_Set.insert(current_node);
++m_Size;
}
// 終端ノードを取得
terminal_node = m_Newest->next;
if (terminal_node != NULL) {
// 1つ以上のノードが存在
m_Newest->next = current_node;
} else {
// 値を持ったノードをはじめて追加する
assert(m_Size == 1);
assert(m_Oldest == m_Newest);
m_Oldest = current_node;
terminal_node = m_Newest;
}
// 元々終端ノードに入っていた値を自ノードに設定
current_node->value = terminal_node->value;
// 自ノードの次を終端ノードに設定
current_node->next = terminal_node;
// 終端ノードに自ノードの値を設定
terminal_node->value = current_value;
// 自ノードをNewestに設定
m_Newest = current_node;
}
void Dump(void) const {
std::cout << "Dump:" << std::endl;
int i;
SList *p;
for (i = 0, p = m_Oldest; p != NULL; ++i, p = p->next) {
std::cout << " " << i << ": next: " << p->next;
if (p->value) {
p->value->Print();
} else {
std::cout << " (null)" << std::endl;
}
}
}
};
int main(int argc, char *argv[])
{
LRUSet<Content> cache;
cache.Dump();
cache.Add(Content("abc", "aaa"));
cache.Dump();
cache.Add(Content("def", "ddd"));
cache.Dump();
cache.Add(Content("ghi", "ggg"));
cache.Dump();
cache.Add(Content("jkl", "jjj"));
cache.Dump();
cache.Get(Content("def"));
cache.Dump();
return 0;
}