Jet  v1.3.3
Public Types | Public Member Functions | List of all members
jet::Array< T, 2 > Class Template Referencefinal

2-D array class. More...

#include <jet/array2.h>

Public Types

typedef std::vector< T > ContainerType
 
typedef ContainerType::iterator Iterator
 
typedef ContainerType::const_iterator ConstIterator
 

Public Member Functions

 Array ()
 Constructs zero-sized 2-D array. More...
 
 Array (const Size2 &size, const T &initVal=T())
 
 Array (size_t width, size_t height, const T &initVal=T())
 
 Array (const std::initializer_list< std::initializer_list< T >> &lst)
 Constructs 2-D array with given initializer list lst. More...
 
 Array (const Array &other)
 Copy constructor. More...
 
 Array (Array &&other)
 Move constructor. More...
 
void set (const T &value)
 Sets entire array with given value. More...
 
void set (const Array &other)
 Copies given array other to this array. More...
 
void set (const std::initializer_list< std::initializer_list< T >> &lst)
 
void clear ()
 Clears the array and resizes to zero. More...
 
void resize (const Size2 &size, const T &initVal=T())
 Resizes the array with size and fill the new element with initVal. More...
 
void resize (size_t width, size_t height, const T &initVal=T())
 
T & at (size_t i)
 Returns the reference to the i-th element. More...
 
const T & at (size_t i) const
 Returns the const reference to the i-th element. More...
 
T & at (const Point2UI &pt)
 Returns the reference to the element at (pt.x, pt.y). More...
 
const T & at (const Point2UI &pt) const
 Returns the const reference to the element at (pt.x, pt.y). More...
 
T & at (size_t i, size_t j)
 Returns the reference to the element at (i, j). More...
 
const T & at (size_t i, size_t j) const
 Returns the const reference to the element at (i, j). More...
 
Size2 size () const
 Returns the size of the array. More...
 
size_t width () const
 Returns the width of the array. More...
 
size_t height () const
 Returns the height of the array. More...
 
T * data ()
 Returns the raw pointer to the array data. More...
 
const T *const data () const
 Returns the const raw pointer to the array data. More...
 
Iterator begin ()
 Returns the begin iterator of the array. More...
 
ConstIterator begin () const
 Returns the begin const iterator of the array. More...
 
Iterator end ()
 Returns the end iterator of the array. More...
 
ConstIterator end () const
 Returns the end const iterator of the array. More...
 
ArrayAccessor2< T > accessor ()
 Returns the array accessor. More...
 
ConstArrayAccessor2< T > constAccessor () const
 Returns the const array accessor. More...
 
void swap (Array &other)
 Swaps the content of the array with other array. More...
 
template<typename Callback >
void forEach (Callback func) const
 Iterates the array and invoke given func for each index. More...
 
template<typename Callback >
void forEachIndex (Callback func) const
 Iterates the array and invoke given func for each index. More...
 
template<typename Callback >
void parallelForEach (Callback func)
 Iterates the array and invoke given func for each index in parallel. More...
 
template<typename Callback >
void parallelForEachIndex (Callback func) const
 Iterates the array and invoke given func for each index in parallel using multi-threading. More...
 
T & operator[] (size_t i)
 Returns the reference to the i-th element. More...
 
const T & operator[] (size_t i) const
 Returns the const reference to the i-th element. More...
 
T & operator() (const Point2UI &pt)
 Returns the reference to the element at (pt.x, pt.y). More...
 
const T & operator() (const Point2UI &pt) const
 Returns the const reference to the element at (pt.x, pt.y). More...
 
T & operator() (size_t i, size_t j)
 Returns the reference to the element at (i, j). More...
 
const T & operator() (size_t i, size_t j) const
 Returns the const reference to the element at (i, j). More...
 
Arrayoperator= (const T &other)
 Sets entire array with given value. More...
 
Arrayoperator= (const Array &other)
 Copies given array other to this array. More...
 
Arrayoperator= (Array &&other)
 Move assignment. More...
 
Arrayoperator= (const std::initializer_list< std::initializer_list< T >> &lst)
 
 operator ArrayAccessor2< T > ()
 Casts to array accessor. More...
 
 operator ConstArrayAccessor2< T > () const
 Casts to const array accessor. More...
 

Detailed Description

template<typename T>
class jet::Array< T, 2 >

2-D array class.

This class represents 2-D array data structure. Internally, the 2-D data is mapped to a linear array such that (i, j) element is actually stroed at (i + (width * j))th element of the linear array. This mapping means iterating i first and j next will give better performance such as:

Array<int, 2> array;
for (size_t j = 0; j < array.height(); ++j) {
for (size_t i = 0; i < array.width(); ++i) {
// Read or write array(i, j)
}
}
Template Parameters
T- Type to store in the array.

Member Typedef Documentation

◆ ConstIterator

template<typename T >
typedef ContainerType::const_iterator jet::Array< T, 2 >::ConstIterator

◆ ContainerType

template<typename T >
typedef std::vector<T> jet::Array< T, 2 >::ContainerType

◆ Iterator

template<typename T >
typedef ContainerType::iterator jet::Array< T, 2 >::Iterator

Constructor & Destructor Documentation

◆ Array() [1/6]

template<typename T >
jet::Array< T, 2 >::Array ( )

Constructs zero-sized 2-D array.

◆ Array() [2/6]

template<typename T >
jet::Array< T, 2 >::Array ( const Size2 size,
const T &  initVal = T() 
)
explicit

Constructs 2-D array with given size and fill it with initVal.

Parameters
sizeInitial size of the array.
initValInitial value of each array element.

◆ Array() [3/6]

template<typename T >
jet::Array< T, 2 >::Array ( size_t  width,
size_t  height,
const T &  initVal = T() 
)

Constructs 2-D array with size width x height and fill it with initVal.

Parameters
widthInitial width of the array.
heightInitial height of the array.
initValInitial value of each array element.

◆ Array() [4/6]

template<typename T >
jet::Array< T, 2 >::Array ( const std::initializer_list< std::initializer_list< T >> &  lst)

Constructs 2-D array with given initializer list lst.

This constructor will build 2-D array with given initializer list lst such as

Array<int, 2> arr = {
{1, 2, 4},
{9, 3, 5}
};

Note the initializer also has 2-D structure. The code above will construct 3 x 2 array.

Parameters
lstInitializer list that should be copy to the new array.

◆ Array() [5/6]

template<typename T >
jet::Array< T, 2 >::Array ( const Array< T, 2 > &  other)

Copy constructor.

◆ Array() [6/6]

template<typename T >
jet::Array< T, 2 >::Array ( Array< T, 2 > &&  other)

Move constructor.

Member Function Documentation

◆ accessor()

template<typename T >
ArrayAccessor2<T> jet::Array< T, 2 >::accessor ( )

Returns the array accessor.

◆ at() [1/6]

template<typename T >
T& jet::Array< T, 2 >::at ( const Point2UI pt)

Returns the reference to the element at (pt.x, pt.y).

◆ at() [2/6]

template<typename T >
const T& jet::Array< T, 2 >::at ( const Point2UI pt) const

Returns the const reference to the element at (pt.x, pt.y).

◆ at() [3/6]

template<typename T >
T& jet::Array< T, 2 >::at ( size_t  i)

Returns the reference to the i-th element.

This function returns the reference to the i-th element of the array where i is the index of linearly mapped elements such that i = x + (width * y) (x and y are the 2-D coordinates of the element).

◆ at() [4/6]

template<typename T >
const T& jet::Array< T, 2 >::at ( size_t  i) const

Returns the const reference to the i-th element.

This function returns the const reference to the i-th element of the array where i is the index of linearly mapped elements such that i = x + (width * y) (x and y are the 2-D coordinates of the element).

◆ at() [5/6]

template<typename T >
T& jet::Array< T, 2 >::at ( size_t  i,
size_t  j 
)

Returns the reference to the element at (i, j).

◆ at() [6/6]

template<typename T >
const T& jet::Array< T, 2 >::at ( size_t  i,
size_t  j 
) const

Returns the const reference to the element at (i, j).

◆ begin() [1/2]

template<typename T >
Iterator jet::Array< T, 2 >::begin ( )

Returns the begin iterator of the array.

◆ begin() [2/2]

template<typename T >
ConstIterator jet::Array< T, 2 >::begin ( ) const

Returns the begin const iterator of the array.

◆ clear()

template<typename T >
void jet::Array< T, 2 >::clear ( )

Clears the array and resizes to zero.

◆ constAccessor()

template<typename T >
ConstArrayAccessor2<T> jet::Array< T, 2 >::constAccessor ( ) const

Returns the const array accessor.

◆ data() [1/2]

template<typename T >
T* jet::Array< T, 2 >::data ( )

Returns the raw pointer to the array data.

◆ data() [2/2]

template<typename T >
const T* const jet::Array< T, 2 >::data ( ) const

Returns the const raw pointer to the array data.

◆ end() [1/2]

template<typename T >
Iterator jet::Array< T, 2 >::end ( )

Returns the end iterator of the array.

◆ end() [2/2]

template<typename T >
ConstIterator jet::Array< T, 2 >::end ( ) const

Returns the end const iterator of the array.

◆ forEach()

template<typename T >
template<typename Callback >
void jet::Array< T, 2 >::forEach ( Callback  func) const

Iterates the array and invoke given func for each index.

This function iterates the array elements and invoke the callback function func. The callback function takes array's element as its input. The order of execution will be the same as the nested for-loop below:

Array<int, 2> array;
for (size_t j = 0; j < array.height(); ++j) {
for (size_t i = 0; i < array.width(); ++i) {
func(array(i, j));
}
}

Below is the sample usage:

Array<int, 2> array(100, 200, 4);
array.forEach([](int elem) {
printf("%d\n", elem);
});

◆ forEachIndex()

template<typename T >
template<typename Callback >
void jet::Array< T, 2 >::forEachIndex ( Callback  func) const

Iterates the array and invoke given func for each index.

This function iterates the array elements and invoke the callback function func. The callback function takes two parameters which are the (i, j) indices of the array. The order of execution will be the same as the nested for-loop below:

Array<int, 2> array;
for (size_t j = 0; j < array.height(); ++j) {
for (size_t i = 0; i < array.width(); ++i) {
func(i, j);
}
}

Below is the sample usage:

Array<int, 2> array(10, 20, 4);
array.forEachIndex([&](size_t i, size_t j) {
array(i, j) = 4.f * i + 7.f * j + 1.5f;
});

◆ height()

template<typename T >
size_t jet::Array< T, 2 >::height ( ) const

Returns the height of the array.

◆ operator ArrayAccessor2< T >()

template<typename T >
jet::Array< T, 2 >::operator ArrayAccessor2< T > ( )

Casts to array accessor.

◆ operator ConstArrayAccessor2< T >()

template<typename T >
jet::Array< T, 2 >::operator ConstArrayAccessor2< T > ( ) const

Casts to const array accessor.

◆ operator()() [1/4]

template<typename T >
T& jet::Array< T, 2 >::operator() ( const Point2UI pt)

Returns the reference to the element at (pt.x, pt.y).

◆ operator()() [2/4]

template<typename T >
const T& jet::Array< T, 2 >::operator() ( const Point2UI pt) const

Returns the const reference to the element at (pt.x, pt.y).

◆ operator()() [3/4]

template<typename T >
T& jet::Array< T, 2 >::operator() ( size_t  i,
size_t  j 
)

Returns the reference to the element at (i, j).

◆ operator()() [4/4]

template<typename T >
const T& jet::Array< T, 2 >::operator() ( size_t  i,
size_t  j 
) const

Returns the const reference to the element at (i, j).

◆ operator=() [1/4]

template<typename T >
Array& jet::Array< T, 2 >::operator= ( Array< T, 2 > &&  other)

Move assignment.

◆ operator=() [2/4]

template<typename T >
Array& jet::Array< T, 2 >::operator= ( const Array< T, 2 > &  other)

Copies given array other to this array.

◆ operator=() [3/4]

template<typename T >
Array& jet::Array< T, 2 >::operator= ( const std::initializer_list< std::initializer_list< T >> &  lst)

Copies given initializer list lst to this array.

This function copies given initializer list lst to the array such as

Array<int, 2> arr;
arr = {
{1, 2, 4},
{9, 3, 5}
};

Note the initializer also has 2-D structure. The code above will build 3 x 2 array.

Parameters
lstInitializer list that should be copy to the new array.

◆ operator=() [4/4]

template<typename T >
Array& jet::Array< T, 2 >::operator= ( const T &  other)

Sets entire array with given value.

◆ operator[]() [1/2]

template<typename T >
T& jet::Array< T, 2 >::operator[] ( size_t  i)

Returns the reference to the i-th element.

This function returns the reference to the i-th element of the array where i is the index of linearly mapped elements such that i = x + (width * y) (x and y are the 2-D coordinates of the element).

See also
Array<T, 2>::at

◆ operator[]() [2/2]

template<typename T >
const T& jet::Array< T, 2 >::operator[] ( size_t  i) const

Returns the const reference to the i-th element.

This function returns the const reference to the i-th element of the array where i is the index of linearly mapped elements such that i = x + (width * y) (x and y are the 2-D coordinates of the element).

See also
Array<T, 2>::at

◆ parallelForEach()

template<typename T >
template<typename Callback >
void jet::Array< T, 2 >::parallelForEach ( Callback  func)

Iterates the array and invoke given func for each index in parallel.

This function iterates the array elements and invoke the callback function func. The callback function takes array's element as its input. The order of execution will be non-deterministic since it runs in parallel. Below is the sample usage:

Array<int, 2> array(100, 200, 4);
array.parallelForEach([](int& elem) {
elem *= 2;
});

The parameter type of the callback function doesn't have to be T&, but const T& or T can be used as well.

◆ parallelForEachIndex()

template<typename T >
template<typename Callback >
void jet::Array< T, 2 >::parallelForEachIndex ( Callback  func) const

Iterates the array and invoke given func for each index in parallel using multi-threading.

This function iterates the array elements and invoke the callback function func in parallel using multi-threading. The callback function takes two parameters which are the (i, j) indices of the array. The order of execution will be non-deterministic since it runs in parallel. Below is the sample usage:

Array<int, 2> array(100, 200, 4);
array.parallelForEachIndex([&](size_t i, size_t j) {
array(i, j) *= 2;
});

◆ resize() [1/2]

template<typename T >
void jet::Array< T, 2 >::resize ( const Size2 size,
const T &  initVal = T() 
)

Resizes the array with size and fill the new element with initVal.

◆ resize() [2/2]

template<typename T >
void jet::Array< T, 2 >::resize ( size_t  width,
size_t  height,
const T &  initVal = T() 
)

Resizes the array with size width x height and fill the new element with initVal.

◆ set() [1/3]

template<typename T >
void jet::Array< T, 2 >::set ( const Array< T, 2 > &  other)

Copies given array other to this array.

◆ set() [2/3]

template<typename T >
void jet::Array< T, 2 >::set ( const std::initializer_list< std::initializer_list< T >> &  lst)

Copies given initializer list lst to this array.

This function copies given initializer list lst to the array such as

Array<int, 2> arr;
arr = {
{1, 2, 4},
{9, 3, 5}
};

Note the initializer also has 2-D structure. The code above will build 3 x 2 array.

Parameters
lstInitializer list that should be copy to the new array.

◆ set() [3/3]

template<typename T >
void jet::Array< T, 2 >::set ( const T &  value)

Sets entire array with given value.

◆ size()

template<typename T >
Size2 jet::Array< T, 2 >::size ( ) const

Returns the size of the array.

◆ swap()

template<typename T >
void jet::Array< T, 2 >::swap ( Array< T, 2 > &  other)

Swaps the content of the array with other array.

◆ width()

template<typename T >
size_t jet::Array< T, 2 >::width ( ) const

Returns the width of the array.


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