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

Dynamic Arrays. More...

#include <modm/container/dynamic_array.hpp>

Classes

class  const_iterator
 Forward const iterator More...
 
class  iterator
 Forward iterator More...
 

Public Typedefs

typedef std::size_t SizeType
 

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...
 
 DynamicArray (const DynamicArray &other)
 
DynamicArray & operator= (const DynamicArray &other)
 
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
 

Friends

class const_iterator
 
class iterator
 

Detailed Description

template<typename T, typename Allocator = allocator::Dynamic<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 = allocator::Dynamic<T>>
modm::DynamicArray< T, Allocator >::DynamicArray ( const Allocator &  allocator = Allocator ())

Default constructor.

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

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

Allocation constructor.

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

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

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 = allocator::Dynamic<T>>
modm::DynamicArray< T, Allocator >::DynamicArray ( std::initializer_list< T >  init,
const Allocator &  allocator = Allocator () 
)

Initializer List constructor.

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

Member Function Documentation

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

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 = allocator::Dynamic<T>>
iterator modm::DynamicArray< T, Allocator >::begin ( )

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 = allocator::Dynamic<T>>
const_iterator modm::DynamicArray< T, Allocator >::begin ( ) const

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 = allocator::Dynamic<T>>
void modm::DynamicArray< T, Allocator >::clear ( )

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 = allocator::Dynamic<T>>
iterator modm::DynamicArray< T, Allocator >::end ( )

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 = allocator::Dynamic<T>>
const_iterator modm::DynamicArray< T, Allocator >::end ( ) const

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 = allocator::Dynamic<T>>
iterator modm::DynamicArray< T, Allocator >::find ( const T &  value)

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 = allocator::Dynamic<T>>
const_iterator modm::DynamicArray< T, Allocator >::find ( const T &  value) const

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 = allocator::Dynamic<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 = allocator::Dynamic<T>>
SizeType modm::DynamicArray< T, Allocator >::getSize ( ) const
inline

Return size.

Returns the number of elements in the container.

template<typename T , typename Allocator = allocator::Dynamic<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 = allocator::Dynamic<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 = allocator::Dynamic<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 = allocator::Dynamic<T>>
void modm::DynamicArray< T, Allocator >::removeAll ( )

Remove all elements.

Keeps the capacity at its current level.

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

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 = allocator::Dynamic<T>>
void modm::DynamicArray< T, Allocator >::reserve ( SizeType  n)

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: