25 #include <type_traits> 32 #include "Portability.h" 37 #ifndef DOXYGEN_SHOULD_SKIP_THIS 59 template<
typename T,
typename I =
unsigned>
80 , m_data(size.width * size.height)
93 , m_data(size.width * size.height, value)
124 std::swap(m_data, other.m_data);
125 std::swap(m_size, other.m_size);
142 return m_data.data();
151 return m_data.size();
190 return m_data.empty();
201 return pos.col < m_size.col && pos.row < m_size.row && (std::is_unsigned<I>::value || (0 <= pos.col && 0 <= pos.row));
256 return { pos % m_size.col, pos / m_size.col };
287 template<
typename Func>
289 visitNeighborsDiamond(pos, func, 1);
313 template<
typename Func>
315 visitNeighborsDiamond(pos, func, 1);
339 template<
typename Func>
341 visitNeighborsDiamond(pos, func, 2);
365 template<
typename Func>
367 visitNeighborsDiamond(pos, func, 2);
391 template<
typename Func>
393 visitNeighborsSquare(pos, func, 1);
419 template<
typename Func>
421 visitNeighborsSquare(pos, func, 1);
445 template<
typename Func>
447 visitNeighborsSquare(pos, func, 2);
471 template<
typename Func>
473 visitNeighborsSquare(pos, func, 2);
490 return m_data.data();
499 const T *
end() const noexcept {
500 return m_data.data() + m_data.size();
510 return m_data.data();
520 return m_data.data() + m_data.size();
529 return { 0, m_size.col * m_size.row };
538 return { 0, m_size.row };
547 return { 0, m_size.col };
554 return m_data[pos.row * m_size.col + pos.col];
558 return m_data[pos.row * m_size.col + pos.col];
561 template<
typename Func>
562 void visitNeighborsSquare(
Vector<I, 2> pos, Func func,
unsigned n)
const {
563 assert(isValid(pos));
565 unsigned colMin = pos.col - std::min(pos.col, n);
566 unsigned colMax = pos.col + std::min(m_size.col - pos.col - 1, n);
567 unsigned rowMin = pos.row - std::min(pos.row, n);
568 unsigned rowMax = pos.row + std::min(m_size.row - pos.row - 1, n);
570 for (
unsigned row = rowMin; row <= rowMax; ++row) {
571 for (
unsigned col = colMin; col <= colMax; ++col) {
572 if (col == pos.col && row == pos.row) {
576 func({ col, row },
get({ col, row }));
581 template<
typename Func>
582 void visitNeighborsSquare(
Vector<I, 2> pos, Func func,
unsigned n) {
583 assert(isValid(pos));
585 unsigned colMin = pos.col - std::min(pos.col, n);
586 unsigned colMax = pos.col + std::min(m_size.col - pos.col - 1, n);
587 unsigned rowMin = pos.row - std::min(pos.row, n);
588 unsigned rowMax = pos.row + std::min(m_size.row - pos.row - 1, n);
590 for (
unsigned row = rowMin; row <= rowMax; ++row) {
591 for (
unsigned col = colMin; col <= colMax; ++col) {
592 if (col == pos.col && row == pos.row) {
596 func({ col, row },
get({ col, row }));
602 template<
typename Func>
603 void visitNeighborsDiamond(
Vector<I, 2> pos, Func func,
unsigned n)
const {
604 assert(isValid(pos));
606 unsigned colMin = pos.col - std::min(pos.col, n);
607 unsigned colMax = pos.col + std::min(m_size.col - pos.col - 1, n);
608 unsigned rowMin = pos.row - std::min(pos.row, n);
609 unsigned rowMax = pos.row + std::min(m_size.row - pos.row - 1, n);
611 for (
unsigned row = rowMin; row <= rowMax; ++row) {
612 for (
unsigned col = colMin; col <= colMax; ++col) {
613 if (col == pos.col && row == pos.row) {
621 func({ col, row },
get({ col, row }));
626 template<
typename Func>
627 void visitNeighborsDiamond(
Vector<I, 2> pos, Func func,
unsigned n) {
628 assert(isValid(pos));
630 unsigned colMin = pos.col - std::min(pos.col, n);
631 unsigned colMax = pos.col + std::min(m_size.col - pos.col - 1, n);
632 unsigned rowMin = pos.row - std::min(pos.row, n);
633 unsigned rowMax = pos.row + std::min(m_size.row - pos.row - 1, n);
635 for (
unsigned row = rowMin; row <= rowMax; ++row) {
636 for (
unsigned col = colMin; col <= colMax; ++col) {
637 if (col == pos.col && row == pos.row) {
645 func({ col, row },
get({ col, row }));
652 std::vector<T> m_data;
655 #ifndef DOXYGEN_SHOULD_SKIP_THIS A two-dimensional array.
Definition: Array2D.h:60
A half-open range of values.
Definition: Range.h:42
T * end() noexcept
Get an iterator to the element following the last element of the array.
Definition: Array2D.h:519
constexpr RangeU getRowRange() const noexcept
Get the row range.
Definition: Array2D.h:537
constexpr unsigned getCols() const noexcept
Get the number of columns.
Definition: Array2D.h:168
const T * begin() const noexcept
Get an iterator to the first element of the array.
Definition: Array2D.h:489
const T * getDataPtr() const noexcept
Get the pointer to raw data.
Definition: Array2D.h:141
std::size_t getDataSize() const noexcept
Get the raw data size.
Definition: Array2D.h:150
void swap(Array2D &other)
Swap with another array.
Definition: Array2D.h:123
constexpr T absdiff(T lhs, T rhs)
Absolute difference of two values.
Definition: Math.h:313
void visit4Neighbors(Vector< I, 2 > pos, Func func) const
Visit the 4 neighbors of a given position.
Definition: Array2D.h:314
void visit12Neighbors(Vector< I, 2 > pos, Func func) const
Visit the 12 neighbors of a given position.
Definition: Array2D.h:366
constexpr RangeU getColRange() const noexcept
Get the column range.
Definition: Array2D.h:546
constexpr Vector< I, 2 > toPosition(std::size_t pos) const noexcept
Transform a 1D position into a 2D position.
Definition: Array2D.h:255
void visit24Neighbors(Vector< I, 2 > pos, Func func) const
Visit the 24 neighbors of a given position.
Definition: Array2D.h:472
void visit8Neighbors(Vector< I, 2 > pos, Func func) const
Visit the 8 neighbors of a given position.
Definition: Array2D.h:420
The namespace for gf classes.
Definition: Action.h:34
Array2D()
Default constructor.
Definition: Array2D.h:67
Array2D(Vector< I, 2 > size)
Constructor with a size.
Definition: Array2D.h:78
constexpr bool isValid(Vector< I, 2 > pos) const noexcept
Check if a position is valid.
Definition: Array2D.h:200
void visit24Neighbors(Vector< I, 2 > pos, Func func)
Visit the 24 neighbors of a given position.
Definition: Array2D.h:446
T & operator()(Vector< I, 2 > pos)
Get the element at a given 2D position.
Definition: Array2D.h:216
constexpr bool isEmpty() const noexcept
Check if the array is empty.
Definition: Array2D.h:189
constexpr RangeZ getPositionRange() const noexcept
Get the 1D position range of the array.
Definition: Array2D.h:528
constexpr Vector< I, 2 > getSize() const noexcept
Get the size of the array.
Definition: Array2D.h:159
T & operator()(std::size_t pos)
Get the element at a given 1D position.
Definition: Array2D.h:226
T * begin() noexcept
Get an iterator to the first element of the array.
Definition: Array2D.h:509
Array2D(Vector< I, 2 > size, const T &value)
Constructor with a size and a value.
Definition: Array2D.h:91
const T & operator()(std::size_t pos) const
Get the element at a given 1D position.
Definition: Array2D.h:245
const T & operator()(Vector< I, 2 > pos) const
Get the element at a given 2D position.
Definition: Array2D.h:235
constexpr unsigned getRows() const noexcept
Get the number of rows.
Definition: Array2D.h:177
void visit12Neighbors(Vector< I, 2 > pos, Func func)
Visit the 12 neighbors of a given position.
Definition: Array2D.h:340
void visit8Neighbors(Vector< I, 2 > pos, Func func)
Visit the 8 neighbors of a given position.
Definition: Array2D.h:392
void visit4Neighbors(Vector< I, 2 > pos, Func func)
Visit the 4 neighbors of a given position.
Definition: Array2D.h:288
const T * end() const noexcept
Get an iterator to the element following the last element of the array.
Definition: Array2D.h:499