-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array.hpp
243 lines (213 loc) · 7.77 KB
/
Array.hpp
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
#ifndef LIBSUPERCPP4WIN_ARRAY_HPP
#define LIBSUPERCPP4WIN_ARRAY_HPP
#include <algorithm>
#include <cstddef>
#include <memory>
#include <stdexcept>
template<typename T>
class Array {
public: // ctor functions
Array() = delete;
/**
* @example
* Array a1{1, 2, 3, 4, 5}; \n
* Array a2(a1);
* @param CopyCtor
*/
Array(const Array &CopyCtor)
: arrayLen(CopyCtor.arrayLen), arrayCapacity(CopyCtor.arrayCapacity) {
if (!CopyCtor.array) return;
array = std::make_unique<T[]>(arrayLen);
std::copy(CopyCtor.array.get(), CopyCtor.array.get() + CopyCtor.arrayLen, array.get());
}
/**
* @example
* Array a1{1, 2, 3, 4, 5}; \n
* Array a2(std::move(a1));
* @param MoveCtor
*/
Array(Array &&MoveCtor) noexcept
: arrayLen(MoveCtor.arrayLen), arrayCapacity(MoveCtor.arrayCapacity) {
if (!MoveCtor.array) return;
array = std::move(MoveCtor.array);
}
/**
* @example
* Array a(100,-1);
* @param ArraySize
* @param DefaultValue
*/
explicit Array(const std::size_t ArraySize, const T DefaultValue = 0)
: arrayLen(ArraySize), arrayCapacity(ArraySize) {
if (arrayLen == 0) return;
array = std::make_unique<T[]>(arrayLen);
std::fill(array.get(), array.get() + arrayLen, DefaultValue);
}
/**
* @example
* Array a = {1,2,3,4,5,6}; \n
* Array a{1,2,3,4,5,6};
* @param InitializerList
*/
Array(const std::initializer_list<T> &InitializerList)
: arrayLen(InitializerList.size()), arrayCapacity(InitializerList.size()) {
if (arrayLen == 0) return;
array = std::make_unique<T[]>(arrayLen);
std::copy(InitializerList.begin(), InitializerList.end(), array.get());
}
/**
* @example
* auto str = new char[6]{"hello"}; \n
* Array a(str, 6); \<- copy '\0'
* @param RawPtr
* @param ArraySize
*/
Array(const T *RawPtr, std::size_t ArraySize)
: arrayLen(ArraySize), arrayCapacity(ArraySize) {
if (RawPtr == nullptr || arrayLen == 0) return;
array = std::make_unique<T[]>(arrayLen);
std::copy(RawPtr, RawPtr + arrayLen, array.get());
}
/**
* @example
* std::vector\<int> v{1, 2, 3, 4, 5, 6, 7}; \n
* Array\<int> a(v.begin(), v.end());
* @param Begin
* @param End
*/
template<typename Iter, typename std::enable_if_t<!std::is_same<typename std::iterator_traits<Iter>::value_type, void>::value, int> = 0>
Array(const Iter &Begin, const Iter &End) {
if (Begin == End) return;
std::size_t size = End - Begin;
arrayLen = size;
arrayCapacity = size;
array = std::make_unique<T[]>(arrayLen);
std::copy(Begin, End, array.get());
}
/**
* @example
* Array a1{1, 2, 3, 4, 5}; \n
* Array a2 = a1;
* @param CopyAssignment
*/
Array &operator=(const Array &CopyAssignment) {
if (this == &CopyAssignment || CopyAssignment.arrayLen == 0) {
return *this;
}
arrayLen = CopyAssignment.arrayLen;
arrayCapacity = CopyAssignment.arrayCapacity;
array = std::make_unique<T[]>(arrayLen);
std::copy(CopyAssignment.array.get(), CopyAssignment.array.get() + arrayLen, array.get());
return *this;
}
/**
* @example
* Array a1{1, 2, 3, 4, 5}; \n
* Array a2 = std::move(a1);
* @param MoveAssignment
*/
Array &operator=(Array &&MoveAssignment) noexcept {
if (this == &MoveAssignment || MoveAssignment.arrayLen == 0) {
return *this;
}
arrayLen = MoveAssignment.arrayLen;
arrayCapacity = MoveAssignment.arrayCapacity;
array = std::move(MoveAssignment.array);
MoveAssignment.arrayLen = 0;
MoveAssignment.arrayCapacity = 0;
return *this;
}
public: // element changer
T &operator[](std::size_t Pos) const {
checkIsOutRange(Pos);
return array[Pos];
}
Array &push_front() {
}
Array &push_back() {
}
Array &insert(std::size_t Pos, const T *InsertBegin, std::size_t InsertLen) {
checkInsertPos(Pos);
const auto &size = InsertLen;
if (size == 0) return *this;
if (arrayLen + size > arrayCapacity) {
std::unique_ptr<T[]> newArray = capacityGrow(size);
std::move(array.get(), array.get() + Pos, newArray.get());
std::copy(InsertBegin, InsertBegin + InsertLen, newArray.get() + Pos);
std::move(array.get() + Pos, array.get() + arrayLen, newArray.get() + size + Pos);
array = std::move(newArray);
} else {
shiftElement(Pos, size);
std::copy(InsertBegin, InsertBegin + InsertLen, array.get() + Pos);
}
arrayLen += size;
return *this;
}
Array &insert(std::size_t Pos, const std::initializer_list<T> &InitializerList) {
checkInsertPos(Pos);
const auto &size = InitializerList.size();
if (size == 0) return *this;
if (arrayLen + size > arrayCapacity) {
std::unique_ptr<T[]> newArray = capacityGrow(size);
std::move(array.get(), array.get() + Pos, newArray.get());
std::copy(InitializerList.begin(), InitializerList.end(), newArray.get() + Pos);
std::move(array.get() + Pos, array.get() + arrayLen, newArray.get() + size + Pos);
array = std::move(newArray);
} else {
shiftElement(Pos, size);
std::copy(InitializerList.begin(), InitializerList.end(), array.get() + Pos);
}
arrayLen += size;
return *this;
}
template<typename Iter, typename std::enable_if_t<!std::is_same<typename std::iterator_traits<Iter>::value_type, void>::value, int> = 0>
Array &insert(std::size_t Pos, const Iter &Begin, const Iter &End) {
checkInsertPos(Pos);
std::size_t size = End - Begin;
if (size == 0) return *this;
if (arrayLen + size > arrayCapacity) {
std::unique_ptr<T[]> newArray = capacityGrow(size);
std::move(array.get(), array.get() + Pos, newArray.get());
std::copy(Begin, End, newArray.get() + Pos);
std::move(array.get() + Pos, array.get() + arrayLen, newArray.get() + size + Pos);
array = std::move(newArray);
} else {
shiftElement(Pos, size);
std::copy(Begin, End, array.get() + Pos);
}
arrayLen += size;
return *this;
}
public:
std::size_t size() const { return arrayLen; }
private:
// check index pos
void checkIsOutRange(std::size_t Pos) const {
// index must be [Begin, End)
if (Pos >= arrayLen) throw std::runtime_error("index out of range");
}
// check insert pos
void checkInsertPos(std::size_t Pos) const {
// insert must be [Begin, End]
if (Pos > arrayLen) throw std::runtime_error("insert pos out of range");
}
// grow array capacity
// new capacity is greater than the old capacity
std::unique_ptr<T[]> capacityGrow(std::size_t NeedCapacity) {
// Determine whether to use 1.5 old capacity or old capacity + need to increase space
arrayCapacity = std::max(arrayCapacity + NeedCapacity, (std::size_t) (arrayCapacity * 1.5));
return std::make_unique<T[]>(arrayCapacity);
}
// move all elements starting at Pos by Offset distance
void shiftElement(std::size_t Pos, std::size_t Offset) {
const auto backElementNum = arrayLen - Pos;
for (std::size_t i = 0; i < backElementNum; ++i) {
array[arrayLen - 1 + Offset - i] = array[arrayLen - 1 - i];
}
}
private:
std::unique_ptr<T[]> array;
std::size_t arrayLen = 0;
std::size_t arrayCapacity = 0;
};
#endif //LIBSUPERCPP4WIN_ARRAY_HPP