cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
cppcore::TArray< T, TAlloc > Class Template Reference

This template class implements a simple array with dynamic boundaries. More...

#include <TArray.h>

Public Types

using Iterator = T*
 The iterator type.
using ConstIterator = const T *
 The const iterator type.
using array_size_type = size_t
 The index address type.

Public Member Functions

 TArray ()=default
 The default class constructor.
 TArray (array_size_type size)
 The class constructor with an initial size.
 TArray (const TArray< T, TAlloc > &rhs)
 The copy constructor.
 ~TArray ()
 The class destructor.
void add (const T &newValue)
 A new item will be added to the array.
void add (const T *newValues, array_size_type numItems)
 An array of new items will be added to the array.
void remove (array_size_type index)
 Removes an item at the given index.
void remove (Iterator it)
 Removes the item describes by a given iterator.
void removeBack ()
 The last item will be removed.
void set (const T &value)
 Will set all items to the value.
void destroy (array_size_type index)
 The item at the given index will be destroyed, the destructor will be called manually.
T & front ()
 Returns the first item.
T & back ()
 Returns the last item.
const T & back () const
 Returns the last item as a const reference.
void move (array_size_type startIdx, array_size_type endIdx)
 Moves the items from the start- to the end-index.
void reserve (array_size_type capacity)
 Ensures, that the capacity of the array is big enough for the given size.
void resize (array_size_type size)
 Resize the array, new items will be created.
void resize (array_size_type size, T val)
 Resize the array, new items will be created.
size_t size () const
 The current size of the array will be returned.
size_t capacity () const
 The current capacity will be returned.
bool isEmpty () const
 Returns true, if the array is empty.
Iterator linearSearch (const T &item)
 Performs a linear search for a given item in the array.
void clear ()
 The array will be cleared, destructor of the items will be called.
Iterator begin ()
 Returns an iterator showing to the first item.
Iterator end ()
 Returns an iterator showing to the end of the array.
T * data () const
 Will return the data pointer.
T & operator[] (array_size_type idx) const
 The [] operator.
TArray< T, TAlloc > & operator= (const TArray< T, TAlloc > &rhs)
 The assignment operator.
bool operator== (const TArray< T, TAlloc > &rOther) const
 The compare operator.

Detailed Description

template<class T, class TAlloc = TDefaultAllocator<T>>
class cppcore::TArray< T, TAlloc >

This template class implements a simple array with dynamic boundaries.

You can use it to add new items, remove them and iterate through them. The data items are stores in an array.

Definition at line 55 of file TArray.h.

Member Typedef Documentation

◆ array_size_type

template<class T, class TAlloc = TDefaultAllocator<T>>
using cppcore::TArray< T, TAlloc >::array_size_type = size_t

The index address type.

Definition at line 62 of file TArray.h.

◆ ConstIterator

template<class T, class TAlloc = TDefaultAllocator<T>>
using cppcore::TArray< T, TAlloc >::ConstIterator = const T *

The const iterator type.

Definition at line 60 of file TArray.h.

◆ Iterator

template<class T, class TAlloc = TDefaultAllocator<T>>
using cppcore::TArray< T, TAlloc >::Iterator = T*

The iterator type.

Definition at line 58 of file TArray.h.

Constructor & Destructor Documentation

◆ TArray() [1/3]

template<class T, class TAlloc = TDefaultAllocator<T>>
cppcore::TArray< T, TAlloc >::TArray ( )
default

The default class constructor.

Referenced by operator=(), operator==(), and TArray().

◆ TArray() [2/3]

template<class T, class TAlloc>
cppcore::TArray< T, TAlloc >::TArray ( array_size_type size)
inlineexplicit

The class constructor with an initial size.

Parameters
[in]sizeThe initial size.

Definition at line 190 of file TArray.h.

References cppcore::Details::getGrowing(), reserve(), resize(), and size().

◆ TArray() [3/3]

template<class T, class TAlloc>
cppcore::TArray< T, TAlloc >::TArray ( const TArray< T, TAlloc > & rhs)
inline

The copy constructor.

Parameters
[in]rhsThe instance to copy from.

Definition at line 197 of file TArray.h.

References resize(), and TArray().

◆ ~TArray()

template<class T, class TAlloc>
cppcore::TArray< T, TAlloc >::~TArray ( )
inline

The class destructor.

Definition at line 205 of file TArray.h.

References clear().

Member Function Documentation

◆ add() [1/2]

template<class T, class TAlloc>
void cppcore::TArray< T, TAlloc >::add ( const T & newValue)
inline

A new item will be added to the array.

Parameters
[in]newValueThe value to add.

Definition at line 210 of file TArray.h.

References cppcore::Details::getGrowing(), and reserve().

◆ add() [2/2]

template<class T, class TAlloc>
void cppcore::TArray< T, TAlloc >::add ( const T * newValues,
array_size_type numItems )
inline

An array of new items will be added to the array.

Parameters
[in]newValuesThe array of new values to add.
[in]numItemsThe number of items in the array.

Definition at line 220 of file TArray.h.

References reserve().

◆ back() [1/2]

template<class T, class TAlloc>
T & cppcore::TArray< T, TAlloc >::back ( )
inline

Returns the last item.

Returns
The last item.

Definition at line 302 of file TArray.h.

◆ back() [2/2]

template<class T, class TAlloc>
const T & cppcore::TArray< T, TAlloc >::back ( ) const
inline

Returns the last item as a const reference.

Returns
The last item.

Definition at line 309 of file TArray.h.

◆ begin()

template<class T, class TAlloc>
TArray< T, TAlloc >::Iterator cppcore::TArray< T, TAlloc >::begin ( )
inline

Returns an iterator showing to the first item.

Returns
An iterator showing to the first item.
Remarks
If the array is empty, end() will be returned.

Definition at line 457 of file TArray.h.

References end(), and size().

Referenced by linearSearch(), and remove().

◆ capacity()

template<class T, class TAlloc>
size_t cppcore::TArray< T, TAlloc >::capacity ( ) const
inline

The current capacity will be returned.

Returns
The current capacity.

Definition at line 428 of file TArray.h.

Referenced by reserve().

◆ clear()

template<class T, class TAlloc>
void cppcore::TArray< T, TAlloc >::clear ( )
inline

The array will be cleared, destructor of the items will be called.

Definition at line 449 of file TArray.h.

Referenced by ~TArray().

◆ data()

template<class T, class TAlloc>
T * cppcore::TArray< T, TAlloc >::data ( ) const
inline

Will return the data pointer.

Returns
The data pointer.

Definition at line 474 of file TArray.h.

◆ destroy()

template<class T, class TAlloc>
void cppcore::TArray< T, TAlloc >::destroy ( array_size_type index)
inline

The item at the given index will be destroyed, the destructor will be called manually.

Parameters
[in]indexThe index of the item.

Definition at line 289 of file TArray.h.

Referenced by move(), and remove().

◆ end()

template<class T, class TAlloc>
TArray< T, TAlloc >::Iterator cppcore::TArray< T, TAlloc >::end ( )
inline

Returns an iterator showing to the end of the array.

Returns
An iterator showing the item after the last item.
Remarks
You cannot use this iterator to get access to the last item. Its only a marker for the end of the array.

Definition at line 466 of file TArray.h.

Referenced by begin(), linearSearch(), and remove().

◆ front()

template<class T, class TAlloc>
T & cppcore::TArray< T, TAlloc >::front ( )
inline

Returns the first item.

Returns
The first item.

Definition at line 295 of file TArray.h.

◆ isEmpty()

template<class T, class TAlloc>
bool cppcore::TArray< T, TAlloc >::isEmpty ( ) const
inline

Returns true, if the array is empty.

Returns
true, if the array is empty, false if not.

Definition at line 433 of file TArray.h.

Referenced by operator=(), removeBack(), and set().

◆ linearSearch()

template<class T, class TAlloc>
TArray< T, TAlloc >::Iterator cppcore::TArray< T, TAlloc >::linearSearch ( const T & item)
inline

Performs a linear search for a given item in the array.

Parameters
[in]itemThe item to look for.
Returns
An iterator showing to the position will be returned.

Definition at line 438 of file TArray.h.

References begin(), and end().

◆ move()

template<class T, class TAlloc>
void cppcore::TArray< T, TAlloc >::move ( array_size_type startIdx,
array_size_type endIdx )
inline

Moves the items from the start- to the end-index.

Parameters
[in]startIdxThe start index.
[in]endIdxThe last index.

Definition at line 316 of file TArray.h.

References destroy(), cppcore::Details::getGrowing(), and resize().

Referenced by remove().

◆ operator=()

template<class T, class TAlloc>
TArray< T, TAlloc > & cppcore::TArray< T, TAlloc >::operator= ( const TArray< T, TAlloc > & rhs)
inline

The assignment operator.

Definition at line 487 of file TArray.h.

References isEmpty(), resize(), size(), and TArray().

◆ operator==()

template<class T, class TAlloc>
bool cppcore::TArray< T, TAlloc >::operator== ( const TArray< T, TAlloc > & rOther) const
inline

The compare operator.

Definition at line 507 of file TArray.h.

References TArray().

◆ operator[]()

template<class T, class TAlloc>
T & cppcore::TArray< T, TAlloc >::operator[] ( array_size_type idx) const
inline

The [] operator.

Definition at line 479 of file TArray.h.

◆ remove() [1/2]

template<class T, class TAlloc>
void cppcore::TArray< T, TAlloc >::remove ( array_size_type index)
inline

Removes an item at the given index.

Parameters
[in]indexThe index of the item to remove.

Definition at line 236 of file TArray.h.

References destroy(), and move().

Referenced by remove(), and removeBack().

◆ remove() [2/2]

template<class T, class TAlloc>
void cppcore::TArray< T, TAlloc >::remove ( Iterator it)
inline

Removes the item describes by a given iterator.

Parameters
[in]itThe iterator describing the position.

Definition at line 250 of file TArray.h.

References begin(), end(), and remove().

◆ removeBack()

template<class T, class TAlloc>
void cppcore::TArray< T, TAlloc >::removeBack ( )
inline

The last item will be removed.

Definition at line 271 of file TArray.h.

References isEmpty(), remove(), and size().

◆ reserve()

template<class T, class TAlloc>
void cppcore::TArray< T, TAlloc >::reserve ( array_size_type capacity)
inline

Ensures, that the capacity of the array is big enough for the given size.

Parameters
[in]capacityThe new capacity.
Remarks
The size will not be modified.

Definition at line 352 of file TArray.h.

References capacity().

Referenced by add(), add(), and TArray().

◆ resize() [1/2]

template<class T, class TAlloc>
void cppcore::TArray< T, TAlloc >::resize ( array_size_type size)
inline

Resize the array, new items will be created.

Parameters
[in]sizeThe new size for the array.

Definition at line 382 of file TArray.h.

References size().

Referenced by move(), operator=(), resize(), TArray(), and TArray().

◆ resize() [2/2]

template<class T, class TAlloc>
void cppcore::TArray< T, TAlloc >::resize ( array_size_type size,
T val )
inline

Resize the array, new items will be created.

Parameters
[in]sizeThe new size for the array.
[in]valThe value for initialization.

Definition at line 413 of file TArray.h.

References resize(), and size().

◆ set()

template<class T, class TAlloc>
void cppcore::TArray< T, TAlloc >::set ( const T & value)
inline

Will set all items to the value.

Parameters
valueThe value to set.

Definition at line 278 of file TArray.h.

References isEmpty(), and size().

◆ size()

template<class T, class TAlloc>
size_t cppcore::TArray< T, TAlloc >::size ( ) const
inline

The current size of the array will be returned.

Returns
The current size.

Definition at line 423 of file TArray.h.

Referenced by begin(), operator=(), removeBack(), resize(), resize(), set(), and TArray().


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