All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Radiant::VectorStorage< T > Class Template Reference

A container for fast array allocation/deallocation. More...

#include <Radiant/VectorStorage.hpp>

Public Types

typedef std::vector< T >::iterator iterator
 Iterator.
 

Public Member Functions

void append (const T &x)
 Appends an object to the vector. More...
 
T & append ()
 Increase the size of the storage by one, and return the last object. More...
 
iterator begin ()
 Returns iterator to the beginning of the vector. More...
 
void clear ()
 Resets the internal object counter to zero. More...
 
T * data ()
 Pointer to the first element. More...
 
const T * data () const
 Pointer to the first element. More...
 
bool empty () const
 Returns true if the vector is empty. More...
 
iterator end ()
 Returns iterator to the end of the vector. More...
 
void erase (size_t index)
 Erase an element. More...
 
void expand (size_t size)
 Expand the size of the storage buffer to desired size. More...
 
const T & get (size_t index) const
 Gets an object, without safety checks. More...
 
T & get (size_t index)
 Gets an object, without safety checks. More...
 
T & getExpand (size_t index)
 Gets a value from the vector and expand the vector if necessary. More...
 
T & getLast ()
 Gets reference to the last elemnt. More...
 
const T & getLast () const
 Gets reference to the last elemnt. More...
 
const T & getLast (size_t n) const
 Gets the element at size() - n - 1. More...
 
const T & getSafe (size_t index) const
 Gets an object, and check that the index is valid. More...
 
T & getSafe (size_t index)
 Gets an object, and check that the index is valid. More...
 
T & last ()
 Gets reference to the last elemnt. More...
 
const T & last (size_t n) const
 Gets the element at size() - n - 1. More...
 
void merge (VectorStorage &that)
 Appends elements from that to the end of this. More...
 
VectorStorageoperator= (const VectorStorage &that)
 Copies a vector. More...
 
T & operator[] (size_t i)
 Returns the element at the given index. More...
 
const T & operator[] (size_t i) const
 Returns the element at the given index. More...
 
void prepend (const T &x)
 Push an object to the beginning of the array. More...
 
void push_back (const T &x)
 Appends an object to the vector, equals append(x). More...
 
void putBack (size_t n)
 Remove n elements from the end of the storage. More...
 
size_t reserved () const
 The number of allocated objects. More...
 
void reset ()
 Resets the internal object counter to zero. More...
 
void resize (size_t size)
 Resizes the vector. More...
 
void setAll (const T &value)
 Fills the vector with the given value. More...
 
size_t size () const
 The number of objecs in the array. More...
 
void swap (VectorStorage &that)
 Swaps two VectorStorages. More...
 
void truncate (size_t n)
 Resets the internal object counter to n. More...
 
std::vector< T > & vector ()
 Returns the internal data storage area. More...
 
 VectorStorage ()
 Creates an empty vector storage object.
 

Detailed Description

template<typename T>
class Radiant::VectorStorage< T >

A container for fast array allocation/deallocation.

This class for handling resources that need to be frequently allocated/deallocated. The key point is that the memory is deallocated only in the destructor or when explicitly desired.

Internally this class uses std::vector to do the real memory handling. It is sometimes used as a member variable of some class.

Example:

VectorStorage<Item> items;
elems.expand(5000); This is entirely optional
Now we would use the "items" object for a longer time inside the
while-loop.
while(keepGoing()) {
  items.reset();
  while(fillingTheBuffer())  {
    Item item;
    items.append(item);
  }
  doSomeThingWithTheItems(items);
}
 

Member Function Documentation

template<typename T >
void Radiant::VectorStorage< T >::append ( const T &  x)
inline

Appends an object to the vector.

The storage area is automatically incremented if necessary.

Parameters
xElement to append
template<typename T >
T& Radiant::VectorStorage< T >::append ( )
inline

Increase the size of the storage by one, and return the last object.

The storage area is automatically incremented if necessary.

Returns
Reference to the last object in the vector
template<typename T >
iterator Radiant::VectorStorage< T >::begin ( )
inline

Returns iterator to the beginning of the vector.

Returns
iterator to the beginning of the vector
template<typename T >
void Radiant::VectorStorage< T >::clear ( void  )
inline

Resets the internal object counter to zero.

This function does not erase any objects.

template<typename T >
T* Radiant::VectorStorage< T >::data ( )
inline

Pointer to the first element.

Returns
pointer to the first element
template<typename T >
const T* Radiant::VectorStorage< T >::data ( ) const
inline

Pointer to the first element.

Returns
pointer to the first element
template<typename T >
bool Radiant::VectorStorage< T >::empty ( ) const
inline

Returns true if the vector is empty.

Returns
True if this is empty
template<typename T >
iterator Radiant::VectorStorage< T >::end ( )
inline

Returns iterator to the end of the vector.

Returns
Iterator to the end of the vector
template<typename T >
void Radiant::VectorStorage< T >::erase ( size_t  index)
inline

Erase an element.

The size of the storage is shrunk by one.

Parameters
indexIndex of element to remove
template<typename T >
void Radiant::VectorStorage< T >::expand ( size_t  size)
inline

Expand the size of the storage buffer to desired size.

This function can be run in software initialization phase, to

avoid the need to resize the buffer later on.

Parameters
sizenew size of buffer
template<typename T >
const T& Radiant::VectorStorage< T >::get ( size_t  index) const
inline

Gets an object, without safety checks.

Parameters
indexIndex of element to retrieve
Returns
Reference to the index-th element
template<typename T >
T& Radiant::VectorStorage< T >::get ( size_t  index)
inline

Gets an object, without safety checks.

Parameters
indexIndex of element to retrieve
Returns
Reference to the index-th element
template<typename T >
T& Radiant::VectorStorage< T >::getExpand ( size_t  index)
inline

Gets a value from the vector and expand the vector if necessary.

Parameters
indexIndex of element to retrieve
Returns
A reference to the index-th element
template<typename T >
T& Radiant::VectorStorage< T >::getLast ( )
inline

Gets reference to the last elemnt.

Returns
Reference to the last element
template<typename T >
const T& Radiant::VectorStorage< T >::getLast ( ) const
inline

Gets reference to the last elemnt.

Returns
Reference to the last element
template<typename T >
const T& Radiant::VectorStorage< T >::getLast ( size_t  n) const
inline

Gets the element at size() - n - 1.

Parameters
nElement-index, counted from the back
Returns
Reference to the n-th last element
template<typename T >
const T& Radiant::VectorStorage< T >::getSafe ( size_t  index) const
inline

Gets an object, and check that the index is valid.

If the index is not valid, then assertion is raised, and the software stops.

Parameters
indexIndex of element to retrieve
Returns
element from index-th position
template<typename T >
T& Radiant::VectorStorage< T >::getSafe ( size_t  index)
inline

Gets an object, and check that the index is valid.

If the index is not valid, then assertion is raised, and the software stops.

Parameters
indexIndex of element to retrieve
Returns
element from index-th position
template<typename T >
T& Radiant::VectorStorage< T >::last ( )
inline

Gets reference to the last elemnt.

Returns
Reference to the last element
template<typename T >
const T& Radiant::VectorStorage< T >::last ( size_t  n) const
inline

Gets the element at size() - n - 1.

Parameters
nElement-index, counted from the back
Returns
Reference to the n-th last element
template<typename T >
void Radiant::VectorStorage< T >::merge ( VectorStorage< T > &  that)
inline

Appends elements from that to the end of this.

Parameters
thatVector to merge with
template<typename T >
VectorStorage& Radiant::VectorStorage< T >::operator= ( const VectorStorage< T > &  that)
inline

Copies a vector.

Parameters
thatVector to copy from
Returns
Reference to self
template<typename T >
T& Radiant::VectorStorage< T >::operator[] ( size_t  i)
inline

Returns the element at the given index.

Returns
the element at the given index
Parameters
iIndex of element to retrieve
template<typename T >
const T& Radiant::VectorStorage< T >::operator[] ( size_t  i) const
inline

Returns the element at the given index.

Returns
the element at the given index
Parameters
iIndex of element to retrieve
template<typename T >
void Radiant::VectorStorage< T >::prepend ( const T &  x)
inline

Push an object to the beginning of the array.

This function call takes some time on larger arrays, so it

should be used with care.

Parameters
xElement to prepend
template<typename T >
void Radiant::VectorStorage< T >::push_back ( const T &  x)
inline

Appends an object to the vector, equals append(x).

This method has been implemented so that this class looks and feels more like a typical STL container.

Parameters
xElement to append
template<typename T >
void Radiant::VectorStorage< T >::putBack ( size_t  n)
inline

Remove n elements from the end of the storage.

Parameters
nNumber of elements to remove
template<typename T >
size_t Radiant::VectorStorage< T >::reserved ( ) const
inline

The number of allocated objects.

Returns
How many elements we are allocated
template<typename T >
void Radiant::VectorStorage< T >::reset ( )
inline

Resets the internal object counter to zero.

This function does not erase any objects.

template<typename T >
void Radiant::VectorStorage< T >::resize ( size_t  size)
inline

Resizes the vector.

Parameters
sizenew size of buffer
template<typename T >
void Radiant::VectorStorage< T >::setAll ( const T &  value)
inline

Fills the vector with the given value.

Parameters
valueto fill with
template<typename T >
size_t Radiant::VectorStorage< T >::size ( ) const
inline

The number of objecs in the array.

Returns
Number of objects in this object
template<typename T >
void Radiant::VectorStorage< T >::swap ( VectorStorage< T > &  that)
inline

Swaps two VectorStorages.

Parameters
thatVector to swap with
template<typename T >
void Radiant::VectorStorage< T >::truncate ( size_t  n)
inline

Resets the internal object counter to n.

Parameters
nNew value of object counter
template<typename T >
std::vector<T>& Radiant::VectorStorage< T >::vector ( )
inline

Returns the internal data storage area.

Returns
Vector acting as internal storage