-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec.cpp
466 lines (383 loc) · 10.8 KB
/
vec.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#include <iostream>
#include <cstdlib>
#include <utility>
using namespace std;
class VectorError : public exception {
const char* msg;
public:
VectorError() : msg("") {}
explicit VectorError(const char *msg) : msg(msg) {}
const char *what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override {
return msg;
}
};
template<class T>
class Vector {
static const int CAPACITY_GROWTH_BATCH_SIZE = 30;
int capacity_;
int size_;
T *data_;
void resize() {
T *old_data_ = data_;
capacity_ += CAPACITY_GROWTH_BATCH_SIZE;
data_ = new T[capacity_];
copy(old_data_, old_data_ + size_, data_);
delete [] old_data_;
}
public:
explicit Vector(int capacity = 10) : capacity_(capacity),
size_(0),
data_(new T[capacity])
{}
~Vector() {
delete [] data_;
}
Vector(const Vector<T> &other) : size_(other.size_),
capacity_(other.capacity_),
data_(new T[other.capacity_]) {
copy(other.data_, other.data_ + other.size_, data_);
}
Vector(Vector<T> &&other) noexcept : capacity_(exchange(other.capacity_, 0)),
size_(exchange(other.size_, 0)),
data_(exchange(other.data_, nullptr))
{}
Vector<T> &operator=(const Vector<T> &other) {
if (this == addressof(other)) {
return *this;
}
delete [] data_;
capacity_ = other.capacity_;
size_ = other.size_;
data_ = new T[capacity_];
copy(other.data_, other.data_ + other.size_, data_);
return *this;
}
Vector<T> &operator=(Vector<T> &&other) noexcept {
if (this == addressof(other)) {
return *this;
}
delete [] data_;
capacity_ = exchange(other.capacity_, 0);
size_ = exchange(other.size_, 0);
data_ = exchange(other.data_, nullptr);
return *this;
}
bool empty() const {
return size_ == 0;
}
int size() const {
return size_;
}
T &operator[](int n) {
if (n > size_) {
throw VectorError("Vector<T>::operator[]: n is larger than size");
}
return data_[n];
}
const T &operator[](int n) const {
if (n > size_) {
throw VectorError("Vector<T>::operator[]: n is larger than size");
}
return data_[n];
}
void clear() {
if (!empty()) {
delete[] data_;
size_ = 0;
}
}
int capacity() const {
return capacity_;
}
T &front() {
return data_[0];
}
const T &front() const {
return data_[0];
}
T &back() {
return data_[size_ - 1];
}
const T &back() const {
return data_[size_ - 1];
}
void push_back(const T &val) {
if (size_ + 1 > capacity_) {
resize();
}
data_[size_++] = val;
}
void pop_back() {
if (empty()) {
throw VectorError("Vector<T>::pop_back: vector is empty");
}
data_[size_ - 1] = nullptr;
size_--;
}
class iterator {
T *elem_;
public:
explicit iterator(T *elem) : elem_(elem) {}
T &operator*() const {
return *elem_;
}
T *operator->() const {
return elem_;
}
iterator operator++() {
++elem_;
return *this;
}
iterator operator++(int) {
iterator tmp = *this;
elem_++;
return tmp;
}
iterator operator--(int) {
iterator tmp = *this;
elem_--;
return tmp;
}
iterator operator--() {
--elem_;
return *this;
}
iterator operator+(int x) {
iterator tmp = *this;
elem_ += x;
return tmp;
}
bool operator!=(const iterator &other) const {
return elem_ != other.elem_;
}
bool operator==(const iterator &other) const {
return elem_ == other.elem_;
}
};
iterator begin() const {
return iterator(data_);
}
iterator end() const {
return iterator(data_ + size_);
}
int where(iterator pos) const {
if (empty()) {
return -1;
}
iterator it = begin();
int i = 0;
while (i < size_ && it != pos) {
i++;
it++;
}
return i >= size_ ? -1 : i;
}
iterator insert(iterator pos, const T &x) {
if (size_ + 1 > capacity_) {
resize();
}
int idx_of_iter = where(pos);
T* tmp = new T[size_ - idx_of_iter];
copy(data_ + idx_of_iter, data_ + size_, tmp);
data_[idx_of_iter] = x;
size_++;
copy(tmp, tmp + (size_ - idx_of_iter), data_ + (idx_of_iter + 1));
delete [] tmp;
return iterator(data_ + (idx_of_iter + 1));
}
iterator erase(iterator pos) {
if (empty()) {
throw VectorError("Vector<T>::erase: vector is empty");
}
int idx_of_iter = where(pos);
if (idx_of_iter == -1) {
throw VectorError("Vector<T>::erase: invalid iterator");
}
data_[idx_of_iter] = NULL;
copy(data_ + (idx_of_iter + 1), data_ + size_, data_ + idx_of_iter);
size_--;
return iterator(data_ + idx_of_iter);
}
iterator erase(iterator first, iterator last) {
if (empty()) {
throw VectorError("Vector<T>::erase: vector is empty");
}
//something here is not right...
while (first != last) {
first = erase(first);
}
return iterator(last);
}
class const_iterator {
const T *elem_;
public:
explicit const_iterator(const T *elem) : elem_(elem) {}
const T &operator*() const {
return *elem_;
}
const T *operator->() const {
return elem_;
}
const_iterator operator++() {
++elem_;
return *this;
}
const_iterator operator++(int) {
const_iterator tmp = *this;
elem_++;
return tmp;
}
const_iterator operator--(int) {
const_iterator tmp = *this;
elem_--;
return tmp;
}
const_iterator operator--() {
--elem_;
return *this;
}
bool operator!=(const const_iterator &other) const {
return elem_ != other.elem_;
}
bool operator==(const const_iterator &other) const {
return elem_ == other.elem_;
}
};
const_iterator cbegin() const {
return const_iterator(data_);
}
const_iterator cend() const {
return const_iterator(data_ + size_);
}
class reverse_iterator {
T *elem_;
public:
explicit reverse_iterator(T *elem) : elem_(elem) {}
T &operator*() const {
return *elem_;
}
T *operator->() const {
return elem_;
}
reverse_iterator operator++() {
--elem_;
return *this;
}
reverse_iterator operator++(int) {
reverse_iterator tmp = *this;
elem_--;
return tmp;
}
reverse_iterator operator--(int) {
reverse_iterator tmp = *this;
elem_++;
return tmp;
}
reverse_iterator operator--() {
++elem_;
return *this;
}
bool operator!=(const reverse_iterator &other) const {
return elem_ != other.elem_;
}
bool operator==(const reverse_iterator &other) const {
return elem_ == other.elem_;
}
};
reverse_iterator rbegin() const {
return reverse_iterator(data_ + ((size_ > 0 ? size_ : 1) - 1));
}
reverse_iterator rend() const {
return reverse_iterator(data_ - 1);
}
class const_reverse_iterator {
T *elem_;
public:
explicit const_reverse_iterator(T *elem) : elem_(elem) {}
const T &operator*() const {
return *elem_;
}
const T *operator->() const {
return elem_;
}
const_reverse_iterator operator++() {
--elem_;
return *this;
}
const_reverse_iterator operator++(int) {
return const_reverse_iterator(elem_--);
}
const_reverse_iterator operator--(int) {
return const_reverse_iterator(elem_++);
}
const_reverse_iterator operator--() {
++elem_;
return *this;
}
bool operator!=(const const_reverse_iterator &other) const {
return elem_ != other.elem_;
}
bool operator==(const const_reverse_iterator &other) const {
return elem_ == other.elem_;
}
};
const_reverse_iterator crbegin() const {
return const_reverse_iterator(data_ + ((size_ > 0 ? size_ : 1) - 1));
}
const_reverse_iterator crend() const {
return const_reverse_iterator(data_ - 1);
}
};
template<class T>
ostream &operator<<(ostream &out, const Vector<T> &v) {
for (typename Vector<T>::const_iterator it = v.cbegin(); it != v.cend(); it++) {
out << *it << ",";
}
return out;
}
int main(int argc, char *argv[]) {
Vector<int> v1, v2;
for (int i = atoi(argv[1]); i < atoi(argv[2]); i++) {
v1.push_back(i);
}
for (int i = atoi(argv[3]); i < atoi(argv[4]); i++) {
v2.push_back(i);
}
cout << "v1: {" << v1 << '}' << endl;
cout << "v2: {" << v2 << '}' << endl;
bool eq = false;
for (auto el : v1) {
for (auto el2 : v2) {
if (el == el2) {
cout << "equal element in v1 and v2: " << el << endl;
eq = true;
}
}
}
if (!eq) {
cout << "no equal elements in v1 and v2" << endl;
}
v1.push_back(-100);
v2.push_back(-100);
cout << "v1: {" << v1 << '}' << endl;
cout << "v2: {" << v2 << '}' << endl;
Vector<int> v;
v = v2;
cout << "v: {" << v << '}' << endl;
for (Vector<int>::reverse_iterator it = v1.rbegin(); it != v1.rend(); it++) {
v.insert(v.begin(), *it);
}
cout << "-v: {" << v << '}' << endl;
v.erase(v.begin());
cout << "--v: {" << v << '}' << endl;
v.erase(--v.end());
cout << "---v: {" << v << '}' << endl;
for (Vector<int>::iterator it = v.begin(); it != v.end(); it++) {
if (*it == -100) {
v.erase(it, v.end());
break;
}
}
cout << "v: {" << v << '}' << endl;
return 0;
}