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

A two-dimensional array. More...

#include <gf/Array2D.h>

Inheritance diagram for gf::Array2D< T, I >:
Inheritance graph
[legend]

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 bool isEmpty () const noexcept
 Check if the array is empty. 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...
 
Iterators
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...
 
- Public Member Functions inherited from gf::Index2D< I >
 Index2D ()
 Default constructor. More...
 
 Index2D (Vector< I, 2 > size)
 Constructor with a size. More...
 
 Index2D (const Index2D &)=default
 Default copy constructor. More...
 
Index2Doperator= (const Index2D &)=default
 Default copy assignment. More...
 
 Index2D (Index2D &&)=default
 Default move constructor. More...
 
Index2Doperator= (Index2D &&)=default
 Default move assignement. More...
 
void swap (Index2D &other)
 Swap with another array. 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 isValid (Vector< I, 2 > pos) const noexcept
 Check if a position is valid. More...
 
constexpr Vector< I, 2 > toPosition (std::size_t index) const noexcept
 Transform a 1D position into a 2D position. More...
 
constexpr std::size_t toIndex (Vector< I, 2 > pos) const noexcept
 Transform a 2D position into a 1D position. 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...
 
NeighborSquareRange< Iget8NeighborsRange (Vector< I, 2 > pos) const noexcept
 Get a range for 8 neighbors (at most) More...
 
NeighborSquareRange< Iget24NeighborsRange (Vector< I, 2 > pos) const noexcept
 Get a range for 24 neighbors (at most) More...
 
NeighborDiamondRange< Iget4NeighborsRange (Vector< I, 2 > pos) const noexcept
 Get a range for 4 neighbors (at most) More...
 
NeighborDiamondRange< Iget12NeighborsRange (Vector< I, 2 > pos) const noexcept
 Get a range for 12 neighbors (at most) More...
 

Related Functions

(Note that these are not member functions.)

template<typename T , typename I >
bool operator== (const Array2D< T, I > &lhs, const Array2D< T, I > &rhs)
 Equality operator for 2D array. 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()

◆ 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

◆ 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

◆ 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

Friends And Related Function Documentation

◆ operator==()

template<typename T , typename I >
bool operator== ( const Array2D< T, I > &  lhs,
const Array2D< T, I > &  rhs 
)
related

Equality operator for 2D array.