-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.cpp
More file actions
191 lines (181 loc) · 4.47 KB
/
cache.cpp
File metadata and controls
191 lines (181 loc) · 4.47 KB
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
#include "cache.h"
// TODO: 필요한 함수 구현
Cache::Cache()
{
Cur_size = 0;
pHead = nullptr;
}
Cache::~Cache()
{
while (pHead != nullptr)
{
struct node* temp = pHead;
pHead = pHead->pNext;
delete temp;
}
}
// int를 cache에 추가한다
void Cache::add(std::string key, int value)
{
if (this->get(key, value))
{
struct node* temp = pHead;
struct node* pPrev = nullptr;
while (temp != nullptr)
{
if (temp->key == key && temp->value_int == value)
{
if (pPrev == nullptr) {
// 매칭되는 노드가 헤드인 경우
pHead = temp->pNext;
} else {
pPrev->pNext = temp->pNext;
}
delete temp;
break;
}
pPrev = temp;
temp = temp->pNext;
}
}
struct node* pNode = new struct node;
pNode->key = key;
pNode->value_int = value;
pNode->value_double = 0;
pNode->pNext = nullptr;
if (Cur_size == 0)
{
pHead = pNode;
Cur_size++;
}
else
{
if (Cur_size >= Max_size)
{
struct node* temp = pHead;
pHead = pHead->pNext;
delete temp;
Cur_size--;
}
struct node* pLastNode = pHead;
while (pLastNode->pNext != nullptr)
{
pLastNode = pLastNode->pNext;
}
pLastNode->pNext = pNode;
Cur_size++;
}
}
// double을 cache에 추가한다
void Cache::add(std::string key, double value)
{
if (this->get(key, value))
{
struct node* temp = pHead;
struct node* pPrev = nullptr;
while (temp != nullptr)
{
if (temp->key == key && temp->value_double == value)
{
if (pPrev == nullptr)
{
// 매칭되는 노드가 헤드인 경우
pHead = temp->pNext;
}
else
{
pPrev->pNext = temp->pNext;
}
delete temp;
break;
}
pPrev = temp;
temp = temp->pNext;
}
}
struct node* pNode = new struct node;
pNode->key = key;
pNode->value_int = 0;
pNode->value_double = value;
pNode->pNext = nullptr;
if (Cur_size == 0)
{
pHead = pNode;
Cur_size++;
}
else
{
if (Cur_size >= Max_size)
{
struct node* temp = pHead;
pHead = pHead->pNext;
delete temp;
Cur_size--;
}
struct node* pLastNode = pHead;
while (pLastNode->pNext != nullptr)
{
pLastNode = pLastNode->pNext;
}
pLastNode->pNext = pNode;
Cur_size++;
}
}
// key에 해당하는 value를 cache에서 가져온다
// 타입과 key가 동일한 원소가 없다면 false를 반환한다.
bool Cache::get(std::string key, int &value)
{
struct node* temp = pHead;
while (temp != nullptr)
{
if (temp->key == key && temp->value_int != 0)
{
value = temp->value_int;
return true;
}
temp = temp->pNext;
}
return false;
}
// key에 해당하는 value를 cache에서 가져온다.
// 타입과 key가 동일한 원소가 없다면 false를 반환한다.
bool Cache::get(std::string key, double &value)
{
struct node* temp = pHead;
while (temp != nullptr)
{
if (temp->key == key && temp->value_double != 0)
{
value = temp->value_double;
return true;
}
temp = temp->pNext;
}
return false;
}
// 디버그 및 채점 용: 연결 리스트를 문자열로 표현하여 반환한다
// 다음과 같이 표현된 문자열을 반환한다
// [key1: value1] -> [key2: value2] -> ... -> [keyN: valueN]
std::string Cache::toString()
{
std::string result = "";
struct node* temp = pHead;
while (temp != nullptr)
{
if (temp->value_int != 0)
{
result += "[" + temp->key + ": " + std::to_string(temp->value_int) + "]";
}
else
{
result += "[" + temp->key + ": " + std::to_string(temp->value_double) + "]";
}
if (temp->pNext != nullptr)
{
result += " -> ";
}
temp = temp->pNext;
}
result += "\n";
return result;
}