-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeque.hpp
More file actions
236 lines (191 loc) · 5.94 KB
/
Copy pathdeque.hpp
File metadata and controls
236 lines (191 loc) · 5.94 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
#ifndef DEQUE_HPP
#define DEQUE_HPP
#include "vector.hpp"
#include <iostream>
template <typename data_t>
class deque{
private:
vector<vector<data_t>> table;
int control_start;
int control_end;
int table_start;
int table_end;
int cap;
void resize_deque(){
vector<vector<data_t>> new_table;
int offset = table.size()/2;
for(int i=0;i<table.size()*2;i++){
vector<data_t> t(128);
new_table.push_back(t);
}
for(int i=0;i<table.size();i++){
new_table[i+offset] = std::move(table[i]);
}
table_start += offset;
table_end += offset;
table = std::move(new_table);
}
public:
deque() {
table_start = 3;
table_end = 3;
cap = 128;
control_start = -1;
control_end = -1;
for(int i=0;i<6;i++){
vector<data_t> u(cap);
table.push_back(u);
}
}
deque(const deque& other_deque){
table_start = other_deque.table_start;
table_end = other_deque.table_end;
table = other_deque.table;
control_start = other_deque.control_start;
control_end = other_deque.control_end;
}
deque(deque&& other_deque) noexcept {
table_start = other_deque.table_start;
table_end = other_deque.table_end;
table = std::move(other_deque.table);
control_start = std::move(other_deque.control_start);
control_end = std::move(other_deque.control_end);
}
deque& operator=(const deque& other_deque){
if(this != &other_deque){
table_start = other_deque.table_start;
table_end = other_deque.table_end;
table = other_deque.table;
control_start = other_deque.control_start;
control_end = other_deque.control_end;
}
return this;
}
deque& operator=(deque&& other_deque) noexcept {
if(this != &other_deque){
table_start = other_deque.table_start;
table_end = other_deque.table_end;
table = std::move(other_deque.table);
control_start = std::move(other_deque.control_start);
control_end = std::move(other_deque.control_end);
}
}
~deque() {}
void push_front(data_t& data){
// empty table, first insertion
if(control_start==-1){
control_start = 0;
control_end = 0;
table[table_start][control_start] = data;
}
// table starting slot starts at 0
else if(control_start==0){
table_start--;
control_start = cap-1;
table[table_start][control_start] = data;
}
// table starting slot is >=0
else {
control_start--;
table[table_start][control_start] = data;
}
if(table_start==0){
resize_deque();
}
}
void push_back(data_t& data){
// empty table slot. first insertion in table_end slot
if(control_end==-1){
control_end = 0;
control_start = 0;
table[table_end][control_end] = data;
}
// end table slot is full.
else if(control_end==cap-1){
table_end++;
control_end = 0;
table[table_end][control_end] = data;
}
// table end slot is < cap
else{
control_end++;
table[table_end][control_end] = data;
}
if(table_end==table.size()-1){
resize_deque();
}
}
data_t& front(){
if(control_start==-1){
throw std::runtime_error("Tried to access member 'front' of an empty deque : deque.hpp\n");
}
return table[table_start][control_start];
}
data_t& back(){
if(control_end==-1){
throw std::runtime_error("Tried to access member 'back' of and empty deque : deque.hpp\n");
}
return table[table_end][control_end];
}
void pop_front(){
if(control_start==-1){
throw std::runtime_error("Tried to access member 'pop_front' of an empty deque : deque.hpp\n");
}
table[table_start][control_start].~data_t();
control_start++;
if(control_start==cap){
control_start = 0;
table_start++;
vector<data_t> replace(128);
table[table_start-1] = std::move(replace);
}
if(table_start>table_end || (table_start==table_end && control_start>control_end)){
control_end = -1;
control_start = -1;
table_start = table.size()/2;
table_end = table.size()/2;
}
}
void pop_back(){
if(control_end==-1){
throw std::runtime_error("Tried to access member 'pop_back' of an empty deque : deque.hpp\n");
}
table[table_end][control_end].~data_t();
control_end--;
if(control_end==-1){
table_end--;
control_end = cap-1;
vector<data_t> replace(128);
table[table_end+1] = std::move(replace);
}
if(table_start>table_end || (table_start==table_end && control_end<control_start)){
control_start = -1;
control_end = -1;
table_start = table.size()/2;
table_end = table.size()/2;
}
}
bool empty(){
return control_start == -1;
}
int size(){
int slots_span = table_end - table_start + 1;
int size_ = slots_span * cap;
size_ -= control_start;
size_ -= cap - 1 - control_end;
return size_;
}
void clear(){
vector<vector<data_t>> new_table;
for(int i=0;i<6;i++){
vector<data_t> t;
new_table.push_back(t);
}
table = std::move(new_table);
table_start = 3;
table_end = 3;
control_start = -1;
control_end = -1;
}
};
#endif