-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.cpp
More file actions
359 lines (292 loc) · 8.81 KB
/
Vector.cpp
File metadata and controls
359 lines (292 loc) · 8.81 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
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
357
358
359
#include "Vector.h"
//template<typename XYVector>
//XYVectorIterator<XYVector> :: XYVectorIterator(PointerType ptr){
// this->ptr = ptr;
//}
//__________________Default Constructor________________________
template<class T>
XYVector<T> :: XYVector(){
reAllocate(2);
}
//__________________Default DeConstructor________________________
template<class T>
XYVector<T> :: ~XYVector(){
// for(int i = 0; i < this->sz; ++i){
// data[i].~T[];
// }
//delete[] this->data;
this->data = nullptr;
this->sz = 0;
this->capacity = 0;
}
//__________________Size Constructor________________________
template<class T>
XYVector<T> :: XYVector(size_t sz){
this->reAllocate(sz+5);
this->sz = sz;
}
//__________________New value Constructor________________________
template<class T>
XYVector<T> :: XYVector(T* arr, size_t n){
this->data = arr;
this->reAllocate(n+5);
this->sz = n;
}
//_________________Copy Constructor_________________________
template<class T>
XYVector<T> :: XYVector(const XYVector& anotherVector){
delete[] this->data;
this->reAllocate(anotherVector.capacity);
for(int i = 0; i < anotherVector.sz; ++i){
data[i] = anotherVector.data[i];
}
this->sz = anotherVector.sz;
}
//_________________Move Constructor_________________________
template<class T>
XYVector<T> :: XYVector(XYVector&& anotherVector){
delete[] this->data;
this->data = std::move(anotherVector.data);
this->sz = anotherVector.sz;
this->reAllocate(anotherVector.capacity);
anotherVector.data = nullptr;
anotherVector.sz = 0;
anotherVector.capacity = 0;
}
//__________________________________________
//_________________Copy Assignment_________________________
template<class T>
XYVector<T>& XYVector<T> :: operator=(const XYVector& anotherVector){
if(anotherVector.data != this->data) {
delete[] this->data;
this->reAllocate(anotherVector.capacity);
for(int i = 0; i < anotherVector.sz; ++i){
data[i] = anotherVector.data[i];
}
this->sz = anotherVector.sz;
}
return *this;
}
//__________________________________________
//___________________Move Assignment_______________________
template<class T>
XYVector<T>& XYVector<T> :: operator=(XYVector&& anotherVector) {
if(anotherVector.data != this->data) {
delete[] this->data;
this->data = std::move(anotherVector.data);
this->sz = anotherVector.sz;
this->reAllocate(anotherVector.capacity);
anotherVector.data = nullptr;
anotherVector.sz = 0;
anotherVector.capacity = 0;
}
return *this;
}
//__________________________________________
//__________________Index Operator________________________
template<class T>
const T& XYVector<T> :: operator[](size_t index) const{
try{
if(index >= sz)
throw std::invalid_argument("OUT of INDEX");
return data[index];
}
catch(std::invalid_argument& error){
std::cerr << error.what() << std::endl;
}
// return data[index];
}
template<class T>
T& XYVector<T> ::operator[](size_t index){
try{
if(index >= sz)
throw std::invalid_argument("OUT of INDEX");
return data[index];
}
catch(std::invalid_argument& error){
std::cerr << error.what() << std::endl;
}
// return data[index];
}
//__________________________________________
//___________________== Operator_______________________
template<class T>
bool XYVector<T> :: operator==(const XYVector<T>& anotherVector){
if(this->sz == anotherVector.sz){
for(int i = 0; i < this->sz; ++i){
if(data[i] == anotherVector.data[i]) continue;
return false;
}
return true;
}
return false;
}
//___________________< >Operator_______________________
template<class T>
bool XYVector<T> :: operator<(const XYVector<T>& anotherVector){
size_t minSize = std::min(this->sz, anotherVector.sz);
for(int i = 0; i < minSize; ++i){
if(data[i] < anotherVector.data[i]) return true;
}
return false;
}
template<class T>
bool XYVector<T> :: operator>(const XYVector<T>& anotherVector){
return ((*this != anotherVector) && !(*this < anotherVector));
}
//___________________Size Getter_______________________
template<class T>
size_t XYVector<T> :: Size() const{
return sz;
}
//__________________________________________
// ___________________Capacity Getter_______________________
template<class T>
size_t XYVector<T> :: Capacity() const{
return capacity;
}
//__________________________________________
// ___________________Check Empty_______________________
template<class T>
bool XYVector<T> :: empty(){
return this->sz == 0;
}
//__________________________________________
//________________PushBack__________________________
template<class T>
void XYVector<T> :: push_back(const T& value){
if(sz >= capacity){
this->reAllocate(capacity + capacity / 2);
}
data[sz++] = value;
}
//__________________________________________
//________________PopBack__________________________
template<class T>
T XYVector<T> :: pop_back(){
this->sz--;
return data[this->sz];
}
//__________________________________________
//________________Insert__________________________
template<class T>
void XYVector<T> ::insert(Iterator& it, T newValue) {
try{
if(it >= &data[this->sz+1]) {
throw std::invalid_argument("OUT of INDEX");
}
//tmp 100 5 2 3 5
this->sz += 1;
for(int i = this->sz-1; i >= 0; --i){
data[i+1] = data[i];
if(&data[i] == it){
data[i] = newValue;
it -= i;
return;
}
}
}
catch (std::invalid_argument& error){
std::cerr << error.what() << std::endl;
}
}
//__________________________________________
//________________Clear__________________________
template<class T>
void XYVector<T> :: clear(){
this->sz = 0;
this->capacity = 0;
this->data = nullptr;
}
//__________________________________________
//________________Erase__________________________
template<class T>
void XYVector<T> :: erase(Iterator& it){
try{
if(it >= &data[this->sz]) {
throw std::invalid_argument("OUT of INDEX");
}
for(int i = 0; i < this->sz; ++i){
if(&data[i] == it){
for(int j = i; j < this->sz; ++j){
if(j == this->sz-1){
it = this->begin();
this->pop_back();
return;
}
std::swap(data[j+1], data[j]);
}
}
}
}
catch (std::invalid_argument& error){
std::cerr << error.what() << std::endl;
}
}
//__________________________________________
// ________________Erase__________________________
template<class T>
void XYVector<T> :: erase(Iterator& it1, Iterator& it2){
try{
if(it2 >= &data[this->sz]|| it1 > it2) {
throw std::invalid_argument("OUT of INDEX");
}
int index1, index2;
for(int i = 0; i < this->sz; ++i){
if(&data[i] == it1){
index1 = i;
}
if(&data[i] == it2){
index2 = i;
}
}
for(int i = index2-1; i >= index1; --i){
std::swap(data[i+1], data[i]);
}
// for(int j = 0; j <this->sz; ++j){
// std::cout << data[j] << " ";
// }
// std::cout << std::endl;
for(int i = this->sz-1; i > index2; --i){
for(int j = this->sz-2; j > index2 - (index2 - index1); --j){
std::swap(data[j+1], data[j]);
}
// for(int j = 0; j <this->sz; ++j){
// std::cout << data[j] << " ";
// }
// std::cout << std::endl;
}
for(int i = 0; i < index2 - index1; ++i){
this->pop_back();
}
it1 -= (index2 - index1 + 1);
it2 -= (index2 - index1 + 1);
}
catch (std::invalid_argument& error){
std::cerr << error.what() << std::endl;
}
}
//__________________________________________
//__________________Reallocate Memory________________________
template<class T>
void XYVector<T> :: reAllocate(size_t newCapacity){
T* newData = new T[newCapacity];
if(newCapacity < this->sz)
this->sz = newCapacity;
for(size_t i = 0; i < this->sz; ++i){
newData[i] = std::move(data[i]);
}
delete[] this->data;
this->data = newData;
this->capacity = newCapacity;
newData = nullptr;
}
//_________________Print Function_________________________
template<class U>
std::ostream& operator<<(std::ostream& out, XYVector<U> data){
for(size_t i = 0; i < data.Size(); ++i){
out << data[i] << " ";
}
out << std::endl;
return out;
}