CORGI
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
corgi::VectorPool< T > Class Template Reference

A pool allocator, implemented as a vector-based pair of linked lists. More...

#include <vector_pool.h>

Detailed Description

template<typename T>
class corgi::VectorPool< T >

A pool allocator, implemented as a vector-based pair of linked lists.

Template Parameters
TThe data type of the data stored by the VectorPool.

Classes

class  IteratorTemplate
 An Iterator for the VectorPool. More...
 
struct  VectorPoolElement
 A struct representing an element inside of a VectorPool. More...
 
class  VectorPoolReference
 A reference object for pointing into the vector pool. It acts as a pointer for vector pool elements and can be queried to check if it has become invalid. (i.e. If the element it pointed at has either been deallocated, or replaced with a new element). More...
 

Public Types

typedef IteratorTemplate< false > Iterator
 A non-const IteratorTemplate.
 
typedef IteratorTemplate< true > ConstIterator
 A const IteratorTemplate.
 

Public Member Functions

 VectorPool ()
 The default constructor for an empty VectorPool.
 
T * GetElementData (size_t index)
 Get the data at the given element index. More...
 
const T * GetElementData (size_t index) const
 Get the data at the given element index. More...
 
VectorPoolReference GetNewElement (AllocationLocation alloc_location)
 Get a VectorPoolReference to a new element. More...
 
void FreeElement (size_t index)
 Frees an element at a given index. More...
 
void FreeElement (VectorPoolReference element)
 Frees a given element. More...
 
Iterator FreeElement (Iterator iter)
 
size_t Size () const
 Get the total size of the vector pool. More...
 
size_t active_count () const
 Gets the total number of active elements. More...
 
void Clear ()
 Clears out all of the elements in the VectorPool and resizes the underlying vector to the minimum size.
 
Iterator begin ()
 Get an Iterator to the first active element in the VectorPool. More...
 
Iterator end ()
 Gets an Iterator to the last active element in the VectorPool. More...
 
ConstIterator cbegin ()
 Gets a ConstIterator to the first active element in the VectorPool. More...
 
ConstIterator cend ()
 Gets a ConstIterator to the last active element in the VectorPool. More...
 
void Reserve (size_t new_size)
 Expands the vector until it is at least as large as new_size. More...
 

Static Public Attributes

static const size_t kOutOfBounds = static_cast<size_t>(-1)
 A sentinel value that represents an out-of-bounds index.
 
static const UniqueIdType kInvalidId = 0
 A sentinel value that represents an invalid ID. More...
 
static const size_t kFirstUsed = 0
 Used to demarcate the first element of our used list. More...
 
static const size_t kLastUsed = 1
 Used to demarcate the last element of our used list. More...
 
static const size_t kFirstFree = 2
 Used to demarcate the first element of our free list. More...
 
static const size_t kLastFree = 3
 Used to demarcate the last element of our free list. More...
 
static const size_t kTotalReserved = 4
 Used to indicate the number of reserved elements. (e.g. kFirstUsed, kLastUsed, kFirstFree, kLastFree).
 

Friends

template<bool >
class IteratorTemplate
 
class VectorPoolReference
 

Member Function Documentation

template<typename T>
size_t corgi::VectorPool< T >::active_count ( ) const
inline

Gets the total number of active elements.

Returns
A size_t representing the total number of active elements in the VectorPool.
template<typename T>
Iterator corgi::VectorPool< T >::begin ( )
inline

Get an Iterator to the first active element in the VectorPool.

This is suitable for traversing all of the active elements in the VectorPool.

Returns
Returns an Iterator to the first active element in the VectorPool.
template<typename T>
ConstIterator corgi::VectorPool< T >::cbegin ( )
inline

Gets a ConstIterator to the first active element in the VectorPool.

This is suitable for traversing all of the active elements in the VectorPool.

Returns
Returns a ConstIterator to the first active element in the VectorPool.
template<typename T>
ConstIterator corgi::VectorPool< T >::cend ( )
inline

Gets a ConstIterator to the last active element in the VectorPool.

This is suitable as an end condition when iterating over all active elements in the VectorPool.

Returns
Returns a ConstIterator to the last active element in the VectorPool.
template<typename T>
Iterator corgi::VectorPool< T >::end ( )
inline

Gets an Iterator to the last active element in the VectorPool.

This is suitable as an end condition when iterating over all active elements in the VectorPool.

Returns
Returns An Iterator to the last active element in the VectorPool.
template<typename T>
void corgi::VectorPool< T >::FreeElement ( size_t  index)
inline

Frees an element at a given index.

Note
This removes the element from the list of active elements, and adds it to the front of the inactive list (to be used later, when we add elements to the VectorPool).
Parameters
[in]indexThe index corresponding to the element that should be freed.
template<typename T>
void corgi::VectorPool< T >::FreeElement ( VectorPoolReference  element)
inline

Frees a given element.

Note
This removes the element from the list of active elements, and adds it to the front of the inactive list (to be used later, when we add elements to the VectorPool).
Parameters
[in]elementA VectorPoolReference to the element that should be freed.
template<typename T>
Iterator corgi::VectorPool< T >::FreeElement ( Iterator  iter)
inline

@ Frees an element that an Iterator points to.

Note
This removes the element from the list of active elements, and adds it to the front of the inactive list (to be used later, when we add elements to the VectorPool).
Parameters
[in]iterAn Iterator that references an element that should be freed.
Returns
Returns an incremented Iterator that refers to the element immediately after the freed element.
template<typename T>
T* corgi::VectorPool< T >::GetElementData ( size_t  index)
inline

Get the data at the given element index.

Note
The pointer is not guaranteed to remain valid, if the vector needs to relocate the data in memory. In general, if you need to hold on to a reference to a data element, it is recommended that you use a VectorPoolReference.
Warning
Asserts if the index is illegal (i.e. out of range for the underlying vector).
Parameters
[in]indexThe index of the data element to return.
Returns
Returns a pointer to the data.
template<typename T>
const T* corgi::VectorPool< T >::GetElementData ( size_t  index) const
inline

Get the data at the given element index.

Note
The pointer is not guaranteed to remain valid, if the vector needs to relocate the data in memory. In general, if you need to hold on to a reference to a data element, it is recommended that you use a VectorPoolReference.
Warning
Asserts if the index is illegal (i.e. out of range for the underlying vector).
Parameters
[in]indexThe index of the data element to return.
Returns
Returns a const pointer to the data.
template<typename T>
VectorPoolReference corgi::VectorPool< T >::GetNewElement ( AllocationLocation  alloc_location)
inline

Get a VectorPoolReference to a new element.

Note
This function grabs the first free element (if one exists). Otherwise, it allocates a new one on the underlying vector.
Parameters
[in]alloc_locationAn AllocationLocation enum determining whether to add to the front or back of the underlying vector.
Returns
Returns a VectorPoolReference pointing to the new element.
template<typename T>
void corgi::VectorPool< T >::Reserve ( size_t  new_size)
inline

Expands the vector until it is at least as large as new_size.

Note
: If the vector already contains at least new_size elements, then there is no effect.
Parameters
[in]new_sizeA size_t indicating the new minimum size for the underlying vector.
template<typename T>
size_t corgi::VectorPool< T >::Size ( ) const
inline

Get the total size of the vector pool.

This is the total number of allocated elements (used AND free) by the underlying vector.

Returns
A size_t representing the total size of the VectorPool (both used AND free elements).

Member Data Documentation

template<typename T>
corgi::VectorPool< T >::kFirstFree = 2
static

Used to demarcate the first element of our free list.

Note
This is never given actual data. It is only used for list demarcation.
template<typename T>
corgi::VectorPool< T >::kFirstUsed = 0
static

Used to demarcate the first element of our used list.

Note
This is never given actual data. It is only used for list demarcation.
template<typename T>
corgi::VectorPool< T >::kInvalidId = 0
static

A sentinel value that represents an invalid ID.

Note
Unique IDs start at 1.
template<typename T>
corgi::VectorPool< T >::kLastFree = 3
static

Used to demarcate the last element of our free list.

Note
This is never given actual data. It is only used for list demarcation.
template<typename T>
corgi::VectorPool< T >::kLastUsed = 1
static

Used to demarcate the last element of our used list.

Note
This is never given actual data. It is only used for list demarcation.

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