This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.h
270 lines (214 loc) · 5.4 KB
/
Vector.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
#pragma once
#include<iostream>
namespace Vector {
template <typename T>
struct Vector
{
private:
T* vec;
size_t size;
size_t capasity;
const size_t growthFactor = 2;
public:
Vector();
Vector(size_t size);
~Vector();
Vector<T>& operator= (const Vector<T>& v);
T& operator[] (const size_t index);
const T& operator[] (const size_t index) const;
void reset();
void reset(const Vector<T>& v);
void clear();
Vector<T>& pushBack(T data);
Vector<T>& pushBack(const Vector<T>& v);
Vector<T>& popBack(size_t pop_size = 1);
Vector<T>& reverse();
T getDataByIndex(size_t index) const;
size_t getSize() const;
size_t getCapasity() const;
void print() const;
void copy(Vector<T>& v);
bool isEqual(const Vector<T>& v) const;
Vector<T>& slice(Vector<T>& slice, const size_t begin, const size_t end);
Vector<T>& getLastWord(Vector<T>& v) const;
static void fill_vectors(Vector<T>& ov, Vector<T>& ova, Vector<T>& in, Vector<T>& ina, Vector<T>& ev, Vector<T>& eva, Vector<T>& idze);
};
}
template<typename T>
inline Vector::Vector<T>::Vector() {
vec = new T[1];
capasity = 1;
size = 0;
}
template<typename T>
inline Vector::Vector<T>::Vector(size_t size) {
vec = new T[size];
capasity = size;
this->size = size;
}
template<typename T>
inline Vector::Vector<T>::~Vector() {
delete[] vec;
vec = nullptr;
}
template<typename T>
inline Vector::Vector<T>& Vector::Vector<T>::operator=(const Vector<T>& v) {
if (this == &v)
return *this; // Защита от самоприсваивания
this->reset(v);
return *this;
}
template<typename T>
inline T& Vector::Vector<T>::operator[](const size_t index) {
if (index >= 0 and index < size)
return vec[index];
std::cout << "Index Error" << std::endl;
}
template<typename T>
inline const T& Vector::Vector<T>::operator[](const size_t index) const {
if (index >= 0 and index < size)
return vec[index];
std::cout << "Index Error" << std::endl;
}
template<typename T>
inline void Vector::Vector<T>::reset() {
delete[] vec;
vec = nullptr;
}
template<typename T>
inline void Vector::Vector<T>::reset(const Vector<T>& v) {
size = 0;
this->pushBack(v);
}
template<typename T>
inline void Vector::Vector<T>::clear() {
size = 0;
}
template<typename T>
inline Vector::Vector<T>& Vector::Vector<T>::pushBack(T data) {
if (size == capasity) {
T* temp = new T[capasity * growthFactor];
for (size_t i{}; i < size; i++) {
temp[i] = vec[i];
}
delete[] vec;
capasity *= growthFactor;
vec = temp;
}
vec[size] = data;
size++;
return *this;
}
template<typename T>
inline Vector::Vector<T>& Vector::Vector<T>::pushBack(const Vector<T>& v) {
if (v.getSize() == 0)
return *this;
for (size_t i{}; i < v.getSize(); i++) {
this->pushBack(v[i]);
}
return *this;
}
template<typename T>
inline Vector::Vector<T>& Vector::Vector<T>::popBack(size_t pop_size) {
size -= pop_size;
return *this;
}
template<typename T>
inline Vector::Vector<T>& Vector::Vector<T>::reverse() {
size_t half = static_cast<size_t>(size / 2);
for (size_t i{}; i < half; i++) {
std::swap(vec[i], vec[size - 1 - i]);
}
return *this;
}
template<typename T>
inline T Vector::Vector<T>::getDataByIndex(size_t index) const {
if (index >= 0 and index < size)
return vec[index];
std::cout << "Index Error" << std::endl;
}
template<typename T>
inline size_t Vector::Vector<T>::getSize() const {
return size;
}
template<typename T>
inline size_t Vector::Vector<T>::getCapasity() const {
return capasity;
}
template<typename T>
inline void Vector::Vector<T>::print() const {
for (size_t i{}; i < size; i++) {
std::cout << vec[i];
}
std::cout << std::endl;
}
template<typename T>
inline void Vector::Vector<T>::copy(Vector<T>& v) {
if (v.getSize() == 0) {
for (size_t i{}; i < size; i++) {
v.pushBack(vec[i]);
}
}
else
std::cout << "Vector is not empty" << std::endl;
}
template<typename T>
inline bool Vector::Vector<T>::isEqual(const Vector<T>& v) const {
if (v.getSize() != size)
return false;
size_t counter{};
for (size_t i{}; i < size; i++) {
if (vec[i] == v[i])
counter++;
}
if (counter == size)
return true;
return false;
}
template<typename T>
inline Vector::Vector<T>& Vector::Vector<T>::slice(Vector<T>& slice, const size_t begin, const size_t end) {
if (slice.getSize() == 0) {
for (size_t i = begin; i < end; i++) {
slice.pushBack((*this)[i]);
}
return slice;
}
std::cout << "Vector is not empty" << std::endl;
return *this;
}
template<typename T>
inline Vector::Vector<T>& Vector::Vector<T>::getLastWord(Vector<T>& v) const {
if (v.getSize() == 0 and this->getSize() > 1) {
for (size_t elem = this->getSize() - 2; (*this)[elem] != ' '; elem--) {
v.pushBack((*this)[elem]);
if (elem == 0)
break;
}
v.reverse();
return v;
}
std::cout << "Source Vector is empty or destination Vector is not empty" << std::endl;
return v;
}
template<typename T>
inline void Vector::Vector<T>::fill_vectors(Vector<T>& ov, Vector<T>& ova, Vector<T>& in, Vector<T>& ina, Vector<T>& ev, Vector<T>& eva, Vector<T>& idze) {
ov.pushBack('o');
ov.pushBack('v');
ova.pushBack('o');
ova.pushBack('v');
ova.pushBack('a');
in.pushBack('i');
in.pushBack('n');
ina.pushBack('i');
ina.pushBack('n');
ina.pushBack('a');
ev.pushBack('e');
ev.pushBack('v');
eva.pushBack('e');
eva.pushBack('v');
eva.pushBack('a');
idze.pushBack('i');
idze.pushBack('d');
idze.pushBack('z');
idze.pushBack('e');
}