forked from HibaMallak/ex3
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQueue.h
306 lines (252 loc) · 6.45 KB
/
Queue.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
297
298
299
300
301
302
303
304
305
306
#ifndef EX3_Queue_H
#define EX3_Queue_H
#include <assert.h>
#include <new>
#include <iostream>
template<class T>
class Queue {
public:
Queue();
Queue(const Queue<T>&);
~Queue();
Queue& operator=(const Queue<T>& other);
void pushBack(const T& element);
T& front();
const T& front() const;
void popFront();
int size() const;
class Iterator;
Iterator begin();
Iterator end();
class ConstIterator;
ConstIterator begin() const;
ConstIterator end() const;
class EmptyQueue {};
private:
void clear();
void copyNodes(const Queue& other);
class Node;
int m_size;
Node *m_head;
Node *m_last;
};
template<class T>
class Queue<T>::Node {
public:
Node(const T& val);
Node(const Node&) = default; // Maybe won't need it
~Node() = default;
Node& operator=(const Node& other) = default;
friend class Queue<T>;
private:
T m_val;
Node *m_next;
};
template<class T>
Queue<T>::Node::Node(const T& val): m_val(val), m_next(nullptr) {}
template<class T>
class Queue<T>::Iterator {
Queue<T>::Node *m_node;
Iterator(Queue<T>::Node *begin);
friend class Queue<T>;
public:
T& operator*() const;
Iterator& operator++();
bool operator!=(const Iterator& other) const;
Iterator(const Queue<T>::Iterator& other) = default;
Iterator& operator=(const Queue<T>::Iterator& other) = default;
~Iterator() = default;
class InvalidOperation {};
};
template<class T>
T& Queue<T>::Iterator::operator*() const {
if (this->m_node == nullptr) {
throw InvalidOperation();
}
return this->m_node->m_val;
}
template<class T>
typename Queue<T>::Iterator& Queue<T>::Iterator::operator++() {
if (this->m_node == nullptr) {
throw InvalidOperation();
}
this->m_node = this->m_node->m_next;
return *this;
}
template<class T>
bool Queue<T>::Iterator::operator!=(const Iterator& other) const {
return this->m_node != other.m_node;
}
template<class T>
Queue<T>::Iterator::Iterator(Queue<T>::Node *begin): m_node(begin) {}
template<class T>
class Queue<T>::ConstIterator {
Queue<T>::Node const *m_node;
ConstIterator(Queue<T>::Node const *begin);
friend class Queue<T>;
public:
const T& operator*() const;
ConstIterator& operator++();
bool operator!=(const ConstIterator& other) const;
ConstIterator(const Queue<T>::ConstIterator& other) = default;
ConstIterator& operator=(const Queue<T>::ConstIterator& other) = default;
~ConstIterator() = default;
class InvalidOperation {};
};
template<class T>
const T& Queue<T>::ConstIterator::operator*() const {
if (this->m_node == nullptr) {
throw InvalidOperation();
}
return this->m_node->m_val;
}
template<class T>
typename Queue<T>::ConstIterator& Queue<T>::ConstIterator::operator++() {
if (this->m_node == nullptr) {
throw InvalidOperation();
}
this->m_node = this->m_node->m_next;
return *this;
}
template<class T>
bool Queue<T>::ConstIterator::operator!=(const ConstIterator& other) const {
return this->m_node != other.m_node;
}
template<class T>
Queue<T>::ConstIterator::ConstIterator(Queue<T>::Node const *begin): m_node(begin) {}
template<class T>
Queue<T>::Queue(): m_size(0), m_head(nullptr), m_last(nullptr) {}
template<class T>
Queue<T>::Queue(const Queue<T>& other): m_size(0), m_head(nullptr), m_last(nullptr) {
try {
copyNodes(other);
}
catch(const std::bad_alloc& e) {
clear();
throw;
}
}
template<class T>
Queue<T>& Queue<T>::operator=(const Queue<T>& other) {
if (this == &other) {
return *this;
}
Queue<T> tempQ;
for (const T& elem : other) {
tempQ.pushBack(elem);
}
// Successfully copied new nodes
clear();
this->m_head = tempQ.m_head;
this->m_last = tempQ.m_last;
this->m_size = tempQ.m_size;
tempQ.m_head = nullptr;
tempQ.m_last = nullptr;
tempQ.m_size = 0;
return *this;
}
template<class T>
void Queue<T>::pushBack(const T& element) {
if (this->m_last == nullptr) {
this->m_head = new Queue<T>::Node(element);
this->m_last = this->m_head;
} else {
this->m_last->m_next = new Queue<T>::Node(element);
this->m_last = this->m_last->m_next;
}
this->m_size++;
}
template<class T>
T& Queue<T>::front() {
if (this->m_head == nullptr) {
throw EmptyQueue();
}
return this->m_head->m_val;
}
template<class T>
const T& Queue<T>::front() const{
if (this->m_head == nullptr) {
throw EmptyQueue();
}
return this->m_head->m_val;
}
template<class T>
void Queue<T>::popFront() {
if (this->m_head == nullptr) {
throw EmptyQueue();
}
Node *newHead = m_head->m_next;
delete this->m_head;
this->m_head = newHead;
this->m_size--;
if (this->m_size == 0) {
this->m_last = nullptr;
}
}
template<class T>
int Queue<T>::size() const {
return this->m_size;
}
template<class T>
typename Queue<T>::Iterator Queue<T>::begin() {
return Queue<T>::Iterator(this->m_head);
}
template<class T>
typename Queue<T>::Iterator Queue<T>::end() {
return Queue<T>::Iterator(nullptr);
}
template<class T>
typename Queue<T>::ConstIterator Queue<T>::begin() const {
return Queue<T>::ConstIterator(this->m_head);
}
template<class T>
typename Queue<T>::ConstIterator Queue<T>::end() const {
return Queue<T>::ConstIterator(nullptr);
}
template<class T>
void Queue<T>::clear() {
Node *next = nullptr;
while (nullptr != this->m_head) {
next = this->m_head->m_next;
delete this->m_head;
this->m_size--;
this->m_head = next;
}
// Finished clear, meaning now m_last points to a deleted node
this->m_last = nullptr;
}
template<class T>
void Queue<T>::copyNodes(const Queue& other) {
for (const T& elem : other) {
pushBack(elem);
}
}
template<class T>
Queue<T>::~Queue() {
clear();
}
template<class T, class PredictFunc>
Queue<T> filter(const Queue<T>& original, PredictFunc predicat) {
assert(predicat);
Queue<T> filtered;
if (!predicat) {
return filtered;
}
for (const T& elem : original) {
if (predicat(elem)) {
filtered.pushBack(elem);
}
}
return filtered;
}
template<class T, class ModifyFunc>
void transform(Queue<T>& queue, ModifyFunc modifier) {
assert(modifier);
if (!modifier) {
return;
}
for (T& elem : queue) {
modifier(elem);
}
}
#endif //EX3_Queue_H