NL3D::CSnappedVector< T, snapPower > Class Template Reference

#include <ps_attrib.h>


Detailed Description

template<class T, const uint snapPower = 5>
class NL3D::CSnappedVector< T, snapPower >

a container that is like a vector, but snapped to (1<<snapPower) byte memory pages

Definition at line 45 of file ps_attrib.h.

Public Types

typedef const T * const_iterator
typedef T * iterator
typedef T value_type

Public Member Functions

const T & back () const
T & back ()
const_iterator begin (void) const
iterator begin (void)
uint capacity () const
void clear ()
 CSnappedVector ()
bool empty () const
const_iterator end (void) const
iterator end (void)
const T & operator[] (uint index) const
T & operator[] (uint index)
void pop_back ()
void push_back (const T &t)
void reserve (uint capacity)
 set a new usable size

void resize (uint size)
void serial (NLMISC::IStream &f) throw (NLMISC::EStream)
 serialization

uint size () const
 ~CSnappedVector ()

Protected Attributes

uint32 _Capacity
uint32 _Size
uint8_Start
T * _Tab


Member Typedef Documentation

template<class T, const uint snapPower = 5>
typedef const T* NL3D::CSnappedVector< T, snapPower >::const_iterator
 

Definition at line 49 of file ps_attrib.h.

Referenced by NL3D::CSnappedVector< T, snapPower >::begin(), and NL3D::CSnappedVector< T, snapPower >::end().

template<class T, const uint snapPower = 5>
typedef T* NL3D::CSnappedVector< T, snapPower >::iterator
 

Definition at line 48 of file ps_attrib.h.

Referenced by NL3D::CSnappedVector< T, snapPower >::begin(), NL3D::CSnappedVector< T, snapPower >::end(), NL3D::CSnappedVector< T, snapPower >::reserve(), NL3D::CSnappedVector< T, snapPower >::resize(), and NL3D::CSnappedVector< T, snapPower >::~CSnappedVector().

template<class T, const uint snapPower = 5>
typedef T NL3D::CSnappedVector< T, snapPower >::value_type
 

Definition at line 50 of file ps_attrib.h.


Constructor & Destructor Documentation

template<class T, const uint snapPower = 5>
NL3D::CSnappedVector< T, snapPower >::CSnappedVector  )  [inline]
 

Definition at line 52 of file ps_attrib.h.

References NL3D::CSnappedVector< T, snapPower >::_Start, and NL3D::CSnappedVector< T, snapPower >::_Tab.

00052 : _Size(0), _Capacity(0), _Start(NULL), _Tab(NULL) {}

template<class T, const uint snapPower = 5>
NL3D::CSnappedVector< T, snapPower >::~CSnappedVector  )  [inline]
 

Definition at line 53 of file ps_attrib.h.

References NL3D::CSnappedVector< T, snapPower >::_Start, NL3D::CSnappedVector< T, snapPower >::_Tab, NL3D::CSnappedVector< T, snapPower >::iterator, and nlassert.

00054         { 
00055                 nlassert(_Size <= _Capacity);
00056                 for (iterator it = _Tab, endIt = _Tab + _Size; it != endIt; ++it)
00057                 {
00058                         it->~T();
00059                 }
00060                 delete _Start; 
00061         }


Member Function Documentation

template<class T, const uint snapPower = 5>
const T& NL3D::CSnappedVector< T, snapPower >::back  )  const [inline]
 

Definition at line 90 of file ps_attrib.h.

References NL3D::CSnappedVector< T, snapPower >::_Tab, and nlassert.

00091         { 
00092                 #ifdef NL_DEBUG
00093                         nlassert(_Size > 0);
00094                 #endif
00095                 return _Tab[_Size - 1];
00096         }

template<class T, const uint snapPower = 5>
T& NL3D::CSnappedVector< T, snapPower >::back  )  [inline]
 

Definition at line 82 of file ps_attrib.h.

References NL3D::CSnappedVector< T, snapPower >::_Tab, and nlassert.

00083         { 
00084                 #ifdef NL_DEBUG
00085                         nlassert(_Size > 0);
00086                 #endif
00087                 return _Tab[_Size - 1];
00088         }

template<class T, const uint snapPower = 5>
const_iterator NL3D::CSnappedVector< T, snapPower >::begin void   )  const [inline]
 

Definition at line 63 of file ps_attrib.h.

References NL3D::CSnappedVector< T, snapPower >::_Tab, and NL3D::CSnappedVector< T, snapPower >::const_iterator.

00063 { return _Tab; }

template<class T, const uint snapPower = 5>
iterator NL3D::CSnappedVector< T, snapPower >::begin void   )  [inline]
 

Definition at line 62 of file ps_attrib.h.

References NL3D::CSnappedVector< T, snapPower >::_Tab, and NL3D::CSnappedVector< T, snapPower >::iterator.

00062 { return _Tab; }

template<class T, const uint snapPower = 5>
uint NL3D::CSnappedVector< T, snapPower >::capacity  )  const [inline]
 

Definition at line 210 of file ps_attrib.h.

References uint.

00210 { return _Capacity; }

template<class T, const uint snapPower = 5>
void NL3D::CSnappedVector< T, snapPower >::clear  )  [inline]
 

Definition at line 240 of file ps_attrib.h.

References NL3D::CSnappedVector< T, snapPower >::resize().

Referenced by NL3D::CSnappedVector< T, snapPower >::serial().

00240 { resize(0); }

template<class T, const uint snapPower = 5>
bool NL3D::CSnappedVector< T, snapPower >::empty  )  const [inline]
 

Definition at line 98 of file ps_attrib.h.

00098 { return _Size == 0; }  

template<class T, const uint snapPower = 5>
const_iterator NL3D::CSnappedVector< T, snapPower >::end void   )  const [inline]
 

Definition at line 65 of file ps_attrib.h.

References NL3D::CSnappedVector< T, snapPower >::_Tab, and NL3D::CSnappedVector< T, snapPower >::const_iterator.

00065 { return _Tab + _Size; }

template<class T, const uint snapPower = 5>
iterator NL3D::CSnappedVector< T, snapPower >::end void   )  [inline]
 

Definition at line 64 of file ps_attrib.h.

References NL3D::CSnappedVector< T, snapPower >::_Tab, and NL3D::CSnappedVector< T, snapPower >::iterator.

00064 { return _Tab + _Size; }

template<class T, const uint snapPower = 5>
const T& NL3D::CSnappedVector< T, snapPower >::operator[] uint  index  )  const [inline]
 

Definition at line 74 of file ps_attrib.h.

References NL3D::CSnappedVector< T, snapPower >::_Tab, index, nlassert, and uint.

00075         { 
00076                 #ifdef NL_DEBUG
00077                         nlassert(index < _Size && _Size);
00078                 #endif
00079                 return _Tab[index]; 
00080         }

template<class T, const uint snapPower = 5>
T& NL3D::CSnappedVector< T, snapPower >::operator[] uint  index  )  [inline]
 

Definition at line 67 of file ps_attrib.h.

References NL3D::CSnappedVector< T, snapPower >::_Tab, index, nlassert, and uint.

00068         { 
00069                 #ifdef NL_DEBUG
00070                         nlassert(index < _Size && _Size);
00071                 #endif
00072                 return _Tab[index];
00073         }

template<class T, const uint snapPower = 5>
void NL3D::CSnappedVector< T, snapPower >::pop_back  )  [inline]
 

Definition at line 202 of file ps_attrib.h.

References NL3D::CSnappedVector< T, snapPower >::_Tab, and nlassert.

00203         {
00204                 nlassert(_Size);
00205                 _Tab[_Size - 1].~T();
00206                 --_Size;
00207         }

template<class T, const uint snapPower = 5>
void NL3D::CSnappedVector< T, snapPower >::push_back const T &  t  )  [inline]
 

Definition at line 171 of file ps_attrib.h.

References NL3D::CSnappedVector< T, snapPower >::_Tab, nlassert, NL3D::CSnappedVector< T, snapPower >::reserve(), and t.

Referenced by NL3D::CSnappedVector< T, snapPower >::serial().

00172         {
00173                 if (!_Size)
00174                 {
00175                         reserve(2);
00176                         new ((void *) _Tab) T(t);
00177                         _Size = 1;
00178                 }
00179                 else
00180                 if (_Size < _Capacity)
00181                 {
00182                         new ((void *) (_Tab + _Size)) T(t);
00183                         ++_Size;
00184                 }
00185                 else                            
00186                 if (_Size == _Capacity) 
00187                 { 
00188                         if (_Capacity == 1) 
00189                         {
00190                                 reserve(2);
00191                         }
00192                         else
00193                         {
00194                                 reserve(_Capacity + (_Capacity>>1));
00195                         }
00196                         nlassert(_Size <= _Capacity);
00197                         new ((void *) (_Tab + _Size)) T(t);
00198                         ++_Size;
00199                 }
00200         }

template<class T, const uint snapPower = 5>
void NL3D::CSnappedVector< T, snapPower >::reserve uint  capacity  )  [inline]
 

set a new usable size

Definition at line 101 of file ps_attrib.h.

References NL3D::CSnappedVector< T, snapPower >::_Start, NL3D::CSnappedVector< T, snapPower >::_Tab, NL3D::CSnappedVector< T, snapPower >::iterator, nlassert, src, uint, and uint8.

Referenced by NL3D::CSnappedVector< T, snapPower >::push_back(), NL3D::CSnappedVector< T, snapPower >::resize(), and NL3D::CSnappedVector< T, snapPower >::serial().

00102         {       
00103                 if (capacity < _Capacity) return;
00104                 uint8 *newStart = NULL;
00105                 try
00106                 {                       
00107                         newStart = new uint8[sizeof(T) * capacity + (1 << snapPower)];
00108                         T *newTab = (T *) ( (uint) (newStart + (1 << snapPower))  & ~((1 << snapPower) - 1)); // snap to a page
00109 
00110                         
00111                 
00112                         for (iterator src = _Tab, end = _Tab + (capacity < _Size ? capacity : _Size), dest = newTab;
00113                              src != end;
00114                                  ++ src, ++dest)
00115                         {
00116                                 new ((void *) dest) T(*src); // copy object
00117                         }
00118 
00119                         // swap datas
00120                         std::swap(_Start, newStart);
00121                         std::swap(_Tab, newTab);
00122 
00123                         // destroy previous objects. We assume that we can't have exceptions raised from destructors
00124                         for (iterator it = newTab /* old tab */, endIt = newTab + _Size; it != endIt; ++ it)
00125                         {
00126                                 it->~T();
00127                         }
00128 
00129                         // set new size
00130                         _Capacity = capacity;
00131                         _Size    = capacity < _Size ? capacity : _Size;
00132 
00133 
00134                         // delete old vect (that was swapped with the new one)
00135                         delete [] newStart;
00136                         nlassert(_Size <= _Capacity);
00137                 }
00138                 catch (...)
00139                 {
00140                         delete [] newStart;
00141                         throw;
00142                 }
00143         
00144         }

template<class T, const uint snapPower = 5>
void NL3D::CSnappedVector< T, snapPower >::resize uint  size  )  [inline]
 

Definition at line 145 of file ps_attrib.h.

References NL3D::CSnappedVector< T, snapPower >::_Tab, NL3D::CSnappedVector< T, snapPower >::iterator, nlassert, NL3D::CSnappedVector< T, snapPower >::reserve(), size, and uint.

Referenced by NL3D::CSnappedVector< T, snapPower >::clear().

00146         {
00147                 nlassert(size < (1 << 16));
00148                 if (size < _Size)
00149                 {
00150                         for (iterator it = _Tab + size, endIt = _Tab + _Size; it != endIt; ++it)
00151                         {
00152                                 it->~T();
00153                         }                       
00154                 }
00155                 else
00156                 {
00157                         if (size > _Capacity) 
00158                         {
00159                                 reserve(size);
00160                         }
00161                         for (iterator it = _Tab + _Size, endIt = _Tab + size; it != endIt; ++it)
00162                         {
00163                                 new ((void *) it) T();
00164                         }
00165                 }
00166 
00167                 _Size = size;
00168                 nlassert(_Size <= _Capacity);
00169         }

template<class T, const uint snapPower = 5>
void NL3D::CSnappedVector< T, snapPower >::serial NLMISC::IStream f  )  throw (NLMISC::EStream) [inline]
 

serialization

Definition at line 214 of file ps_attrib.h.

References NL3D::CSnappedVector< T, snapPower >::_Tab, NL3D::CSnappedVector< T, snapPower >::clear(), NL3D::CSnappedVector< T, snapPower >::push_back(), NL3D::CSnappedVector< T, snapPower >::reserve(), size, uint, and uint32.

00215         {
00216                 if (f.isReading())
00217                 {
00218                         clear();
00219                         uint32 size, maxsize;
00220                         f.serial(size, maxsize);
00221                         reserve(maxsize);
00222                         for (uint k = 0; k < size; ++k)
00223                         {
00224                                 T tmp;
00225                                 f.serial(tmp);
00226                                 push_back(tmp);
00227                         }
00228                 }
00229                 else
00230                 {
00231                         f.serial(_Size, _Capacity);
00232                         for (uint k = 0; k < _Size; ++k)
00233                         {
00234                                 f.serial(_Tab[k]);
00235                         }
00236                 }               
00237         }

template<class T, const uint snapPower = 5>
uint NL3D::CSnappedVector< T, snapPower >::size  )  const [inline]
 

Definition at line 211 of file ps_attrib.h.

References uint.

00211 { return _Size; }


Field Documentation

template<class T, const uint snapPower = 5>
uint32 NL3D::CSnappedVector< T, snapPower >::_Capacity [protected]
 

Definition at line 247 of file ps_attrib.h.

template<class T, const uint snapPower = 5>
uint32 NL3D::CSnappedVector< T, snapPower >::_Size [protected]
 

Definition at line 246 of file ps_attrib.h.

template<class T, const uint snapPower = 5>
uint8* NL3D::CSnappedVector< T, snapPower >::_Start [protected]
 

Definition at line 244 of file ps_attrib.h.

Referenced by NL3D::CSnappedVector< T, snapPower >::CSnappedVector(), NL3D::CSnappedVector< T, snapPower >::reserve(), and NL3D::CSnappedVector< T, snapPower >::~CSnappedVector().

template<class T, const uint snapPower = 5>
T* NL3D::CSnappedVector< T, snapPower >::_Tab [protected]
 

Definition at line 245 of file ps_attrib.h.

Referenced by NL3D::CSnappedVector< T, snapPower >::back(), NL3D::CSnappedVector< T, snapPower >::begin(), NL3D::CSnappedVector< T, snapPower >::CSnappedVector(), NL3D::CSnappedVector< T, snapPower >::end(), NL3D::CSnappedVector< T, snapPower >::operator[](), NL3D::CSnappedVector< T, snapPower >::pop_back(), NL3D::CSnappedVector< T, snapPower >::push_back(), NL3D::CSnappedVector< T, snapPower >::reserve(), NL3D::CSnappedVector< T, snapPower >::resize(), NL3D::CSnappedVector< T, snapPower >::serial(), and NL3D::CSnappedVector< T, snapPower >::~CSnappedVector().


The documentation for this class was generated from the following file:
Generated on Tue Mar 16 07:47:13 2004 for NeL by doxygen 1.3.6