WARPXM v1.10.0
Loading...
Searching...
No Matches
wxrange.h
Go to the documentation of this file.
1#ifndef wxrange_h
2#define wxrange_h
3
4// WarpX includes
5#include "wxbox.h"
6#include "wxexcept.h"
7
8// std includes
9#include <algorithm>
10#include <limits>
11
22class WxRange : protected WxBox<int>
23{
24
25public:
29 WxRange() = default;
30
34 WxRange(unsigned ndims) : WxBox<int>(ndims)
35 {
36 }
37
45 WxRange(unsigned ndims, const int* lower, const int* upper)
46 : WxBox<int>(ndims, lower, upper)
47 {
48 }
49
58 WxRange(const std::vector<int>& lower, const std::vector<int>& extent)
59 : WxBox<int>(lower.size())
60 {
61 for (unsigned int i = 0; i < ndims(); ++i)
62 {
64 WxBox<int>::upper(i, lower[i] + extent[i] - 1);
65 }
66 }
67
75 WxRange(unsigned ndims, const int* length) : WxBox<int>(ndims)
76 {
77 for (unsigned int i = 0; i < ndims; ++i)
78 {
79 int newLength = length[i] - 1;
80 WxBox<int>::resize(i, newLength);
81 }
82 }
83
89 WxRange(const WxBox<int>& templateBox) : WxBox<int>(templateBox)
90 {
91 }
92
96 WxRange(const WxRange& b) : WxBox<int>(b)
97 {
98 }
99
101 {
103 return *this;
104 }
105
109 using WxBox<int>::max_dims;
110
111 using WxBox<int>::ndims;
112 using WxBox<int>::setup;
113 using WxBox<int>::upper;
114 using WxBox<int>::lower;
115 using WxBox<int>::extDim;
116 using WxBox<int>::reduceDim;
117 using WxBox<int>::boundsPair;
118 using WxBox<int>::coordinateString;
119
120 // TODO: lookup how Doxygen handles these 'using' cases
128 using WxBox<int>::contains;
129
130 // TODO: uhg, I don't understand why 'using' the base function generates the error:
131 // 'WxBox<int>' is an inaccessible base of 'WxRange' but it has something to do with
132 // my protected inheritance and that the function argument is a reference to the base
133 // class.
134 // using WxBox<int>::encloses;
135 // so I redefine here and have no problem.
143 bool encloses(const WxRange& candidateRange) const
144 {
145 return WxBox<int>::encloses(candidateRange);
146 }
147
154 bool compareShape(const WxRange& candidateRange) const
155 {
156 return WxBox<int>::compareShape(candidateRange);
157 }
158
159 // overloaded functions that now include extremum
160
161 // extend functions need to return WxRange not WxBox
170 WxRange extend(const int low[], const int upp[]) const
171 {
172 return WxRange(this->WxBox<int>::extend(low, upp));
173 }
174
182 WxRange extend(const int uniformPad[]) const
183 {
184 return WxRange(this->WxBox<int>::extend(uniformPad));
185 }
193 WxRange extend(int uniformPad) const
194 {
195 return WxRange(this->WxBox<int>::extend(uniformPad));
196 }
197
206 void extrude(const WxRange& range)
207 {
208 this->WxBox<int>::extrude(range);
209 }
210
220 WxRange intersect(const WxRange& otherRange) const
221 {
222 // we override the parent WxBox::intersect because for range, the same coordinate
223 // point for lower and upper means length 1, not length 0. so we can never really
224 // have an empty range, just a negative range. isEmpty provides for checking
225 if (ndims() != otherRange.ndims())
226 {
227 return WxRange();
228 }
229 int lowerAdj[max_dims], upperAdj[max_dims];
230 for (unsigned j = 0; j < _ndims; ++j)
231 {
232 lowerAdj[j] = std::max(_lower[j], otherRange._lower[j]);
233 upperAdj[j] = std::min(_upper[j], otherRange._upper[j]);
234 if (lowerAdj[j] > upperAdj[j])
235 return WxRange(); // non-intersection in dim j
236 }
237
238 return WxRange(_ndims, lowerAdj, upperAdj);
239 }
240
253 WxRange getShiftedRange(const std::vector<int>& newLowerCornerCoordinates) const
254 {
255 int upperAdj[max_dims];
256 int lowerAdj[max_dims];
257 size_t j;
258 for (j = 0;
259 j < std::min(static_cast<size_t>(_ndims), newLowerCornerCoordinates.size());
260 ++j)
261 {
262 lowerAdj[j] = newLowerCornerCoordinates[j];
263 upperAdj[j] = newLowerCornerCoordinates[j] + (_upper[j] - _lower[j]);
264 }
265 for (; j < _ndims; ++j)
266 {
267 lowerAdj[j] = _lower[j];
268 upperAdj[j] = _upper[j];
269 }
270 return WxRange(_ndims, lowerAdj, upperAdj);
271 }
272
278 bool isEmpty() const
279 {
280 if (_ndims <= 0)
281 return true; // rank < 1 implies empty.
282 for (unsigned i = 0; i < _ndims; ++i)
283 if (_length[i] + 1 <= 0)
284 return true;
285 return false;
286 }
287
294 int length(unsigned dim) const
295 {
296 return _length[dim] + 1;
297 }
298
304 int size() const
305 {
306 if (_ndims == 0)
307 return 0;
308 int ar = 1;
309 for (unsigned i = 0; i < _ndims; ++i)
310 ar *= _length[i] + 1;
311 return ar;
312 }
313
320 bool operator==(const WxRange& r) const
321 {
322 return WxBox<int>::operator==(r);
323 }
324
331 bool operator!=(const WxRange& r) const
332 {
333 return !WxRange::operator==(r);
334 }
335
336 friend std::ostream& operator<<(std::ostream& out, const WxRange& range)
337 {
338 out << "Range ndims " << range._ndims << ", length[";
339 for (unsigned i = 0; i < range._ndims; i++)
340 {
341 out << range.length(i);
342 if (i != range._ndims - 1)
343 {
344 out << ", ";
345 }
346 }
347 out << "], lower[";
348 for (unsigned i = 0; i < range._ndims; i++)
349 {
350 out << range.lower(i);
351 if (i != range._ndims - 1)
352 {
353 out << ", ";
354 }
355 }
356 out << "], upper[";
357 for (unsigned i = 0; i < range._ndims; i++)
358 {
359 out << range.upper(i);
360 if (i != range._ndims - 1)
361 {
362 out << ", ";
363 }
364 }
365 out << "]";
366 return out;
367 }
368};
369
371#endif // wxrange_h
WxBox represents a n-dimensional box of elements specified by lower coordinates and upper coordinates...
Definition: wxbox.h:25
bool operator==(const WxBox< TYPE > &b) const
Check if this box is equal to one supplied.
bool compareShape(const WxBox< TYPE > &candidateBox) const
Check if all side lengths of the supplied box match this one.
std::pair< int, int > boundsPair(unsigned dim) const
Pair of upper and lower bound along dimension 'dim'.
Definition: wxbox.h:170
unsigned _ndims
Definition: wxbox.h:324
static const int max_dims
Maximum rank box that can be represented.
Definition: wxbox.h:321
bool contains(const int coord[]) const
Check if the box contains the given point.
virtual void setup(const WxCryptSet &wxc)
Constructs a box specified in a cryptset.
void extrude(const WxBox &box)
Increases the rank of this box to match that of the supplied box argument.
unsigned ndims() const
Dimensionality of box.
Definition: wxbox.h:113
int _lower[max_dims]
Definition: wxbox.h:325
std::string coordinateString() const
String of box coordinates.
int upper(unsigned dim) const
Upper bound along dimension 'dim'.
Definition: wxbox.h:147
WxBox< int > reduceDim(unsigned int newDim) const
Returns a new box which has up to new number of dimensions.
int lower(unsigned dim) const
Lower bound along dimension 'dim'.
Definition: wxbox.h:124
int _upper[max_dims]
Definition: wxbox.h:325
WxBox & operator=(const WxBox< TYPE > &b)
bool encloses(const WxBox< TYPE > &candidateBox) const
Check if the box encloses the given box.
WxBox< int > extDim(int low, int upp) const
Returns a new box which has one greater dimension than this one.
int _length[max_dims]
Definition: wxbox.h:325
void resize(const unsigned dim, const TYPE newLength)
Modify upper indicex based on newly supplied length.
WxRange represents a hyper-rectangular domain of an n-dimensional space of integers.
Definition: wxrange.h:23
bool operator!=(const WxRange &r) const
Check if this range is not equal to one supplied.
Definition: wxrange.h:331
friend std::ostream & operator<<(std::ostream &out, const WxRange &range)
Definition: wxrange.h:336
bool isEmpty() const
Determine if range is empty.
Definition: wxrange.h:278
WxRange(unsigned ndims, const int *length)
Constucts a range given lengths of each side.
Definition: wxrange.h:75
int length(unsigned dim) const
Length of edge along dimension 'dim'.
Definition: wxrange.h:294
WxRange & operator=(const WxRange &b)
Definition: wxrange.h:100
WxRange getShiftedRange(const std::vector< int > &newLowerCornerCoordinates) const
Returns a range which has the same shape as this one, but has been shifted to a new position.
Definition: wxrange.h:253
WxRange(const WxBox< int > &templateBox)
Constructs a range matching a giving WxBox<int> template.
Definition: wxrange.h:89
bool compareShape(const WxRange &candidateRange) const
Check if all side lengths of the supplied range match this one.
Definition: wxrange.h:154
WxRange extend(const int uniformPad[]) const
Returns a range which is extended by the given amount along both sides in each dimension.
Definition: wxrange.h:182
WxRange extend(const int low[], const int upp[]) const
Returns a range which is extended by the given amount along each side in each dimension.
Definition: wxrange.h:170
bool encloses(const WxRange &candidateRange) const
Check if the range encloses the given range.
Definition: wxrange.h:143
WxRange(const std::vector< int > &lower, const std::vector< int > &extent)
Constucts a range with given 'lower' coordinates, and extent as specified.
Definition: wxrange.h:58
WxRange(unsigned ndims)
Create a 'ndims' dimensional range.
Definition: wxrange.h:34
WxRange(unsigned ndims, const int *lower, const int *upper)
Constucts a range with given 'lower' and 'upper' bounds.
Definition: wxrange.h:45
bool operator==(const WxRange &r) const
Check if this range is equal to one supplied.
Definition: wxrange.h:320
void extrude(const WxRange &range)
Increases the rank of this range to match that of the supplied range argument.
Definition: wxrange.h:206
int size() const
Number of elements in range.
Definition: wxrange.h:304
WxRange intersect(const WxRange &otherRange) const
Returns a range which is the intersection or logical union of this and the supplied range.
Definition: wxrange.h:220
WxRange()=default
Creates an empty range with no dimensions or extent.
WxRange extend(int uniformPad) const
Returns a range which is extended by the given amount along both sides in every dimension.
Definition: wxrange.h:193
WxRange(const WxRange &b)
Copy constructor and assignment operators.
Definition: wxrange.h:96