-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.h
228 lines (188 loc) · 5.58 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
#ifndef EPC_VECTOR
#define EPC_VECTOR
#include <cstdlib>
#include <algorithm>
#include <new>
namespace epc {
template<typename T, size_t N>
class vector {
std::size_t capacity_{N};
std::size_t size_{0};
alignas(T)
unsigned char buff_[sizeof(T)*N];
T* data_{reinterpret_cast<T*>(buff_)};
bool is_short() const { return capacity_==N; }
public:
vector()
noexcept =
default;
vector(const vector& src)
:capacity_{src.is_short() ? N : src.size_}, size_{src.size_}
{
std::size_t s{0};
if (!src.is_short())
data_ = static_cast<T*>(::operator new(size_*sizeof(T)));
try {
for (s = 0; s<src.size(); s++)
new(data_+s) T(src.data_[s]);
}
catch (...) {
for (std::size_t d{0}; d<s; d++)
data_[d].T::~T();
if (!is_short()) ::operator delete(data_);
throw;
}
}
vector& operator=(const vector& rhs)
{
if (&rhs!=this) {
// copy-and-swap idiom
vector temp(rhs);
swap(temp);
}
return *this;
}
vector(vector&& src)
:capacity_{src.is_short() ? N : src.size_}, size_{src.size_}
{
if (!src.is_short()) {
data_ = src.data_;
src.data_ = reinterpret_cast<T*>(src.buff_);
src.capacity_ = N;
}
else {
for (std::size_t s = 0; s<src.size(); s++) {
new(data_+s) T(std::move(src.data_[s]));
src.data_[s].T::~T();
}
}
src.size_ = 0;
}
vector& operator=(vector&& rhs)
{
if (&rhs!=this) {
vector temp(std::move(rhs));
swap(temp);
}
return *this;
}
~vector()
{
for (std::size_t i{0}; i<size_; i++)
data_[i].T::~T();
if (!is_short()) ::operator delete(data_);
}
T* data()
{
return data_;
}
const T* data() const
{
return data_;
}
T& operator[](size_t i)
{
return data_[i];
}
const T& operator[](size_t i) const
{
return data_[i];
}
void push_back(const T& el)
{
emplace_back(el);
}
void push_back(T&& el)
{
emplace_back(std::move(el));
}
template<typename... Ts>
void emplace_back(Ts&& ...args)
{
if (size_==capacity_) reserve((!capacity_) ? 1 : capacity_*2);
new(data_+(size_)) T(std::forward<Ts>(args)...);
size_++;
}
size_t capacity() const
{
return capacity_;
}
size_t size() const
{
return size_;
}
void swap(vector& other)
{
vector* shorter, * longer;
if (size_<other.size_) {
shorter = this;
longer = &other;
}
else {
shorter = &other;
longer = this;
}
if (shorter->is_short()) {
T*shorter_buff = reinterpret_cast<T*>(shorter->buff_);
T*longer_buff = reinterpret_cast<T*>(longer->buff_);
// two short
if (longer->is_short()) {
for (std::size_t i{0}; i<shorter->size_; i++)
std::swap(shorter_buff[i], longer_buff[i]);
for (std::size_t i{shorter->size_}; i<longer->size_; i++) {
new(shorter_buff+i) T(longer_buff[i]);
longer_buff[i].T::~T();
}
}
// long and short
else {
for (std::size_t i{0}; i<shorter->size_; i++) {
new(longer_buff+i) T(std::move(shorter_buff[i]));
shorter_buff[i].T::~T();
}
shorter->data_ = longer->data_;
longer->data_ = longer_buff;
}
}
// two long
else {
std::swap(shorter->data_, longer->data_);
}
std::swap(capacity_, other.capacity_);
std::swap(size_, other.size_);
}
void reserve(size_t capacity)
{
if (capacity>capacity_) {
T*temp;
std::size_t s{0};
temp = static_cast<T*>(::operator new(capacity*sizeof(T)));
try {
for (s = 0; s<size_; s++)
new(temp+s) T(std::move(data_[s]));
}
catch (...) {
for (std::size_t d{0}; d<s; d++)
temp[d].T::~T();
::operator delete(temp);
throw;
}
for (std::size_t i{0}; i<size_; i++)
data_[i].T::~T();
if (!is_short())
::operator delete(data_);
data_ = temp;
capacity_ = capacity;
}
}
void pop_back()
{
data_[--size_].T::~T();
}
void clear()
{
while (size_) pop_back();
}
};
}
#endif