-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.cpp
More file actions
236 lines (198 loc) · 4.98 KB
/
Copy pathvector.cpp
File metadata and controls
236 lines (198 loc) · 4.98 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
#include <algorithm>
#include "vector.h"
Vector::Vector() {
size_ = 0;
capacity_ = 0;
start_ = nullptr;
}
Vector::Vector(size_t size) {
size_ = size;
capacity_ = size;
start_ = new ValueType[capacity_ + 1];
for (size_t i = 0; i < size_; ++i) {
start_[i] = 0;
}
}
Vector::Vector(std::initializer_list<ValueType> list) {
size_ = list.size();
capacity_ = list.size();
start_ = nullptr;
if (size_ != 0) {
start_ = new ValueType[capacity_ + 1];
size_t i = 0;
for (auto it = list.begin(); it != list.end(); ++it) {
start_[i++] = *it;
}
}
}
Vector::~Vector() {
size_ = 0;
capacity_ = 0;
delete[] start_;
}
Vector::Vector(const Vector &other) {
size_ = other.size_;
capacity_ = other.capacity_;
start_ = new ValueType[capacity_ + 1];
for (size_t i = 0; i < other.size_; ++i) {
start_[i] = other.start_[i];
}
}
Vector &Vector::operator=(const Vector &other) {
Vector::~Vector();
size_ = other.size_;
capacity_ = other.capacity_;
start_ = new ValueType[capacity_ + 1];
for (size_t i = 0; i < other.size_; ++i) {
start_[i] = other.start_[i];
}
return *this;
}
Vector::SizeType Vector::Size() const {
return size_;
}
Vector::SizeType Vector::Capacity() const {
return capacity_;
}
const Vector::ValueType *Vector::Data() const {
return start_;
}
bool Vector::operator==(const Vector &other) const {
if (size_ != other.size_) {
return false;
}
for (size_t i = 0; i < size_; ++i) {
if (start_[i] != other.start_[i]) {
return false;
}
}
return true;
}
bool Vector::operator!=(const Vector &other) const {
return !(*this == other);
}
std::strong_ordering Vector::operator<=>(const Vector &other) const {
for (size_t i = 0; i < std::min(size_, other.size_); ++i) {
if (start_[i] <=> other.start_[i] != 0) {
return start_[i] <=> other.start_[i];
}
}
return size_ <=> other.size_;
}
Vector::ValueType &Vector::operator[](size_t position) {
return start_[position];
}
Vector::ValueType Vector::operator[](size_t position) const {
return start_[position];
}
void Vector::Reserve(SizeType new_capacity) {
if (capacity_ >= new_capacity) {
return;
}
ValueType *new_start = new ValueType[new_capacity + 1];
SizeType new_size = size_;
for (size_t i = 0; i < size_; ++i) {
new_start[i] = start_[i];
}
Vector::~Vector();
start_ = new_start;
size_ = new_size;
capacity_ = new_capacity;
}
void Vector::Clear() {
size_ = 0;
}
void Vector::PushBack(const ValueType &new_element) {
if (size_ == capacity_) {
if (capacity_ != 0) {
Reserve(capacity_ * 2);
} else {
Reserve(1);
}
}
start_[size_++] = new_element;
}
void Vector::PopBack() {
if (size_ > 0) {
size_--;
}
}
void Vector::Swap(Vector &other) {
std::swap(size_, other.size_);
std::swap(capacity_, other.capacity_);
int *tmp = other.start_;
other.start_ = start_;
start_ = tmp;
}
Vector::Iterator Vector::Begin() {
return Iterator(start_);
}
Vector::Iterator Vector::End() {
return Iterator(start_ + size_);
}
Vector::Iterator Vector::begin() {
return Begin();
}
Vector::Iterator Vector::end() {
return End();
}
Vector::Iterator::Iterator() {
current_ = nullptr;
}
Vector::Iterator::Iterator(ValueType *pointer) {
current_ = pointer;
}
Vector::ValueType &Vector::Iterator::operator*() const {
return *current_;
}
Vector::ValueType *Vector::Iterator::operator->() const {
return current_;
}
Vector::Iterator &Vector::Iterator::operator=(Iterator other) {
if (this != &other) {
current_ = other.current_;
}
return *this;
}
Vector::Iterator &Vector::Iterator::operator++() {
++current_;
return *this;
}
Vector::Iterator Vector::Iterator::operator++(int) {
Iterator tmp(current_);
++current_;
return tmp;
}
Vector::Iterator &Vector::Iterator::operator--() {
--current_;
return *this;
}
Vector::Iterator Vector::Iterator::operator--(int) {
Iterator tmp(current_);
--current_;
return tmp;
}
Vector::Iterator Vector::Iterator::operator+(DifferenceType shift) {
Iterator tmp = Iterator(current_ + shift);
return tmp;
}
Vector::DifferenceType Vector::Iterator::operator-(Iterator other) {
return current_ - other.current_;
}
Vector::Iterator &Vector::Iterator::operator+=(DifferenceType shift) {
*this = *this + shift;
return *this;
}
Vector::Iterator &Vector::Iterator::operator-=(DifferenceType shift) {
current_ -= shift;
return *this;
}
bool Vector::Iterator::operator==(const Iterator &other) const {
return current_ == other.current_;
}
bool Vector::Iterator::operator!=(const Iterator &other) const {
return !(*this == other);
}
std::strong_ordering Vector::Iterator::operator<=>(const Iterator &other) const {
return current_ <=> other.current_;
}