-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSegmentedDeque.h
345 lines (270 loc) · 9.67 KB
/
SegmentedDeque.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
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
#include <stdexcept>
#include "Sequence.h"
#include "Exeptions.h"
#include "Exeptions+.h"
#include "MutableSequence.h"
#include "DynamicArray.h"
#include "LinkedList.h"
template <typename T>
class SegmentedDeque {
private:
int size = 0; //äëèíà âñåãî äåêà (êîëè÷åñòâî ýëåìåíòîâ)
int buff_size = 0; //äëèíà ñåãìåíòà
DynamicArray<T*>* arr_ptr = nullptr; // ìàññèâ óêàçàòåëåé
int offset_tail = 0; // ñìåùåíèå â ñàìîì ïðàâîì ñåãìåíòå
int offset_head = 0; // ñìåùåíèå â ñàìîì ëåâîì ñåãìåíòå
private:
void assertSizeCorrect() {
if (size == 0) throw EmptyExeption;
}
void assertIndexCorrect(const int index) {
if (index < 0 || index >= size) throw IndexOutOfRange;
}
//private methods
void increaseFromHead() { // äîáàâëÿåì ñåãìåíò ñïðàâà
arr_ptr->append(new T[buff_size]);
}
void increaseFromTail() { // äîáàâëÿåì ñåãìåíò ñëåâà
arr_ptr->prepend(new T[buff_size]);
}
void decreaseFromHead() { //óäàëÿåì ñåãìåíò ñïðàâà
arr_ptr->resize(arr_ptr->getSize() - 1);
}
void decreaseFromTail() { // óäàëÿåì ñåãìåíò ñëåâà
new_size = arr_ptr->getSize() - 1;
DynamicArray<T*>* temp_arr = new DynamicArray<T*>(new_size);
for (int i = 0; i < new_size; i++) {
temp_arr->set(i, (*arr_ptr)[i + 1]);
}
delete arr_ptr;
arr_ptr = temp_arr;
}
T& getElement(int index) const {
assertIndexCorrect(index);
int segment_ind = index / buff_size;
index %= buff_size;
if (offset_tail == 0) return (*arr_ptr)[segment_ind][index];
if (offset_tail < index + 1) return (*arr_ptr)[segment_ind + 1][index - offset_tail];
return (*arr_ptr)[segment_ind][(buff_size - offset_tail + index) % buff_size];
}
void deleteSegDeque() {
if (size == 0) return;
for (int i = 0; i < arr_ptr->getSize(); i++) {
delete[](*arr_ptr)[i];
}
delete arr_ptr;
}
public:
//constructors
SegmentedDeque(int buff_size = 4) : buff_size(buff_size) {};
SegmentedDeque(const T& arr, int size, int buff_size = 4) : size(0), buff_size(buff_size) {
if (size > 0):
for (int i = 0; i < size; i++) {
append(arr[i]);
}
};
SegmentedDeque(const LinkedList<T>& other, int buff_size = 4) : buff_size(buff_size) {
for (int i = 0; i < other.getLength(); ++i) {
append(other.get(i));
}
}
SegmentedDeque(const DynamicArray<T>& other, int buff_size = 4) : buff_size(buff_size) {
for (int i = 0; i < other.getSize(); ++i) {
append(other.get(i));
}
}
SegmentedDeque(const SegmentedDeque<T>& other, int buff_size = 4) : buff_size(buff_size) {
for (int i = 0; i < other.getLength(); ++i) {
append(other.get(i));
}
}
SegmentedDeque(const Sequence<T>& other, int buff_size = 4) : buff_size(buff_size) {
for (int i = 0; i < other.getLength(); ++i) {
append(other.get(i));
}
}
//SegmentedDeque(T* array, int size, int bufSize = 4) : bufSize(bufSize) {
// for (int i = 0; i < size; ++i) {
// append(array[i]);
// }
//}
//destructor
~SegmentedDeque() {
deleteSegDeque();
}
//overriden operators
T& operator[](int index) { // äëÿ ïîñëåäîâàòåëüíîãî îáðàùåíèÿ ïî èíäåêñó
return getElement(index);
}
const T& operator[](int index) const { // ñîîòâåòâóåò îñòàëüíàì êîíñòàíòíûì îïåðàòîðàì [] (äëÿ íàñëåäîâàíèÿ)
return getElement(index);
}
SegmentedDeque<T>& operator=(const SegmentedDeque<T>& other) {
deleteSegDeque();
size = 0;
offset_tail = 0;
offset_head = 0;
buff_size = other.buff_size;
for (int i = 0; i < other.getLength(); i++) {
append(other.get(i));
}
return *this;
}
//methods
int getLength() const {
return size;
}
const T& getFirst() const {
assertSizeCorrect();
return (*this)[0];
}
const T& getLast() const {
assertSizeCorrect();
return (*this)[size - 1];
}
const T& get(int index) const {
assertIndexCorrect(index);
return (*this)[index];
}
void set(int index, const T& item) {
(*this)[index] = item;
}
void append(const T& item) {
if (size == 0) arr_ptr = new DynamicArray<T*>(0);
if (offset_head == 0) { // äîáàâëÿåì ñåãìåíò â êîíåö
increaseFromHead();
(*arr_ptr)[arr_ptr->getSize() - 1][offset_head] = item;
offset_head = 1;
}
else { // íå äîáàâëÿåì ñåãìåíò
(*arr_ptr)[arr_ptr->getSize() - 1][offset_head] = item;
offset_head = (offset_head + 1) % buff_size;
}
size++;
size++;
}
void prepend(const T& item) {
if (size == 0) arr_ptr = new DynamicArray<T*>(0);
if (offset_tail == 0) { // äîáàâëÿåì ñåãìåíò â íà÷àëî
increaseFromTail();
(*arr_ptr)[0][buff_size - 1] = item; // çàïîëíÿåì ýëåìåíòû ñ êîíöà ñåãìåíòà
offset_tail++;
}
else { // íå äîáàâëÿåì ñåãìåíò
(*arr_ptr)[0][buff_size - offset_tail - 1] = item;// çàïîëíÿåì ýëåìåíòû ñ êîíöà ñåãìåíòà
offset_tail = (offset_tail + 1) % buff_size;
}
size++;
}
void insertAt(int index, const T& item) {
assertIndexCorrect(index);
if (size == 0) arr_ptr = new DynamicArray<T*>(0);
if (index == size) {
append(item);
return;
}
prepend(0); // äîáàâëÿåì ýëåìåíò âíà÷àëå (item)
for (int i = 0; i < index; i++) {
(*this)[i] = (*this)[i + 1];
}
(*this)[index] = item;
}
void popHead() {
assertSizeCorrect();
if (offset_head == 1) { // óäàëÿåì ïîñëåäíèé ñåãìåíò
decreaseFromHead();
offset_head = 0;
}
else { // íå óäàëÿåì ïîñëåäíèé ñåãìåíò
if (offset_head == 0) {
offset_head = buff_size;
}
--offset_head;
}
--size;
}
void popTail() {
assertSizeCorrect();
if (offset_tail == 1) { // óäàëÿåì ïåðâûé ñåãìåíò
decreaseFromTail();
offset_tail = 0;
}
else { // íå óäàëÿåì ñåãìåíò
if (offset_tail == 0) {
offset_tail = buff_size;
}
--offset_tail;
}
--size;
}
void removeAt(int index) {
assertIndexCorrect(index);
for (int i = index; i < (size - 1); i++) {
(*this)[i] = (*this)[i + 1];
}
popHead();
}
int getBuffSize() const {
return buff_size;
}
int size()
if tail > head
return n - head + tail
else
return tail - head
function pushBack(x : T) :
if (head == (tail + 1) % n)
T newDeque[n * 2]
for i = 0 to n - 2
newDeque[i] = d[head]
head = (head + 1) % n
d = newDeque
head = 0
tail = n - 1
n *= 2
d[tail] = x
tail = (tail + 1) % n
T popBack() :
if (empty())
return error "underflow"
if (size() < n / 4)
T newDeque[n / 2]
int dequeSize = size()
for i = 0 to dequeSize - 1
newDeque[i] = d[head]
head = (head + 1) % n
d = newDeque
head = 0
tail = dequeSize
n /= 2
tail = (tail - 1 + n) % n
return d[tail]
function pushFront(x : T) :
if (head == (tail + 1) % n)
T newDeque[n * 2]
for i = 0 to n - 2
newDeque[i] = d[head]
head = (head + 1) % n
d = newDeque
head = 0
tail = n - 1
n *= 2
head = (head - 1 + n) % n
d[head] = x
T popFront() :
if (empty())
return error "underflow"
if (size() < n / 4)
T newDeque[n / 2]
int dequeSize = size()
for i = 0 to dequeSize - 1
newDeque[i] = d[head]
head = (head + 1) % n
d = newDeque
head = 0
tail = dequeSize
n /= 2
T ret = d[head]
head = (head + 1) % n
return ret
};