-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.cpp
240 lines (222 loc) · 5.44 KB
/
cache.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
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
struct el {
int id;
vector<string> data;
};
class Cache {
private:
class Node;
Node *first;
Node *last;
int length;
int str_size;
vector<string> structure;
string get_list(vector<el> &list);
string get_list_print(vector<el> &list);
el get_el(string str);
string get_table();
public:
string filename;
Cache();
~Cache();
void save();
void push(vector<string> data);
void set_struct(vector<string> data);
void print();
void print(string value);
int get_struct_size();
void load(string filename);
void set_file(string str);
void update(string name, vector<string> &data);
};
class Cache::Node {
public:
int id;
vector<el> data;
Node *prev;
Node *next;
Node(int id) { this->id = id; };
Node(){};
};
Cache::Cache() {
str_size = 0;
length = 1;
first = new Node();
last = first;
};
Cache::~Cache() {
for (int i = length - 2; i > 0; i--) {
last = last->prev;
length--;
delete last->next;
}
delete first;
length = 0;
};
void Cache::push(vector<string> data) {
if (str_size == 0) {
cout << "Database structure is not set, type help for more info" << endl;
return;
}
int id = get_first_id(data[0]);
el newEl = {get_second_id(data[0]), data};
Node *tmp = first;
for (int i = 0; i < length - 1; i++) {
if (id == tmp->id) {
tmp->data.push_back(newEl);
return;
}
tmp = tmp->next;
}
if (length == 1) {
first->id = id;
first->data.push_back(newEl);
length++;
return;
} else {
Node *newNode = new Node(id);
newNode->data.push_back(newEl);
newNode->prev = last;
last->next = newNode;
last = newNode;
length++;
return;
}
};
string Cache::get_list(vector<el> &list) {
string result;
for (int i = 0; i < list.size(); i++) {
result = result + to_string(list[i].id);
result = result + " | " + join(list[i].data, ' ') + '\t';
}
return result;
};
string Cache::get_list_print(vector<el> &list) {
string result = "| " + join(list[0].data, " | ");
for (int i = 1; i < list.size(); i++)
result = result + "\n| " + join(list[i].data, " | ");
return result;
};
void Cache::print() {
if (str_size == 0) {
cout << "Database is empty\n";
return;
}
Node *tmp = first;
cout << "| ";
for (int i = 0; i < str_size; i++)
cout << structure[i] + " | ";
for (int i = 0; i < length - 1; i++) {
cout << '\n' << this->get_list_print(tmp->data);
tmp = tmp->next;
}
cout << '\n';
};
void Cache::print(string value) {
int id = get_first_id(value);
Node *result = first;
if (first->id == id) result = first;
else
for (int i = 0; i < length - 1; i++) {
if (result->id == id) break;
result = result->next;
}
cout << this->get_list_print(result->data) << '\n';
};
el Cache::get_el(string str) {
int id = get_first_id(str);
Node *result = nullptr;
if (first->id == id) result = first;
else {
Node *tmp = first;
for (int i = 0; i < length - 1; i++) {
if (tmp->id == id) break;
tmp = tmp->next;
}
result = tmp;
}
if (result != nullptr) {
int second_id = get_second_id(str);
for (int i = 0; i < result->data.size(); i++)
if (result->data[i].id == second_id) return result->data[i];
}
vector<string> res;
return el{-1, res};
};
string Cache::get_table() {
string result = structure[0];
for (int i = 1; i < str_size; i++)
result = result + " | " + structure[i];
result += '\n';
Node *tmp = first;
for (int i = 0; i < length - 1; i++) {
result = result + to_string(tmp->id);
result = result + '\t' + this->get_list(tmp->data) + '\n';
tmp = tmp->next;
}
return result;
};
void Cache::save() {
if (str_size == 0) {
cout << "Database structure is not set, type help for more info" << endl;
return;
}
fstream file;
file.open(filename, ios::out);
if (file.is_open()) {
file << get_table();
file.close();
}
};
void Cache::set_file(string str) { filename = str + ".bullsheet"; };
int Cache::get_struct_size() { return str_size; };
void Cache::set_struct(vector<string> data) {
structure = data;
str_size = data.size();
};
void Cache::load(string filename) {
ifstream file;
file.open(filename, ios::in);
string st;
getline(file, st);
vector<string> tmp_struct;
vector<string> tmp = split(st, '|');
tmp_struct.push_back(split(tmp[0], ' ')[0]);
for (int i = 1; i < tmp.size(); i++)
tmp_struct.push_back(split(tmp[i], ' ')[1]);
if (tmp_struct.size() == 0) cout << "Wrong or damaged file.";
set_struct(tmp_struct);
if (file.is_open()) {
this->filename = filename;
string data;
do {
getline(file, data);
vector<string> arr = split(data, '|');
for (int i = 1; i < arr.size(); i++) {
vector<string> el = split(arr[i], ' ');
vector<string> tmp;
for (int i = 1; i <= get_struct_size(); i++) {
string temp = el[i];
tmp.push_back(temp);
}
push(tmp);
}
} while (!data.empty());
} else cout << "Can't read file" << filename << endl;
};
void Cache::update(string name, vector<string> &data) {
int id = get_first_id(name);
Node *result = nullptr;
if (first->id == id) result = first;
else {
Node *tmp = first;
for (int i = 0; i < length - 1; i++) {
if (tmp->id == id) break;
tmp = tmp->next;
};
result = tmp;
};
for (int i = 0; i < result->data.size(); i++)
if (result->data[i].data[0] == name)
result->data.erase(result->data.begin() + i);
push(data);
};