PlayRho  1.1.0
An Interactive Real-Time-Oriented C++ Physics Engine & Library
playrho::ArrayAllocator< T > Class Template Reference

Array allocator. More...

#include <ArrayAllocator.hpp>

Inheritance diagram for playrho::ArrayAllocator< T >:
[legend]
Collaboration diagram for playrho::ArrayAllocator< T >:
[legend]

Public Types

using value_type = T
 Element type.
 
using size_type = typename std::vector< value_type >::size_type
 Size type.
 
using reference = typename std::vector< value_type >::reference
 Reference type alias.
 
using const_reference = typename std::vector< value_type >::const_reference
 Constant reference type alias.
 

Public Member Functions

size_type GetIndex (value_type *ptr) const
 Gets the index of the given pointer. More...
 
template<class... Args>
size_type Allocate (Args &&... args)
 Allocates an entry in the array with the given constructor parameters. More...
 
size_type Allocate (const value_type &copy)
 Allocates an entry in the array with the given instance. More...
 
void Free (size_type index)
 Frees the previously-allocated index entry. More...
 
bool FindFree (size_type index) const noexcept
 Finds whether the given index is in the free list. More...
 
reference operator[] (size_type pos)
 Array index operator. More...
 
const_reference operator[] (size_type pos) const
 Constant array index operator. More...
 
reference at (size_type pos)
 Bounds checking indexed array accessor.
 
const_reference at (size_type pos) const
 Bounds checking indexed array accessor.
 
size_type size () const noexcept
 Gets the size of this instance in number of elements. More...
 
size_type max_size () const noexcept
 Gets the maximum theoretical size this instance can have in number of elements.
 
size_type free_size () const noexcept
 Gets the number of elements currently free. More...
 
void reserve (size_type value)
 Reserves the given number of elements from dynamic memory. More...
 
void clear () noexcept
 Clears this instance's free pool and allocated pool.
 

Private Attributes

std::vector< value_typem_data
 Array data (both used & free).
 
std::vector< size_typem_free
 Indices of free elements.
 

Detailed Description

template<typename T>
class playrho::ArrayAllocator< T >

Array allocator.

Member Function Documentation

◆ Allocate() [1/2]

template<typename T >
template<class... Args>
size_type playrho::ArrayAllocator< T >::Allocate ( Args &&...  args)
inline

Allocates an entry in the array with the given constructor parameters.

Note
Call Free(size_type) if the allocated entry needs to be freed.
Postcondition
size() - free_size() will return a value one less than before.
See also
Free, size, free_size.

Referenced by playrho::d2::WorldImpl::CreateBody(), and playrho::d2::WorldImpl::CreateFixture().

◆ Allocate() [2/2]

template<typename T >
size_type playrho::ArrayAllocator< T >::Allocate ( const value_type copy)
inline

Allocates an entry in the array with the given instance.

Note
Call Free(size_type) if the allocated entry needs to be freed.
Postcondition
size() - free_size() will return a value one less than before.
See also
Free, size, free_size.

◆ FindFree()

template<typename T >
bool playrho::ArrayAllocator< T >::FindFree ( size_type  index) const
inlinenoexcept

Finds whether the given index is in the free list.

Note
Complexity is at most O(n) where n is the number of elements free.
See also
Free, free_size.
https://en.wikipedia.org/wiki/Big_O_notation

◆ Free()

template<typename T >
void playrho::ArrayAllocator< T >::Free ( size_type  index)
inline

Frees the previously-allocated index entry.

Postcondition
FindFree(size_type) returns true for the freed index.
free_size() returns a value one greater than before.
Exceptions
std::out_of_rangeif given an index that's greater than or equal to the size of this instance as returned by size().
Todo:
Consider having this function check if given index already freed.
See also
Allocate, FindFree, free_size, size.

Referenced by playrho::d2::WorldImpl::Destroy().

◆ free_size()

template<typename T >
size_type playrho::ArrayAllocator< T >::free_size ( ) const
inlinenoexcept

Gets the number of elements currently free.

See also
Free.

◆ GetIndex()

template<typename T >
size_type playrho::ArrayAllocator< T >::GetIndex ( value_type ptr) const
inline

Gets the index of the given pointer.

Returns
-1 if the given pointer is not within the range of the allocator's allocation, otherwise return index of pointer within allocator.

◆ operator[]() [1/2]

template<typename T >
reference playrho::ArrayAllocator< T >::operator[] ( size_type  pos)
inline

Array index operator.

Note
This does not do bounds checking.
See also
at

◆ operator[]() [2/2]

template<typename T >
const_reference playrho::ArrayAllocator< T >::operator[] ( size_type  pos) const
inline

Constant array index operator.

Note
This does not do bounds checking.
See also
at

◆ reserve()

template<typename T >
void playrho::ArrayAllocator< T >::reserve ( size_type  value)
inline

Reserves the given number of elements from dynamic memory.

Note
This may increase this instance's capacity; not its size.

◆ size()

template<typename T >
size_type playrho::ArrayAllocator< T >::size ( ) const
inlinenoexcept

Gets the size of this instance in number of elements.

See also
Allocate, Free.

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