-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirus_genealogy.h
296 lines (253 loc) · 9.53 KB
/
virus_genealogy.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#ifndef VIRUS_GENEALOGY_H
#define VIRUS_GENEALOGY_H
#include <iterator>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
class VirusNotFound : public std::exception {
public:
const char *what() const noexcept override {
return "VirusNotFound";
}
};
class VirusAlreadyCreated : public std::exception {
public:
const char *what() const noexcept override {
return "VirusAlreadyCreated";
}
};
class TriedToRemoveStemVirus : public std::exception {
public:
const char *what() const noexcept override {
return "TriedToRemoveStemVirus";
}
};
template <typename Virus>
class VirusGenealogy {
public:
using virus_set_iterator = typename std::set<std::shared_ptr<Virus>>::iterator;
struct Iterator {
using iterator_category = std::bidirectional_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = Virus;
using pointer = std::shared_ptr<Virus>;
using reference = const Virus &;
explicit Iterator(virus_set_iterator ptr) : m_ptr(ptr) {
}
Iterator() = default;
reference operator*() const {
return **m_ptr;
}
pointer operator->() {
return *m_ptr;
}
// Prefix increment
Iterator &operator++() {
m_ptr++;
return *this;
}
// Postfix increment
Iterator operator++(int) {
Iterator tmp = *this;
++(*this);
return tmp;
}
// Prefix decrement
Iterator &operator--() {
m_ptr--;
return *this;
}
// Postfix decrement
Iterator operator--(int) {
Iterator tmp = *this;
--(*this);
return tmp;
}
friend bool operator==(const Iterator &a, const Iterator &b) {
return a.m_ptr == b.m_ptr;
};
friend bool operator!=(const Iterator &a, const Iterator &b) {
return a.m_ptr != b.m_ptr;
};
private:
virus_set_iterator m_ptr;
};
using children_iterator = Iterator;
VirusGenealogy(const VirusGenealogy<Virus> &) = delete;
VirusGenealogy &operator=(const VirusGenealogy<Virus> &) = delete;
// Tworzy nową genealogię.
// Tworzy także węzeł wirusa macierzystego o identyfikatorze stem_id.
explicit VirusGenealogy(typename Virus::id_type const &stem_id)
: stem_id(stem_id), graph{{stem_id, Node(stem_id)}} {
}
// Zwraca identyfikator wirusa macierzystego.
typename Virus::id_type get_stem_id() const noexcept {
return stem_id;
}
// Zwraca iterator pozwalający przeglądać listę identyfikatorów
// bezpośrednich następników wirusa o podanym identyfikatorze.
// Zgłasza wyjątek VirusNotFound, jeśli dany wirus nie istnieje.
// Iterator musi spełniać koncept bidirectional_iterator oraz
// typeid(*v.get_children_begin()) == typeid(const Virus &).
VirusGenealogy<Virus>::children_iterator
get_children_begin(typename Virus::id_type const &id) const {
auto it = graph.find(id);
if (it == graph.end()) {
throw VirusNotFound();
}
return Iterator(it->second.children_virus_ptrs.begin());
}
// Iterator wskazujący na element za końcem wyżej wspomnianej listy.
// Zgłasza wyjątek VirusNotFound, jeśli dany wirus nie istnieje.
VirusGenealogy<Virus>::children_iterator
get_children_end(typename Virus::id_type const &id) const {
auto it = graph.find(id);
if (it == graph.end()) {
throw VirusNotFound();
}
return Iterator(it->second.children_virus_ptrs.end());
}
// Zwraca listę identyfikatorów bezpośrednich poprzedników wirusa
// o podanym identyfikatorze.
// Zgłasza wyjątek VirusNotFound, jeśli dany wirus nie istnieje.
std::vector<typename Virus::id_type> get_parents(typename Virus::id_type const &id) const {
auto it = graph.find(id);
if (it == graph.end()) {
throw VirusNotFound();
}
return std::vector<typename Virus::id_type>(it->second.parent_ids.begin(),
it->second.parent_ids.end());
}
// Sprawdza, czy wirus o podanym identyfikatorze istnieje.
bool exists(typename Virus::id_type const &id) const noexcept {
return graph.find(id) != graph.end();
}
// Zwraca referencję do obiektu reprezentującego wirus o podanym
// identyfikatorze.
// Zgłasza wyjątek VirusNotFound, jeśli żądany wirus nie istnieje.
const Virus &operator[](typename Virus::id_type const &id) const {
auto it = graph.find(id);
if (it == graph.end()) {
throw VirusNotFound();
}
return *(it->second.virus);
}
// Tworzy węzeł reprezentujący nowy wirus o identyfikatorze id
// powstały z wirusów o podanych identyfikatorach parent_ids.
// Zgłasza wyjątek VirusAlreadyCreated, jeśli wirus o identyfikatorze
// id już istnieje.
// Zgłasza wyjątek VirusNotFound, jeśli któryś z wyspecyfikowanych
// poprzedników nie istnieje.
void create(typename Virus::id_type const &id,
std::vector<typename Virus::id_type> const &parent_ids) {
if (parent_ids.empty()) {
return;
}
auto it_new_node = graph.find(id);
if (it_new_node != graph.end()) {
throw VirusAlreadyCreated();
}
std::vector<parent_id_iterator> parents_its;
for (auto parent_id : parent_ids) {
auto temporary = graph.find(parent_id);
if (temporary == graph.end()) {
throw VirusNotFound();
}
parents_its.push_back(temporary);
}
auto new_node = Node(id);
std::vector<virus_set_iterator> edges_to_parents;
for (auto parent_id : parent_ids) {
new_node.parent_ids.insert(parent_id);
}
try {
for (auto parent_it : parents_its) {
auto [it, added] = parent_it->second.children_virus_ptrs.insert(new_node.virus);
edges_to_parents.push_back(it);
}
graph.insert({id, new_node});
} catch (std::exception &e) {
for (size_t i = 0; i < edges_to_parents.size(); ++i) {
parents_its[i]->second.children_virus_ptrs.erase(edges_to_parents[i]);
}
throw;
}
}
// Tworzy węzeł reprezentujący nowy wirus o identyfikatorze id
// powstały z wirusa o podanym identyfikatorze parent_id.
// Zgłasza wyjątek VirusAlreadyCreated, jeśli wirus o identyfikatorze
// id już istnieje.
// Zgłasza wyjątek VirusNotFound, jeśli któryś z wyspecyfikowanych
// poprzedników nie istnieje.
void create(typename Virus::id_type const &id, typename Virus::id_type const &parent_id) {
create(id, std::vector<typename Virus::id_type>(1, parent_id));
}
// Dodaje nową krawędź w grafie genealogii.
// Zgłasza wyjątek VirusNotFound, jeśli któryś z podanych wirusów nie istnieje.
void connect(typename Virus::id_type const &child_id,
typename Virus::id_type const &parent_id) {
auto child_it = graph.find(child_id);
auto parent_it = graph.find(parent_id);
if (child_it == graph.end() || parent_it == graph.end()) {
throw VirusNotFound();
}
if (!child_it->second.parent_ids.insert(parent_id).second) {
return;
}
try {
parent_it->second.children_virus_ptrs.insert(child_it->second.virus);
} catch (std::exception &e) {
child_it->second.parent_ids.erase(parent_id);
throw;
}
}
// Usuwa wirus o podanym identyfikatorze.
// Zgłasza wyjątek VirusNotFound, jeśli żądany wirus nie istnieje.
// Zgłasza wyjątek TriedToRemoveStemVirus przy próbie usunięcia
// wirusa macierzystego.
void remove(typename Virus::id_type const &id) {
if (id == stem_id) {
throw TriedToRemoveStemVirus();
}
auto it = graph.find(id);
if (it == graph.end()) {
throw VirusNotFound();
}
std::vector<parent_id_iterator> parent_its;
for (auto parent_id : it->second.parent_ids) {
parent_its.push_back(graph.find(parent_id));
}
for (auto child : it->second.children_virus_ptrs) {
auto child_node = graph.find(child->get_id());
child_node->second.parent_ids.erase(id);
if (child_node->second.parent_ids.empty()) {
try {
remove(child_node->first);
} catch (std::exception &e) {
child_node->second.parent_ids.insert(id);
throw;
}
}
}
for (auto parent_it : parent_its) {
parent_it->second.children_virus_ptrs.erase(it->second.virus);
}
graph.erase(id);
}
private:
class Node {
public:
std::shared_ptr<Virus> virus;
std::set<typename Virus::id_type> parent_ids;
std::set<std::shared_ptr<Virus>> children_virus_ptrs;
explicit Node(typename Virus::id_type const &virus_id) {
virus = std::make_shared<Virus>(virus_id);
}
};
typename Virus::id_type const stem_id;
std::map<typename Virus::id_type, Node> graph{};
using parent_id_iterator = typename std::map<typename Virus::id_type, Node>::iterator;
};
#endif // VIRUS_GENEALOGY_H