Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_BoxIterator.H
Go to the documentation of this file.
1#ifndef AMREX_BOXITERATOR_H_
2#define AMREX_BOXITERATOR_H_
3#include <AMReX_Config.H>
4
10#include <AMReX_BLassert.H>
11#include <AMReX_REAL.H>
12#include <AMReX_SPACE.H>
13#include <AMReX_IntVect.H>
14
15#include <cstdlib>
16
17namespace amrex
18{
19
20template<int dim>
21class BoxND;
22
57template<int dim>
59{
60public:
61
63 BoxIteratorND () noexcept = default;
64
69 explicit BoxIteratorND (const BoxND<dim>& a_bx) noexcept {
70 define(a_bx);
71 }
72
74 void setBox (const BoxND<dim>& a_bx) noexcept {
75 define(a_bx);
76 }
77
82 void define (const BoxND<dim>& a_bx) noexcept {
83 if (a_bx.ok()) {
84 m_current = a_bx.smallEnd();
85 m_boxLo = a_bx.smallEnd();
86 m_boxHi = a_bx.bigEnd();
87 } else {
91 }
92 }
93
95 BoxIteratorND begin () noexcept {
96 m_current = m_boxLo;
97 return *this;
98 }
99
101 [[nodiscard]] BoxIteratorND end () const noexcept {
102 BoxIteratorND ret = *this;
103 // set current of end to the past-the-end IntVect
104 ret.m_current = m_boxLo;
105 if (m_boxLo.allLE(m_boxHi)) {
106 ret.m_current[dim-1] = m_boxHi[dim-1] + 1;
107 }
108 return ret;
109 }
110
112 void reset () noexcept {
113 begin();
114 }
115
118 next();
119 return *this;
120 }
121
123 void next () noexcept {
124 for (int i=0; i<dim; ++i) {
125 ++m_current[i];
126 if ((i+1<dim) && m_current[i] > m_boxHi[i]) {
127 m_current[i] = m_boxLo[i];
128 continue;
129 }
130 break;
131 }
132 }
133
135 [[nodiscard]] const IntVectND<dim>& operator () () const noexcept {
136 AMREX_ASSERT(m_current.allLE(m_boxHi));
137 AMREX_ASSERT(m_current.allGE(m_boxLo));
138 return m_current;
139 }
140
142 [[nodiscard]] IntVectND<dim> operator* () const noexcept {
143 AMREX_ASSERT(m_current.allLE(m_boxHi));
144 AMREX_ASSERT(m_current.allGE(m_boxLo));
145 // Returning by value gives cleaner assembly output.
146 return m_current;
147 }
148
150 [[nodiscard]] bool ok () const noexcept {
151 return m_current.allLE(m_boxHi);
152 }
153
155 [[nodiscard]] friend
156 bool operator != (const BoxIteratorND& b1, const BoxIteratorND& b2) noexcept {
157 // Only check one value for better performance.
158 return b1.m_current[dim-1] != b2.m_current[dim-1];
159 }
160
161private:
165};
166
170
171}
172#endif
Assertion macros used across AMReX for runtime consistency checks.
#define AMREX_ASSERT(EX)
Definition AMReX_BLassert.H:38
Iterator over all IntVects in a Box.
Definition AMReX_BoxIterator.H:59
friend bool operator!=(const BoxIteratorND &b1, const BoxIteratorND &b2) noexcept
Inequality test used by range-based for loops (checks only the last dimension).
Definition AMReX_BoxIterator.H:156
BoxIteratorND() noexcept=default
Default constructor. Constructs an invalid iterator; call define() before use.
BoxIteratorND begin() noexcept
Reset to the first IntVect (smallEnd) and return *this for range-based for.
Definition AMReX_BoxIterator.H:95
IntVectND< dim > operator*() const noexcept
Dereference: return the current IntVect by value.
Definition AMReX_BoxIterator.H:142
void next() noexcept
Advance to the next IntVect in the Box.
Definition AMReX_BoxIterator.H:123
void setBox(const BoxND< dim > &a_bx) noexcept
Associate this iterator with Box a_bx (equivalent to define()).
Definition AMReX_BoxIterator.H:74
void define(const BoxND< dim > &a_bx) noexcept
Associate this iterator with Box a_bx.
Definition AMReX_BoxIterator.H:82
const IntVectND< dim > & operator()() const noexcept
Return the current IntVect.
Definition AMReX_BoxIterator.H:135
bool ok() const noexcept
Return true if the iterator is within the Box.
Definition AMReX_BoxIterator.H:150
BoxIteratorND end() const noexcept
Return a past-the-end sentinel for use by range-based for loops.
Definition AMReX_BoxIterator.H:101
BoxIteratorND & operator++() noexcept
Advance to the next IntVect in the Box (pre-increment).
Definition AMReX_BoxIterator.H:117
void reset() noexcept
Reset to the first IntVect (smallEnd) of the Box.
Definition AMReX_BoxIterator.H:112
A Rectangular Domain on an Integer Lattice.
Definition AMReX_Box.H:54
An Integer Vector in dim-Dimensional Space.
Definition AMReX_IntVect.H:149
__host__ static __device__ constexpr IntVectND< dim > TheUnitVector() noexcept
This static member function returns a reference to a constant IntVectND object, all of whose dim argu...
Definition AMReX_IntVect.H:781
__host__ static __device__ constexpr IntVectND< dim > TheZeroVector() noexcept
This static member function returns a reference to a constant IntVectND object, all of whose dim argu...
Definition AMReX_IntVect.H:771
Definition AMReX_Amr.cpp:50