-
Notifications
You must be signed in to change notification settings - Fork 19
/
Vector.h
411 lines (290 loc) · 6.61 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#pragma once
struct out_of_range
{
};
template<class T> class Vector
{
public:
/* ----- Constructors ----- */
// Default constructor
Vector();
explicit Vector(int s);
// Copy constructor
Vector(const Vector& arg);
// Copy Assingment
Vector<T>& operator=(const Vector<T>& arg);
// Destructor
~Vector();
/*----------------------------*/
/* -------- ITERATORS --------*/
class iterator;
iterator begin();
const iterator begin() const;
iterator end();
const iterator end() const;
const iterator cbegin() const;
const iterator cend() const;
/*----------------------------*/
/* -------- CAPACITY -------- */
bool empty() const;
// Returns size of allocated storate capacity
size_t capacity() const;
// Requests a change in capacity
// reserve() will never decrase the capacity.
void reserve(int newmalloc);
// Changes the Vector's size.
// If the newsize is smaller, the last elements will be lost.
// Has a default value param for custom values when resizing.
void resize(int newsize, T val = T());
// Returns the size of the Vector (number of elements).
size_t size() const;
// Returns the maximum number of elements the Vector can hold
size_t max_size() const;
// Reduces capcity to fit the size
void shrink_to_fit();
/*----------------------------*/
/* -------- MODIFIERS --------*/
// Removes all elements from the Vector
// Capacity is not changed.
void clear();
// Inserts element at the back
void push_back(const T& d);
// Removes the last element from the Vector
void pop_back();
/*----------------------------*/
/* ----- ELEMENT ACCESS ----- */
// Access elements with bounds checking
T& at(int n);
// Access elements with bounds checking for constant Vectors.
const T& at(int n) const;
// Access elements, no bounds checking
T& operator[](int i);
// Access elements, no bounds checking
const T& operator[](int i) const;
// Returns a reference to the first element
T& front();
// Returns a reference to the first element
const T& front() const;
// Returns a reference to the last element
T& back();
// Returns a reference to the last element
const T& back() const;
// Returns a pointer to the array used by Vector
T* data();
// Returns a pointer to the array used by Vector
const T* data() const;
/*----------------------------*/
private:
size_t _size; // Number of elements in Vector
T* _elements; // Pointer to first element of Vector
size_t _space; // Total space used by Vector including
// elements and free space.
};
template<class T> class Vector<T>::iterator
{
public:
iterator(T* p)
:_curr(p)
{}
iterator& operator++()
{
_curr++;
return *this;
}
iterator& operator--()
{
_curr--;
return *this;
}
T& operator*()
{
return *_curr;
}
bool operator==(const iterator& b) const
{
return *_curr == *b._curr;
}
bool operator!=(const iterator& b) const
{
return *_curr != *b._curr;
}
private:
T* _curr;
};
// Constructors/Destructor
template<class T>
Vector<T>::Vector()
:_size(0), _elements(0), _space(0)
{}
template<class T>
inline Vector<T>::Vector(int s)
:_size(s), _elements(new T[s], _space(s))
{
for (int index = 0; index < _size; ++index)
_elements[index] = T();
}
template<class T>
inline Vector<T>::Vector(const Vector & arg)
:_size(arg._size), _elements(new T[arg._size])
{
for (int index = 0; index < arg._size; ++index)
_elements[index] = arg._elements[index];
}
template<class T>
inline Vector<T>& Vector<T>::operator=(const Vector<T>& a)
{
if (this == &a) return *this; // Self-assingment not work needed
// Current Vector has enough space, so there is no need for new allocation
if (a._size <= _space)
{
for (int index = 0; index < a._size; ++index)
{
_elements[index] = a._elements[index];
_size = a._size;
return *this;
}
}
T* p = new T[a._size];
for (int index = 0; index < a._size; ++index)
p[index] = a._elements[index];
delete[] _elements;
_size = a._size;
_space = a._size;
_elements = p;
return *this;
}
template<class T>
Vector<T>::~Vector()
{
delete[] _elements;
}
// Iterators
template<class T>
inline typename Vector<T>::iterator Vector<T>::begin()
{
return Vector<T>::iterator(&_elements[0]);
}
template<class T>
inline typename const Vector<T>::iterator Vector<T>::begin() const
{
return Vector<T>::iterator(&_elements[0]);
}
template<class T>
inline typename Vector<T>::iterator Vector<T>::end()
{
return Vector<T>::iterator(&_elements[_size]);
}
template<class T>
inline typename const Vector<T>::iterator Vector<T>::end() const
{
return Vector<T>::iterator(&_elements[_size]);
}
template<class T>
inline typename const Vector<T>::iterator Vector<T>::cbegin() const
{
return Vector<T>::iterator(&_elements[0]);
}
template<class T>
inline typename const Vector<T>::iterator Vector<T>::cend() const
{
return Vector<T>::iterator(&_elements[_size]);
}
// Capacity
template<class T>
inline bool Vector<T>::empty() const
{
return (_size == 0);
}
template<class T>
inline size_t Vector<T>::capacity() const
{
return _space;
}
template<class T>
inline void Vector<T>::reserve(int newalloc)
{
if (newalloc <= _space) return;
T* p = new T[newalloc];
for (int i = 0; i < _size; ++i)
p[i] = _elements[i];
delete[] _elements;
_elements = p;
_space = newalloc;
}
template<class T>
inline void Vector<T>::resize(int newsize, T val)
{
reserve(newsize);
for (int index = _size; index < newsize; ++index)
_elements[i] = T();
_size = newsize;
}
template<class T>
inline size_t Vector<T>::size() const
{
return _size;
}
// Modifiers
template<class T>
inline void Vector<T>::push_back(const T& d)
{
if (_space == 0)
reserve(8);
else if (_size == _space)
reserve(2 * _space);
_elements[_size] = d;
++_size;
}
// Accessors
template<class T>
inline T & Vector<T>::at(int n)
{
if (n < 0 || _size <= n) throw out_of_range();
return _elements[n];
}
template<class T>
inline const T & Vector<T>::at(int n) const
{
if (n < 0 || _size <= n) throw out_of_range();
return _elements[n];
}
template<class T>
inline T & Vector<T>::operator[](int i)
{
return _elements[i];
}
template<class T>
inline const T & Vector<T>::operator[](int i) const
{
return _elements[i];
}
template<class T>
inline T& Vector<T>::front()
{
return _elements[0];
}
template<class T>
inline const T& Vector<T>::front() const
{
return _elements[0];
}
template<class T>
inline T& Vector<T>::back()
{
return _elements[_size - 1];
}
template<class T>
inline const T& Vector<T>::back() const
{
return _elements[_size - 1];
}
template<class T>
inline T* Vector<T>::data()
{
return _elements;
}
template<class T>
inline const T* Vector<T>::data() const
{
return _elements;
}