26 #include <type_traits> 33 #include "Portability.h" 35 #include "SerializationFwd.h" 39 #ifndef DOXYGEN_SHOULD_SKIP_THIS 64 template<
typename T,
typename I =
unsigned>
85 , m_data(static_cast<
std::size_t>(size.width) * static_cast<
std::size_t>(size.height))
98 , m_data(static_cast<
std::size_t>(size.width) * static_cast<
std::size_t>(size.height), value)
129 std::swap(m_data, other.m_data);
130 std::swap(m_size, other.m_size);
147 return m_data.data();
156 return m_data.size();
195 return m_data.empty();
206 return pos.col < m_size.col && pos.row < m_size.row && (std::is_unsigned<I>::value || (0 <= pos.col && 0 <= pos.row));
232 return m_data[index];
251 return m_data[index];
261 return { pos % m_size.col, pos / m_size.col };
292 template<
typename Func>
294 visitNeighborsDiamond(pos, func, 1);
318 template<
typename Func>
320 visitNeighborsDiamond(pos, func, 1);
344 template<
typename Func>
346 visitNeighborsDiamond(pos, func, 2);
370 template<
typename Func>
372 visitNeighborsDiamond(pos, func, 2);
396 template<
typename Func>
398 visitNeighborsSquare(pos, func, 1);
424 template<
typename Func>
426 visitNeighborsSquare(pos, func, 1);
450 template<
typename Func>
452 visitNeighborsSquare(pos, func, 2);
476 template<
typename Func>
478 visitNeighborsSquare(pos, func, 2);
495 return m_data.data();
504 const T *
end() const noexcept {
505 return m_data.data() + m_data.size();
515 return m_data.data();
525 return m_data.data() + m_data.size();
534 return { 0, m_size.col * m_size.row };
543 return { 0, m_size.row };
552 return { 0, m_size.col };
561 return { getColRange(), getRowRange() };
568 return m_data[pos.row * m_size.col + pos.col];
572 return m_data[pos.row * m_size.col + pos.col];
575 template<
typename Func>
576 void visitNeighborsSquare(
Vector<I, 2> pos, Func func,
I n)
const {
577 assert(isValid(pos));
579 auto colMin = pos.col - std::min(pos.col, n);
580 auto colMax = pos.col + std::min(m_size.col - pos.col - 1, n);
581 auto rowMin = pos.row - std::min(pos.row, n);
582 auto rowMax = pos.row + std::min(m_size.row - pos.row - 1, n);
584 for (
auto row = rowMin; row <= rowMax; ++row) {
585 for (
auto col = colMin; col <= colMax; ++col) {
586 if (col == pos.col && row == pos.row) {
590 func({ col, row },
get({ col, row }));
595 template<
typename Func>
596 void visitNeighborsSquare(
Vector<I, 2> pos, Func func,
I n) {
597 assert(isValid(pos));
599 auto colMin = pos.col - std::min(pos.col, n);
600 auto colMax = pos.col + std::min(m_size.col - pos.col - 1, n);
601 auto rowMin = pos.row - std::min(pos.row, n);
602 auto rowMax = pos.row + std::min(m_size.row - pos.row - 1, n);
604 for (
auto row = rowMin; row <= rowMax; ++row) {
605 for (
auto col = colMin; col <= colMax; ++col) {
606 if (col == pos.col && row == pos.row) {
610 func({ col, row },
get({ col, row }));
616 template<
typename Func>
617 void visitNeighborsDiamond(
Vector<I, 2> pos, Func func,
I n)
const {
618 assert(isValid(pos));
620 auto colMin = pos.col - std::min(pos.col, n);
621 auto colMax = pos.col + std::min(m_size.col - pos.col - 1, n);
622 auto rowMin = pos.row - std::min(pos.row, n);
623 auto rowMax = pos.row + std::min(m_size.row - pos.row - 1, n);
625 for (
auto row = rowMin; row <= rowMax; ++row) {
626 for (
auto col = colMin; col <= colMax; ++col) {
627 if (col == pos.col && row == pos.row) {
635 func({ col, row },
get({ col, row }));
640 template<
typename Func>
641 void visitNeighborsDiamond(
Vector<I, 2> pos, Func func,
I n) {
642 assert(isValid(pos));
644 auto colMin = pos.col - std::min(pos.col, n);
645 auto colMax = pos.col + std::min(m_size.col - pos.col - 1, n);
646 auto rowMin = pos.row - std::min(pos.row, n);
647 auto rowMax = pos.row + std::min(m_size.row - pos.row - 1, n);
649 for (
auto row = rowMin; row <= rowMax; ++row) {
650 for (
auto col = colMin; col <= colMax; ++col) {
651 if (col == pos.col && row == pos.row) {
659 func({ col, row },
get({ col, row }));
666 std::vector<T> m_data;
673 template<
typename T,
typename I>
678 if (lhsSize.width != rhsSize.width || lhsSize.height != rhsSize.height) {
689 template<
typename T,
typename I>
692 ar | size.width | size.height;
694 for (
auto& item : array) {
705 template<
typename T,
typename I>
708 ar | size.width | size.height;
712 for (
auto& item : tmp) {
716 array = std::move(tmp);
721 #ifndef DOXYGEN_SHOULD_SKIP_THIS A deserializer from a binary file.
Definition: Serialization.h:153
A two-dimensional array.
Definition: Array2D.h:65
Serializer & operator|(Serializer &ar, const Array2D< T, I > &array)
Serialize a 2D array.
Definition: Array2D.h:690
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:524
Deserializer & operator|(Deserializer &ar, Array2D< T, I > &array)
Deserialize a 2D array.
Definition: Array2D.h:706
const T * begin() const noexcept
Get an iterator to the first element of the array.
Definition: Array2D.h:494
const T * getDataPtr() const noexcept
Get the pointer to raw data.
Definition: Array2D.h:146
std::size_t getDataSize() const noexcept
Get the raw data size.
Definition: Array2D.h:155
void swap(Array2D &other)
Swap with another array.
Definition: Array2D.h:128
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:319
constexpr Range< I > getRowRange() const noexcept
Get the row range.
Definition: Array2D.h:542
constexpr PositionRange< I > getPositionRange() const noexcept
Get the position range.
Definition: Array2D.h:560
void visit12Neighbors(Vector< I, 2 > pos, Func func) const
Visit the 12 neighbors of a given position.
Definition: Array2D.h:371
constexpr I getCols() const noexcept
Get the number of columns.
Definition: Array2D.h:173
constexpr Vector< I, 2 > toPosition(std::size_t pos) const noexcept
Transform a 1D position into a 2D position.
Definition: Array2D.h:260
void visit24Neighbors(Vector< I, 2 > pos, Func func) const
Visit the 24 neighbors of a given position.
Definition: Array2D.h:477
void visit8Neighbors(Vector< I, 2 > pos, Func func) const
Visit the 8 neighbors of a given position.
Definition: Array2D.h:425
A serializer to a binary file.
Definition: Serialization.h:45
The namespace for gf classes.
Definition: Action.h:35
Array2D()
Default constructor.
Definition: Array2D.h:72
Array2D(Vector< I, 2 > size)
Constructor with a size.
Definition: Array2D.h:83
constexpr bool isValid(Vector< I, 2 > pos) const noexcept
Check if a position is valid.
Definition: Array2D.h:205
void visit24Neighbors(Vector< I, 2 > pos, Func func)
Visit the 24 neighbors of a given position.
Definition: Array2D.h:451
T & operator()(Vector< I, 2 > pos)
Get the element at a given 2D position.
Definition: Array2D.h:221
const T & operator()(std::size_t index) const
Get the element at a given 1D index.
Definition: Array2D.h:250
constexpr bool isEmpty() const noexcept
Check if the array is empty.
Definition: Array2D.h:194
constexpr I getRows() const noexcept
Get the number of rows.
Definition: Array2D.h:182
constexpr Vector< I, 2 > getSize() const noexcept
Get the size of the array.
Definition: Array2D.h:164
A 2D range.
Definition: Range.h:225
constexpr Range< I > getColRange() const noexcept
Get the column range.
Definition: Array2D.h:551
T * begin() noexcept
Get an iterator to the first element of the array.
Definition: Array2D.h:514
Array2D(Vector< I, 2 > size, const T &value)
Constructor with a size and a value.
Definition: Array2D.h:96
constexpr RangeZ getIndexRange() const noexcept
Get the 1D index range of the array.
Definition: Array2D.h:533
bool operator==(const Array2D< T, I > &lhs, const Array2D< T, I > &rhs)
Equality operator for 2D array.
Definition: Array2D.h:674
T & operator()(std::size_t index)
Get the element at a given 1D index.
Definition: Array2D.h:231
const T & operator()(Vector< I, 2 > pos) const
Get the element at a given 2D position.
Definition: Array2D.h:240
void visit12Neighbors(Vector< I, 2 > pos, Func func)
Visit the 12 neighbors of a given position.
Definition: Array2D.h:345
void visit8Neighbors(Vector< I, 2 > pos, Func func)
Visit the 8 neighbors of a given position.
Definition: Array2D.h:397
void visit4Neighbors(Vector< I, 2 > pos, Func func)
Visit the 4 neighbors of a given position.
Definition: Array2D.h:293
const T * end() const noexcept
Get an iterator to the element following the last element of the array.
Definition: Array2D.h:504