-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.h
More file actions
276 lines (239 loc) · 5.96 KB
/
Copy pathArray.h
File metadata and controls
276 lines (239 loc) · 5.96 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
264
265
266
267
268
269
270
271
272
273
274
275
276
#pragma once
#include "Iterator.h"
#include <iostream>
#include <cmath>
#include <initializer_list>
template<typename T>
class Array
{
public:
using iterator = Iterator<T>;
using const_iterator = Iterator<T>;
//CONSTRUCTORS & DESTRUCTOR
Array();
Array(const size_t count, const T& value = T());
Array(const Array<T>& array);
Array(std::initializer_list<T> init);
~Array();
//OPERATORS
//Assignment operator
Array<T>& operator=(const Array<T>& other);
//Subscript operator
T& operator[](const size_t index);
T& operator[](const size_t index) const;
//MEMBER FUNCTIONS
//Add/Remove from end of array
void PopBack();
void PushBack(const T& value);
//Add one or more elements to middle of array
iterator Insert(const_iterator pos, const T& value);
//Remove from middle of array, ordered and unordered
iterator RemoveOrdered(const_iterator pos);
iterator RemoveUnordered(const_iterator pos);
//Allocate more or less space in array
void Allocate(size_t chunk);
void Deallocate(size_t chunk);
//Clear array
void Clear();
//Search function
iterator Find(const T& value);
//Array information functions
unsigned int Size() const;
unsigned int Capacity() const;
bool Empty();
//Get elements
T& Front();
T& Back();
//Iterators
iterator begin() { return &data[0]; }
iterator end() { return &data[size]; }
const_iterator begin() const { return &data[0]; }
const_iterator end() const { return &data[size]; }
const_iterator cbegin() const noexcept { return &data[0]; }
const_iterator cend() const noexcept { return &data[size]; }
private:
unsigned int size;
unsigned int capacity;
T* data;
};
//------------------------------------------------------------------------------------------------------------------------------------------
template<typename T>
Array<T>::Array()
{
data = nullptr;
size = 0;
capacity = 0;
}
template<typename T>
Array<T>::Array(size_t count, const T& value)
{
data = new T[count]{ value };
size = count;
capacity = count;
}
template<typename T>
Array<T>::Array(const Array<T>& array)
{
data = new T[array.capacity];
size = array.size;
capacity = array.capacity;
for (size_t i = 0; i < capacity; i++) data[i] = array.data[i];
}
template<typename T>
Array<T>::Array(std::initializer_list<T> init)
{
data = new T[init.size()];
size = init.size();
capacity = init.size();
int i = 0;
for (auto& value : init)
{
data[i] = value;
i++;
}
}
template<typename T>
Array<T>::~Array()
{
delete[] data;
}
//------------------------------------------------------------------------------------------------------------------------------------------
template<typename T>
Array<T>& Array<T>::operator=(const Array<T>& other)
{
data = new T[other.capacity];
size = other.size;
capacity = other.capacity;
for (size_t i = 0; i < other.size; i++) data[i] = other.data[i];
return *this;
}
template<typename T>
T& Array<T>::operator[](const size_t index)
{
return data[index];
}
template<typename T>
T& Array<T>::operator[](const size_t index) const
{
return data[index];
}
//------------------------------------------------------------------------------------------------------------------------------------------
template<typename T>
void Array<T>::PopBack()
{
size--;
}
template<typename T>
void Array<T>::PushBack(const T& value)
{
if (size == capacity) Allocate(std::ceil(capacity * 1.5));
*end() = value;
size++;
}
//------------------------------------------------------------------------------------------------------------------------------------------
template<typename T>
typename Array<T>::iterator Array<T>::Insert(const_iterator pos, const T& value)
{
auto offset = pos - begin();
if (size == capacity) Allocate(std::ceil(capacity * 1.5));
pos = begin() + offset;
PushBack(Back());
for (auto it = end() - 1; it != pos; --it)
{
*it = *(it - 1);
}
*pos = value;
return pos;
}
template<typename T>
typename Array<T>::iterator Array<T>::RemoveOrdered(const_iterator pos)
{
for (auto it = pos; it != end() - 1; ++it)
{
*it = *(it + 1);
}
size--;
return pos;
}
template<typename T>
typename Array<T>::iterator Array<T>::RemoveUnordered(const_iterator pos)
{
*pos = *(end() - 1);
size--;
return pos;
}
//------------------------------------------------------------------------------------------------------------------------------------------
template<typename T>
void Array<T>::Allocate(size_t chunk)
{
if (chunk == 0) return;
Array temp;
temp = *this;
data = new T[capacity + chunk];
capacity += chunk;
for (size_t i = 0; i < temp.Size(); i++) data[i] = temp.data[i];
}
template<typename T>
void Array<T>::Deallocate(size_t chunk)
{
if (chunk >= capacity || chunk == 0) return;
Array temp;
temp = *this;
size_t newCap;
newCap = capacity - chunk;
data = new T[newCap];
capacity = newCap;
if (newCap < size)
{
int diff = size - newCap;
for (size_t i = 0; i < size - diff; i++) data[i] = temp.data[i];
}
else
{
for (size_t i = 0; i < size; i++) data[i] = temp.data[i];
}
}
//------------------------------------------------------------------------------------------------------------------------------------------
template<typename T>
void Array<T>::Clear()
{
delete[] data;
data = new T[capacity];
size = 0;
}
template<typename T>
typename Array<T>::iterator Array<T>::Find(const T& value)
{
for (auto it = begin(); it != end(); ++it)
{
if (*it == value) return it;
}
return end();
}
//------------------------------------------------------------------------------------------------------------------------------------------
template<typename T>
unsigned int Array<T>::Size() const
{
return size;
}
template<typename T>
unsigned int Array<T>::Capacity() const
{
return capacity;
}
template<typename T>
inline bool Array<T>::Empty()
{
return begin() = end();
}
//------------------------------------------------------------------------------------------------------------------------------------------
template<typename T>
T& Array<T>::Front()
{
return *begin();
}
template<typename T>
T& Array<T>::Back()
{
return *(end() - 1);
}