-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.h
executable file
·339 lines (307 loc) · 7.44 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
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
#ifndef H_VECTOR
#define H_VECTOR
// #define DEBUG
#include <assert.h>
#include <algorithm>
#include <iostream>
template <class T>
class Vector {
public:
using value_type = T;
using size_type = size_t;
using reference = T&;
using const_reference = const T&;
using pointer = T*;
using const_pointer = const T*;
using iterator = T*;
using const_iterator = const T*;
Vector();
explicit Vector(size_type size);
Vector(const Vector<T>& another);
~Vector();
void swap(Vector<T>& another) noexcept;
Vector<T>& operator=(const Vector<T>& another);
reference operator[](size_type pos);
reference at(size_type pos);
reference front();
reference back();
bool empty() const;
size_type size() const;
size_type capacity() const;
void reserve(size_type new_cap);
void resize(size_type count);
void resize(size_type count, const value_type& value);
void clear();
void push_back(const T& value);
void pop_back();
iterator begin();
iterator end();
iterator cbegin() const;
iterator cend() const;
void DUMP();
private:
static const size_type initSize = 4;
value_type* buffer;
size_type bufferSize;
size_type elementsCount;
void is_OK() const;
void DUMP();
};
template <class T>
Vector<T>::Vector() : bufferSize(initSize), elementsCount(0) {
try {
buffer = new value_type[bufferSize];
#ifdef DEBUG
std::cout << "INIT VECTOR\n";
DUMP();
#endif
} catch (std::bad_alloc& e) {
std::cout << "allocation failed\n";
throw e;
}
}
template <class T>
Vector<T>::Vector(size_type size) : bufferSize(size), elementsCount(0) {
if (size < initSize) bufferSize = initSize;
try {
buffer = new value_type[bufferSize];
#ifdef DEBUG
std::cout << "INIT VECTOR\n";
DUMP();
#endif
} catch (std::bad_alloc& e) {
std::cout << "allocation failed\n";
throw e;
}
}
template <class T>
Vector<T>::~Vector() {
is_OK();
#ifdef DEBUG
DUMP();
#endif
delete[] buffer;
#ifdef DEBUG
std::cout << "VECTOR DELETED\n"
<< "deletion address: " << buffer << "\n";
#endif
}
template <class T>
Vector<T>::Vector(const Vector<T>& another)
: bufferSize(initSize), elementsCount(0) {
bufferSize = another.capacity();
elementsCount = another.size();
buffer = new value_type[bufferSize];
size_type i = 0;
for (iterator it = another.cbegin();
it != another.cend() && i < elementsCount; it++) {
buffer[i] = (*it);
i++;
}
#ifdef DEBUG
std::cout << "INIT VECTOR\n";
DUMP();
#endif
}
template <class T>
void Vector<T>::swap(Vector<T>& another) noexcept {
for (size_t it = 0; it < size(); it++) {
std::swap(at(it), another.at(it));
}
}
template <class T>
Vector<T>& Vector<T>::operator=(const Vector<T>& another) {
is_OK();
reserve(another.capacity());
resize(another.size());
Vector<T> temp(another);
this->swap(temp);
return *this;
}
// -------------------------------------
template <class T>
typename Vector<T>::reference Vector<T>::operator[](size_type pos) {
is_OK();
return buffer[pos];
}
template <class T>
typename Vector<T>::reference Vector<T>::at(size_type pos) {
is_OK();
try {
if (pos >= elementsCount)
throw(std::out_of_range("error: position is out of range\n"));
return buffer[pos];
} catch (std::out_of_range& e) {
std::cout << e.what();
throw e;
}
}
template <class T>
typename Vector<T>::reference Vector<T>::front() {
is_OK();
try {
if (elementsCount == 0)
throw(std::length_error("error: vector is empty\n"));
return buffer[0];
} catch (std::length_error& e) {
std::cout << e.what();
throw e;
}
}
template <class T>
typename Vector<T>::reference Vector<T>::back() {
is_OK();
try {
if (elementsCount == 0)
throw(std::length_error("error: vector is empty\n"));
return buffer[elementsCount - 1];
} catch (std::length_error& e) {
std::cout << e.what();
throw e;
}
}
template <class T>
bool Vector<T>::empty() const {
is_OK();
return (elementsCount == 0);
}
template <class T>
typename Vector<T>::size_type Vector<T>::size() const {
is_OK();
return (elementsCount);
}
template <class T>
typename Vector<T>::size_type Vector<T>::capacity() const {
is_OK();
return (bufferSize);
}
template <class T>
void Vector<T>::reserve(size_type new_cap) {
is_OK();
if (new_cap <= bufferSize) return;
value_type* bufferCopy = new value_type[new_cap];
for (size_type i = 0; i < bufferSize; i++) {
bufferCopy[i] = buffer[i];
}
delete[] buffer;
buffer = bufferCopy;
bufferSize = new_cap;
#ifdef DEBUG
DUMP();
#endif
}
template <class T>
void Vector<T>::resize(size_type count) {
is_OK();
if (count <= bufferSize) {
elementsCount = count;
return;
}
size_type countToReserve = count < bufferSize * 2 ? bufferSize * 2 : count;
reserve(countToReserve);
elementsCount = count;
#ifdef DEBUG
DUMP();
#endif
}
template <class T>
void Vector<T>::resize(size_type count, const value_type& value) {
is_OK();
if (count <= bufferSize) {
elementsCount = count;
return true;
}
size_type countToReserve = count < bufferSize * 2 ? bufferSize * 2 : count;
if (!reserve(countToReserve)) throw(-1);
for (size_type i = elementsCount; i < count; i++) {
buffer[i] = value;
}
elementsCount = count;
#ifdef DEBUG
DUMP();
#endif
}
template <class T>
void Vector<T>::clear() {
is_OK();
elementsCount = 0;
}
template <class T>
void Vector<T>::push_back(const T& value) {
#ifdef DEBUG
std::cout << "push back " << value << "\n";
#endif
is_OK();
if (elementsCount + 1 > bufferSize) {
try {
reserve(bufferSize * 2);
} catch (std::bad_alloc& e) {
std::cout << "error: reached memory limit\n";
DUMP();
throw e;
}
}
buffer[elementsCount] = value;
elementsCount++;
}
template <class T>
void Vector<T>::pop_back() {
std::cout << "call pop back\n";
is_OK();
try {
if (elementsCount == 0) {
throw(-1);
DUMP();
}
elementsCount--;
} catch (int) {
std::cout << "error: vector is empty\n";
}
}
template <class T>
typename Vector<T>::iterator Vector<T>::begin() {
is_OK();
return buffer;
}
template <class T>
typename Vector<T>::iterator Vector<T>::end() {
is_OK();
return buffer + elementsCount;
}
template <class T>
typename Vector<T>::iterator Vector<T>::cbegin() const {
is_OK();
return buffer;
}
template <class T>
typename Vector<T>::iterator Vector<T>::cend() const {
is_OK();
return buffer + elementsCount;
}
template <class T>
void Vector<T>::is_OK() const {
assert(bufferSize > 0);
assert(elementsCount <= bufferSize);
assert(elementsCount >= 0);
assert(buffer != NULL);
}
template <class T>
void Vector<T>::DUMP() {
is_OK();
std::cout << "\n-----printing the vector:-----\n";
std::cout << "this: " << this << "\n";
std::cout << "capacity = " << capacity() << "\n";
std::cout << size() << " elements, " << empty() << " empty\n";
if (!empty()) {
for (iterator it = begin(); it != end(); it++) {
std::cout << (*it) << " ";
}
std::cout << "\nrepeat:\n";
for (size_type i = 0; i < elementsCount; i++) {
std::cout << at(i) << " ";
}
std::cout << "\n";
}
std::cout << "-----vector is printed-----\n\n";
}
#endif