SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLVector< T, U > Class Template Reference

Template class for dynamic vector. More...

#include <SLVector.h>

Public Member Functions

 SLVector ()
 creates empty array More...
 
 SLVector (SLuint size)
 creates array w. size More...
 
 SLVector (const SLVector &a)
 creates a copy of array a More...
 
virtual ~SLVector ()
 standard destructor More...
 
SLVector< T, U > & operator= (const SLVector &a)
 assignment operator More...
 
SLVector< T, U > & operator= (const SLVector *a)
 assignment operator More...
 
T & operator[] (SLuint i)
 access operator More...
 
void set (const SLVector &a)
 set array with another More...
 
size ()
 returns size More...
 
capacity ()
 returns internal size More...
 
void push_back (const T element)
 appends element at end More...
 
void pop_back ()
 deletes element at end More...
 
void erase (U i)
 delete element at pos i More...
 
T & at (SLuint i)
 returns element at pos i More...
 
void reverse ()
 reverses the order More...
 
void clear ()
 deletes all More...
 
void resize (SLuint64 size=0)
 deletes all, sets _size=size More...
 
void reserve (SLuint64 newSize)
 set capacity = newSize More...
 

Private Attributes

_size
 real size of array of type U More...
 
_capacity
 internal size of array of type U More...
 
T * _contents
 pointer to the array of type T More...
 

Detailed Description

template<class T, class U>
class SLVector< T, U >

Template class for dynamic vector.

Implements a minimal dynamic sized array like the STL vector. The array can be of a class type T and can have the max. size of type U. Compatibility is given as long no iterators are used. Bounds checks are only done in _DEBUG mode within the access methods and operators.

Definition at line 28 of file SLVector.h.

Constructor & Destructor Documentation

◆ SLVector() [1/3]

template<class T , class U >
SLVector< T, U >::SLVector

creates empty array

Definition at line 60 of file SLVector.h.

61 {
62  _size = 0;
63  _capacity = 0;
64  _contents = 0;
65 }
U _capacity
internal size of array of type U
Definition: SLVector.h:54
T * _contents
pointer to the array of type T
Definition: SLVector.h:55
U _size
real size of array of type U
Definition: SLVector.h:53

◆ SLVector() [2/3]

template<class T , class U >
SLVector< T, U >::SLVector ( SLuint  size)

creates array w. size

Definition at line 68 of file SLVector.h.

69 {
70  _size = size;
71  _capacity = size;
72  _contents = new T[_capacity];
73 }
U size()
returns size
Definition: SLVector.h:41

◆ SLVector() [3/3]

template<class T , class U >
SLVector< T, U >::SLVector ( const SLVector< T, U > &  a)

creates a copy of array a

Definition at line 76 of file SLVector.h.

77 {
78  _size = 0;
79  _capacity = 0;
80  _contents = 0;
81  set(a);
82 }
void set(const SLVector &a)
set array with another
Definition: SLVector.h:144

◆ ~SLVector()

template<class T , class U >
SLVector< T, U >::~SLVector
virtual

standard destructor

Definition at line 85 of file SLVector.h.

86 {
87  delete[] _contents;
88 }

Member Function Documentation

◆ at()

template<class T , class U >
T & SLVector< T, U >::at ( SLuint  i)
inline

returns element at pos i

Returns the element at position i. Overrun is checked in _DEBUG mode and causes Warning but returns a value so that the caller can be reached.

Definition at line 115 of file SLVector.h.

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 }
#define SL_LOG(...)
Definition: SL.h:233

◆ capacity()

template<class T , class U >
U SLVector< T, U >::capacity ( )
inline

returns internal size

Definition at line 42 of file SLVector.h.

◆ clear()

template<class T , class U >
void SLVector< T, U >::clear ( )
inline

deletes all

Definition at line 48 of file SLVector.h.

◆ erase()

template<class T , class U >
void SLVector< T, U >::erase ( i)

delete element at pos i

Definition at line 221 of file SLVector.h.

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 }
unsigned int SLuint
Definition: SL.h:171
#define SL_EXIT_MSG(message)
Definition: SL.h:240
void pop_back()
deletes element at end
Definition: SLVector.h:281

◆ operator=() [1/2]

template<class T , class U >
SLVector< T, U > & SLVector< T, U >::operator= ( const SLVector< T, U > &  a)

assignment operator

Definition at line 129 of file SLVector.h.

130 {
131  this->set(a);
132  return (*this);
133 }

◆ operator=() [2/2]

template<class T , class U >
SLVector< T, U > & SLVector< T, U >::operator= ( const SLVector< T, U > *  a)

assignment operator

Definition at line 137 of file SLVector.h.

138 {
139  this->set(*a);
140  return (*this);
141 }

◆ operator[]()

template<class T , class U >
T & SLVector< T, U >::operator[] ( SLuint  i)
inline

access operator

The bracket operator as used in arrays. You can use it on the left or right side of =. Overrun is checked in _DEBUG mode and causes Warning but returns a value so that the caller can be reached.

Definition at line 96 of file SLVector.h.

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 }

◆ pop_back()

template<class T , class U >
void SLVector< T, U >::pop_back

deletes element at end

Definition at line 281 of file SLVector.h.

282 {
283  _size--;
284 }

◆ push_back()

template<class T , class U >
void SLVector< T, U >::push_back ( const T  element)

appends element at end

Definition at line 200 of file SLVector.h.

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 }
void reserve(SLuint64 newSize)
set capacity = newSize
Definition: SLVector.h:175

◆ reserve()

template<class T , class U >
void SLVector< T, U >::reserve ( SLuint64  newSize)

set capacity = newSize

Internal, the SLVector is representet by an c++Array not of size _size, but of an internal size. The function reserve changes the internal representation and can make adding much more faster. If s is smaller than the actual size, it will be ignored.

Definition at line 175 of file SLVector.h.

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 }
#define MAXCAPACITY
Definition: SLVector.h:18

◆ resize()

template<class T , class U >
void SLVector< T, U >::resize ( SLuint64  size = 0)

deletes all, sets _size=size

Definition at line 264 of file SLVector.h.

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 }

◆ reverse()

template<class T , class U >
void SLVector< T, U >::reverse

reverses the order

Definition at line 250 of file SLVector.h.

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 }

◆ set()

template<class T , class U >
void SLVector< T, U >::set ( const SLVector< T, U > &  a)

set array with another

Definition at line 144 of file SLVector.h.

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 }

◆ size()

template<class T , class U >
U SLVector< T, U >::size ( )
inline

returns size

Definition at line 41 of file SLVector.h.

Member Data Documentation

◆ _capacity

template<class T , class U >
U SLVector< T, U >::_capacity
private

internal size of array of type U

Definition at line 54 of file SLVector.h.

◆ _contents

template<class T , class U >
T* SLVector< T, U >::_contents
private

pointer to the array of type T

Definition at line 55 of file SLVector.h.

◆ _size

template<class T , class U >
U SLVector< T, U >::_size
private

real size of array of type U

Definition at line 53 of file SLVector.h.


The documentation for this class was generated from the following file: