Gamedev Framework (gf)  0.9.0
A C++14 framework for 2D games
Public Member Functions | List of all members
gf::Array2D< T, I > Class Template Reference

A two-dimensional array. More...

#include <gf/Array2D.h>

Public Member Functions

 Array2D ()
 Default constructor. More...
 
 Array2D (Vector< I, 2 > size)
 Constructor with a size. More...
 
 Array2D (Vector< I, 2 > size, const T &value)
 Constructor with a size and a value. More...
 
 Array2D (const Array2D &)=default
 Default copy constructor. More...
 
Array2Doperator= (const Array2D &)=default
 Default copy assignment. More...
 
 Array2D (Array2D &&)=default
 Default move constructor. More...
 
Array2Doperator= (Array2D &&)=default
 Default move assignement. More...
 
void swap (Array2D &other)
 Swap with another array. More...
 
Raw data access
const TgetDataPtr () const noexcept
 Get the pointer to raw data. More...
 
std::size_t getDataSize () const noexcept
 Get the raw data size. More...
 
constexpr Vector< I, 2 > getSize () const noexcept
 Get the size of the array. More...
 
constexpr I getCols () const noexcept
 Get the number of columns. More...
 
constexpr I getRows () const noexcept
 Get the number of rows. More...
 
constexpr bool isEmpty () const noexcept
 Check if the array is empty. More...
 
constexpr bool isValid (Vector< I, 2 > pos) const noexcept
 Check if a position is valid. More...
 
Elements access
Toperator() (Vector< I, 2 > pos)
 Get the element at a given 2D position. More...
 
Toperator() (std::size_t index)
 Get the element at a given 1D index. More...
 
const Toperator() (Vector< I, 2 > pos) const
 Get the element at a given 2D position. More...
 
const Toperator() (std::size_t index) const
 Get the element at a given 1D index. More...
 
constexpr Vector< I, 2 > toPosition (std::size_t pos) const noexcept
 Transform a 1D position into a 2D position. More...
 
Visitors
template<typename Func >
void visit4Neighbors (Vector< I, 2 > pos, Func func)
 Visit the 4 neighbors of a given position. More...
 
template<typename Func >
void visit4Neighbors (Vector< I, 2 > pos, Func func) const
 Visit the 4 neighbors of a given position. More...
 
template<typename Func >
void visit12Neighbors (Vector< I, 2 > pos, Func func)
 Visit the 12 neighbors of a given position. More...
 
template<typename Func >
void visit12Neighbors (Vector< I, 2 > pos, Func func) const
 Visit the 12 neighbors of a given position. More...
 
template<typename Func >
void visit8Neighbors (Vector< I, 2 > pos, Func func)
 Visit the 8 neighbors of a given position. More...
 
template<typename Func >
void visit8Neighbors (Vector< I, 2 > pos, Func func) const
 Visit the 8 neighbors of a given position. More...
 
template<typename Func >
void visit24Neighbors (Vector< I, 2 > pos, Func func)
 Visit the 24 neighbors of a given position. More...
 
template<typename Func >
void visit24Neighbors (Vector< I, 2 > pos, Func func) const
 Visit the 24 neighbors of a given position. More...
 
Iterators and ranges
const Tbegin () const noexcept
 Get an iterator to the first element of the array. More...
 
const Tend () const noexcept
 Get an iterator to the element following the last element of the array. More...
 
Tbegin () noexcept
 Get an iterator to the first element of the array. More...
 
Tend () noexcept
 Get an iterator to the element following the last element of the array. More...
 
constexpr RangeZ getIndexRange () const noexcept
 Get the 1D index range of the array. More...
 
constexpr Range< IgetRowRange () const noexcept
 Get the row range. More...
 
constexpr Range< IgetColRange () const noexcept
 Get the column range. More...
 
constexpr PositionRange< IgetPositionRange () const noexcept
 Get the position range. More...
 

Detailed Description

template<typename T, typename I = unsigned>
class gf::Array2D< T, I >

A two-dimensional array.

gf::Array represents a two-dimensional array, organized in row-major order.

The array is templated with the type of the data and the type of the indices (defaults to unsigned).

Contrary to the usual way of accessing 2D arrays, the first coordinate is the column and the second coordinate is the row. So that, if size is the size of the array and pos is the position in the array:

Some convinient visitors are provided to visit the four neighbors (up, down, left and right), or the eight neighbors.

See also
gf::Matrix

Constructor & Destructor Documentation

◆ Array2D() [1/5]

template<typename T, typename I = unsigned>
gf::Array2D< T, I >::Array2D ( )
inline

Default constructor.

Creates an empty array.

◆ Array2D() [2/5]

template<typename T, typename I = unsigned>
gf::Array2D< T, I >::Array2D ( Vector< I, 2 >  size)
inline

Constructor with a size.

Parameters
sizeThe size of the array

◆ Array2D() [3/5]

template<typename T, typename I = unsigned>
gf::Array2D< T, I >::Array2D ( Vector< I, 2 >  size,
const T value 
)
inline

Constructor with a size and a value.

Parameters
sizeThe size of the array
valueThe initial value in the array

◆ Array2D() [4/5]

template<typename T, typename I = unsigned>
gf::Array2D< T, I >::Array2D ( const Array2D< T, I > &  )
default

Default copy constructor.

◆ Array2D() [5/5]

template<typename T, typename I = unsigned>
gf::Array2D< T, I >::Array2D ( Array2D< T, I > &&  )
default

Default move constructor.

Member Function Documentation

◆ begin() [1/2]

template<typename T, typename I = unsigned>
const T* gf::Array2D< T, I >::begin ( ) const
inlinenoexcept

Get an iterator to the first element of the array.

Returns
A begin iterator to the array
See also
end()

◆ begin() [2/2]

template<typename T, typename I = unsigned>
T* gf::Array2D< T, I >::begin ( )
inlinenoexcept

Get an iterator to the first element of the array.

Returns
A begin iterator to the array
See also
end()

◆ end() [1/2]

template<typename T, typename I = unsigned>
const T* gf::Array2D< T, I >::end ( ) const
inlinenoexcept

Get an iterator to the element following the last element of the array.

Returns
An end iterator to the array
See also
begin()

◆ end() [2/2]

template<typename T, typename I = unsigned>
T* gf::Array2D< T, I >::end ( )
inlinenoexcept

Get an iterator to the element following the last element of the array.

Returns
An end iterator to the array
See also
begin()

◆ getColRange()

template<typename T, typename I = unsigned>
constexpr Range<I> gf::Array2D< T, I >::getColRange ( ) const
inlinenoexcept

Get the column range.

Returns
A range with all the columns

◆ getCols()

template<typename T, typename I = unsigned>
constexpr I gf::Array2D< T, I >::getCols ( ) const
inlinenoexcept

Get the number of columns.

Returns
The number of columns

◆ getDataPtr()

template<typename T, typename I = unsigned>
const T* gf::Array2D< T, I >::getDataPtr ( ) const
inlinenoexcept

Get the pointer to raw data.

The returned pointer is const so you can not modify the array with this function.

Returns
The pointer to raw data

◆ getDataSize()

template<typename T, typename I = unsigned>
std::size_t gf::Array2D< T, I >::getDataSize ( ) const
inlinenoexcept

Get the raw data size.

Returns
The total number of elements in the array

◆ getIndexRange()

template<typename T, typename I = unsigned>
constexpr RangeZ gf::Array2D< T, I >::getIndexRange ( ) const
inlinenoexcept

Get the 1D index range of the array.

Returns
A range with all the 1D index in the array

◆ getPositionRange()

template<typename T, typename I = unsigned>
constexpr PositionRange<I> gf::Array2D< T, I >::getPositionRange ( ) const
inlinenoexcept

Get the position range.

Returns
A range for iterating among the positions

◆ getRowRange()

template<typename T, typename I = unsigned>
constexpr Range<I> gf::Array2D< T, I >::getRowRange ( ) const
inlinenoexcept

Get the row range.

Returns
A range with all the rows

◆ getRows()

template<typename T, typename I = unsigned>
constexpr I gf::Array2D< T, I >::getRows ( ) const
inlinenoexcept

Get the number of rows.

Returns
The number of rows

◆ getSize()

template<typename T, typename I = unsigned>
constexpr Vector<I, 2> gf::Array2D< T, I >::getSize ( ) const
inlinenoexcept

Get the size of the array.

Returns
The size of the array

◆ isEmpty()

template<typename T, typename I = unsigned>
constexpr bool gf::Array2D< T, I >::isEmpty ( ) const
inlinenoexcept

Check if the array is empty.

An empty array is an array with \( 0 \) elements, i.e. either the number of columns is \( 0 \) or the number of rows is \( 0 \).

Returns
True if the array is empty

◆ isValid()

template<typename T, typename I = unsigned>
constexpr bool gf::Array2D< T, I >::isValid ( Vector< I, 2 >  pos) const
inlinenoexcept

Check if a position is valid.

A valid position is a position inside the array

Returns
True if the position is valid

◆ operator()() [1/4]

template<typename T, typename I = unsigned>
T& gf::Array2D< T, I >::operator() ( Vector< I, 2 >  pos)
inline

Get the element at a given 2D position.

Parameters
posThe 2D position of the element

◆ operator()() [2/4]

template<typename T, typename I = unsigned>
T& gf::Array2D< T, I >::operator() ( std::size_t  index)
inline

Get the element at a given 1D index.

Parameters
indexThe 1D index of the element
See also
getPositionRange()

◆ operator()() [3/4]

template<typename T, typename I = unsigned>
const T& gf::Array2D< T, I >::operator() ( Vector< I, 2 >  pos) const
inline

Get the element at a given 2D position.

Parameters
posThe 2D position of the element

◆ operator()() [4/4]

template<typename T, typename I = unsigned>
const T& gf::Array2D< T, I >::operator() ( std::size_t  index) const
inline

Get the element at a given 1D index.

Parameters
indexThe 1D index of the element
See also
getPositionRange()

◆ operator=() [1/2]

template<typename T, typename I = unsigned>
Array2D& gf::Array2D< T, I >::operator= ( const Array2D< T, I > &  )
default

Default copy assignment.

◆ operator=() [2/2]

template<typename T, typename I = unsigned>
Array2D& gf::Array2D< T, I >::operator= ( Array2D< T, I > &&  )
default

Default move assignement.

◆ swap()

template<typename T, typename I = unsigned>
void gf::Array2D< T, I >::swap ( Array2D< T, I > &  other)
inline

Swap with another array.

Parameters
otherAn other array

◆ toPosition()

template<typename T, typename I = unsigned>
constexpr Vector<I, 2> gf::Array2D< T, I >::toPosition ( std::size_t  pos) const
inlinenoexcept

Transform a 1D position into a 2D position.

Parameters
posA 1D position
Returns
The corresponding 2D position

◆ visit12Neighbors() [1/2]

template<typename T, typename I = unsigned>
template<typename Func >
void gf::Array2D< T, I >::visit12Neighbors ( Vector< I, 2 >  pos,
Func  func 
)
inline

Visit the 12 neighbors of a given position.

This function calls a callback function for every neighbor in the vertical and horizontal direction. The function checks if the neighbor actually exists.

The callback function has the following prototype:

void callback(Vector<I, 2> pos, T value);
// pos is the position of the neighbor
// value is the value of the neighbor

The callback function can be a simple function but also a lambda expression.

Parameters
posThe position
funcA callback function

◆ visit12Neighbors() [2/2]

template<typename T, typename I = unsigned>
template<typename Func >
void gf::Array2D< T, I >::visit12Neighbors ( Vector< I, 2 >  pos,
Func  func 
) const
inline

Visit the 12 neighbors of a given position.

This function calls a callback function for every neighbor in the vertical and horizontal direction. The function checks if the neighbor actually exists.

The callback function has the following prototype:

void callback(Vector<I, 2> pos, T value);
// pos is the position of the neighbor
// value is the value of the neighbor

The callback function can be a simple function but also a lambda expression.

Parameters
posThe position
funcA callback function

◆ visit24Neighbors() [1/2]

template<typename T, typename I = unsigned>
template<typename Func >
void gf::Array2D< T, I >::visit24Neighbors ( Vector< I, 2 >  pos,
Func  func 
)
inline

Visit the 24 neighbors of a given position.

This function calls a callback function for every neighbor in the vertical, horizontal and diagonal direction. The function checks if the neighbor actually exists.

The callback function has the following prototype:

void callback(Vector<I, 2> pos, T value);
// pos is the position of the neighbor
// value is the value of the neighbor

The callback function can be a simple function but also a lambda expression.

Parameters
posThe position
funcA callback function

◆ visit24Neighbors() [2/2]

template<typename T, typename I = unsigned>
template<typename Func >
void gf::Array2D< T, I >::visit24Neighbors ( Vector< I, 2 >  pos,
Func  func 
) const
inline

Visit the 24 neighbors of a given position.

This function calls a callback function for every neighbor in the vertical, horizontal and diagonal direction. The function checks if the neighbor actually exists.

The callback function has the following prototype:

void callback(Vector<I, 2> pos, T value);
// pos is the position of the neighbor
// value is the value of the neighbor

The callback function can be a simple function but also a lambda expression.

Parameters
posThe position
funcA callback function

◆ visit4Neighbors() [1/2]

template<typename T, typename I = unsigned>
template<typename Func >
void gf::Array2D< T, I >::visit4Neighbors ( Vector< I, 2 >  pos,
Func  func 
)
inline

Visit the 4 neighbors of a given position.

This function calls a callback function for every neighbor in the vertical and horizontal direction. The function checks if the neighbor actually exists.

The callback function has the following prototype:

void callback(Vector<I, 2> pos, T value);
// pos is the position of the neighbor
// value is the value of the neighbor

The callback function can be a simple function but also a lambda expression.

Parameters
posThe position
funcA callback function

◆ visit4Neighbors() [2/2]

template<typename T, typename I = unsigned>
template<typename Func >
void gf::Array2D< T, I >::visit4Neighbors ( Vector< I, 2 >  pos,
Func  func 
) const
inline

Visit the 4 neighbors of a given position.

This function calls a callback function for every neighbor in the vertical and horizontal direction. The function checks if the neighbor actually exists.

The callback function has the following prototype:

void callback(Vector<I, 2> pos, T value);
// pos is the position of the neighbor
// value is the value of the neighbor

The callback function can be a simple function but also a lambda expression.

Parameters
posThe position
funcA callback function

◆ visit8Neighbors() [1/2]

template<typename T, typename I = unsigned>
template<typename Func >
void gf::Array2D< T, I >::visit8Neighbors ( Vector< I, 2 >  pos,
Func  func 
)
inline

Visit the 8 neighbors of a given position.

This function calls a callback function for every neighbor in the vertical, horizontal and diagonal direction. The function checks if the neighbor actually exists.

The callback function has the following prototype:

void callback(Vector<I, 2> pos, T value);
// pos is the position of the neighbor
// value is the value of the neighbor

The callback function can be a simple function but also a lambda expression.

Parameters
posThe position
funcA callback function

◆ visit8Neighbors() [2/2]

template<typename T, typename I = unsigned>
template<typename Func >
void gf::Array2D< T, I >::visit8Neighbors ( Vector< I, 2 >  pos,
Func  func 
) const
inline

Visit the 8 neighbors of a given position.

This function calls a callback function for every neighbor in the vertical, horizontal and diagonal direction. The function checks if the neighbor actually exists.

The callback function has the following prototype:

void callback(Vector<I, 2> pos, T value);
// pos is the position of the neighbor
// value is the value of the neighbor

The callback function can be a simple function but also a lambda expression.

Parameters
posThe position
funcA callback function