-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicArray.h
356 lines (283 loc) · 8.33 KB
/
DynamicArray.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#pragma once
#include <iostream>
#include <algorithm>
#include <cassert>
template<typename Var>
class DynamicArray;
template<typename Var>
void swap(DynamicArray<Var>& lhs, DynamicArray<Var>& rhs);
template <typename Var>
class DynamicArray
{
Var* data_;
int capacity_;
int size_;
public:
using value_type = Var;
DynamicArray(); //defaul constructor
DynamicArray(std::initializer_list<Var> lst); // DynamicArray<T> name{1,2,3,4,5,...};
explicit DynamicArray(const Var* arr, int size); //converts primitive Var array to DynamicArray
explicit DynamicArray(const Var* begin, const Var* end); //converts a section of a primitive Var array to a DynamicArray. end is exclusive
explicit DynamicArray(int new_size, const Var& val = Var{}); // DynamicArray<T> name(5, 70); => 70, 70, 70, 70, 70
DynamicArray(const DynamicArray& rhs); //copy ctor
DynamicArray(DynamicArray&& rhs) noexcept; //will learn in upper-years...
~DynamicArray(); //dtor
DynamicArray& operator=(DynamicArray rhs); //copy/move assignment operator
Var& operator[](int i); //lookup index
const Var& operator[](int i) const; //for "const Var"
//basic container operators
bool IsEmpty() const;
int Capacity() const; //2* multipier (faster than 1.5*, but less memory-efficient)
int Size() const;
//specialized container operators
Var& Front(); //gets ref to front index
const Var& Front() const;
Var& Back(); //gets ref to back index
const Var& Back() const;
Var* begin(); //required for "(for const auto& i : vector) {...}"
const Var* begin() const;
Var* end(); //required for "(for const auto& i : vector) {...}"
const Var* end() const;
void PushBack(const Var& val);
void PushBack(Var&& val);
void PopBack();
void Clear();
//custom memory management
void Resize(int new_size, const Var& val = Var{});
void Reserve(int new_capacity);
void ShrinkToFit(); //shrinks capacity to size
Var* Data(); //returns the address of the internal data
const Var* Data() const;
friend void swap<Var>(DynamicArray<Var>& lhs, DynamicArray<Var>& rhs);
};
template<typename Var>
void swap(DynamicArray<Var>& lhs, DynamicArray<Var>& rhs)
{
using std::swap;
swap(lhs.data_, rhs.data_);
swap(lhs.capacity_, rhs.capacity_);
swap(lhs.size_, rhs.size_);
}
template <typename Var>
DynamicArray<Var>::DynamicArray() :
data_(static_cast<Var*>(operator new[](sizeof(Var)))), capacity_(1), size_(0)
{
//empty
}
template <typename Var>
DynamicArray<Var>::DynamicArray(const Var* arr, int size) :
data_(static_cast<Var*>(operator new[](size * sizeof(Var)))), capacity_(static_cast<int>(size)), size_(static_cast<int>(size))
{
assert(size >= 0);
std::uninitialized_copy(arr, arr + size, data_);
}
template <typename Var>
DynamicArray<Var>::DynamicArray(const Var* begin, const Var* end) : DynamicArray(begin, static_cast<int>(end - begin))
{
assert(begin <= end);
}
template <typename Var>
DynamicArray<Var>::DynamicArray(std::initializer_list<Var> lst) : DynamicArray(lst.begin(), static_cast<int>(lst.size()))
{
assert(lst.size() >= 0);
}
template <typename Var>
DynamicArray<Var>::DynamicArray(int new_size, const Var& val) :
data_(static_cast<Var*>(operator new[](new_size * sizeof(Var)))), capacity_(new_size), size_(new_size)
{
assert(new_size >= 0);
//std::fill calls operator=, which is undefined behavior when used on uninitialized object)
//alternative: loop + placement new
std::uninitialized_fill(data_, data_ + size_, val);
}
template <typename Var>
DynamicArray<Var>::DynamicArray(const DynamicArray& rhs) :
data_(static_cast<Var*>(operator new[](rhs.capacity_ * sizeof(Var)))), capacity_(rhs.capacity_), size_(rhs.size_)
{
assert(rhs.capacity_ >= 0);
//std::copy calls operator=, which is undefined behavior when used on uninitialized object)
//alternative: loop + placement new
std::uninitialized_copy(rhs.data_, rhs.data_ + rhs.size_, data_);
}
template <typename Var>
DynamicArray<Var>::DynamicArray(DynamicArray&& rhs) noexcept :
data_(std::exchange(rhs.data_, nullptr)), capacity_(std::exchange(rhs.capacity_, 0)), size_(std::exchange(rhs.size_, 0)) {}
template <typename Var>
DynamicArray<Var>::~DynamicArray()
{
//destruct objects
std::destroy(data_, data_ + size_);
//free the memory
operator delete[](data_);
}
//operators overloading
template <typename Var>
DynamicArray<Var>& DynamicArray<Var>::operator=(DynamicArray<Var> rhs)
{
swap(*this, rhs);
return *this;
}
template <typename Var>
Var& DynamicArray<Var>::operator[](int i)
{
assert(0 <= i && i < size_);
return data_[i];
}
template <typename Var>
const Var& DynamicArray<Var>::operator[](int i) const
{
assert(0 <= i && i < size_);
return data_[i];
}
//basic container operators
template <typename Var>
bool DynamicArray<Var>::IsEmpty() const
{
return size_ == 0;
}
template<typename Var>
int DynamicArray<Var>::Capacity() const
{
return capacity_;
}
template <typename Var>
int DynamicArray<Var>::Size() const
{
return size_;
}
//specialized container operators
template <typename Var>
Var& DynamicArray<Var>::Front()
{
assert(!this->IsEmpty());
return data_[0];
}
template <typename Var>
const Var& DynamicArray<Var>::Front() const
{
assert(!this->IsEmpty());
return data_[0];
}
template <typename Var>
Var& DynamicArray<Var>::Back()
{
assert(!this->IsEmpty());
return data_[size_ - 1];
}
template <typename Var>
const Var& DynamicArray<Var>::Back() const
{
assert(!this->IsEmpty());
return data_[size_ - 1];
}
template <typename Var>
Var* DynamicArray<Var>::begin()
{
return data_;
}
template <typename Var>
const Var* DynamicArray<Var>::begin() const
{
return data_;
}
template <typename Var>
Var* DynamicArray<Var>::end()
{
return data_ + size_; //pointer arithmetic
}
template <typename Var>
const Var* DynamicArray<Var>::end() const
{
return data_ + size_; //pointer arithmetic
}
template <typename Var>
void DynamicArray<Var>::PushBack(const Var& val)
{
//check if exceed capacity
if (size_ >= capacity_) Reserve(2 * capacity_);
//copy construct
new(data_ + size_) Var(val);
++size_;
}
template <typename Var>
void DynamicArray<Var>::PushBack(Var&& val)
{
//check if exceed capacity
if (size_ >= capacity_) Reserve(2 * capacity_);
//move construct
new(data_ + size_) Var(std::move(val));
++size_;
}
template <typename Var>
void DynamicArray<Var>::PopBack()
{
data_[--size_].~Var();
}
template <typename Var>
void DynamicArray<Var>::Clear()
{
//destroy the objects
std::destroy(data_, data_ + size_);
size_ = 0;
}
//custom memory management
template <typename Var>
void DynamicArray<Var>::Resize(int new_size, const Var& val)
{
assert(new_size >= 0);
//shrinking
if (new_size < size_)
{
//destroy truncated objects
std::destroy(data_ + new_size, data_ + size_);
}
else
{
//expand capacity if appropriate
Reserve(new_size);
//fill with the new objects
std::uninitialized_fill(data_ + size_, data_ + new_size, val);
}
size_ = new_size;
}
template <typename Var>
void DynamicArray<Var>::Reserve(int new_capacity)
{
if (new_capacity > capacity_)
{
//allocate heap with increased capacity
Var* new_data = static_cast<Var*>(operator new[](new_capacity * sizeof(Var)));
//move in the old data
std::uninitialized_move(data_, data_ + size_, new_data);
//destroy the old data, free the old heap
std::destroy(data_, data_ + size_);
operator delete[](data_);
//update the container
data_ = new_data;
capacity_ = new_capacity;
}
}
template <typename Var>
void DynamicArray<Var>::ShrinkToFit()
{
//allocate heap with size-equivalent capacity
Var* new_data = static_cast<Var*>(operator new[](size_ * sizeof(Var)));
//move in the old data
std::uninitialized_move(data_, data_ + size_, new_data);
//destroy the old data, free the old heap
std::destroy(data_, data_ + size_);
operator delete[](data_);
//update the container
data_ = new_data;
capacity_ = size_;
}
template <typename Var>
Var* DynamicArray<Var>::Data()
{
return data_;
}
template <typename Var>
const Var* DynamicArray<Var>::Data() const
{
return data_;
}