-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.h
248 lines (215 loc) · 6.11 KB
/
vector.h
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
// Copyright Ben Roberts 2024
#pragma once
#include <iterator>
template <typename ValueType> class Vector {
private:
size_t capacity_;
size_t size_;
ValueType *array_;
public:
// Default constructor
Vector() : capacity_(0), size_(0), array_(nullptr) {}
// Overload constructor
explicit Vector(size_t initial_capacity)
: capacity_(initial_capacity), size_(initial_capacity), array_(nullptr) {
if (capacity_ > 0) {
array_ = new ValueType[capacity_];
for (size_t i = 0; i < size_; ++i) {
array_[i] = ValueType();
}
}
}
// Init-list constructor
Vector(std::initializer_list<ValueType> init)
: capacity_(init.size()), size_(init.size()), array_(nullptr) {
if (capacity_ > 0) {
array_ = new ValueType[capacity_];
size_t idx = 0;
for (const auto &val : init) {
array_[idx++] = val;
}
}
}
// Clear vector container
void clear() {
for (size_t i = 0; i < size_; ++i) {
array_[i].~ValueType();
}
size_ = 0;
}
// Destructor
~Vector() {
clear();
delete[] array_;
array_ = nullptr;
}
// Copy constructor
Vector(const Vector &other) : capacity_(other.capacity_), size_(other.size_) {
if (capacity_ > 0) {
array_ = new ValueType[capacity_];
for (size_t i = 0; i < size_; ++i) {
array_[i] = other.array_[i];
}
} else {
array_ = nullptr;
}
}
// Move constructor
Vector(Vector &&other) noexcept
: capacity_(other.capacity_), size_(other.size_), array_(other.array_) {
other.size_ = 0;
other.capacity_ = 0;
other.array_ = nullptr;
}
// Copy assignment
Vector &operator=(const Vector &other) noexcept {
if (this != &other) {
// If allocated capacity is smaller than other's size, re-allocate
if (capacity_ < other.size_) {
clear();
array_ = new ValueType[other.capacity_];
capacity_ = other.capacity_;
}
// Copy over the size_ elements
size_ = other.size_;
for (size_t i = 0; i < size_; ++i) {
array_[i] = other.array_[i];
}
}
return *this;
}
// Move assignment
Vector &operator=(Vector &&other) noexcept {
if (this != &other) {
clear();
delete[] array_;
capacity_ = other.capacity_;
size_ = other.size_;
array_ = other.array_;
// Null out other's data
other.capacity_ = 0;
other.size_ = 0;
other.array_ = nullptr;
}
return *this;
}
size_t capacity() const noexcept { return capacity_; }
size_t size() const noexcept { return size_; }
bool empty() const noexcept { return size_ == 0; }
void push_back(const ValueType &value) {
if (size_ == capacity_) {
reserve(capacity_ == 0 ? 8 : 2 * capacity_);
}
array_[size_++] = value;
}
void push_back(ValueType &&value) {
if (size_ == capacity_) {
reserve(capacity_ == 0 ? 8 : 2 * capacity_);
}
array_[size_++] = std::move(value);
}
void pop_back() {
if (size_ > 0) {
size_--;
}
}
ValueType &front() { return array_[0]; }
const ValueType &front() const { return array_[0]; }
ValueType &back() { return array_[size_ - 1]; }
const ValueType &back() const { return array_[size_ - 1]; }
ValueType &operator[](size_t i) { return array_[i]; }
const ValueType &operator[](size_t i) const { return array_[i]; }
void reserve(size_t new_cap) {
if (new_cap > capacity_) {
ValueType *new_data = new ValueType[new_cap];
for (size_t i = 0; i < size_; ++i) {
new_data[i] = std::move(array_[i]);
}
delete[] array_;
array_ = new_data;
capacity_ = new_cap;
}
}
void resize(size_t count) {
if (count < size_) {
for (size_t i = count; i < size_; ++i) {
array_[i].~ValueType();
}
size_ = count;
} else if (count > size_) {
if (count > capacity_) {
reserve(count);
}
for (size_t i = size_; i < count; ++i) {
array_[i] = ValueType();
}
size_ = count;
}
}
void resize(size_t count, const ValueType &value) {
if (count < size_) {
for (size_t i = size_; i < count; ++i) {
array_[i].~ValueType();
}
size_ = count;
} else if (count > size_) {
if (count > capacity_) {
reserve(count);
}
for (size_t i = size_; i < count; ++i) {
array_[i] = value;
}
size_ = count;
}
}
// Non-const iterator
class Iterator {
private:
ValueType *ptr_;
public:
explicit Iterator(ValueType *p) : ptr_(p) {}
ValueType &operator*() const { return *ptr_; }
ValueType *operator->() const { return ptr_; }
Iterator &operator++() {
++ptr_;
return *this;
}
Iterator operator++(int) {
Iterator tmp = *this;
++(*this);
return tmp;
}
bool operator==(const Iterator &other) const { return ptr_ == other.ptr_; }
bool operator!=(const Iterator &other) const { return !(*this == other); }
};
// Const iterator
class ConstIterator {
private:
const ValueType *ptr_;
public:
explicit ConstIterator(const ValueType *p) : ptr_(p) {}
const ValueType &operator*() const { return *ptr_; }
const ValueType *operator->() const { return ptr_; }
ConstIterator &operator++() {
++ptr_;
return *this;
}
ConstIterator operator++(int) {
ConstIterator tmp = *this;
++(*this);
return tmp;
}
bool operator==(const ConstIterator &other) const {
return ptr_ == other.ptr_;
}
bool operator!=(const ConstIterator &other) const {
return !(*this == other);
}
};
// Overloads for normal iteration
Iterator begin() { return Iterator(array_); }
Iterator end() { return Iterator(array_ + size_); }
// Overloads for const iteration
ConstIterator begin() const { return ConstIterator(array_); }
ConstIterator end() const { return ConstIterator(array_ + size_); }
};