modm API documentation
modm::DynamicArray< T, Allocator > Class Template Reference

Dynamic Arrays. More...

#include <modm/container/dynamic_array.hpp>

Public Typedefs

using SizeType = std::size_t
 
using const_iterator = std::vector< T, Allocator >::const_iterator
 
using iterator = std::vector< T, Allocator >::iterator
 

Public Member Functions

 DynamicArray (const Allocator &allocator=Allocator ())
 Default constructor. More...
 
 DynamicArray (SizeType n, const Allocator &allocator=Allocator ())
 Allocation constructor. More...
 
 DynamicArray (SizeType n, const T &value, const Allocator &allocator=Allocator ())
 Repetitive sequence constructor. More...
 
 DynamicArray (std::initializer_list< T > init, const Allocator &allocator=Allocator ())
 Initializer List constructor. More...
 
bool isEmpty () const
 Test whether dynamic array is empty. More...
 
SizeType getSize () const
 Return size. More...
 
SizeType getCapacity () const
 Return size of allocated storage. More...
 
void reserve (SizeType n)
 Request a change in capacity. More...
 
void clear ()
 Remove all elements and set capacity to zero. More...
 
void removeAll ()
 Remove all elements. More...
 
T & operator[] (SizeType index)
 Access element. More...
 
const T & operator[] (SizeType index) const
 Access element. More...
 
void append (const T &value)
 Add element at the end. More...
 
void removeBack ()
 Delete last element. More...
 
const T & getFront () const
 
T & getFront ()
 
const T & getBack () const
 
T & getBack ()
 
iterator begin ()
 
const_iterator begin () const
 
iterator end ()
 
const_iterator end () const
 
iterator find (const T &value)
 
const_iterator find (const T &value) const
 

Detailed Description

template<typename T, typename Allocator = std::allocator<T>>
class modm::DynamicArray< T, Allocator >

Dynamic Arrays.

Dynamic Arrays are a kind of sequence containers. As such, their elements are ordered following a strict linear sequence.

Just as regular arrays, dynamic arrays containers have their elements stored in contiguous storage locations, which means that their elements can be accessed not only using iterators but also using offsets on regular pointers to elements.

Reallocations may be a costly operation in terms of performance, since they generally involve the entire storage space used by the dynamic array to be copied to a new location. Therefore, whenever large increases in size are planned for a dynamic array, it is recommended to explicitly indicate a capacity for the dynamic array using member function DynamicArray::reserve().

Author
Fabian Greif fabian.greif@rwth-aachen.de

Constructor & Destructor Documentation

template<typename T , typename Allocator = std::allocator<T>>
modm::DynamicArray< T, Allocator >::DynamicArray ( const Allocator &  allocator = Allocator())
inline

Default constructor.

Constructs an empty dynamic array, with no content and a size of zero.

template<typename T , typename Allocator = std::allocator<T>>
modm::DynamicArray< T, Allocator >::DynamicArray ( SizeType  n,
const Allocator &  allocator = Allocator() 
)
inline

Allocation constructor.

Construct a dynamic array of given capacity. The array will still be empty.

template<typename T , typename Allocator = std::allocator<T>>
modm::DynamicArray< T, Allocator >::DynamicArray ( SizeType  n,
const T &  value,
const Allocator &  allocator = Allocator() 
)
inline

Repetitive sequence constructor.

Construct a dynamic array of given size.

Initializes the dynamic array with its content set to a repetition, n times, of copies of value.

template<typename T , typename Allocator = std::allocator<T>>
modm::DynamicArray< T, Allocator >::DynamicArray ( std::initializer_list< T >  init,
const Allocator &  allocator = Allocator() 
)
inline

Initializer List constructor.

Construct a dynamic array that holds the values specified in the initialize list

Member Function Documentation

template<typename T , typename Allocator = std::allocator<T>>
void modm::DynamicArray< T, Allocator >::append ( const T &  value)
inline

Add element at the end.

Adds a new element at the end of the dynamic array, after its current last element. The content of this new element is initialized to a copy of value.

This effectively increases the dynamic array size by one, which causes a reallocation of the internal allocated storage if the dynamic array size was equal to the dynamic array capacity before the call. Reallocations invalidate all previously obtained iterators, references and pointers.

template<typename T , typename Allocator = std::allocator<T>>
iterator modm::DynamicArray< T, Allocator >::begin ( )
inline

Returns a read/write iterator that points to the first element in the list. Iteration is done in ordinary element order.

template<typename T , typename Allocator = std::allocator<T>>
const_iterator modm::DynamicArray< T, Allocator >::begin ( ) const
inline

Returns a read-only (constant) iterator that points to the first element in the list. Iteration is done in ordinary element order.

template<typename T , typename Allocator = std::allocator<T>>
void modm::DynamicArray< T, Allocator >::clear ( )
inline

Remove all elements and set capacity to zero.

Frees all allocated memory and sets the capacity of the container to zero.

Warning
This will discard all the items in the container
template<typename T , typename Allocator = std::allocator<T>>
iterator modm::DynamicArray< T, Allocator >::end ( )
inline

Returns a read/write iterator that points one past the last element in the list. Iteration is done in ordinary element order.

template<typename T , typename Allocator = std::allocator<T>>
const_iterator modm::DynamicArray< T, Allocator >::end ( ) const
inline

Returns a read-only (constant) iterator that points one past the last element in the list. Iteration is done in ordinary element order.

template<typename T , typename Allocator = std::allocator<T>>
iterator modm::DynamicArray< T, Allocator >::find ( const T &  value)
inline

Returns a read/write iterator that points to the first element that contains value. If value is not found, it points to the last element.

template<typename T , typename Allocator = std::allocator<T>>
const_iterator modm::DynamicArray< T, Allocator >::find ( const T &  value) const
inline

Returns a read-only (constant) iterator that points to the first element that contains value. If value is not found, it points to the last element.

template<typename T , typename Allocator = std::allocator<T>>
SizeType modm::DynamicArray< T, Allocator >::getCapacity ( ) const
inline

Return size of allocated storage.

Returns the size of the allocated storage space in the dynamic array object.

Notice that, in dynamic arrays, the capacity is not necessarily equal to the number of elements that conform the underlying dynamic array content (this can be obtained with member DynamicArray::getSize()), but the capacity of the actual allocated space, which is either equal or greater than the content size.

See also
getSize()
template<typename T , typename Allocator = std::allocator<T>>
SizeType modm::DynamicArray< T, Allocator >::getSize ( ) const
inline

Return size.

Returns the number of elements in the container.

template<typename T , typename Allocator = std::allocator<T>>
bool modm::DynamicArray< T, Allocator >::isEmpty ( ) const
inline

Test whether dynamic array is empty.

Returns whether the dynamic array container is empty, i.e. whether its size is 0.

template<typename T , typename Allocator = std::allocator<T>>
T& modm::DynamicArray< T, Allocator >::operator[] ( SizeType  index)
inline

Access element.

Returns a reference to the element at position n in the dynamic array container.

Warning
Do not use this operator to access an element the first time. Use the append methode to create it.
See also
append()
template<typename T , typename Allocator = std::allocator<T>>
const T& modm::DynamicArray< T, Allocator >::operator[] ( SizeType  index) const
inline

Access element.

Returns a reference to the element at position n in the dynamic array container.

template<typename T , typename Allocator = std::allocator<T>>
void modm::DynamicArray< T, Allocator >::removeAll ( )
inline

Remove all elements.

Keeps the capacity at its current level.

template<typename T , typename Allocator = std::allocator<T>>
void modm::DynamicArray< T, Allocator >::removeBack ( )
inline

Delete last element.

Removes the last element in the dynamic array, effectively reducing the dynamic array size by one and invalidating all iterators and references to it.

This calls the removed element's destructor.

template<typename T , typename Allocator = std::allocator<T>>
void modm::DynamicArray< T, Allocator >::reserve ( SizeType  n)
inline

Request a change in capacity.

Requests that the capacity of the allocated storage space for the elements of the dynamic array container be at least enough to hold n more elements.

This informs the dynamic array of a planned increase in size, although notice that the parameter n informs of a minimum, so the resulting capacity may be any capacity equal or larger than this.

See also
getCapacity()

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