forked from NoaAmsalem/ex3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.h
More file actions
252 lines (223 loc) · 6.35 KB
/
Copy pathQueue.h
File metadata and controls
252 lines (223 loc) · 6.35 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
#ifndef EX3_QUEUE_H
#define EX3_QUEUE_H
#include <iostream>
const int DEFAULT_SIZE = 10;
const int INITIAL_INDEX = 0;
template<class T>
class Queue{
public:
class Iterator;
class ConstIterator;
Iterator begin() {
return Iterator(this, 0);
}
Iterator end() {
return Iterator(this, size());
}
ConstIterator begin() const{
return ConstIterator(this, 0);
}
ConstIterator end() const{
return ConstIterator(this, size());
}
class EmptyQueue{};
//C'tor of Queue
Queue():m_maxSize(DEFAULT_SIZE),
m_data(new T [DEFAULT_SIZE]),
m_firstIndex(INITIAL_INDEX),
m_nextIndex(INITIAL_INDEX)
{}
//Copy C'tor of Queue
Queue(const Queue& queue): m_maxSize(queue.m_maxSize),
m_data(new T [queue.m_maxSize]),
m_firstIndex(INITIAL_INDEX),
m_nextIndex(queue.size())
{
for(int i = queue.m_firstIndex; i<queue.m_nextIndex; i++){
m_data[i - queue.m_firstIndex] = queue.m_data[i];
}
}
//D'tor of Queue
~Queue(){
delete[] m_data;
}
Queue<T>& operator=(const Queue<T>& queue){
if(this == &queue){
return *this;
}
m_maxSize = queue.m_maxSize;
T* newData = new T[m_maxSize];
delete[] m_data;
m_data = newData;
for(int i = queue.m_firstIndex; i < queue.m_nextIndex; i++){
m_data[i - queue.m_firstIndex] = queue.m_data[i];
}
m_firstIndex = INITIAL_INDEX;
m_nextIndex = queue.size();
return *this;
}
/* if the nextIndex is out the array, update the array or create a new one
according to the requirement
*/
void expandTheQueue(){
if (this->size() < m_maxSize - (DEFAULT_SIZE/2)){
if (m_firstIndex > INITIAL_INDEX){
for(int i = m_firstIndex; i<m_nextIndex; i++){
this->m_data[i - m_firstIndex] = this->m_data[i];
}
}
}
else{
T* newData = new T [m_maxSize + DEFAULT_SIZE];
for(int i = m_firstIndex; i<m_nextIndex; i++){
newData[i - m_firstIndex] = this->m_data[i];
}
T* dataToDelete = m_data;
m_data = newData;
delete [] dataToDelete;
m_maxSize += DEFAULT_SIZE;
}
m_nextIndex = this->size();
m_firstIndex = INITIAL_INDEX;
}
// copy the data into a smaller array
void shrinkTheQueue(){
T* newData = new T [m_maxSize - DEFAULT_SIZE];
for(int i = m_firstIndex; i<m_nextIndex; i++){
newData[i - m_firstIndex] = m_data[i];
}
T* dataToDelete = m_data;
m_data = newData;
delete [] dataToDelete;
m_nextIndex = this->size();
m_firstIndex = INITIAL_INDEX;
}
//insert a new element into the data
void pushBack(const T& toPush){
if(m_nextIndex == m_maxSize){
expandTheQueue();
}
m_data[m_nextIndex] = toPush;
m_nextIndex++;
}
//return the first element of the data
T& front() const{
if (m_firstIndex==m_nextIndex){
throw EmptyQueue();
}
return m_data[m_firstIndex];
}
//remove the first element of the data
void popFront(){
if (m_firstIndex==m_nextIndex){
throw EmptyQueue();
}
m_firstIndex++;
if (this->size() < m_maxSize - DEFAULT_SIZE){
shrinkTheQueue();
}
}
//return the actual size of the data
int size() const{
return m_nextIndex - m_firstIndex;
}
private:
int m_maxSize;
T* m_data;
int m_firstIndex;
int m_nextIndex;
};
//create a new queue that include the filtered elements of the actual queue
template <class T , class F>
Queue<T> filter(const Queue<T>& queue, F fonction){
Queue<T> newQueue;
Queue<T> tmpQueue = queue;
for(int i = 0; i < queue.size(); i++){
if(fonction(tmpQueue.front())){
newQueue.pushBack(tmpQueue.front());
}
tmpQueue.popFront();
}
return newQueue;
}
//create a new queue with all the elements of the actual queue after transformation
template <class T , class F>
void transform(Queue<T>& queue, F fonction){
for(int i = 0; i < queue.size(); i++){
T actualElement = queue.front();
fonction (actualElement);
queue.popFront();
queue.pushBack(actualElement);
}
}
template <class T>
class Queue<T>::Iterator{
const Queue<T> *m_queue;
int m_index;
Iterator(const Queue<T> *queue, int index ):m_queue(queue),
m_index(index)
{}
friend class Queue<T>;
public:
class InvalidOperation {} ;
T& operator*() const{
return m_queue->m_data[m_index + m_queue->m_firstIndex];
}
Iterator& operator++(){
if (m_index == m_queue->size()){
throw InvalidOperation();
}
++m_index;
return *this;
}
Iterator& operator++(int){
if (m_index == m_queue->size()){
throw InvalidOperation();
}
Iterator result = *this;
++(*this);
return result;
}
bool operator==(const Iterator& i) const {
return m_index == i.m_index;
}
bool operator!=(const Iterator& i) const {
return !(*this == i);
}
};
template <class T>
class Queue<T>::ConstIterator{
const Queue<T> *m_queue;
int m_index;
ConstIterator(const Queue<T> *queue, int index ):m_queue(queue),
m_index(index)
{}
friend class Queue<T>;
public:
class InvalidOperation{};
const T& operator*() const{
return m_queue->m_data[m_index + m_queue->m_firstIndex];
}
ConstIterator& operator++(){
if (m_index == m_queue->size()){
throw InvalidOperation();
}
++m_index;
return *this;
}
ConstIterator& operator++(int){
if (m_index == m_queue->size()){
throw InvalidOperation();
}
Iterator result = *this;
++(*this);
return result;
}
bool operator==(const ConstIterator& i) const {
return m_index == i.m_index;
}
bool operator!=(const ConstIterator& i) const {
return !(*this == i);
}
};
#endif //EX3_QUEUE_H