SavvyUI C++ UI Library
Loading...
Searching...
No Matches
Collections.h
Go to the documentation of this file.
1#pragma once
2
4{
5
6};
7
8template<class T> class Vector
9{
10public:
11 /* ----- Constructors ----- */
12
13 // Default constructor
14 Vector();
15
16 explicit Vector(int s);
17
18 // Copy constructor
19 Vector(const Vector& arg);
20
21 // Copy Assingment
22 Vector<T>& operator=(const Vector<T>& arg);
23
24 // Destructor
25 ~Vector();
26
27 /*----------------------------*/
28
29
30
31
32
33 /* -------- ITERATORS --------*/
34
35 class iterator;
36
38
39 const iterator begin() const;
40
41 iterator end();
42
43 const iterator end() const;
44
45 const iterator cbegin() const;
46
47 const iterator cend() const;
48
49 /*----------------------------*/
50
51
52
53
54
55 /* -------- CAPACITY -------- */
56
57 bool empty() const;
58
59 // Returns size of allocated storate capacity
60 size_t capacity() const;
61
62 // Requests a change in capacity
63 // reserve() will never decrase the capacity.
64 void reserve(int newmalloc);
65
66 // Changes the Vector's size.
67 // If the newsize is smaller, the last elements will be lost.
68 // Has a default value param for custom values when resizing.
69 void resize(int newsize, T val = T());
70
71 // Returns the size of the Vector (number of elements).
72 size_t size() const;
73
74 // Returns the maximum number of elements the Vector can hold
75 size_t max_size() const;
76
77 // Reduces capcity to fit the size
79
80 /*----------------------------*/
81
82
83
84
85
86 /* -------- MODIFIERS --------*/
87
88 // Removes all elements from the Vector
89 // Capacity is not changed.
90 void clear();
91
92 // Inserts element at the back
93 void push_back(const T& d);
94
95 // Removes the item at index
96 void erase(size_t index);
97
98 // Removes the last element from the Vector
99 void pop_back();
100
101 /*----------------------------*/
102
103
104
105
106
107 /* ----- ELEMENT ACCESS ----- */
108
109 // Access elements with bounds checking
110 T& at(int n);
111
112 // Access elements with bounds checking for constant Vectors.
113 const T& at(int n) const;
114
115 // Access elements, no bounds checking
116 T& operator[](int i);
117
118 // Access elements, no bounds checking
119 const T& operator[](int i) const;
120
121 // Returns a reference to the first element
122 T& front();
123
124 // Returns a reference to the first element
125 const T& front() const;
126
127 // Returns a reference to the last element
128 T& back();
129
130 // Returns a reference to the last element
131 const T& back() const;
132
133 // Returns a pointer to the array used by Vector
134 T* data();
135
136 // Returns a pointer to the array used by Vector
137 const T* data() const;
138
139 /*----------------------------*/
140
141private:
142 size_t _size; // Number of elements in Vector
143 T* _elements; // Pointer to first element of Vector
144 size_t _space; // Total space used by Vector including
145 // elements and free space.
146};
147
148
149
150template<class T> class Vector<T>::iterator
151{
152public:
154 :_curr(p)
155 {}
156
158 {
159 _curr++;
160 return *this;
161 }
162
164 {
165 _curr--;
166 return *this;
167 }
168
170 {
171 return *_curr;
172 }
173
174 bool operator==(const iterator& b) const
175 {
176 return *_curr == *b._curr;
177 }
178
179 bool operator!=(const iterator& b) const
180 {
181 return *_curr != *b._curr;
182 }
183
184private:
185 T* _curr;
186};
187
188
189
190// Constructors/Destructor
191template<class T>
193 :_size(0), _elements(0), _space(0)
194{}
195
196template<class T>
197inline Vector<T>::Vector(int s)
198 :_size(s), _elements(new T[s])
199{
200 for (int index = 0; index < _size; ++index)
201 _elements[index] = T();
202}
203
204template<class T>
205inline Vector<T>::Vector(const Vector & arg)
206 :_size(arg._size), _elements(new T[arg._size])
207{
208 for (int index = 0; index < arg._size; ++index)
209 _elements[index] = arg._elements[index];
210}
211
212template<class T>
214{
215 if (this == &a) return *this; // Self-assingment not work needed
216
217 // Current Vector has enough space, so there is no need for new allocation
218 if (a._size <= _space)
219 {
220 for (int index = 0; index < a._size; ++index)
221 {
222 _elements[index] = a._elements[index];
223 _size = a._size;
224 return *this;
225 }
226 }
227
228 T* p = new T[a._size];
229
230 for (int index = 0; index < a._size; ++index)
231 p[index] = a._elements[index];
232
233 delete[] _elements;
234 _size = a._size;
235 _space = a._size;
236 _elements = p;
237 return *this;
238}
239
240template<class T>
242{
243 delete[] _elements;
244}
245
246
247
248// Iterators
249template<class T>
251{
252 return Vector<T>::iterator(&_elements[0]);
253}
254
255template<class T>
256inline typename const Vector<T>::iterator Vector<T>::begin() const
257{
258 return Vector<T>::iterator(&_elements[0]);
259}
260
261template<class T>
263{
264 return Vector<T>::iterator(&_elements[_size]);
265}
266
267template<class T>
268inline typename const Vector<T>::iterator Vector<T>::end() const
269{
270 return Vector<T>::iterator(&_elements[_size]);
271}
272
273template<class T>
274inline typename const Vector<T>::iterator Vector<T>::cbegin() const
275{
276 return Vector<T>::iterator(&_elements[0]);
277}
278
279template<class T>
280inline typename const Vector<T>::iterator Vector<T>::cend() const
281{
282 return Vector<T>::iterator(&_elements[_size]);
283}
284
285
286
287// Capacity
288template<class T>
289inline bool Vector<T>::empty() const
290{
291 return (_size == 0);
292}
293
294template<class T>
295inline size_t Vector<T>::capacity() const
296{
297 return _space;
298}
299
300template<class T>
301inline void Vector<T>::reserve(int newalloc)
302{
303 if (newalloc <= _space) return;
304
305 T* p = new T[newalloc];
306
307 for (int i = 0; i < _size; ++i)
308 p[i] = _elements[i];
309
310 delete[] _elements;
311
312 _elements = p;
313
314 _space = newalloc;
315}
316
317template<class T>
318inline void Vector<T>::resize(int newsize, T val)
319{
320 reserve(newsize);
321
322 for (int index = _size; index < newsize; ++index)
323 _elements[index] = T();
324
325 _size = newsize;
326}
327
328template<class T>
329inline size_t Vector<T>::size() const
330{
331 return _size;
332}
333
334
335
336// Modifiers
337template<class T>
338inline void Vector<T>::push_back(const T& d)
339{
340 if (_space == 0)
341 reserve(8);
342 else if (_size == _space)
343 reserve(2 * _space);
344
345 _elements[_size] = d;
346
347 ++_size;
348}
349
350template<class T>
351inline void Vector<T>::erase(size_t index)
352{
353 if (index >= 0 && index < size())
354 {
355 for(size_t i=index;i<(size()-1);i++)
356 _elements[i] = _elements[i+1];
357
358 _size--;
359 }
360}
361
362
363
364// Accessors
365template<class T>
366inline T & Vector<T>::at(int n)
367{
368 if (n < 0 || _size <= n) throw out_of_range();
369 return _elements[n];
370}
371
372template<class T>
373inline const T & Vector<T>::at(int n) const
374{
375 if (n < 0 || _size <= n) throw out_of_range();
376 return _elements[n];
377}
378
379template<class T>
380inline T & Vector<T>::operator[](int i)
381{
382 return _elements[i];
383}
384
385template<class T>
386inline const T & Vector<T>::operator[](int i) const
387{
388 return _elements[i];
389}
390
391template<class T>
393{
394 return _elements[0];
395}
396
397template<class T>
398inline const T& Vector<T>::front() const
399{
400 return _elements[0];
401}
402
403template<class T>
405{
406 return _elements[_size - 1];
407}
408
409template<class T>
410inline const T& Vector<T>::back() const
411{
412 return _elements[_size - 1];
413}
414
415template<class T>
417{
418 return _elements;
419}
420
421template<class T>
422inline const T* Vector<T>::data() const
423{
424 return _elements;
425}
Definition Collections.h:151
iterator & operator++()
Definition Collections.h:157
iterator(T *p)
Definition Collections.h:153
bool operator!=(const iterator &b) const
Definition Collections.h:179
bool operator==(const iterator &b) const
Definition Collections.h:174
T & operator*()
Definition Collections.h:169
iterator & operator--()
Definition Collections.h:163
T & front()
Definition Collections.h:392
const iterator cend() const
Definition Collections.h:280
const iterator cbegin() const
Definition Collections.h:274
void clear()
void erase(size_t index)
Definition Collections.h:351
Vector()
Definition Collections.h:192
Vector< T > & operator=(const Vector< T > &arg)
Definition Collections.h:213
size_t max_size() const
T * data()
Definition Collections.h:416
void resize(int newsize, T val=T())
Definition Collections.h:318
T & back()
Definition Collections.h:404
size_t capacity() const
Definition Collections.h:295
size_t size() const
Definition Collections.h:329
T & at(int n)
Definition Collections.h:366
iterator end()
Definition Collections.h:262
iterator begin()
Definition Collections.h:250
void shrink_to_fit()
bool empty() const
Definition Collections.h:289
void reserve(int newmalloc)
Definition Collections.h:301
T & operator[](int i)
Definition Collections.h:380
void pop_back()
void push_back(const T &d)
Definition Collections.h:338
~Vector()
Definition Collections.h:241
Definition Collections.h:4