-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncryptorList.cpp
223 lines (189 loc) · 6.52 KB
/
EncryptorList.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
/**
* @file EncryptorList.cpp
* @author Gaál Botond
* @brief
*/
#include "EncryptorList.h"
/*A láncolt lista egy eleme. Mivel a láncolt lista ebben az esetben egy heterogén tároló,
egy Node-ban az adat base pointer. Természetesen ezen kívül van egy-egy mutató az
előző és a következő elemre.*/
EncryptorList::Node::Node(Encryptor* pEncr): pEncr(pEncr), next(nullptr), prev(nullptr) {}
EncryptorList::Node::~Node() {
delete pEncr; //az elemek dinamikusan foglalt területre mutató pointerek -> felszabadítjuk
}
EncryptorList::EncryptorList(): head(nullptr), tail(nullptr) {}
EncryptorList::EncryptorList(const EncryptorList& other): head(nullptr), tail(nullptr) {
for (iterator iter = other.begin(); iter != other.end(); iter++) {
/*Az iterátort ha dereferáljuk, megkapjuk a láncolt lista elemeiben tárolt adatot,
ami (mivel az heterogén tároló) egy Encryptor absztrakt típusra mutató pointer.
Tehát *iter az Encryptor pointere, és ezért az Encryptor.clone()-hoz az
(*iter)->clone() visz. A nyíl precedenciája erősebb, ezért kell a zárójel. */
append((*iter)->clone());
}
}
EncryptorList& EncryptorList::operator=(const EncryptorList& other) {
if (this != &other) {
/*kitisztítjuk a meglévő adatokat. Mivel itt törölve lesz az aktuális Node,
nem használhatjuk az egyszerű iterátort (iter++ hibát dobna)*/
Node* current = head;
while (current != nullptr) {
Node* next = current->next; // elmentjük, hova mutat
delete current; //kitöröljük
current = next; //és nézzük, ahova mutatott
}
head = tail = nullptr;
//copy over
for (iterator iter=other.begin(); iter != other.end(); iter++) {
append((*iter)->clone()); //*iter típusa Encryptor*, ezért ->
}
}
return *this;
}
EncryptorList::iterator::iterator(Node* current): current(current) {}
EncryptorList::iterator& EncryptorList::iterator::operator++() {
if (current != nullptr) {
current = current->next;
}
return *this;
}
EncryptorList::iterator& EncryptorList::iterator::operator--() {
if (current != nullptr) {
current = current->prev;
}
return *this;
}
EncryptorList::iterator EncryptorList::iterator::operator++(int) {
iterator copy = *this;
++(*this);
return copy;
}
EncryptorList::iterator EncryptorList::iterator::operator--(int) {
iterator copy = *this;
--(*this);
return copy;
}
Encryptor* EncryptorList::iterator::operator*() {
return current->pEncr;
}
Encryptor** EncryptorList::iterator::operator->() {
return &(current->pEncr);
}
bool EncryptorList::iterator::operator==(EncryptorList::iterator rhs) const {
return current == rhs.current;
}
bool EncryptorList::iterator::operator!=(EncryptorList::iterator rhs) const {
return current != rhs.current;
}
EncryptorList::iterator EncryptorList::begin() const {
return EncryptorList::iterator(head);
}
EncryptorList::iterator EncryptorList::end() const {
return EncryptorList::iterator(nullptr);
}
EncryptorList::iterator EncryptorList::rBegin() const {
return EncryptorList::iterator(tail);
}
EncryptorList::iterator EncryptorList::rEnd() const {
return EncryptorList::iterator(nullptr);
}
EncryptorList::~EncryptorList() {
/*kitisztítjuk a meglévő adatokat. Mivel itt törölve lesz az aktuális Node,
nem használhatjuk az egyszerű iterátort (iter++ hibát dobna)*/
Node* current = head;
while (current != nullptr) {
Node* next = current->next;
delete current;
current = next;
}
}
void EncryptorList::append(const Encryptor& encr) {
Node* newNode = new Node(encr.clone());
//ha eddig üres volt a lista, tail->next hibát dobna, máshogy kezeljük
if (head == nullptr) {
head = newNode; //head->prev és head->next is 0 (friss Node)
tail = newNode; //tail->prev és tail->next is 0 (friss Node)
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}
void EncryptorList::append(Encryptor* pEncr) {
Node* newNode = new Node(pEncr);
if (head == nullptr) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}
Encryptor* EncryptorList::clone() const {
EncryptorList* newEncryptorPtr = new EncryptorList();
//felépítjük elejétől végéig ugyanazokkal a titkosítókkal (dinamikusan foglaltak!)
for (iterator iter = begin(); iter != end(); ++iter) {
newEncryptorPtr->append((*iter)->clone()); //*iter: Encryptor*
}
return newEncryptorPtr;
}
Encryptor* EncryptorList::cloneInverse() const {
EncryptorList* newEncryptorPtr = new EncryptorList();
/*Ahhoz, hogy ez dekódoljon, a végéről kezdve kell az eredeti kódolók inverzeit
bele pakolni (ugyanúgy dinamikusan foglalva)*/
for (iterator iter = rBegin(); iter != rEnd(); --iter) {
newEncryptorPtr->append((*iter)->cloneInverse()); //*iter: Encryptor*, **iter: Encryptor (clone())
}
return newEncryptorPtr;
}
char EncryptorList::encode(char c) const {
char encoded = c;
for (iterator iter = begin(); iter!= end(); ++iter) {
encoded = (*iter)->encode(encoded);
}
return encoded;
}
char EncryptorList::decode(char c) const {
char decoded = c;
for (iterator iter = rBegin(); iter!= rEnd(); --iter) {
decoded = (*iter)->decode(decoded);
}
return decoded;
}
EncryptorList EncryptorList::operator-() const {
EncryptorList ret;
for (iterator iter = rBegin(); iter!= rEnd(); --iter) {
ret.append((*iter)->cloneInverse());
}
return ret; //ezeknél ki van használva a másoló konstruktor
}
EncryptorList operator+(const Encryptor& e1, const Encryptor& e2) {
EncryptorList ret;
ret.append(e1);
ret.append(e2);
return ret;
}
EncryptorList operator-(const Encryptor& e1, const Encryptor& e2) {
EncryptorList ret;
ret.append(e1);
ret.append(e2.cloneInverse());
return ret;
}
EncryptorList& EncryptorList::operator+=(const Encryptor& rhs) {
append(rhs);
return *this;
}
EncryptorList& EncryptorList::operator-=(const Encryptor& rhs) {
append(rhs.cloneInverse());
return *this;
}
EncryptorList& EncryptorList::operator+=(Encryptor* rhs) {
append(rhs);
return *this;
}
EncryptorList& EncryptorList::operator-=(Encryptor* rhs) {
append(rhs->cloneInverse());
delete rhs;
return *this;
}
#include "memtrace.h"