SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLVector.h
Go to the documentation of this file.
1 /**
2  * \file SLVector.h
3  * \authors Jonas Lottner, Marcus Hudritsch
4  * \date July 2014
5  * \authors Institut fr Informatik, FHBB Muttenz, Switzerland
6  * \copyright http://opensource.org/licenses/GPL-3.0
7  * \remarks Please use clangformat to format the code. See more code style on
8  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
9 */
10 
11 #ifndef SLVECTOR_H
12 #define SLVECTOR_H
13 
14 #include <SL.h>
15 #include <SLMath.h>
16 
17 //-----------------------------------------------------------------------------
18 #define MAXCAPACITY (SLint)(pow(2.0f, (SLfloat)(sizeof(U) * 8))) - 1
19 //-----------------------------------------------------------------------------
20 //! Template class for dynamic vector
21 /*!
22 Implements a minimal dynamic sized array like the STL vector.
23 The array can be of a class type T and can have the max. size of type U.
24 Compatibility is given as long no iterators are used. Bounds checks are only
25 done in _DEBUG mode within the access methods and operators.
26 */
27 template<class T, class U>
28 class SLVector
29 {
30 public:
31  SLVector(); //!< creates empty array
32  SLVector(SLuint size); //!< creates array w. size
33  SLVector(const SLVector& a); //!< creates a copy of array a
34  virtual ~SLVector(); //!< standard destructor
35 
36  SLVector<T, U>& operator=(const SLVector& a); //!< assignment operator
37  SLVector<T, U>& operator=(const SLVector* a); //!< assignment operator
38  inline T& operator[](SLuint i); //!< access operator
39 
40  void set(const SLVector& a); //!< set array with another
41  U size() { return _size; } //!< returns size
42  U capacity() { return _capacity; } //!< returns internal size
43  void push_back(const T element); //!< appends element at end
44  void pop_back(); //!< deletes element at end
45  void erase(U i); //!< delete element at pos i
46  inline T& at(SLuint i); //!< returns element at pos i
47  void reverse(); //!< reverses the order
48  void clear() { resize(0); } //!< deletes all
49  void resize(SLuint64 size = 0); //!< deletes all, sets _size=size
50  void reserve(SLuint64 newSize); //!< set capacity = newSize
51 
52 private:
53  U _size; //!< real size of array of type U
54  U _capacity; //!< internal size of array of type U
55  T* _contents; //!< pointer to the array of type T
56 };
57 
58 //-----------------------------------------------------------------------------
59 template<class T, class U>
61 {
62  _size = 0;
63  _capacity = 0;
64  _contents = 0;
65 }
66 //-----------------------------------------------------------------------------
67 template<class T, class U>
69 {
70  _size = size;
71  _capacity = size;
72  _contents = new T[_capacity];
73 }
74 //-----------------------------------------------------------------------------
75 template<class T, class U>
77 {
78  _size = 0;
79  _capacity = 0;
80  _contents = 0;
81  set(a);
82 }
83 //-----------------------------------------------------------------------------
84 template<class T, class U>
86 {
87  delete[] _contents;
88 }
89 //-----------------------------------------------------------------------------
90 /*!
91 The bracket operator as used in arrays. You can use it on the left or right
92 side of =. Overrun is checked in _DEBUG mode and causes Warning but returns
93 a value so that the caller can be reached.
94 */
95 template<class T, class U>
97 {
98 #ifdef _DEBUG
99  if (i >= _size)
100  {
101  SL_LOG("SLVector::operator[]: Index >= size! Overflow! Argh!");
102  return _contents[_size - 1]; // return something for debug
103  }
104 #endif
105  return _contents[i];
106 }
107 //-----------------------------------------------------------------------------
108 /*!
109 Returns the element at position i.
110 Overrun is checked in _DEBUG mode and causes Warning but returns
111 a value so that the caller can be reached.
112 */
113 template<class T, class U>
114 inline T&
116 {
117 #ifdef _DEBUG
118  if (i >= _size)
119  {
120  SL_LOG("SLVector::operator[]: Index >= size! Overflow! Argh!");
121  return _contents[_size - 1]; // return something for debug
122  }
123 #endif
124  return _contents[i];
125 }
126 //-----------------------------------------------------------------------------
127 template<class T, class U>
130 {
131  this->set(a);
132  return (*this);
133 }
134 //-----------------------------------------------------------------------------
135 template<class T, class U>
138 {
139  this->set(*a);
140  return (*this);
141 }
142 //-----------------------------------------------------------------------------
143 template<class T, class U>
145 {
146  SLuint i;
147  if (&a != this)
148  {
149  T* temp = 0;
150 
151  if (_capacity > 0)
152  delete[] _contents;
153 
154  _size = a._size;
155  _capacity = _size;
156  _contents = 0;
157 
158  if (_capacity > 0)
159  {
160  temp = new T[_capacity];
161  for (i = 0; i < _capacity; ++i)
162  temp[i] = a._contents[i];
163  _contents = temp;
164  }
165  }
166 }
167 //-----------------------------------------------------------------------------
168 /*!
169 Internal, the SLVector is representet by an c++Array not of size _size, but of
170 an internal size. The function reserve changes the internal representation and
171 can make adding much more faster. If s is smaller than the actual size, it
172 will be ignored.
173 */
174 template<class T, class U>
176 {
177  T* temp;
178  SLuint i;
179 
180 #ifdef _DEBUG
181  if (newSize > MAXCAPACITY)
182  {
183  SL_LOG("SLVector::reserve: newSize > Max. capacity");
184  newSize = MAXCAPACITY;
185  }
186 #endif
187 
188  if ((U)newSize >= _size)
189  {
190  _capacity = (U)newSize;
191  temp = new T[_capacity];
192  for (i = 0; i < _size; i++)
193  temp[i] = _contents[i];
194  delete[] _contents;
195  _contents = temp;
196  }
197 }
198 //-----------------------------------------------------------------------------
199 template<class T, class U>
200 void SLVector<T, U>::push_back(const T element)
201 {
202  if (_capacity != 0)
203  {
204  if (_size >= _capacity)
205  {
206  reserve(_capacity * 2);
207  }
208  _contents[_size] = element;
209  _size++;
210  }
211  else
212  {
213  _capacity = 2;
214  _size = 1;
215  _contents = new T[_capacity];
216  _contents[0] = element;
217  }
218 }
219 //-----------------------------------------------------------------------------
220 template<class T, class U>
222 {
223  if (index >= _size)
224  {
225  SL_EXIT_MSG("SLVector::erase(SLuint index): Index out of range!");
226  }
227  else if (index == _size - 1)
228  {
229  pop_back();
230  }
231  else
232  {
233  SLuint j;
234  _size--;
235  _capacity = _size;
236  T* temp = new T[_capacity];
237  for (j = 0; j < _size; ++j)
238  {
239  if (j < index)
240  temp[j] = _contents[j];
241  else
242  temp[j] = _contents[j + 1];
243  }
244  delete[] _contents;
245  _contents = temp;
246  }
247 }
248 //-----------------------------------------------------------------------------
249 template<class T, class U>
251 {
252  SLuint i;
253  T temp;
254 
255  for (i = 0; i < (_size / 2); i++)
256  {
257  temp = _contents[i];
258  _contents[i] = _contents[_size - i - 1];
259  _contents[_size - i - 1] = temp;
260  }
261 }
262 //-----------------------------------------------------------------------------
263 template<class T, class U>
265 {
266 #ifdef _DEBUG
267  if (newSize > MAXCAPACITY)
268  {
269  SL_LOG("SLVector::reserve: newSize > Max. capacity");
270  newSize = MAXCAPACITY;
271  }
272 #endif
273  _size = (U)newSize;
274  _capacity = (U)newSize;
275  delete[] _contents;
276  _contents = 0;
277  if (_capacity) _contents = new T[_capacity];
278 }
279 //-----------------------------------------------------------------------------
280 template<class T, class U>
282 {
283  _size--;
284 }
285 //-----------------------------------------------------------------------------
286 #endif
#define SL_LOG(...)
Definition: SL.h:233
uint64_t SLuint64
Definition: SL.h:186
unsigned int SLuint
Definition: SL.h:171
#define SL_EXIT_MSG(message)
Definition: SL.h:240
#define MAXCAPACITY
Definition: SLVector.h:18
Template class for dynamic vector.
Definition: SLVector.h:29
virtual ~SLVector()
standard destructor
Definition: SLVector.h:85
U size()
returns size
Definition: SLVector.h:41
U _capacity
internal size of array of type U
Definition: SLVector.h:54
void push_back(const T element)
appends element at end
Definition: SLVector.h:200
void resize(SLuint64 size=0)
deletes all, sets _size=size
Definition: SLVector.h:264
T * _contents
pointer to the array of type T
Definition: SLVector.h:55
void clear()
deletes all
Definition: SLVector.h:48
U _size
real size of array of type U
Definition: SLVector.h:53
void erase(U i)
delete element at pos i
Definition: SLVector.h:221
void reverse()
reverses the order
Definition: SLVector.h:250
void reserve(SLuint64 newSize)
set capacity = newSize
Definition: SLVector.h:175
void pop_back()
deletes element at end
Definition: SLVector.h:281
T & at(SLuint i)
returns element at pos i
Definition: SLVector.h:115
SLVector()
creates empty array
Definition: SLVector.h:60
U capacity()
returns internal size
Definition: SLVector.h:42
SLVector< T, U > & operator=(const SLVector &a)
assignment operator
Definition: SLVector.h:129
T & operator[](SLuint i)
access operator
Definition: SLVector.h:96
void set(const SLVector &a)
set array with another
Definition: SLVector.h:144
void set(std::string path, const std::vector< char > &data)
Definition: SLIOMemory.cpp:29