-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.hpp
More file actions
708 lines (617 loc) · 17.7 KB
/
Copy pathvector.hpp
File metadata and controls
708 lines (617 loc) · 17.7 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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//
// Created by Freewings on 2025/5/7.
//
#ifndef VECTOR_HPP
#define VECTOR_HPP
#include <climits>
#include <cstddef>
#include <stdexcept>
#include <memory>
namespace sjtu {
class exception {
protected:
const std::string variant = "";
std::string detail = "";
public:
exception() {
}
exception(const exception &ec) : variant(ec.variant), detail(ec.detail) {
}
virtual std::string what() {
return variant + " " + detail;
}
};
class index_out_of_bound : public exception {
/* __________________________ */
};
class runtime_error : public exception {
/* __________________________ */
};
class invalid_iterator : public exception {
/* __________________________ */
};
class container_is_empty : public exception {
/* __________________________ */
};
/**
* a data container like std::vector
* store data in a successive memory and support random access.
*/
template<typename T>
class vector {
private:
T *data;
std::allocator<T> alloc;
using Traits = std::allocator_traits<std::allocator<T> >;
// _size - The total number of the vector saved.
// _capacity - The space it uses to store datas.
// if not empty , the _size is from 1 ~ n
size_t _size, _capacity;
public:
/**
* TODO
* a type for actions of the elements of a vector, and you should write
* a class named const_iterator with same interfaces.
*/
/**
* you can see RandomAccessIterator at CppReference for help.
*/
class const_iterator;
class reverse_iterator;
class iterator {
// The following code is written for the C++ type_traits library.
// Type traits is a C++ feature for describing certain properties of a type.
// For instance, for an iterator, iterator::value_type is the type that the
// iterator points to.
// STL algorithms and containers may use these type_traits (e.g. the following
// typedef) to work properly. In particular, without the following code,
// @code{std::sort(iter, iter1);} would not compile.
// See these websites for more information:
// https://en.cppreference.com/w/cpp/header/type_traits
// About value_type: https://blog.csdn.net/u014299153/article/details/72419713
// About iterator_category: https://en.cppreference.com/w/cpp/iterator
public:
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = T *;
using reference = T &;
using iterator_category = std::output_iterator_tag;
private:
/**
* TODO add data members
* just add whatever you want.
*/
//friend class const_iterator;
pointer ptr;
public:
iterator(pointer ptr_) : ptr(ptr_) {
}
/**
* return a new iterator which pointer n-next elements
* as well as operator-
*/
iterator operator+(const int &n) const {
//TODO
return iterator(ptr + n);
}
iterator operator-(const int &n) const {
//TODO
return iterator(ptr - n);
}
// return the distance between two iterators,
// if these two iterators point to different vectors, throw invaild_iterator.
int operator-(const iterator &rhs) const {
//TODO
return abs(ptr - rhs.ptr);
}
iterator &operator+=(const int &n) {
//TODO
this->ptr += n;
return *this;
}
iterator &operator-=(const int &n) {
//TODO
this->ptr -= n;
return *this;
}
/**
* TODO iter++
*/
iterator operator++(int) {
iterator tmp(*this);
tmp.ptr = this->ptr;
this->ptr = this->ptr + 1;
return tmp;
}
/**
* TODO ++iter
*/
iterator &operator++() {
this->ptr = this->ptr + 1;
return *this;
}
/**
* TODO iter--
*/
iterator operator--(int) {
iterator tmp;
tmp.ptr = this->ptr;
this->ptr = this->ptr - 1;
return tmp;
}
/**
* TODO --iter
*/
iterator &operator--() {
this->ptr = this->ptr - 1;
return *this;
}
/**
* TODO *it
*/
T &operator*() const {
return *ptr;
}
/**
* a operator to check whether two iterators are same (pointing to the same memory address).
*/
bool operator==(const iterator &rhs) const {
return ptr == rhs.ptr;
}
bool operator==(const const_iterator &rhs) const {
return ptr == rhs.ptr;
}
/**
* some other operator for iterator.
*/
bool operator!=(const iterator &rhs) const {
return ptr != rhs.ptr;
}
bool operator!=(const const_iterator &rhs) const {
return ptr != rhs.ptr;
}
};
class reverse_iterator {
// The following code is written for the C++ type_traits library.
// Type traits is a C++ feature for describing certain properties of a type.
// For instance, for an iterator, iterator::value_type is the type that the
// iterator points to.
// STL algorithms and containers may use these type_traits (e.g. the following
// typedef) to work properly. In particular, without the following code,
// @code{std::sort(iter, iter1);} would not compile.
// See these websites for more information:
// https://en.cppreference.com/w/cpp/header/type_traits
// About value_type: https://blog.csdn.net/u014299153/article/details/72419713
// About iterator_category: https://en.cppreference.com/w/cpp/iterator
public:
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = T *;
using reference = T &;
using iterator_category = std::output_iterator_tag;
private:
/**
* TODO add data members
* just add whatever you want.
*/
//friend class const_iterator;
pointer ptr;
public:
reverse_iterator(pointer ptr_) : ptr(ptr_) {
}
/**
* return a new iterator which pointer n-next elements
* as well as operator-
*/
reverse_iterator operator+(const int &n) const {
//TODO
return iterator(ptr + n);
}
reverse_iterator operator-(const int &n) const {
//TODO
return iterator(ptr - n);
}
// return the distance between two iterators,
// if these two iterators point to different vectors, throw invaild_iterator.
int operator-(const reverse_iterator &rhs) const {
//TODO
return abs(ptr - rhs.ptr);
}
reverse_iterator &operator+=(const int &n) {
//TODO
this->ptr += n;
return *this;
}
reverse_iterator &operator-=(const int &n) {
//TODO
this->ptr -= n;
return *this;
}
/**
* TODO iter++
*/
reverse_iterator operator++(int) {
iterator tmp(*this);
tmp.ptr = this->ptr;
this->ptr = this->ptr - 1;
return tmp;
}
/**
* TODO ++iter
*/
reverse_iterator &operator++() {
this->ptr = this->ptr - 1;
return *this;
}
/**
* TODO iter--
*/
iterator operator--(int) {
iterator tmp;
tmp.ptr = this->ptr;
this->ptr = this->ptr + 1;
return tmp;
}
/**
* TODO --iter
*/
iterator &operator--() {
this->ptr = this->ptr + 1;
return *this;
}
/**
* TODO *it
*/
T &operator*() const {
return *ptr;
}
/**
* a operator to check whether two iterators are same (pointing to the same memory address).
*/
bool operator==(const reverse_iterator &rhs) const {
return ptr == rhs.ptr;
}
bool operator==(const const_iterator &rhs) const {
return ptr == rhs.ptr;
}
/**
* some other operator for iterator.
*/
bool operator!=(const reverse_iterator &rhs) const {
return ptr != rhs.ptr;
}
bool operator!=(const const_iterator &rhs) const {
return ptr != rhs.ptr;
}
};
/**
* TODO
* has same function as iterator, just for a const object.
*/
class const_iterator {
public:
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = T *;
using reference = T &;
using iterator_category = std::output_iterator_tag;
private:
/*TODO*/
pointer ptr;
public:
const_iterator(pointer ptr_) : ptr(ptr_) {
}
const_iterator operator+(const int &n) const {
//TODO
return const_iterator(ptr + n);
}
const_iterator operator-(const int &n) const {
//TODO
return const_iterator(ptr - n);
}
// return the distance between two iterators,
// if these two iterators point to different vectors, throw invaild_iterator.
int operator-(const const_iterator &rhs) const {
//TODO
return abs(ptr - rhs.ptr);
}
const_iterator operator--(int) {
const_iterator tmp(ptr);
this->ptr = this->ptr - 1;
return tmp;
}
const_iterator &operator--() {
this->ptr = this->ptr - 1;
return *this;
}
const_iterator operator++(int) {
const_iterator tmp(ptr);
this->ptr = this->ptr + 1;
return tmp;
}
const_iterator &operator++() {
this->ptr = this->ptr + 1;
return *this;
}
/**
* TODO *it
*/
const T &operator*() const {
return *ptr;
}
/**
* a operator to check whether two iterators are same (pointing to the same memory address).
*/
bool operator==(const iterator &rhs) const {
return ptr == rhs.ptr;
}
bool operator==(const const_iterator &rhs) const {
return ptr == rhs.ptr;
}
/**
* some other operator for iterator.
*/
bool operator!=(const iterator &rhs) const {
return ptr != rhs.ptr;
}
bool operator!=(const const_iterator &rhs) const {
return ptr != rhs.ptr;
}
};
/**
* TODO Constructs
* At least two: default constructor, copy constructor
*/
vector() {
_size = 0;
_capacity = 10;
data = Traits::allocate(alloc, _capacity);
}
vector(const vector &other) {
this->_size = other._size;
this->_capacity = 2 * this->_size;
data = Traits::allocate(alloc, _capacity);
for (size_t i = 0; i < other._size; i++)
Traits::construct(alloc, data + i, other.data[i]);
}
/**
* TODO Destructor
*/
~vector() {
for (int i = 0; i < _size; i++)
Traits::destroy(alloc, data + i);
Traits::deallocate(alloc, data, _capacity);
data = nullptr;
_size = 0;
_capacity = 0;
}
/**
* TODO Assignment operator
*/
vector &operator=(const vector &other) {
if (this->data == other.data) {
return *this;
}
for (size_t i = 0; i < _size; i++) {
Traits::destroy(alloc, data + i);
}
Traits::deallocate(alloc, data, _capacity);
this->_size = other._size;
this->_capacity = 2 * this->_size;
data = Traits::allocate(alloc, _capacity);
for (size_t i = 0; i < other._size; i++)
Traits::construct(alloc, data + i, other.data[i]);
return *this;
}
/**
* assigns specified element with bounds checking
* throw index_out_of_bound if pos is not in [0, size)
*/
T &at(const size_t &pos) {
if (pos < 0 || pos >= _size) throw sjtu::index_out_of_bound();
return data[pos];
}
const T &at(const size_t &pos) const {
if (pos < 0 || pos >= _size) throw sjtu::index_out_of_bound();
return data[pos];
}
/**
* assigns specified element with bounds checking
* throw index_out_of_bound if pos is not in [0, size)
* !!! Pay attentions
* In STL this operator does not check the boundary but I want you to do.
*/
T &operator[](const size_t &pos) {
if (pos < 0 || pos >= _size) throw sjtu::index_out_of_bound();
return data[pos];
}
const T &operator[](const size_t &pos) const {
if (pos < 0 || pos >= _size) throw sjtu::index_out_of_bound();
return data[pos];
}
/**
* access the first element.
* throw container_is_empty if size == 0
*/
const T &front() const {
if (_size == 0) throw sjtu::container_is_empty();
else return data[0];
}
/**
* access the last element.
* throw container_is_empty if size == 0
*/
const T &back() const {
if (_size == 0) throw sjtu::container_is_empty();
else return data[this->_size - 1];
}
/**
* returns an iterator to the beginning.
*/
iterator begin() {
return iterator(data);
}
reverse_iterator rbegin() {
return reverse_iterator(data + _size - 1);
}
reverse_iterator rend() {
return reverse_iterator(data - 1);
}
const_iterator cbegin() const {
return const_iterator(data);
}
/**
* returns an iterator to the end.
*/
iterator end() {
return iterator(data + _size);
}
const_iterator cend() const {
return const_iterator(data + _size);
}
/**
* checks whether the container is empty
*/
bool empty() const {
return _size == 0;
}
/**
* returns the number of elements
*/
size_t size() const {
return _size;
}
/**
* clears the contents
*/
void clear() {
for (int i = 0; i < _size; i++)
Traits::destroy(alloc, data + i);
_size = 0;
}
/**
* inserts value before pos
* returns an iterator pointing to the inserted value.
*/
iterator insert(iterator pos, const T &value) {
if (_size == _capacity) {
_capacity *= 2;
T *new_data = Traits::allocate(alloc, _capacity);
size_t start_pos = pos - this->begin();
for (size_t i = start_pos; i < _size; i++) {
Traits::construct(alloc, new_data + i + 1, data[i]);
Traits::destroy(alloc, data + i);
}
Traits::construct(alloc, new_data + start_pos, value);
for (size_t i = 0; i < start_pos; i++) {
Traits::construct(alloc, new_data + i, data[i]);
Traits::destroy(alloc, data + i);
}
Traits::deallocate(alloc, data, _size);
_size++;
data = new_data;
return iterator(data + start_pos);
} else {
Traits::construct(alloc, data + _size, value);
for (auto t = this->end(); t != pos; --t) {
*t = *(t - 1);
}
*pos = value;
_size++;
return pos;
}
}
/**
* inserts value at index ind.
* after inserting, this->at(ind) == value
* returns an iterator pointing to the inserted value.
* throw index_out_of_bound if ind > size (in this situation ind can be size because after inserting the size will increase 1.)
*/
iterator insert(const size_t &ind, const T &value) {
if (ind > _size) {
throw sjtu::index_out_of_bound();
} else if (_size == _capacity) {
_capacity *= 2;
T *new_data = Traits::allocate(alloc, _capacity);
for (size_t i = ind; i < _size; i++) {
Traits::construct(alloc, new_data + i + 1, data[i]);
Traits::destroy(alloc, data + i);
}
Traits::construct(alloc, new_data + ind, value);
for (size_t i = 0; i < ind; i++) {
Traits::construct(alloc, new_data + i, data[i]);
Traits::destroy(alloc, data + i);
}
Traits::deallocate(alloc, data, _size);
_size++;
data = new_data;
} else {
Traits::construct(alloc, data + _size, value);
for (size_t i = _size; i > ind; i--) {
data[i] = data[i - 1];
}
data[ind] = value;
_size++;
}
return iterator(data + ind);
}
/**
* removes the element at pos.
* return an iterator pointing to the following element.
* If the iterator pos refers the last element, the end() iterator is returned.
*/
iterator erase(iterator pos) {
for (size_t i = pos - data; i < _size; ++i) {
data[i] = data[i + 1];
}
_size--;
Traits::destroy(alloc, data + _size);
return iterator(pos + 1);
}
/**
* removes the element with index ind.
* return an iterator pointing to the following element.
* throw index_out_of_bound if ind >= size
*/
iterator erase(const size_t &ind) {
for (size_t i = ind; i < _size; i++) {
data[i] = data[i + 1];
}
_size--;
Traits::destroy(alloc, data + _size);
return iterator(data + ind + 1);
}
/**
* adds an element to the end.
*/
void push_back(const T &value) {
if (_size == _capacity) {
_capacity *= 2;
T *new_data = Traits::allocate(alloc, _capacity);
for (size_t i = 0; i < _size; i++) {
Traits::construct(alloc, new_data + i, data[i]);
Traits::destroy(alloc, data + i);
}
Traits::deallocate(alloc, data, _size);
Traits::construct(alloc, new_data + _size, value);
data = new_data;
} else {
Traits::construct(alloc, data + _size, value);
}
_size++;
}
/**
* remove the last element from the end.
* throw container_is_empty if size() == 0
*/
void pop_back() {
if (_size == 0) {
throw sjtu::container_is_empty();
} else {
--_size;
Traits::destroy(alloc, data + _size);
}
}
};
}
#endif