32#include "Utility/PAssert.h"
49 , length_m(l - f + 1) {
50 PAssert_GE(l - f + 1, 0);
59 }
else if ((l > f) ^ (s < 0)) {
60 length_m = (l - f) / s + 1;
67 : first_m(b.first_m * m + a)
68 , stride_m(b.stride_m * m)
69 , length_m(b.length_m) {}
71 KOKKOS_INLINE_FUNCTION
Index::Index(
int f,
int s,
const Index* b)
74 , length_m(b->length_m) {}
93 return (length_m == 0) ? first_m : first_m + stride_m * (length_m - 1);
97 return (stride_m >= 0) ? first_m : first_m + stride_m * (length_m - 1);
101 return (stride_m >= 0) ? first_m + stride_m * (length_m - 1) : first_m;
104 KOKKOS_INLINE_FUNCTION
Index& Index::operator+=(
int off) {
109 KOKKOS_INLINE_FUNCTION Index& Index::operator-=(
int off) {
114 KOKKOS_INLINE_FUNCTION Index operator+(
const Index& i,
int off) {
115 return Index(1, off, i);
118 KOKKOS_INLINE_FUNCTION Index operator+(
int off,
const Index& i) {
119 return Index(1, off, i);
122 KOKKOS_INLINE_FUNCTION Index operator-(
const Index& i,
int off) {
123 return Index(1, -off, i);
126 KOKKOS_INLINE_FUNCTION Index operator-(
int off,
const Index& i) {
127 return Index(-1, off, i);
130 KOKKOS_INLINE_FUNCTION Index operator-(
const Index& i) {
131 return Index(-1, 0, i);
134 KOKKOS_INLINE_FUNCTION Index operator*(
const Index& i,
int m) {
135 return Index(m, 0, i);
138 KOKKOS_INLINE_FUNCTION Index operator*(
int m,
const Index& i) {
139 return Index(m, 0, i);
142 KOKKOS_INLINE_FUNCTION Index operator/(
const Index& i,
int d) {
143 return Index(i.first_m / d, i.stride_m / d, &i);
146 KOKKOS_INLINE_FUNCTION Index Index::reverse()
const {
149 j.length_m = length_m;
150 j.stride_m = -stride_m;
154 KOKKOS_INLINE_FUNCTION
bool Index::touches(
const Index& a)
const {
155 return (
min() <= a.max()) && (
max() >= a.min());
158 KOKKOS_INLINE_FUNCTION
bool Index::contains(
const Index& a)
const {
159 return (
min() <= a.min()) && (
max() >= a.max());
162 KOKKOS_INLINE_FUNCTION
bool Index::split(Index& l, Index& r)
const {
163 PAssert_EQ(stride_m, 1);
164 PAssert_GT(length_m, 1);
165 auto mid = first_m + length_m / 2 - 1;
166 l =
Index(first_m, mid);
167 r =
Index(mid + 1, first_m + length_m - 1);
171 KOKKOS_INLINE_FUNCTION
bool Index::split(Index& l, Index& r,
int i)
const {
172 PAssert_EQ(stride_m, 1);
173 PAssert_GT(length_m, 1);
174 if (i >= first_m +
static_cast<int>(length_m)) {
177 l =
Index(first_m, i);
178 r =
Index(i + 1, first_m + length_m - 1);
182 KOKKOS_INLINE_FUNCTION
bool Index::split(Index& l, Index& r,
double a)
const {
183 PAssert_EQ(stride_m, 1);
184 PAssert_GT(length_m, 1);
187 int mid = first_m +
static_cast<int>(length_m * a + 0.5) - 1;
188 l =
Index(first_m, mid);
189 r =
Index(mid + 1, first_m + length_m - 1);
200 KOKKOS_INLINE_FUNCTION
void lcm(
int s1,
int s2,
int& s,
int& m1,
int& m2) {
244 KOKKOS_INLINE_FUNCTION Index Index::intersect(
const Index& rhs)
const {
246 if ((
stride() == 1) && (rhs.stride() == 1)) {
248 int rf = rhs.first();
251 int f = lf > rf ? lf : rf;
252 int l = ll < rl ? ll : rl;
254 ret.length_m = ((l >= f) ? l - f + 1 : 0);
257 ret = general_intersect(rhs);
261 KOKKOS_INLINE_FUNCTION Index Index::grow(
int ncells)
const {
264 index.first_m = this->first_m - ncells;
265 index.length_m = this->length_m + 2 * ncells;
266 index.stride_m = this->stride_m;
271 KOKKOS_INLINE_FUNCTION
static Index do_intersect(
const Index& a,
const Index& b) {
272 PAssert_GT(a.stride(), 0);
273 PAssert_GT(b.stride(), 0);
277 lcm(a.stride(), b.stride(),
278 newStride, a_mul, b_mul);
282 int a_i = (b.first() - a.first()) / a.stride();
283 int a_off = a.first() + a_i * a.stride();
284 if (a_off < b.first()) {
289 PAssert_GE(a_off, b.first());
293 for (
int a_m = 0; (a_m < a_mul) && (a_i < (
int)a.length());
294 a_m++, a_i++, a_off += a.stride()) {
295 int b_off = b.first();
297 for (
int b_m = 0; (b_m < b_mul) && (b_m < (
int)b.length()); b_m++, b_off += b.stride())
298 if (a_off == b_off) {
301 int m = am < bm ? am : bm;
302 return Index(a_off, m, newStride);
308 KOKKOS_INLINE_FUNCTION Index Index::general_intersect(
const Index& that)
const {
310 if ((
min() > that.max()) || (that.min() >
max()))
312 if ((stride_m == 0) || (that.stride_m == 0))
316 if (that.stride_m < 0)
317 return intersect(that.reverse());
320 r = reverse().intersect(that).reverse();
327 if (first_m < that.first_m)
328 r = do_intersect(*
this, that);
330 r = do_intersect(that, *
this);
KOKKOS_INLINE_FUNCTION int min() const noexcept
Definition Index.hpp:96
KOKKOS_INLINE_FUNCTION int first() const noexcept
Definition Index.hpp:76
KOKKOS_INLINE_FUNCTION int stride() const noexcept
Definition Index.hpp:80
KOKKOS_INLINE_FUNCTION Index()
Definition Index.hpp:36
KOKKOS_INLINE_FUNCTION bool empty() const noexcept
Definition Index.hpp:84
KOKKOS_INLINE_FUNCTION size_t length() const noexcept
Definition Index.hpp:88
KOKKOS_INLINE_FUNCTION int max() const noexcept
Definition Index.hpp:100
KOKKOS_INLINE_FUNCTION int last() const noexcept
Definition Index.hpp:92