-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.h
More file actions
605 lines (469 loc) · 12 KB
/
vector.h
File metadata and controls
605 lines (469 loc) · 12 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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#pragma once
#include <cstddef>
#include <iostream>
template <typename T>
class vector {
public:
using value_type = T;
using reference = T&;
using const_reference = const T&;
using pointer = T*;
using const_pointer = const T*;
using iterator = pointer;
using const_iterator = const_pointer;
private:
pointer _data;
size_t _size, _capacity, _cur_index;
void copy(const vector& other);
public:
// O(1) nothrow
vector() noexcept;
// O(N) strong
vector(const vector& other);
// O(1) strong
vector(vector&& other);
// O(N) strong
vector& operator=(const vector& other);
// O(1) strong
vector& operator=(vector&& other);
// O(N) nothrow
~vector() noexcept;
// O(1) nothrow
reference operator[](size_t index);
// O(1) nothrow
const_reference operator[](size_t index) const;
// O(1) nothrow
pointer data() noexcept;
// O(1) nothrow
const_pointer data() const noexcept;
// O(1) nothrow
size_t size() const noexcept;
// O(1) nothrow
reference front();
// O(1) nothrow
const_reference front() const;
// O(1) nothrow
reference back();
// O(1) nothrow
const_reference back() const;
// O(1)* strong
void push_back(const T& value);
// O(1) nothrow
void pop_back();
// O(1) nothrow
bool empty() const noexcept;
// O(1) nothrow
size_t capacity() const noexcept;
// O(N) strong
void reserve(size_t new_capacity);
// // O(N) strong
void shrink_to_fit();
// O(N) nothrow
void clear() noexcept;
// // O(1) nothrow
// void swap(vector& other) noexcept;
// // O(1) nothrow
iterator begin() noexcept;
// // O(1) nothrow
iterator end() noexcept;
// // O(1) nothrow
const_iterator begin() const noexcept;
// // O(1) nothrow
const_iterator end() const noexcept;
// // O(N) strong
iterator insert(const_iterator pos, const T& value);
// // O(N) nothrow(swap)
iterator erase(const_iterator pos);
// // O(N) nothrow(swap)
iterator erase(const_iterator first, const_iterator last);
};
template<typename T>
vector<T>::vector() noexcept : _data{nullptr}, _size{0}, _capacity{0}, _cur_index{0} {
//printf("constructor vector() called\n");
}
template<typename T>
void vector<T>::copy(const vector& other) {
if (!other.empty())
{
size_t mem_size = other.size() * sizeof(T), copied = 0;
_data = static_cast<T*>(operator new(mem_size));
try {
for (size_t i = 0; i < other.size(); i++) {
new (_data + i) T(other[i]);
copied++;
}
} catch (...) {
for (size_t i = 0; i < copied; i++) {
_data[i].~T();
}
operator delete (_data);
_data = nullptr;
throw;
}
} else {
_data = nullptr;
}
_size = _capacity = other.size();
//printf("copy other. new size: %lu, new cap: %lu\n", _size, _capacity);
}
template<typename T>
vector<T>::vector(const vector &other) {
//printf("constructor vector(& other) called\n");
if (this == &other) {
return;
}
_data=nullptr; _size = _capacity = _cur_index = 0;
copy(other);
}
template<typename T>
vector<T>::vector(vector &&other) {
//printf("constructor vector(&& other) called\n");
_data = other._data;
_size = other._size;
_capacity = other._capacity;
other._size = 0;
other._capacity = 0;
other._data = nullptr;
}
// O(N) strong
template<typename T>
vector<T>& vector<T>::operator=(const vector<T>& other) {
//printf("copy assign called\n");
if (this != &other) {
if (!empty()) {
clear();
operator delete (_data);
_capacity = 0;
}
copy(other);
}
return *this;
}
// O(1) strong
template <typename T>
vector<T>& vector<T>::operator=(vector<T>&& other) {
// printf("move assign called\n");
if (this != &other) {
clear();
operator delete(_data);
_data = other._data;
_size = other._size;
_capacity = other._capacity;
other._size = 0;
other._capacity = 0;
other._data = nullptr;
}
return *this;
}
template <typename T>
vector<T>::~vector() noexcept {
if (_size > 0) {
for (size_t i = _size; i > 0; i--) {
_data[i-1].~T();
}
operator delete(_data);
}
}
// O(1) nothrow
template<typename T>
T& vector<T>::operator[](size_t index) {
//printf("operator[] called\n");
return _data[index];
}
// O(1) nothrow
template<typename T>
const T& vector<T>::operator[](size_t index) const {
//printf("const operator[] called\n");
return _data[index];
}
template<typename T>
T* vector<T>::data() noexcept {
return _data;
}
template<typename T>
const T* vector<T>::data() const noexcept {
return _data;
}
template<typename T>
size_t vector<T>::size() const noexcept {
return _size;
}
// O(1) nothrow
template<typename T>
T& vector<T>::front() {
return _data[0];
}
// O(1) nothrow
template<typename T>
const T& vector<T>::front() const {
return _data[0];
}
// O(1) nothrow
template<typename T>
T& vector<T>::back() {
return _data[_size-1];
}
// O(1) nothrow
template<typename T>
const T& vector<T>::back() const {
return _data[_size-1];
}
template<typename T>
void vector<T>::push_back(const T& value) {
//printf("push_back\n");
size_t first_alloc_size = 2;
size_t new_capacity = 0;
if (_capacity == 0) {
new_capacity = first_alloc_size;
} else if (_capacity < _size + 1) {
new_capacity = _capacity*2;
}
if (new_capacity > 0) {
//printf("start reserve %lu...\n", new_capacity);
T* new_data = static_cast<T*>(operator new(new_capacity * sizeof(T)));
// T* new_data = static_cast<T*>(malloc(new_capacity * sizeof(T)));
if (_capacity > 0) {
size_t copied = 0;
try
{
for (size_t i = 0; i < _size; i++) {
new (new_data + i) T(_data[i]);
copied++;
}
new (new_data + _size) T(value);
copied++;
}
catch(...)
{
if (copied > 0) {
for (size_t i = 0; i < copied; i++) {
new_data[i].~T();
}
}
operator delete(new_data);
throw;
}
for (size_t i = 0; i < _size; i++) {
_data[i].~T();
}
operator delete(_data);
// free(_data);
_size++;
} else {
new (new_data + _size) T(value);
_size++;
}
//printf("reserved %lu\n", new_capacity);
_capacity = new_capacity;
_data = new_data;
} else {
new (_data + _size) T(value);
_size++;
}
// new (_data + _size) T(value);
// _size++;
}
template<typename T>
void vector<T>::pop_back() {
if (_size > 0)
{
_data[_size-1].~T();
_size--;
}
}
template<typename T>
bool vector<T>::empty() const noexcept {
return _size == 0;
}
template<typename T>
size_t vector<T>::capacity() const noexcept {
return _capacity;
}
template<typename T>
void vector<T>::reserve(size_t new_capacity) {
// trying to reserve 0 or less than already reserved
if (new_capacity <= _capacity)
{
return;
}
//printf("start reserve %lu...\n", new_capacity);
T* new_data = static_cast<T*>(operator new(new_capacity * sizeof(T)));
if (_capacity > 0) {
size_t copied = 0;
try {
for (size_t i = 0; i < _size; i++) {
new (new_data + i) T(_data[i]);
copied++;
}
} catch (...) {
for (size_t i = 0; i < copied; i++) {
new_data[i].~T();
}
operator delete(new_data);
throw;
}
for (size_t i = 0; i < _size; i++) {
_data[i].~T();
}
operator delete(_data);
}
//printf("reserved %lu\n", new_capacity);
_capacity = new_capacity;
_data = new_data;
}
// O(N) strong
template<typename T>
void vector<T>::shrink_to_fit() {
if (_capacity > _size) {
//printf("start reserve %lu...\n", new_capacity);
T* new_data = static_cast<T*>(operator new(_size * sizeof(T)));
size_t copied = 0;
try {
for (size_t i = 0; i < _size; i++) {
new (new_data + i) T(_data[i]);
copied++;
}
} catch (...) {
for (size_t i = 0; i < copied; i++) {
new_data[i].~T();
}
operator delete(new_data);
throw;
}
for (size_t i = 0; i < _size; i++) {
_data[i].~T();
}
operator delete(_data);
//printf("reserved %lu\n", new_capacity);
_capacity = _size;
_data = new_data;
}
}
// O(N) nothrow
template <typename T>
void vector<T>::clear() noexcept {
for (size_t i = 0; i < _size; i++)
{
_data[i].~T();
}
_size = 0;
}
// O(1) nothrow
// void swap(vector& other) noexcept;
// O(1) nothrow
template <typename T>
T* vector<T>::begin() noexcept {
return _data;
}
template <typename T>
T* vector<T>::end() noexcept {
return _data + _size;
}
template <typename T>
const T* vector<T>::begin() const noexcept {
return _data;
}
template <typename T>
const T* vector<T>::end() const noexcept {
return _data + _size;
}
template <typename T>
T* vector<T>::insert(const T* pos, const T& value) {
if (empty()) {
push_back(value);
return begin();
}
size_t idx = pos - _data;
size_t new_capacity = 0;
if (_capacity < _size + 1) {
new_capacity = _capacity * 2;
}
if (new_capacity > 0) {
// reserve
T* new_data = static_cast<T*>(operator new(new_capacity * sizeof(T)));
size_t copied = 0;
try {
for (size_t i = 0; i < idx; i++) {
new (new_data + i) T(_data[i]);
copied++;
}
new (new_data + idx) T(value);
copied++;
for (size_t i = _size; i > idx; i--) {
new (new_data + i) T(_data[i-1]);
copied++;
}
} catch (...) {
for (size_t i = 0; i < copied; i++) {
new_data[i].~T();
}
operator delete(new_data);
throw;
}
for (size_t i = 0; i < _size; i++) {
_data[i].~T();
}
operator delete(_data);
//printf("reserved %lu\n", new_capacity);
_capacity = new_capacity;
_data = new_data;
// end reserve
} else {
new (_data + _size) T(value);
for (size_t i = _size; i > idx; i--) {
_data[i].~T();
new (_data + i) T(_data[i - 1]);
}
_data[idx].~T();
new (_data + idx) T(value);
}
_size++;
return _data + idx;
}
template <typename T>
T* vector<T>::erase(const T* pos) {
if (empty()) {
return nullptr;
}
size_t idx = pos - _data;
for (size_t i = idx; i < _size - 1; i++) {
_data[i] = _data[i+1];
}
_data[_size-1].~T();
_size--;
return _data + idx;
}
template <typename T>
T* vector<T>::erase(const T* first, const T* last) {
if (empty()) {
return nullptr;
}
/*
10 20 30 40 50 60 70 75 80
21 22 23 24 25 26 27 28 29
10 20 70 75 80
21 22 23 24 25
size = 30
first = 23
last = 26
EC (erase count) = last - first + 1 = 26-23+1 = 4
to = size - EC = 30 - 4 = 26
data[first] = data[first + EC]
23 27
24 28
25 29
for (i = to ... size):
~T()
*/
size_t first_i = first - _data;
size_t last_i = last - _data;
size_t ec = last_i - first_i, erase_to = _size - ec;
for (size_t i = first_i; i < erase_to; i++) {
_data[i] = _data[i + ec];
}
for (size_t i = erase_to; i < _size; i++) {
_data[i].~T();
}
_size -= ec;
return _data + first_i;
}