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

A two-dimensional array. More...

#include <gf/Array2D.h>

Public Member Functions

 Array2D ()
 Default constructor. More...
 
 Array2D (Vector2u size)
 Constructor with a size. More...
 
 Array2D (Vector2u 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...
 
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 Vector2u getSize () const noexcept
 Get the size of the array. More...
 
constexpr unsigned getCols () const noexcept
 Get the number of columns. More...
 
constexpr unsigned getRows () const noexcept
 Get the number of rows. More...
 
constexpr bool isEmpty () const noexcept
 Check if the array is empty. More...
 
Elements access
Toperator() (Vector2u pos)
 Get the element at a given 2D position. More...
 
Toperator() (std::size_t pos)
 Get the element at a given 1D position. More...
 
const Toperator() (Vector2u pos) const
 Get the element at a given 2D position. More...
 
const Toperator() (std::size_t pos) const
 Get the element at a given 1D position. More...
 
constexpr Vector2u toPosition (std::size_t pos) const noexcept
 Transform a 1D position into a 2D position. More...
 
Visitors
template<typename Func >
void visit4Neighbours (Vector2u pos, Func func)
 Visit the 4 neighbours of a given position. More...
 
template<typename Func >
void visit4Neighbours (Vector2u pos, Func func) const
 Visit the 4 neighbours of a given position. More...
 
template<typename Func >
void visit8Neighbours (const Vector2u &pos, Func func)
 Visit the 8 neighbours of a given position. More...
 
template<typename Func >
void visit8Neighbours (Vector2u pos, Func func) const
 Visit the 8 neighbours 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 getPositionRange () const noexcept
 Get the 1D position range of the array. More...
 
constexpr RangeU getRowRange () const noexcept
 Get the row range. More...
 
constexpr RangeU getColRange () const noexcept
 Get the column range. More...
 

Detailed Description

template<class T>
class gf::Array2D< T >

A two-dimensional array.

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

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 if the position in the array:

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

See also
gf::Matrix

Constructor & Destructor Documentation

template<class T>
gf::Array2D< T >::Array2D ( )
inline

Default constructor.

Creates an empty array.

template<class T>
gf::Array2D< T >::Array2D ( Vector2u  size)
inline

Constructor with a size.

Parameters
sizeThe size of the array
template<class T>
gf::Array2D< T >::Array2D ( Vector2u  size,
const T value 
)
inline

Constructor with a size and a value.

Parameters
sizeThe size of the array
valueThe initial value in the array
template<class T>
gf::Array2D< T >::Array2D ( const Array2D< T > &  )
default

Default copy constructor.

template<class T>
gf::Array2D< T >::Array2D ( Array2D< T > &&  )
default

Default move constructor.

Member Function Documentation

template<class T>
const T* gf::Array2D< T >::begin ( ) const
inlinenoexcept

Get an iterator to the first element of the array.

Returns
A begin iterator to the array
See also
end()
template<class T>
T* gf::Array2D< T >::begin ( )
inlinenoexcept

Get an iterator to the first element of the array.

Returns
A begin iterator to the array
See also
end()
template<class T>
const T* gf::Array2D< T >::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()
template<class T>
T* gf::Array2D< T >::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()
template<class T>
constexpr RangeU gf::Array2D< T >::getColRange ( ) const
inlinenoexcept

Get the column range.

Returns
A range with all the columns
template<class T>
constexpr unsigned gf::Array2D< T >::getCols ( ) const
inlinenoexcept

Get the number of columns.

Returns
The number of columns
template<class T>
const T* gf::Array2D< T >::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
template<class T>
std::size_t gf::Array2D< T >::getDataSize ( ) const
inlinenoexcept

Get the raw data size.

Returns
The total number of elements in the array
template<class T>
constexpr RangeZ gf::Array2D< T >::getPositionRange ( ) const
inlinenoexcept

Get the 1D position range of the array.

Returns
A range with all the 1D positions in the array
template<class T>
constexpr RangeU gf::Array2D< T >::getRowRange ( ) const
inlinenoexcept

Get the row range.

Returns
A range with all the rows
template<class T>
constexpr unsigned gf::Array2D< T >::getRows ( ) const
inlinenoexcept

Get the number of rows.

Returns
The number of rows
template<class T>
constexpr Vector2u gf::Array2D< T >::getSize ( ) const
inlinenoexcept

Get the size of the array.

Returns
The size of the array
template<class T>
constexpr bool gf::Array2D< T >::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
template<class T>
T& gf::Array2D< T >::operator() ( Vector2u  pos)
inline

Get the element at a given 2D position.

Parameters
posThe 2D position of the element
template<class T>
T& gf::Array2D< T >::operator() ( std::size_t  pos)
inline

Get the element at a given 1D position.

Parameters
posThe 1D position of the element
See also
getPositionRange()
template<class T>
const T& gf::Array2D< T >::operator() ( Vector2u  pos) const
inline

Get the element at a given 2D position.

Parameters
posThe 2D position of the element
template<class T>
const T& gf::Array2D< T >::operator() ( std::size_t  pos) const
inline

Get the element at a given 1D position.

Parameters
posThe 1D position of the element
See also
getPositionRange()
template<class T>
Array2D& gf::Array2D< T >::operator= ( const Array2D< T > &  )
default

Default copy assignment.

template<class T>
Array2D& gf::Array2D< T >::operator= ( Array2D< T > &&  )
default

Default move assignement.

template<class T>
constexpr Vector2u gf::Array2D< T >::toPosition ( std::size_t  pos) const
inlinenoexcept

Transform a 1D position into a 2D position.

Parameters
posA 1D position
Returns
The corresponding 2D position
template<class T>
template<typename Func >
void gf::Array2D< T >::visit4Neighbours ( Vector2u  pos,
Func  func 
)
inline

Visit the 4 neighbours of a given position.

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

The callback function has the following prototype:

void callback(Vector2u pos, T value);
// pos is the position of the neighbour
// value is the value of the neighbour

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

Parameters
posThe position
funcA callback function
template<class T>
template<typename Func >
void gf::Array2D< T >::visit4Neighbours ( Vector2u  pos,
Func  func 
) const
inline

Visit the 4 neighbours of a given position.

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

The callback function has the following prototype:

void callback(Vector2u pos, T value);
// pos is the position of the neighbour
// value is the value of the neighbour

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

Parameters
posThe position
funcA callback function
template<class T>
template<typename Func >
void gf::Array2D< T >::visit8Neighbours ( const Vector2u pos,
Func  func 
)
inline

Visit the 8 neighbours of a given position.

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

The callback function has the following prototype:

void callback(Vector2u pos, T value);
// pos is the position of the neighbour
// value is the value of the neighbour

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

Parameters
posThe position
funcA callback function
template<class T>
template<typename Func >
void gf::Array2D< T >::visit8Neighbours ( Vector2u  pos,
Func  func 
) const
inline

Visit the 8 neighbours of a given position.

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

The callback function has the following prototype:

void callback(Vector2u pos, T value);
// pos is the position of the neighbour
// value is the value of the neighbour

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

Parameters
posThe position
funcA callback function