-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.h
341 lines (282 loc) · 8.64 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
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
#pragma once
#include <memory>
namespace mystd {
using std::allocator;
template<typename T>
class vector {
public:
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using iterator = T*;
using const_iterator = const T*;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
private:
const size_type INIT_CAPACITY = 1;
const size_type EXPAND_RATE = 2;
private:
allocator<T> alloc_;
pointer elem_ = nullptr; //elements of vector
pointer end_ = nullptr; //point to position after last elem
pointer free_ = nullptr; //the first space after capacity pf vector
private:
void destroyElem(iterator begin, iterator end) {
for (iterator it = begin; it < end; ++it)
alloc_.destroy(it);
}
//destroy elements and free the space
void clearMem() {
if (elem_) {
destroyElem(elem_, end_);
alloc_.deallocate(elem_, capacity());
elem_ = free_ = end_ = nullptr;
}
}
void expandCapacity(size_type new_capacity) {
if (new_capacity <= capacity())
return;
pointer new_elem_ = nullptr, new_end_, new_free_;
try {
new_elem_ = alloc_.allocate(new_capacity);
}
catch (...) {
alloc_.deallocate(new_elem_, new_capacity);
throw;
}
new_end_ = new_elem_ + size();
new_free_ = new_elem_ + new_capacity;
std::uninitialized_copy(elem_, end_, new_elem_);
clearMem();
elem_ = new_elem_;
end_ = new_end_;
free_ = new_free_;
}
void checkCapacity() {
if (end_ == free_)
expandCapacity(empty() ? INIT_CAPACITY : capacity() * EXPAND_RATE);
}
public:
/******constructor******/
//default
explicit vector() = default;
//fill
explicit vector(size_type n, const value_type& val = value_type()) {
try {
elem_ = alloc_.allocate(n);
}
catch (...) {
alloc_.deallocate(elem_, n);
throw;
}
std::uninitialized_fill_n(elem_, n, val);
end_ = free_ = elem_ + n;
}
//to simplify my vector, it only requires vector::iterator
vector(const_iterator first, const_iterator last) {
if (first == nullptr && last == nullptr)
return;
if (first > last)
throw std::out_of_range("at vector()");
difference_type n = std::distance(first, last);
pointer new_elem_;
new_elem_ = alloc_.allocate(n);
try {
std::uninitialized_copy(first, last, new_elem_);
}
catch (...) {
alloc_.deallocate(new_elem_, n);
throw;
}
clearMem();
elem_ = new_elem_;
end_ = free_ = elem_ + n;
}
vector(const vector& other) : vector(other.cbegin(), other.cend()) {}
vector(vector&& other) noexcept {
swap(other);
}
vector& operator=(const vector& other) {
if (&other == this)
return *this;
vector copy(other);
swap(copy);
return *this;
}
vector& operator=(vector&& other) {
if (&other == this)
return *this;
swap(other);
return *this;
}
//TO DO: construct with an initializer_list
/******Destructor******/
~vector() {
clearMem();
}
/******Capacity******/
size_type size() const noexcept {
return end_ - elem_;
}
size_type capacity() const noexcept {
return free_ - elem_;
}
bool empty() const noexcept {
return end_ == elem_;
}
void reserve(size_type n) {
expandCapacity(n);
}
void resize(size_type n) {
resize(n, value_type());
}
void resize(size_type n, const value_type& val) {
if (n < size()) {
erase(begin() + n, end());
}
else if (n > size()) {
insert(end(), size() - n, val);
}
}
//TO DO:
/******Element access******/
reference operator[](size_type index) {
return elem_[index];
}
const_reference operator[](size_type index) const {
return const_cast<vector*>(this)->operator[](index);
}
reference at(size_type index) {
if (index >= size())
throw std::out_of_range("at operator[]");
return this->operator[](index);
}
const_reference at(size_type index) const {
return const_cast<vector*>(this)->at(index);
}
reference front() {
return *begin();
}
const_reference front() const{
return const_cast<vector*>(this)->front();
}
reference back() {
if (!elem_)
throw std::out_of_range("at back()");
return *(end_ - 1);
}
const_reference back() const{
if (!elem_)
throw std::out_of_range("at back()");
return const_cast<vector*>(this)->back();
}
//TO DO: const_reference versions
/******iterator******/
iterator begin() noexcept {
return elem_;
}
const_iterator begin() const noexcept {
return elem_;
}
const_iterator cbegin() const noexcept {
return elem_;
}
iterator end() noexcept {
return end_;
}
const_iterator end() const noexcept {
return end_;
}
const_iterator cend() const noexcept {
return end_;
}
//TO DO: rbegin, rend
/******Modifiers******/
void push_back(const value_type& val) {
checkCapacity();
alloc_.construct(end_, val);
++end_;
}
void push_back(value_type&& val) {
checkCapacity();
new(end_) value_type(std::move(val));
++end_;
}
void pop_back() {
if (empty())
throw std::out_of_range("at pop_back()");
alloc_.destroy(end_);
--end_;
}
iterator insert(const_iterator position, const value_type& val) {
value_type val_ = val;
return insert(position, std::move(val_));
}
iterator insert(const_iterator position, size_type n, const value_type& val) {
iterator non_const_pos = const_cast<iterator>(position);
if (n == 0)
return non_const_pos;
for (size_type i = 0; i < n; ++i) {
non_const_pos = insert(non_const_pos, val);
}
return non_const_pos;
}
iterator insert(const_iterator position, value_type&& val) {
if (position < cbegin() || position > cend()) {
throw std::out_of_range("at insert()");
}
//if vector is expanded, position will be valid
difference_type idx = position - cbegin();
checkCapacity();
iterator non_const_pos = begin() + idx;
if (non_const_pos == end()) {
alloc_.construct(end_, val);
iterator ret = end();
++end_;
return ret;
}
alloc_.construct(end_, *(end_ - 1));
++end_;
for (iterator it = end() - 2; it > non_const_pos; --it) {
*it = std::move(*(it - 1));
}
*non_const_pos = val;
return non_const_pos;
}
//[first, last)
iterator erase(const_iterator first, const_iterator last) {
if (empty() || first < cbegin() || last > cend())
throw std::out_of_range("at erase()");
if (last == cend()) {
destroyElem(const_cast<iterator>(first), end_);
end_ = const_cast<iterator>(first);
return end();
}
iterator iter = std::move(const_cast<iterator>(last), end_, const_cast<iterator>(first));
destroyElem(iter, end_);
end_ = iter;
return const_cast<iterator>(first);
}
iterator erase(const_iterator position) {
if (position == cend()) {
alloc_.destroy(--end_);
return end();
}
return erase(position, position + 1);
}
void clear() noexcept {
if (empty())
throw std::out_of_range("at clear()");
destroyElem(elem_, end_);
end_ = elem_;
}
void swap(vector& other) {
std::swap(elem_, other.elem_);
std::swap(end_, other.end_);
std::swap(free_, other.free_);
}
//TO DO: insert(range), assign
};
}