latest version v1.9 - last update 10 Apr 2010 |
Array class. More...
#include <ltiArray.h>
Public Member Functions | |
array () | |
array (int from, int to, const T &iniValue=T()) | |
array (const array< T > &other) | |
array (const vector< T > &other, int theOffset=0) | |
array (bool init, int from, int to) | |
virtual | ~array () |
virtual const char * | getTypeName () const |
void | resize (int from, int to, const T &iniValue=T(), bool copyData=true, bool initNew=true) |
void | shift (int sh) |
void | fill (const T &data, int from=MinInt32, int to=MaxInt32) |
int | firstIdx () const |
void | setFirstIdx (int newIdx) |
int | lastIdx () const |
T & | at (int x) |
const T & | at (int x) const |
T & | operator[] (int x) |
const T & | operator[] (int x) const |
array< T > & | copy (const vector< T > &other) |
array< T > & | copy (const array< T > &other) |
array< T > & | operator= (const vector< T > &other) |
array< T > & | operator= (const array< T > &other) |
template<class U > | |
array< T > & | castFrom (const array< U > &other) |
virtual bool | write (ioHandler &handler, const bool complete=true) const |
virtual bool | read (ioHandler &handler, const bool complete=true) |
Apply Methods | |
array< T > & | apply (T(*function)(T)) |
array< T > & | apply (const array< T > &other, T(*function)(T)) |
array< T > & | apply (T(*function)(const T &)) |
array< T > & | apply (const array< T > &other, T(*function)(const T &)) |
array< T > & | apply (const array< T > &other, T(*function)(const T &, const T &)) |
array< T > & | apply (const array< T > &other, T(*function)(T, T)) |
array< T > & | apply (const array< T > &a, const array< T > &b, T(*function)(const T &, const T &)) |
array< T > & | apply (const array< T > &a, const array< T > &b, T(*function)(T, T)) |
Protected Attributes | |
int | offset |
int | firstArrayElement |
int | lastArrayElement |
T * | theElement0 |
Array class.
A n-dimensional array indexed between from and to, where to-from+1 = n. The indices from and to can be negative, but from must be lower or equal to.
All inherited arithmetical operations still need to be reviewed, since the vector methods will only check if the size n of the array is correct. Use these operations at your own risk. It is safer if you use the on-place version of the arithmetical methods, since they won't change the index range of the array. Otherwise, the range will be change to [0,n-1].
lti::array< T >::array | ( | ) |
default constructor creates an empty array;
lti::array< T >::array | ( | int | from, | |
int | to, | |||
const T & | iniValue = T() | |||
) |
constructor.
creates an array indexed between from
and to
and initializes it with iniValue
lti::array< T >::array | ( | const array< T > & | other | ) |
lti::array< T >::array | ( | const vector< T > & | other, | |
int | theOffset = 0 | |||
) |
lti::array< T >::array | ( | bool | init, | |
int | from, | |||
int | to | |||
) |
If init is true this constructor is equivalent to calling array(int theSize), and thus initializing all elements with T().
However, in some cases the elements need not be initialized during construction, since complex initializion is required. Especially for large arrays, the unnecessary constructor initialization is very time consuming.
If init is false, memory is allocated but no initialization takes place.
init | initialize matrix or not | |
from | first index | |
to | last index |
virtual lti::array< T >::~array | ( | ) | [virtual] |
destructor
array<T>& lti::array< T >::apply | ( | const array< T > & | a, | |
const array< T > & | b, | |||
T(*)(T, T) | function | |||
) |
a two-parameter C-function receives the i-th elements of the given arrays and leaves the result here.
Note that both arrays MUST have the same size! If both arrays have different size, the function will throw an assertion without changing anything!
Reimplemented from lti::genericVector< T >.
array<T>& lti::array< T >::apply | ( | const array< T > & | a, | |
const array< T > & | b, | |||
T(*)(const T &, const T &) | function | |||
) |
a two-parameter C-function receives the i-th elements of the given arrays and leaves the result here.
Note that both arrays MUST have the same size! If both arrays have different size, the function will throw an assertion without changing anything!
The following example uses lti::min as function. The arrays a and b contain the values [1,2,3,4] and [4,3,2,1], respectively. After applying the function, array c contains the values [1,2,2,1].
iarray a,b,c; int i=0; for (i=0; i<4; ++i) { a.at(i)=i+1; b.at(i)=4-i; } c.apply(a,b,lti::min);
Reimplemented from lti::genericVector< T >.
array<T>& lti::array< T >::apply | ( | const array< T > & | other, | |
T(*)(T, T) | function | |||
) |
a two-parameter C-function receives the i-th elements of this and the given array and the result will be left in this array.
Note that both arrays MUST have the same size! If both arrays have different size, the function will throw an assertion without changing anything!
other | the second array to be considered (the first array will be this object!) | |
function | a pointer to a two parameters C-function |
Reimplemented from lti::genericVector< T >.
array<T>& lti::array< T >::apply | ( | const array< T > & | other, | |
T(*)(const T &, const T &) | function | |||
) |
a two-parameter C-function receives the i-th elements of this and the given array and the result will be left in this array.
Note that both arrays MUST have the same size! If both arrays have different size, the function will throw an assertion without changing anything!
other | the second array to be considered (the first array will be this object!) | |
function | a pointer to a two parameters C-function |
Reimplemented from lti::genericVector< T >.
array<T>& lti::array< T >::apply | ( | const array< T > & | other, | |
T(*)(const T &) | function | |||
) |
applies a C-function to each element the other array and leaves the result here.
other | the array with the source data | |
function | a pointer to a C-function |
Reimplemented from lti::genericVector< T >.
array<T>& lti::array< T >::apply | ( | T(*)(const T &) | function | ) |
applies a C-function to each element of the array.
function | a pointer to a C-function |
Reimplemented from lti::genericVector< T >.
array<T>& lti::array< T >::apply | ( | const array< T > & | other, | |
T(*)(T) | function | |||
) |
applies a C-function to each element of the other array and leaves the result here.
other | the source array | |
function | a pointer to a C-function |
Reimplemented from lti::genericVector< T >.
array<T>& lti::array< T >::apply | ( | T(*)(T) | function | ) |
applies a C-function to each element of the array.
In the following example, array vct is initialized with 4.0. After applying sqrt(), all elements of vct are 2.0.
function | a pointer to a C-function |
Reimplemented from lti::genericVector< T >.
const T& lti::array< T >::at | ( | int | x | ) | const [inline] |
const access operator.
access element x with firstIdx() <= x <= lastIdx().
Reimplemented from lti::genericVector< T >.
T& lti::array< T >::at | ( | int | x | ) | [inline] |
access operator.
access element x with firstIdx() <= x <= lastIdx().
Reimplemented from lti::genericVector< T >.
Referenced by lti::array< int >::operator[]().
array<T>& lti::array< T >::castFrom | ( | const array< U > & | other | ) | [inline] |
cast and copy the elements of the "other" array
Reimplemented in lti::kernel1D< T >.
Referenced by lti::array< int >::castFrom().
array<T>& lti::array< T >::copy | ( | const array< T > & | other | ) |
assigment operator.
copy content of array other
Reimplemented from lti::genericVector< T >.
Reimplemented in lti::kernel1D< T >.
array<T>& lti::array< T >::copy | ( | const vector< T > & | other | ) |
void lti::array< T >::fill | ( | const T & | data, | |
int | from = MinInt32 , |
|||
int | to = MaxInt32 | |||
) |
fills the array with value data
between from
and to
.
Reimplemented from lti::genericVector< T >.
int lti::array< T >::firstIdx | ( | ) | const [inline] |
returns first index.
the returned value is equal to from
by construction or resizing.
Reimplemented from lti::genericVector< T >.
Referenced by lti::array< int >::at(), and lti::array< int >::castFrom().
virtual const char* lti::array< T >::getTypeName | ( | ) | const [virtual] |
returns the name of this type
Reimplemented from lti::vector< T >.
Reimplemented in lti::kernel1D< T >, lti::kernel1D< double >, and lti::kernel1D< float >.
int lti::array< T >::lastIdx | ( | ) | const [inline] |
returns last index.
the returned value is equal to to
by contruction or resizing
Reimplemented from lti::genericVector< T >.
Referenced by lti::array< int >::at(), and lti::array< int >::castFrom().
array<T>& lti::array< T >::operator= | ( | const array< T > & | other | ) | [inline] |
array<T>& lti::array< T >::operator= | ( | const vector< T > & | other | ) | [inline] |
assigment operator.
(alias for copy(other).
const T& lti::array< T >::operator[] | ( | int | x | ) | const [inline] |
const access operator (alias for at(x))
Reimplemented from lti::genericVector< T >.
T& lti::array< T >::operator[] | ( | int | x | ) | [inline] |
access operator (alias for at(x)).
Reimplemented from lti::genericVector< T >.
virtual bool lti::array< T >::read | ( | ioHandler & | handler, | |
const bool | complete = true | |||
) | [virtual] |
read the object from the given ioHandler
Reimplemented from lti::genericVector< T >.
Reimplemented in lti::kernel1D< T >, lti::kernel1D< double >, and lti::kernel1D< float >.
void lti::array< T >::resize | ( | int | from, | |
int | to, | |||
const T & | iniValue = T() , |
|||
bool | copyData = true , |
|||
bool | initNew = true | |||
) |
change the dimension and/or range of the array.
If the new dimension of the array (to-from+1) is different from the old one or if this array doesn't own the data, new memory is allocated.
If copyData
is true the data is copied according to the indices, e.g. if the old array is (-2, 2) and contains the values 0 to 4 and a resize(0,4,10) is performed the new array will be [2, 3, 4, 10, 10]. If the indices don't overlap no copying takes place.
To adjust the range of an array, i.e. the data contained and the size stays the same, but the start and end indices change, use shift(int).
If the new size is not equal to the old size (i.e. to-from+1), the array always owns the data afterwards (i.e. new memory is allocated) even if it didn't own the data before. Otherwise the ownership remains unchanged. You can use restoreOwnership() if you just want to own the data.
void lti::array< T >::setFirstIdx | ( | int | newIdx | ) |
change the first index of the array, without changing the content
void lti::array< T >::shift | ( | int | sh | ) |
virtual bool lti::array< T >::write | ( | ioHandler & | handler, | |
const bool | complete = true | |||
) | const [virtual] |
write the object in the given ioHandler
Reimplemented from lti::genericVector< T >.
Reimplemented in lti::kernel1D< T >, lti::kernel1D< double >, and lti::kernel1D< float >.
int lti::array< T >::firstArrayElement [protected] |
index of the first element of the array
Referenced by lti::array< int >::castFrom(), and lti::array< int >::firstIdx().
int lti::array< T >::lastArrayElement [protected] |
index of the last element of the array.
if the array is casted to a vector, the "lastIdx()" should return size()-1 instead of the array lastIdx!. That's the reason why we use a new member for the last index in the array!
Referenced by lti::array< int >::castFrom(), and lti::array< int >::lastIdx().
int lti::array< T >::offset [protected] |
offset for the 0-th element
Referenced by lti::array< int >::castFrom().
T* lti::array< T >::theElement0 [protected] |
A pointer to the element at index 0 (This allows a faster access than adding the offset each time).
Referenced by lti::array< int >::at(), and lti::array< int >::castFrom().