-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSets.cpp
More file actions
383 lines (314 loc) · 7.5 KB
/
Copy pathSets.cpp
File metadata and controls
383 lines (314 loc) · 7.5 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include "Sets.h"
#include "Logger.h"
#include <iostream>
#include <bitset>
// === ARRAY ===
ArraySet::ArraySet()
{
array[0] = '\0';
};
ArraySet::ArraySet(const char* str)
{
int i {0};
for (; i < SET_POWER && str[i] != '\0'; ++i) {
array[i] = str[i];
}
array[i] = '\0';
};
ArraySet ArraySet::operator & (const ArraySet& another) const
{
Logger::instance()->log("Crossing sets: " + to_str() + " and " + another.to_str());
ArraySet new_set {};
for (int i = 0; array[i] != '\0'; i++)
for (int j = 0; another.array[j] != '\0'; j++)
if (array[i] == another.array[j]) {
new_set.add(array[i]);
}
return new_set;
};
ArraySet ArraySet::operator | (const ArraySet& another) const
{
Logger::instance()->log("Uniting sets: " + to_str() + " and " + another.to_str());
ArraySet new_set {};
for (int i = 0; i < array[i] != '\0'; i++) {
int j = 0;
for (; another.array[j] != '\0'; j++)
if (array[i] == another.array[j]) {
break;
}
if (another.array[j] == '\0') {
new_set.add(array[i]);
}
}
for (int i = 0; another.array[i] != '\0'; ++i) {
new_set.add(another.array[i]);
}
return new_set;
};
bool ArraySet::operator==(const ArraySet& another) const
{
for (int i = 0;
array[i] != '\0' && another.array[i] != '\0';
i++
) {
if (array[i] != another.array[i]) {
return false;
}
}
return true;
}
bool ArraySet::operator==(const char* s) const
{
ArraySet str_set {s};
return *this == str_set;
}
void ArraySet::add(char element)
{
for (int i = 0; i < SET_POWER; ++i) {
if (array[i] == element)
return;
else if (array[i] == '\0') {
array[i] = element;
array[i+1] = '\0';
return;
}
}
}
void ArraySet::remove(char element)
{
}
void ArraySet::show()
{
std::cout << array << std::endl;
}
std::string ArraySet::to_str() const
{
return std::string{array};
}
// === LIST ===
ListSet::ListSet() : head {nullptr}, tail {nullptr} {};
ListSet::ListSet(const char* str) : head(nullptr), tail(nullptr)
{
for (int i = 0; str[i] != '\0'; i++) {
add(str[i]);
}
};
ListSet::ListSet(const ListSet& another)
{
head = nullptr;
tail = nullptr;
for (auto i = another.head; i != nullptr; i = i->next) {
add(i->data);
}
};
ListSet::ListSet(ListSet && another) : head {another.head}, tail {another.tail} {
another.head = nullptr;
another.tail = nullptr;
};
ListSet& ListSet::operator=(const ListSet& another)
{
if (this != &another) {
head = nullptr;
tail = nullptr;
for (auto i = another.head; i != nullptr; i = i->next) {
add(i->data);
}
}
return *this;
};
ListSet& ListSet::operator=(ListSet && another)
{
if (this != &another) {
head = another.head;
tail = another.tail;
another.head = nullptr;
another.tail = nullptr;
}
return *this;
};
ListSet::~ListSet()
{
while (head) {
auto fordel = head;
head = head->next;
delete fordel;
};
};
ListSet ListSet::operator&(const ListSet& another) const
{
Logger::instance()->log("Crossing sets: " + to_str() + " and " + another.to_str());
ListSet new_set {};
for (auto cur = head; cur; cur = cur->next) {
for (auto cur2 = another.head; cur2; cur2 = cur2->next)
if (cur->data == cur2->data) {
new_set.add(cur->data);
}
}
return new_set;
};
ListSet ListSet::operator|(const ListSet& another) const
{
Logger::instance()->log("Uniting sets: " + to_str() + " and " + another.to_str());
ListSet new_set {};
for (auto cur = head; cur; cur = cur->next) {
auto cur2 = another.head;
for (; cur2; cur2 = cur2->next)
if (cur->data == cur2->data) {
break;
}
if (!cur2) {
new_set.add(cur->data);
}
}
for (auto cur2 = another.head; cur2; cur2 = cur2->next) {
new_set.add(cur2->data);
}
return new_set;
};
bool ListSet::operator==(const ListSet& another) const
{
for (auto i = head; i != nullptr; i = i->next) {
for (auto j = another.head; j != nullptr; j = j->next) {
if (i->data != j->data) {
return false;
}
}
}
return true;
};
bool ListSet::operator==(const char* s) const
{
ListSet str_set {s};
return *this == str_set;
};
void ListSet::add(char element)
{
if (!get_Node(element)) {
if (tail) {
tail->next = new Node {element};
tail = tail->next;
} else {
head = new Node {element};
tail = head;
}
}
};
void ListSet::remove(char element)
{
};
void ListSet::show()
{
for (auto i = head; i != nullptr; i = i->next) {
std::cout << i->data;
}
std::cout << std::endl;
}
ListSet::Node* ListSet::get_Node(char elem_data)
{
for (auto i = head; i != nullptr; i = i->next) {
if (elem_data == i->data) {
return i;
}
}
return nullptr;
};
std::string ListSet::to_str() const
{
std::string s{};
for (auto i = head; i != nullptr; i = i->next) {
s += i->data;
}
return s;
}
// === BITS ===
BitSet::BitSet() : bit_array {0x0} {};
BitSet::BitSet(int i) : bit_array {i} {};
BitSet::BitSet(const char* str)
{
bit_array = 0x0;
for (int i = 0; str[i] != '\0'; ++i) {
add(str[i]);
}
}
BitSet BitSet::operator&(const BitSet& another) const
{
Logger::instance()->log("Crossing sets: " + to_str() + " and " + another.to_str());
return bit_array & another.bit_array;
}
BitSet BitSet::operator|(const BitSet& another) const
{
Logger::instance()->log("Uniting sets: " + to_str() + " and " + another.to_str());
return bit_array | another.bit_array;
}
bool BitSet::operator==(const BitSet& another) const
{
return bit_array == another.bit_array;
}
bool BitSet::operator==(const char* s) const
{
BitSet str_set{s};
return *this == str_set;
}
void BitSet::add(char ch)
{
bit_array |= ctb(ch);
};
void BitSet::remove(char ch)
{
bit_array &= ~ctb(ch);
};
int BitSet::ctb(char ch)
{
int bytes = 0x0;
if (ch >= '0' && ch <= '9') {
bytes = 0x1 << (ch - '0');
} else if (ch >= 'A' && ch <= 'F') {
bytes = 0x400 << (ch - 'A');
} else if (ch >= 'a' && ch <= 'f') {
bytes = 0x400 << (ch - 'a');
}
return bytes;
}
std::string BitSet::bts(int bytes) const
{
std::string str {};
char cur_hex_digit {'0'};
while (cur_hex_digit != 'F' + 1) {
if (cur_hex_digit == '9' + 1) {
cur_hex_digit = 'A';
}
if (bytes & 0x1) {
str += cur_hex_digit;
}
cur_hex_digit++;
bytes >>= 1;
}
return str;
};
void BitSet::show()
{
std::cout << bts(bit_array) << std::endl;
}
void BitSet::showBits()
{
std::cout << std::bitset<32>(bit_array) << std::endl;
}
std::string BitSet::to_str() const
{
return std::string{bts(bit_array)};
}
std::ostream& operator << (std::ostream& os, const ArraySet& hs)
{
return os << hs.array;
}
std::ostream& operator << (std::ostream& os, const ListSet& hs)
{
for (auto i = hs.head; i != nullptr; i = i->next) {
os << i->data;
}
return os;
}
std::ostream& operator << (std::ostream& os, const BitSet& hs)
{
return os << hs.bts(hs.bit_array);
}