-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSet.cpp
328 lines (298 loc) · 9.18 KB
/
CSet.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
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
/*
* File: CSet.cpp
* Brief description: CSet class implementation
* Details: File contain CSet class container implementation.
* Author: Martin Bezecny
*/
#include "CSet.h"
// Internal functions
void CSet::Copy(const CSet& aVal) { //Function for copying sets
CEntity* temp1 = aVal.iFirst;
if (aVal.iFirst) {
iFirst = new CEntity(*aVal.iFirst);
temp1 = dynamic_cast<CEntity*>(aVal.iFirst->NextItem());
}
CEntity* temp2 = iFirst;
while (temp1) {
temp2->SetNextItem(new CEntity(*temp1));
temp2 = dynamic_cast<CEntity*>(temp2->NextItem());
temp1 = dynamic_cast<CEntity*>(temp1->NextItem());
}
iSize = aVal.iSize;
}
void CSet::Destroy() { //function for deallocating sets
CEntity* temp = iFirst, * next;
iFirst = nullptr;
while (temp) {
next = dynamic_cast<CEntity*>(temp->NextItem());
temp->SetNextItem(nullptr);
delete temp;
temp = next;
--iSize;
}
}
//C'tors
CSet::CSet(const char* aStr) : iFirst(nullptr), iSize(0) {
std::istringstream iss(aStr, std::istringstream::in);
iss >> *this;
}
CSet::CSet(size_t aSize) : iFirst(nullptr), iSize(0) {
for (size_t i = 0; i < aSize; ++i) {
this->add(CEntity(CEntity::TestValueRandom()));
++iSize;
}
}
CSet::CSet(CEntity* aVal, size_t aSize) : iFirst(nullptr), iSize(0) {
size_t i = 0;
size_t living = ClassInfo<CEntity>::Living();
while (i < aSize - 1) {
if (aVal->ID() > ClassInfo<CEntity>::Total()) throw std::runtime_error("Given size is larger than array size!");
this->add(aVal[i]);
++i;
}
}
//Operators
CSet& CSet::operator=(const CSet& aVal) {
if (this->DeepCompare(aVal)) return *this;
Destroy();
Copy(aVal);
return *this;
}
CSet& CSet::operator-() {
CEntity* temp = iFirst;
while (temp) {
CEntity inv_val = CEntity(-*temp);
temp->SetValue(inv_val.Value());
temp = dynamic_cast<CEntity*>(temp->NextItem());
}
return *this;
}
CSet CSet::operator -(const CSet& aVal) const {
if (this->iFirst == nullptr || aVal.iFirst == nullptr) return *this;
CSet difference = CSet(*this);
CEntity* temp = aVal.iFirst;
while (temp) {
if (difference.is_element_of(*temp)) difference.erase(*temp);
temp = dynamic_cast<CEntity*>(temp->NextItem());
}
return difference;
}
CSet& CSet::operator +=(const CSet& aVal) {
if (aVal.iFirst == nullptr) return *this;
CEntity* temp = aVal.iFirst;
while (temp) {
if (!this->is_element_of(*temp)) this->add(*temp);
temp = dynamic_cast<CEntity*>(temp->NextItem());
}
return *this;
}
CSet CSet::operator+(const CSet& aVal) const {
if (aVal.iFirst == nullptr) return *this;
if (this->iFirst == nullptr) return aVal;
CSet sum = CSet(*this);
CEntity* temp = aVal.iFirst;
while (temp) {
if (!sum.is_element_of(*temp)) sum.add(*temp);
temp = dynamic_cast<CEntity*>(temp->NextItem());
}
return sum;
}
CSet operator + (const CSet& aSet, const CEntity& aVal) {
CSet emp = CSet(aSet);
emp.add(aVal);
return emp;
}
std::istream& operator >>(std::istream& aIStream, CSet& aValue) {
char ch = '\0';
std::string temp_str;
CEntity temp_ent;
if (!aIStream.good())
throw std::runtime_error("Input stream data integrity error!");
while (!aIStream.eof()) {
aIStream >> std::noskipws >> ch;
if (ch == '[' && ch != ',') {
aIStream >> ch;
while (ch != ']') {
if (ch != ';') {
temp_str.push_back(ch);
}
else {
temp_str.push_back(' ');
}
aIStream >> ch;
}
temp_ent = CEntity(temp_str);
if (aValue.iFirst == nullptr) aValue = CSet(temp_ent);
else aValue.add(temp_ent);
}
temp_str = "";
}
return aIStream;
}
std::ostream& operator <<(std::ostream& aOStream, const CSet& aValue) {
if (aValue.iFirst == nullptr) {
aOStream << "";
return aOStream;
}
CEntity* temp = aValue.iFirst;
while (temp) {
aOStream << '[' << temp->Value() << ']';
if (temp->NextItem()) aOStream << ',';
temp = dynamic_cast<CEntity*>(temp->NextItem());
}
return aOStream;
}
//Methods
bool CSet::is_subset_of(const CSet& aVal) const {
if (iSize < aVal.iSize) return false;
if (iSize == aVal.iSize) {
if (this->DeepCompare(aVal)) return true;
return false;
}
CEntity* sub_current = aVal.iFirst;
while (sub_current) {
CEntity* this_current = iFirst;
bool is_member = false;
while (this_current) {
if (sub_current->Value() == this_current->Value()) {
is_member = true;
break;
}
this_current = dynamic_cast<CEntity*>(this_current->NextItem());
}
if (!is_member) return false;
sub_current = dynamic_cast<CEntity*>(sub_current->NextItem());
}
return true;
}
CSet CSet::intersection(const CSet& aVal) const {
if (this->iFirst == nullptr) return *this;
if (aVal.iFirst == nullptr) return aVal;
if (this->DeepCompare(aVal)) return *this;
CSet intersect = CSet();
CEntity* this_curr = iFirst;
while (this_curr) {
CEntity* aVal_curr = aVal.iFirst;
while (aVal_curr) {
if (this_curr->Value() == aVal_curr->Value()) {
intersect.add(*aVal_curr);
break;
}
aVal_curr = dynamic_cast<CEntity*>(aVal_curr->NextItem());
}
this_curr = dynamic_cast<CEntity*>(this_curr->NextItem());
}
return intersect;
}
bool CSet::are_same(const CSet& aVal) const {
return this->DeepCompare(aVal);
}
bool CSet::DeepCompare(const CSet& aVal) const {
if (aVal.iSize != this->iSize) return false;
CEntity* aVal_curr = aVal.iFirst;
while (aVal_curr) {
if (!this->is_element_of(*aVal_curr)) return false;
aVal_curr = dynamic_cast<CEntity*>(aVal_curr->NextItem());
}
return true;
}
CSet CSet::complement(const CSet& aVal) const {
if (aVal.is_empty()) return *this;
CSet comp = CSet(*this - aVal);
return comp;
}
CSet CSet::section_smaller(const CEntity& aVal) const {
if (iSize == 0) return *this;
CSet smaller;
CEntity* temp = iFirst;
while (temp) {
if (aVal.Value() > temp->Value()) smaller.add(*temp);
temp = dynamic_cast<CEntity*>(temp->NextItem());
}
return smaller;
}
CSet CSet::section_larger(const CEntity& aVal) const {
if (iSize == 0) return *this;
CSet larger;
CEntity* temp = iFirst;
while (temp) {
if (aVal.Value() < temp->Value()) larger.add(*temp);
temp = dynamic_cast<CEntity*>(temp->NextItem());
}
return larger;
}
void CSet::add(const CEntity& aVal) {
if (!this->is_element_of(aVal)) {
CEntity* temp_node = new CEntity(aVal);
if (iFirst == nullptr) {
iFirst = temp_node;
}
else {
CEntity* temp_first = iFirst;
while (temp_first->NextItem()) temp_first = dynamic_cast<CEntity*>(temp_first->NextItem());
temp_first->SetNextItem(temp_node);
temp_first = dynamic_cast<CEntity*>(temp_first->NextItem());
}
iSize++;
}
}
void CSet::erase(const CEntity& aVal) {
if (this->is_element_of(aVal)) {
CEntity* temp = iFirst;
CEntity* prev = temp;
while (temp->Value() != aVal.Value()) {
prev = temp;
temp = dynamic_cast<CEntity*>(temp->NextItem());
}
if (temp == iFirst) {
iFirst = dynamic_cast<CEntity*>(iFirst->NextItem());
}
else {
prev->SetNextItem(temp->NextItem());
}
temp->SetNextItem(nullptr);
delete(temp);
iSize--;
}
}
CSet& CSet::Reverse() {
CEntity* curr = iFirst;
CEntity* prev = nullptr, * next = nullptr;
while (curr != nullptr) {
next = dynamic_cast<CEntity*>(curr->NextItem());
curr->SetNextItem(prev);
prev = curr;
curr = next;
}
iFirst = prev;
return *this;
}
double CSet::usage() const {
size_t set_size = sizeof(*this);
size_t num = this->num_of_elements();
CEntity* list = new CEntity[num];
size_t list_size = sizeof(*list);
delete[] list;
double efect = ((double)set_size / (double)list_size) * 100;
return efect;
}
CSet Reverse(const CSet& aVal) {
CSet reversed = CSet(aVal);
return reversed.Reverse();
}
bool CSet::is_element_of(const CEntity& aVal) const {
CEntity* temp = iFirst;
while (temp) {
if (temp->Value() == aVal.Value()) return true;
temp = dynamic_cast<CEntity*>(temp->NextItem());
}
return false;
}
int CSet::Compare(const CSet& aVal) const {
if (iSize == aVal.iSize) return 0;
if (iSize < aVal.iSize) return -1;
return 1;
}
CEntity* CSet::first_elem() const {
return iFirst;
}