25 #include <type_traits> 37 #ifndef DOXYGEN_SHOULD_SKIP_THIS 62 template<
typename T,
typename I =
unsigned>
127 std::swap(m_data, other.m_data);
128 std::swap(m_size, other.m_size);
145 return m_data.data();
154 return m_data.size();
193 return m_data.empty();
204 return pos.col < m_size.col && pos.row < m_size.row && (std::is_unsigned<I>::value || (0 <= pos.col && 0 <= pos.row));
230 return m_data[index];
249 return m_data[index];
259 return { pos % m_size.col, pos / m_size.col };
290 template<
typename Func>
292 visitNeighborsDiamond(pos, func, 1);
316 template<
typename Func>
318 visitNeighborsDiamond(pos, func, 1);
342 template<
typename Func>
344 visitNeighborsDiamond(pos, func, 2);
368 template<
typename Func>
370 visitNeighborsDiamond(pos, func, 2);
394 template<
typename Func>
396 visitNeighborsSquare(pos, func, 1);
422 template<
typename Func>
424 visitNeighborsSquare(pos, func, 1);
448 template<
typename Func>
450 visitNeighborsSquare(pos, func, 2);
474 template<
typename Func>
476 visitNeighborsSquare(pos, func, 2);
493 return m_data.data();
502 const T *
end()
const noexcept {
503 return m_data.data() + m_data.size();
513 return m_data.data();
523 return m_data.data() + m_data.size();
532 return { 0, m_size.col * m_size.row };
541 return { 0, m_size.row };
550 return { 0, m_size.col };
565 T& get(
Vector<I, 2> pos) {
566 return m_data[pos.row * m_size.col + pos.col];
569 const T& get(
Vector<I, 2> pos)
const {
570 return m_data[pos.row * m_size.col + pos.col];
573 template<
typename Func>
574 void visitNeighborsSquare(
Vector<I, 2> pos, Func func, I n)
const {
577 auto colMin = pos.col - std::min(pos.col, n);
578 auto colMax = pos.col + std::min(m_size.col - pos.col - 1, n);
579 auto rowMin = pos.row - std::min(pos.row, n);
580 auto rowMax = pos.row + std::min(m_size.row - pos.row - 1, n);
582 for (
auto row = rowMin; row <= rowMax; ++row) {
583 for (
auto col = colMin; col <= colMax; ++col) {
584 if (col == pos.col && row == pos.row) {
588 func({ col, row }, get({ col, row }));
593 template<
typename Func>
594 void visitNeighborsSquare(
Vector<I, 2> pos, Func func, I n) {
597 auto colMin = pos.col - std::min(pos.col, n);
598 auto colMax = pos.col + std::min(m_size.col - pos.col - 1, n);
599 auto rowMin = pos.row - std::min(pos.row, n);
600 auto rowMax = pos.row + std::min(m_size.row - pos.row - 1, n);
602 for (
auto row = rowMin; row <= rowMax; ++row) {
603 for (
auto col = colMin; col <= colMax; ++col) {
604 if (col == pos.col && row == pos.row) {
608 func({ col, row }, get({ col, row }));
614 template<
typename Func>
615 void visitNeighborsDiamond(
Vector<I, 2> pos, Func func, I n)
const {
618 auto colMin = pos.col - std::min(pos.col, n);
619 auto colMax = pos.col + std::min(m_size.col - pos.col - 1, n);
620 auto rowMin = pos.row - std::min(pos.row, n);
621 auto rowMax = pos.row + std::min(m_size.row - pos.row - 1, n);
623 for (
auto row = rowMin; row <= rowMax; ++row) {
624 for (
auto col = colMin; col <= colMax; ++col) {
625 if (col == pos.col && row == pos.row) {
629 if (
gf::absdiff(col, pos.col) +
gf::absdiff(row, pos.row) > n) {
633 func({ col, row }, get({ col, row }));
638 template<
typename Func>
639 void visitNeighborsDiamond(
Vector<I, 2> pos, Func func, I n) {
642 auto colMin = pos.col - std::min(pos.col, n);
643 auto colMax = pos.col + std::min(m_size.col - pos.col - 1, n);
644 auto rowMin = pos.row - std::min(pos.row, n);
645 auto rowMax = pos.row + std::min(m_size.row - pos.row - 1, n);
647 for (
auto row = rowMin; row <= rowMax; ++row) {
648 for (
auto col = colMin; col <= colMax; ++col) {
649 if (col == pos.col && row == pos.row) {
653 if (
gf::absdiff(col, pos.col) +
gf::absdiff(row, pos.row) > n) {
657 func({ col, row }, get({ col, row }));
664 std::vector<T> m_data;
667 #ifndef DOXYGEN_SHOULD_SKIP_THIS A two-dimensional array.
Definition: Array2D.h:63
A half-open range of values.
Definition: Range.h:42
Array2D & operator=(const Array2D &)=default
Default copy assignment.
T * end() noexcept
Get an iterator to the element following the last element of the array.
Definition: Array2D.h:522
const T * begin() const noexcept
Get an iterator to the first element of the array.
Definition: Array2D.h:492
const T * getDataPtr() const noexcept
Get the pointer to raw data.
Definition: Array2D.h:144
std::size_t getDataSize() const noexcept
Get the raw data size.
Definition: Array2D.h:153
void swap(Array2D &other)
Swap with another array.
Definition: Array2D.h:126
Array2D(const Array2D &)=default
Default copy constructor.
Array2D(Array2D &&)=default
Default move constructor.
void visit4Neighbors(Vector< I, 2 > pos, Func func) const
Visit the 4 neighbors of a given position.
Definition: Array2D.h:317
constexpr Range< I > getRowRange() const noexcept
Get the row range.
Definition: Array2D.h:540
constexpr PositionRange< I > getPositionRange() const noexcept
Get the position range.
Definition: Array2D.h:558
void visit12Neighbors(Vector< I, 2 > pos, Func func) const
Visit the 12 neighbors of a given position.
Definition: Array2D.h:369
constexpr I getCols() const noexcept
Get the number of columns.
Definition: Array2D.h:171
constexpr Vector< I, 2 > toPosition(std::size_t pos) const noexcept
Transform a 1D position into a 2D position.
Definition: Array2D.h:258
void visit24Neighbors(Vector< I, 2 > pos, Func func) const
Visit the 24 neighbors of a given position.
Definition: Array2D.h:475
void visit8Neighbors(Vector< I, 2 > pos, Func func) const
Visit the 8 neighbors of a given position.
Definition: Array2D.h:423
The namespace for gf classes.
Definition: Action.h:34
Array2D()
Default constructor.
Definition: Array2D.h:70
Array2D(Vector< I, 2 > size)
Constructor with a size.
Definition: Array2D.h:81
constexpr bool isValid(Vector< I, 2 > pos) const noexcept
Check if a position is valid.
Definition: Array2D.h:203
void visit24Neighbors(Vector< I, 2 > pos, Func func)
Visit the 24 neighbors of a given position.
Definition: Array2D.h:449
T & operator()(Vector< I, 2 > pos)
Get the element at a given 2D position.
Definition: Array2D.h:219
Array2D & operator=(Array2D &&)=default
Default move assignement.
const T & operator()(std::size_t index) const
Get the element at a given 1D index.
Definition: Array2D.h:248
constexpr bool isEmpty() const noexcept
Check if the array is empty.
Definition: Array2D.h:192
constexpr I getRows() const noexcept
Get the number of rows.
Definition: Array2D.h:180
constexpr Vector< I, 2 > getSize() const noexcept
Get the size of the array.
Definition: Array2D.h:162
A 2D range.
Definition: Range.h:225
constexpr Range< I > getColRange() const noexcept
Get the column range.
Definition: Array2D.h:549
T * begin() noexcept
Get an iterator to the first element of the array.
Definition: Array2D.h:512
Array2D(Vector< I, 2 > size, const T &value)
Constructor with a size and a value.
Definition: Array2D.h:94
constexpr RangeZ getIndexRange() const noexcept
Get the 1D index range of the array.
Definition: Array2D.h:531
General purpose math vector.
Definition: Vector.h:60
T & operator()(std::size_t index)
Get the element at a given 1D index.
Definition: Array2D.h:229
const T & operator()(Vector< I, 2 > pos) const
Get the element at a given 2D position.
Definition: Array2D.h:238
void visit12Neighbors(Vector< I, 2 > pos, Func func)
Visit the 12 neighbors of a given position.
Definition: Array2D.h:343
void visit8Neighbors(Vector< I, 2 > pos, Func func)
Visit the 8 neighbors of a given position.
Definition: Array2D.h:395
void visit4Neighbors(Vector< I, 2 > pos, Func func)
Visit the 4 neighbors of a given position.
Definition: Array2D.h:291
const T * end() const noexcept
Get an iterator to the element following the last element of the array.
Definition: Array2D.h:502