-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathVector-better.cpp
More file actions
263 lines (257 loc) · 8.13 KB
/
Copy pathVector-better.cpp
File metadata and controls
263 lines (257 loc) · 8.13 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
// also available live: https://wandbox.org/permlink/6GkCH1abSp1JMroz
#include <cstddef>
#include <algorithm>
#include <utility>
#include <initializer_list>
#include <iterator>
#include <cstdlib> // HERE
#include <memory> // HERE
template <class T>
class Vector {
public:
using value_type = T;
using size_type = std::size_t;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
private:
pointer elems{};
size_type nelems{},
cap{};
// ...
public:
size_type size() const { return nelems; }
size_type capacity() const { return cap; }
bool empty() const { return size() == 0; }
private:
bool full() const { return size() == capacity(); }
// ...
public:
using iterator = pointer;
using const_iterator = const_pointer;
iterator begin() { return elems; }
const_iterator begin() const { return elems; }
const_iterator cbegin() const { return begin(); }
iterator end() { return begin() + size(); }
const_iterator end() const { return begin() + size(); }
const_iterator cend() const { return end(); }
// ...
Vector() = default;
// HERE
Vector(size_type n, const_reference init)
: elems{ static_cast<pointer>(std::malloc(n * sizeof(value_type))) },
nelems{ n }, cap{ n } {
try {
std::uninitialized_fill(begin(), end(), init);
} catch (...) {
std::free(elems);
throw;
}
}
// HERE
Vector(const Vector& other)
: elems{ static_cast<pointer>(std::malloc(other.size() * sizeof(value_type))) },
nelems{ other.size() }, cap{ other.size() } {
try {
std::uninitialized_copy(other.begin(), other.end(), begin());
} catch (...) {
std::free(elems);
throw;
}
}
// ...
Vector(Vector&& other) noexcept
: elems{ std::exchange(other.elems, nullptr) },
nelems{ std::exchange(other.nelems, 0) },
cap{ std::exchange(other.cap, 0) } {
}
// ...
// HERE
Vector(std::initializer_list<T> src)
: elems{ static_cast<pointer>(std::malloc(src.size() * sizeof(value_type))) },
nelems{ src.size() }, cap{ src.size() } {
try {
std::uninitialized_copy(src.begin(), src.end(), begin());
} catch (...) {
std::free(elems);
throw;
}
}
// HERE
~Vector() {
std::destroy(begin(), end());
std::free(elems);
}
// ...
void swap(Vector& other) noexcept {
using std::swap;
swap(elems, other.elems);
swap(nelems, other.nelems);
swap(cap, other.cap);
}
Vector& operator=(const Vector& other) {
Vector{ other }.swap(*this);
return *this;
}
Vector& operator=(Vector&& other) {
Vector{ std::move(other) }.swap(*this);
return *this;
}
// ...
reference operator[](size_type n) { return elems[n]; }
const_reference operator[](size_type n) const { return elems[n]; }
// precondition: !empty()
reference front() { return (*this)[0]; }
const_reference front() const { return (*this)[0]; }
reference back() { return (*this)[size() - 1]; }
const_reference back() const { return (*this)[size() - 1]; }
// ...
bool operator==(const Vector& other) const {
return size() == other.size() &&
std::equal(begin(), end(), other.begin());
}
// can be omitted since C++20
bool operator!=(const Vector& other) const {
return !(*this == other);
}
// ...
void push_back(const_reference val) {
if (full())
grow();
// HERE
std::construct_at(end(), val);
++nelems;
}
void push_back(T&& val) {
if (full())
grow();
// HERE
std::construct_at(end(), std::move(val));
++nelems;
}
template <class ... Args>
reference emplace_back(Args &&...args) {
if (full())
grow();
// HERE
std::construct_at(end(), std::forward<Args>(args)...);
++nelems;
return back();
}
private:
// HERE
void grow() {
reserve(capacity()? capacity() * 2 : 16);
}
public:
// HERE
void reserve(size_type new_cap) {
if(new_cap <= capacity()) return;
auto p = static_cast<pointer>(std::malloc(new_cap * sizeof(T)));
if constexpr(std::is_nothrow_move_assignable_v<T>) {
std::uninitialized_move(begin(), end(), p);
} else try {
std::uninitialized_copy(begin(), end(), p);
} catch (...) {
std::free(p);
throw;
}
std::destroy(begin(), end());
std::free(elems);
elems = p;
cap = new_cap;
}
// HERE
void resize(size_type new_cap) {
if(new_cap <= capacity()) return;
auto p = static_cast<pointer>(std::malloc(new_cap * sizeof(T)));
if constexpr(std::is_nothrow_move_assignable_v<T>) {
std::uninitialized_move(begin(), end(), p);
} else try {
std::uninitialized_copy(begin(), end(), p);
} catch (...) {
std::free(p);
throw;
}
std::uninitialized_fill(p + size(), p + capacity(), value_type{});
std::destroy(begin(), end());
std::free(elems);
elems = p;
nelems = new_cap;
cap = new_cap;
}
// etc.
// two small examples, one that inserts elements
// at a given position in the container and one
// that erases an element at a given position
// in the container
template <class It>
iterator insert(const_iterator pos, It first, It last) {
iterator pos_ = const_cast<iterator>(pos);
// deliberate usage of unsigned integrals
const std::size_t remaining = capacity() - size();
const std::size_t n = std::distance(first, last);
if (remaining < n) {
auto index = std::distance(begin(), pos_);
reserve(capacity() + n - remaining);
pos_ = std::next(begin(), index);
}
if (auto m = std::distance(std::next(pos_, n), end()); m > 0) {
std::uninitialized_copy(pos_ + n, end(), end() + n - m);
std::uninitialized_copy(pos_ + m, pos_ + n, end());
std::copy_backward(pos_, pos_ + m, pos_ + n + m);
std::copy(first, last, pos_);
} else {
std::uninitialized_copy(pos_, end(), end() + n - (n + m));
std::uninitialized_copy(first + n + m, last, end());
std::copy(first, first + n + m, pos_);
}
nelems += n;
return pos_;
}
iterator erase(const_iterator pos) {
iterator pos_ = const_cast<iterator>(pos);
if (pos_ == end()) return pos_;
std::copy(std::next(pos_), end(), pos_);
*std::prev(end()) = {};
--nelems;
return pos_;
}
};
#include <iostream>
template <class T>
std::ostream& operator<<(std::ostream& os, const Vector<T>& v) {
if (v.empty()) return os;
os << v.front();
for (auto p = std::next(v.begin()); p != v.end(); ++p)
os << ',' << *p;
return os;
}
int main() {
Vector v0{ 2,3,5,7,11 };
Vector v1 = v0; // copy ctor
std::cout << v1 << '\n'; // 2,3,5,7,11
for (int n : { 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 })
v0.push_back(n); // will call grow() at some point
// Size: 15, capacity: 20
// 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47
std::cout << "Size: " << v0.size() << ", capacity: " << v0.capacity() << '\n'
<< v0 << '\n';
int arr[]{ -2, -3, -4 };
v1.insert(v1.begin(), std::begin(arr), std::end(arr));
// Size: 8, capacity: 8
// -2,-3,-4,2,3,5,7,11
std::cout << "Size: " << v1.size() << ", capacity: " << v1.capacity() << '\n'
<< v1 << '\n';
v1.insert(v1.end(), std::begin(arr), std::end(arr));
// Size: 11, capacity: 11
// -2,-3,-4,2,3,5,7,11,-2,-3-,4
std::cout << "Size: " << v1.size() << ", capacity: " << v1.capacity() << '\n'
<< v1 << '\n';
v1.erase(std::next(v1.begin(), 2));
// Size: 10, capacity: 11
// -2,-3,2,3,5,7,11,-2,-3,-4
std::cout << "Size: " << v1.size() << ", capacity: " << v1.capacity() << '\n'
<< v1 << '\n';
}