-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic_array.h
288 lines (241 loc) · 7.55 KB
/
dynamic_array.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
#include <initializer_list>
#include <algorithm>
#include <cstring>
#include <iostream>
template<typename T>
class dynamic_array
{
public:
////////////////////////////////////////////////////////////////////////////////
//constructors
////////////////////////////////////////////////////////////////////////////////
dynamic_array(){}
//constructor for member list initialization
dynamic_array(std::initializer_list<T> il)
{
size_ += il.size();
capacity_ = size_;
data_ = new T[capacity_];
// data_ = (T*)malloc((sizeof *data_) * size_);
std::copy(il.begin(),il.end(), data_);
}
~dynamic_array()
{
std::cout << "destructor called\n";
delete[] data_;
}
////////////////////////////////////////////////////////////////////////////////
//element access
////////////////////////////////////////////////////////////////////////////////
T& operator[](size_t index){ return data_[index]; }
const T& operator[](size_t index) const { return data_[index]; }
////////////////////////////////////////////////////////////////////////////////
//operations
////////////////////////////////////////////////////////////////////////////////
//allocate memory so the container can hold at least a number of elements
//that is equal to size
//if size is less than capacity do nothing
void reserve(size_t size)
{
if(size <= capacity_)
return;
capacity_ = size;
T* data = new T[size_];
std::copy(data_, data_ + size_, data);
if(data_)
delete[] data_;
data_ = new T[capacity_];
std::copy(data, data + size_, data_);
delete[] data;
// data_ = (T*)realloc(data_, (sizeof *data_) * capacity_);
}
//returns element at index
//throws if index is invalid
constexpr const T& at(size_t index) const
{
if(index < size())
return data_[index];
throw std::out_of_range ("Index out of range");
}
//resize by 2*N for amortized constant time insertion
void push_back(const T& data)
{
if(!size_ && !capacity_)
{
data_ = new T[1];
data_[size_] = data;
size_++;
capacity_ = size_;
return;
}
//double capacity on memory reallocation
if (size_ == capacity_)
reserve(capacity_ * 2);
data_[size_] = data;
size_++;
}
//pop the last element of the dynamic array by decreasing the size.
//when size is half of capacity reallocate the memory to shrink
//the container so you get amortized constant time for popping as well
void pop_back()
{
size_--;
if(size_ == capacity_/2)
{
//shrink the capacity
capacity_ /= 2;
//reallocate the memory block and copy over the values
T* data = new T[size_];
std::copy(data_, data_ + size_, data);
if(data_)
delete[] data_;
data_ = new T[capacity_];
std::copy(data, data + size_, data_);
delete[] data;
}
}
//replaces the contents of the dynamic array with a number of size elements
//equal to the value of data
void assign(size_t size, const T& data = 0)
{
if(data_)
delete[] data_;
size_ = size;
capacity_ = size_;
data_ = new T[capacity_];
for(uint32_t i= 0;i<capacity_;++i)
data_[i] = data;
}
//replaces the contents of the dynamic array with contents of memory block
//of size being pointed at by the data pointer
void assign (size_t size, const T* data)
{
if(data_)
delete[] data_;
size_ = size;
capacity_ = size_;
data_ = new T[capacity_];
std::copy(data, data + size, data_);
}
//inserts new element(s) before the specified position
//
//input:
// position - pointer to the dynamic array element where to insert
// data - data to insert
// size - how many items of data to insert
void insert(T* position, const T& data, size_t size = 1)
{
//calculate insert index
uint32_t index = position - data_;
//if size is to small, reallocate
if(size_ + size > capacity_)
reserve(2 * capacity_);
//do size number of shifts to make room for new elements
uint32_t shifts = size;
while(shifts)
{
//shift array elements to the right
for(uint32_t i = size_+size-1; i>index; --i)
data_[i] = data_[i-1];
shifts--;
}
size_ += size;
//insert size elements to the array from the index position
for(uint32_t i = index; i<index+size; ++i)
data_[i] = data;
}
//insert elements stored in memory block pointed to by data, of size
//returns pointer to the first of the newly inserted elements
void insert(const T* position, T* data, size_t size)
{
//calculate insert index
uint32_t index = position - data_;
//if size is to small, reallocate
if(size_ + size > capacity_)
reserve(2 * capacity_);
//do size number of shifts to make room for new elements
uint32_t shifts = size;
while(shifts)
{
//shift array elements to the right
for(uint32_t i = size_+size; i>index; --i)
data_[i] = data_[i-1];
shifts--;
}
size_ += size;
//insert size elements to the array from the index position
std::copy(data, data+size, &data_[index]);
}
void resize(size_t size, const T& data = 0)
{
//shrink the dynamic array to the specified size
if(size < size_)
{
size_ = size;
capacity_ = size_;
//allocate temporary block
T* data = new T[size];
//copy the data
std::copy(data_, data_ + size_, data);
//free the current block of memory
if(data_)
delete[] data_;
//reallocate the resized block
data_ = new T[capacity_];
//copy the data from the temporary block
std::copy(data, data + size_, data_);
//free temporary block
delete[] data;
}
//resize the dynamic array and fill it up to capacity with 0 or
//specified value
if(size > capacity_)
{
reserve(size);
if(data)
{
for(uint32_t i= size_;i<capacity_;++i)
data_[i] = data;
size_ = capacity_;
}
else
size_ = capacity_;
}
}
//free unused memory in dynamic array where capacity > size
void shrink_capacity()
{
if (size_ >= capacity_)
return;
capacity_ = size_;
//copy data to temp
T* data = new T[size_];
std::copy(data_, data_ + size_, data);
delete[] data_;
//copy data from temp
data_ = new T[size_];
std::copy(data, data+ size_, data_);
delete[] data;
}
//access front and back elements
constexpr const T& front() const { return data_[0]; }
constexpr const T& back() const { return data_[size()-1]; }
//check if empty
constexpr bool empty() { return size() == 0; }
//get size and data
constexpr size_t size() const { return size_; }
constexpr size_t capacity() const { return capacity_; }
T* data(){ return data_; }
const T* data() const { return data_; }
//returns pointer to specified position in the vector
const T* position(uint32_t index) const
{
//throw exception for invalid index
if(index > size_)
throw std::out_of_range("Index out of range!");
return &data_[index];
}
private:
T* data_;
size_t size_ = 0, capacity_ = 0;
};