SPHinXsys  alpha version
Public Member Functions | Protected Attributes | Friends | List of all members
SPH::Array< T > Class Template Reference

#include <array.h>

Public Member Functions

 Array (const T &aDefaultValue=T(), int aSize=0, int aCapacity=Array_CAPMIN)
 Default constructor. More...
 
 Array (const Array< T > &aArray)
 Copy constructor. More...
 
virtual ~Array ()
 Destructor. More...
 
bool arrayEquals (const Array< T > &aArray) const
 A non-operator version of operator ==.
 
T & operator[] (int aIndex) const
 Get the array element at a specified index. This overloaded operator can be used both to set and get element values: More...
 
Array< T > & operator= (const Array< T > &aArray)
 Assign this array to a specified array. This operator makes a complete copy of the specified array; all member variables are copied. So, the result is two identical, independent arrays. More...
 
bool operator== (const Array< T > &aArray) const
 Determine if two arrays are equal. More...
 
bool computeNewCapacity (int aMinCapacity, int &rNewCapacity)
 Compute a new capacity that is at least as large as a specified minimum capacity; this method does not change the capacity, it simply computes a new recommended capacity. More...
 
bool ensureCapacity (int aCapacity)
 Ensure that the capacity of this array is at least the specified amount. Note that the newly allocated array elements are not initialized. More...
 
void trim ()
 
int getCapacity () const
 
void setCapacityIncrement (int aIncrement)
 Set the amount by which the capacity is increased when the capacity of of the array in exceeded. More...
 
int getCapacityIncrement () const
 Get the amount by which the capacity is increased.
 
bool setSize (int aSize)
 
int getSize () const
 Get the size of the array. More...
 
int size () const
 
int append (const T &aValue)
 Append a value onto the array. More...
 
int append (const Array< T > &aArray)
 Append an array of values. More...
 
int append (int aSize, const T *aArray)
 Append an array of values. More...
 
int insert (int aIndex, const T &aValue)
 Insert a value into the array at a specified index. More...
 
int remove (int aIndex)
 Remove a value from the array at a specified index. More...
 
void set (int aIndex, const T &aValue)
 Set the value at a specified index. More...
 
T * get ()
 Get a pointer to the low-level array. More...
 
const T * get () const
 Get a pointer to the low-level array. More...
 
const T & get (int aIndex) const
 Get a const reference to the value at a specified array index. More...
 
const T & getLast () const
 
T & updLast () const
 
int findIndex (const T &aValue) const
 
int rfindIndex (const T &aValue) const
 

Protected Attributes

int size_
 
int capacity_
 
int capacityIncrement_
 
defaultValue_
 
T * array_
 

Friends

std::ostream & operator<< (std::ostream &aOut, const Array< T > &aArray)
 Implementation of the output operator. The output for an array looks like the following:

. More...
 
std::istream & operator>> (std::istream &in, Array< T > &out)
 

Detailed Description

template<class T>
class SPH::Array< T >

@ details A class for storing an array of values of type T. The capacity of the class grows as needed. To use this template for a class of type T, class T should implement the following methods: default constructor, copy constructor, assignment operator (=), equality operator (==), and less than operator (<).

Constructor & Destructor Documentation

◆ Array() [1/2]

template<class T>
SPH::Array< T >::Array ( const T &  aDefaultValue = T(),
int  aSize = 0,
int  aCapacity = Array_CAPMIN 
)
inlineexplicit

Default constructor.

Parameters
aDefaultValueDefault value of an array element. This value is used to initialize array elements as the size of the array is changed.
aSizeInitial size of the array. The array elements are initialized to aDefaultValue.
aCapacityInitial capacity of the array. The initial capacity is guaranteed to be at least as large as aSize + 1.

◆ Array() [2/2]

template<class T>
SPH::Array< T >::Array ( const Array< T > &  aArray)
inline

Copy constructor.

Parameters
aArrayArray to be copied.

◆ ~Array()

template<class T>
virtual SPH::Array< T >::~Array ( )
inlinevirtual

Destructor.

When the array is deleted, references to elements of this array become invalid.

Member Function Documentation

◆ append() [1/3]

template<class T>
int SPH::Array< T >::append ( const T &  aValue)
inline

Append a value onto the array.

Parameters
aValueValue to be appended.
Returns
New size of the array, or, equivalently, the index to the new first empty element of the array.

◆ append() [2/3]

template<class T>
int SPH::Array< T >::append ( const Array< T > &  aArray)
inline

Append an array of values.

Parameters
aArrayArray of values to append.
Returns
New size of the array, or, equivalently, the index to the new first empty element of the array.

◆ append() [3/3]

template<class T>
int SPH::Array< T >::append ( int  aSize,
const T *  aArray 
)
inline

Append an array of values.

Parameters
aSizeSize of the array to append.
aArrayArray of values to append.
Returns
New size of the array, or, equivalently, the index to the new first empty element of the array.

◆ computeNewCapacity()

template<class T>
bool SPH::Array< T >::computeNewCapacity ( int  aMinCapacity,
int &  rNewCapacity 
)
inline

Compute a new capacity that is at least as large as a specified minimum capacity; this method does not change the capacity, it simply computes a new recommended capacity.

If the capacity increment is negative, the current capacity is doubled until the computed capacity is greater than or equal to the specified minimum capacity. If the capacity increment is positive, the current capacity increment by this amount until the computed capacity is greater than or equal to the specified minimum capacity. If the capacity increment is zero, the computed capacity is set to the current capacity and false is returned.

Parameters
rNewCapacityNew computed capacity.
aMinCapacityMinimum new computed capacity. The computed capacity is incremented until it is at least as large as aMinCapacity, assuming the capacity increment is not zero.
Returns
True if the new capacity was increased, false otherwise (i.e., if the capacity increment is set to 0).
See also
setCapacityIncrement()

◆ ensureCapacity()

template<class T>
bool SPH::Array< T >::ensureCapacity ( int  aCapacity)
inline

Ensure that the capacity of this array is at least the specified amount. Note that the newly allocated array elements are not initialized.

Parameters
aCapacityDesired capacity.
Returns
true if the capacity was successfully obtained, false otherwise.

◆ findIndex()

template<class T>
int SPH::Array< T >::findIndex ( const T &  aValue) const
inline

Linear search for an element matching a given value.

Parameters
aValueValue to which the array elements are compared.
Returns
Index of the array element matching aValue. If there is more than one such elements with the same value the index of the first of these elements is returned. If no match is found, -1 is returned.

◆ get() [1/3]

template<class T>
T* SPH::Array< T >::get ( )
inline

Get a pointer to the low-level array.

Returns
Vecder to the low-level array.

◆ get() [2/3]

template<class T>
const T* SPH::Array< T >::get ( ) const
inline

Get a pointer to the low-level array.

Returns
Vecder to the low-level array.

◆ get() [3/3]

template<class T>
const T& SPH::Array< T >::get ( int  aIndex) const
inline

Get a const reference to the value at a specified array index.

If the index is negative or passed the end of the array, an exception is thrown.

For faster execution, the array elements can be accessed through the overloaded operator[], which does no bounds checking.

Parameters
aIndexIndex of the desired array element.
Returns
const reference to the array element.
Exceptions
Exceptionif (aIndex<0)||(aIndex>=size_).
See also
operator[].

◆ getCapacity()

template<class T>
int SPH::Array< T >::getCapacity ( ) const
inline

Get the capacity of this storage instance.

◆ getLast()

template<class T>
const T& SPH::Array< T >::getLast ( ) const
inline

Get the last value in the array.

Returns
Last value in the array.
Exceptions
Exceptionif the array is empty.

◆ getSize()

template<class T>
int SPH::Array< T >::getSize ( ) const
inline

Get the size of the array.

Returns
Size of the array.

◆ insert()

template<class T>
int SPH::Array< T >::insert ( int  aIndex,
const T &  aValue 
)
inline

Insert a value into the array at a specified index.

This method is relatively computationally costly since many of the array elements may need to be shifted.

Parameters
aValueValue to be inserted.
aIndexIndex at which to insert the new value. All current elements from aIndex to the end of the array are shifted one place in the direction of the end of the array. If the specified index is greater than the current size of the array, the size of the array is increased to aIndex+1 and the intervening new elements are initialized to the default value that was specified at the time of construction.
Returns
Size of the array after the insertion.

◆ operator=()

template<class T>
Array<T>& SPH::Array< T >::operator= ( const Array< T > &  aArray)
inline

Assign this array to a specified array. This operator makes a complete copy of the specified array; all member variables are copied. So, the result is two identical, independent arrays.

Parameters
aArrayArray to be copied.
Returns
Reference to this array.

◆ operator==()

template<class T>
bool SPH::Array< T >::operator== ( const Array< T > &  aArray) const
inline

Determine if two arrays are equal.

Two arrays are equal if their contents are equal. That is, each array must be the same length and their corresponding array elements must be equal.

Parameters
aArrayArray to be tested as equal.
Returns
True if equal, false if not equal.

◆ operator[]()

template<class T>
T& SPH::Array< T >::operator[] ( int  aIndex) const
inline

Get the array element at a specified index. This overloaded operator can be used both to set and get element values:

Array<T> array(2);
T value = array[i];
array[i] = value;

This operator is intended for accessing array elements with as little overhead as possible, so no error checking is performed. The caller must make sure the specified index is within the bounds of the array. If error checking is desired, use Array::get().

Parameters
aIndexIndex of the desired element (0 <= aIndex < size_).
Returns
Reference to the array element.
See also
get().

◆ remove()

template<class T>
int SPH::Array< T >::remove ( int  aIndex)
inline

Remove a value from the array at a specified index.

This method is relatively computationally costly since many of the array elements may need to be shifted.

Parameters
aIndexIndex of the value to remove. All elements from aIndex to the end of the array are shifted one place toward the beginning of the array. If aIndex is less than 0 or greater than or equal to the current size of the array, no element is removed.
Returns
Size of the array after the removal.

◆ rfindIndex()

template<class T>
int SPH::Array< T >::rfindIndex ( const T &  aValue) const
inline

Linear search in reverse for an element matching a given value.

Parameters
aValueValue to which the array elements are compared.
Returns
Index of the array element matching aValue. If there is more than one such elements with the same value the index of the last of these elements is returned. If no match is found, -1 is returned.

◆ set()

template<class T>
void SPH::Array< T >::set ( int  aIndex,
const T &  aValue 
)
inline

Set the value at a specified index.

Parameters
aIndexIndex of the array element to be set. It is permissible for aIndex to be past the current end of the array- the capacity will be increased if necessary. Values between the current end of the array and aIndex are not initialized.
aValueValue.

◆ setCapacityIncrement()

template<class T>
void SPH::Array< T >::setCapacityIncrement ( int  aIncrement)
inline

Set the amount by which the capacity is increased when the capacity of of the array in exceeded.

If the specified increment is negative, the capacity is set to double whenever the capacity is exceeded.

Parameters
aIncrementDesired capacity increment.

◆ setSize()

template<class T>
bool SPH::Array< T >::setSize ( int  aSize)
inline

Set the size of the array. This method can be used to either increase or decrease the size of the array. If this size of the array is increased, the new elements are initialized to the default value that was specified at the time of construction.

Note that the size of an array is different than its capacity. The size indicates how many valid elements are stored in an array. The capacity indicates how much the size of the array can be increased without allocated more memory. At all times size <= capacity.

Parameters
aSizeDesired size of the array. The size must be greater than or equal to zero.

◆ size()

template<class T>
int SPH::Array< T >::size ( ) const
inline

Alternate name for getSize().

◆ trim()

template<class T>
void SPH::Array< T >::trim ( )
inline

Trim the capacity of this array so that it is one larger than the size of this array. This is useful for reducing the amount of memory used by this array. This capacity is kept at one larger than the size so that, for example, an array of characters can be treated as a nullptr terminated string.

◆ updLast()

template<class T>
T& SPH::Array< T >::updLast ( ) const
inline

Get writable reference to last value in the array.

Returns
writable reference to Last value in the array.
Exceptions
Exceptionif the array is empty.

Friends And Related Function Documentation

◆ operator<<

template<class T>
std::ostream& operator<< ( std::ostream &  aOut,
const Array< T > &  aArray 
)
friend

Implementation of the output operator. The output for an array looks like the following:

.

T[0] T[1] T[2] ... T[size-1].

Parameters
aOutOutput stream.
aArrayArray to be output.
Returns
Reference to the output stream.

Member Data Documentation

◆ array_

template<class T>
T* SPH::Array< T >::array_
protected

Array of values.

◆ capacity_

template<class T>
int SPH::Array< T >::capacity_
protected

Current capacity of the array.

◆ capacityIncrement_

template<class T>
int SPH::Array< T >::capacityIncrement_
protected

Increment by which the current capacity is increased when the capacity of this storage instance is reached. If negative, capacity doubles.

◆ defaultValue_

template<class T>
T SPH::Array< T >::defaultValue_
protected

Default value of elements.

◆ size_

template<class T>
int SPH::Array< T >::size_
protected

Size of the array. Also the index of the first empty array element.


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