IPPL (Independent Parallel Particle Layer)
IPPL
Loading...
Searching...
No Matches
Index.h
Go to the documentation of this file.
1//
2// Class Index
3// Define a slice in an array.
4//
5// This essentially defines a list of evenly spaced numbers.
6// Most commonly this list will be increasing (positive stride)
7// but it can also have negative stride and be decreasing.
8//
9// Index() --> A null interval with no elements.
10// Index(n) --> make an Index on [0..n-1]
11// Index(a,b) --> make an Index on [a..b]
12// Index(a,b,s) --> make an Index on [a..b] with stride s
13//
14// Example1:
15// --------
16// Index I(10); --> Index on [0..9]
17// Index Low(5); --> Index on [0..4]
18// Index High(5,9); --> Index on [5..9]
19// Index IOdd(1,9,2); --> Index on [1..9] stride 2
20// Index IEven(0,9,2); --> Index on [0..9] stride 2
21//
22// Given an Index I(a,n,s), and an integer j you can do the following:
23//
24// I+j : a+j+i*s for i in [0..n-1]
25// j-I : j-a-i*s
26// j*I : j*a + i*j*s
27// I/j : a/j + i*s/j
28//
29// j/I we don't do because it is not a uniform stride, and we don't
30// allow strides that are fractions.
31//
32#ifndef IPPL_INDEX_H
33#define IPPL_INDEX_H
34
35#include <Kokkos_Core.hpp>
36
37#include <iostream>
38
39namespace ippl {
40 class Index {
41 public:
42 class iterator {
43 public:
45 : current_m(0)
46 , stride_m(0) {}
47
48 iterator(int current, int stride = 1)
49 : current_m(current)
50 , stride_m(stride) {}
51
52 int operator*() { return current_m; }
53
55 iterator tmp = *this;
56 current_m -= stride_m; // Post decrement
57 return tmp;
58 }
59
62 return (*this);
63 }
64
66 iterator tmp = *this;
67 current_m += stride_m; // Post increment
68 return tmp;
69 }
70
73 return (*this);
74 }
75
77 current_m += (stride_m * i);
78 return *this;
79 }
80
82 current_m -= (stride_m * i);
83 return *this;
84 }
85
86 iterator operator+(int i) const { return iterator(current_m + i * stride_m, stride_m); }
87
88 iterator operator-(int i) const { return iterator(current_m - i * stride_m, stride_m); }
89
90 int operator[](int i) const { return current_m + i * stride_m; }
91
92 bool operator==(const iterator& y) const {
93 return (current_m == y.current_m) && (stride_m == y.stride_m);
94 }
95
96 bool operator<(const iterator& y) const {
97 return (current_m < y.current_m)
98 || ((current_m == y.current_m) && (stride_m < y.stride_m));
99 }
100
101 bool operator!=(const iterator& y) const { return !((*this) == y); }
102
103 bool operator>(const iterator& y) const { return y < (*this); }
104
105 bool operator<=(const iterator& y) const { return !(y < (*this)); }
106
107 bool operator>=(const iterator& y) const { return !((*this) < y); }
108
109 private:
112 };
113
117 KOKKOS_INLINE_FUNCTION Index();
118
123 KOKKOS_INLINE_FUNCTION Index(size_t n);
124
131 KOKKOS_INLINE_FUNCTION Index(int f, int l);
132
139 KOKKOS_INLINE_FUNCTION Index(int f, int l, int s);
140
141 KOKKOS_DEFAULTED_FUNCTION
142 ~Index() = default;
143
147 KOKKOS_INLINE_FUNCTION int min() const noexcept;
148
152 KOKKOS_INLINE_FUNCTION int max() const noexcept;
153
157 KOKKOS_INLINE_FUNCTION size_t length() const noexcept;
158
162 KOKKOS_INLINE_FUNCTION int stride() const noexcept;
163
167 KOKKOS_INLINE_FUNCTION int first() const noexcept;
168
172 KOKKOS_INLINE_FUNCTION int last() const noexcept;
173
177 KOKKOS_INLINE_FUNCTION bool empty() const noexcept;
178
179 KOKKOS_INLINE_FUNCTION Index& operator+=(int);
180
181 KOKKOS_INLINE_FUNCTION Index& operator-=(int);
182
183 // Additive operations.
184 KOKKOS_INLINE_FUNCTION friend Index operator+(const Index&, int);
185
186 KOKKOS_INLINE_FUNCTION friend Index operator+(int, const Index&);
187
188 KOKKOS_INLINE_FUNCTION friend Index operator-(const Index&, int);
189
190 KOKKOS_INLINE_FUNCTION friend Index operator-(int, const Index&);
191
192 // Multipplicative operations.
193 KOKKOS_INLINE_FUNCTION friend Index operator-(const Index&);
194
195 KOKKOS_INLINE_FUNCTION friend Index operator*(const Index&, int);
196
197 KOKKOS_INLINE_FUNCTION friend Index operator*(int, const Index&);
198
199 KOKKOS_INLINE_FUNCTION friend Index operator/(const Index&, int);
200
201 // Intersect with another Index.
202 KOKKOS_INLINE_FUNCTION Index intersect(const Index&) const;
203
204 // Intersect with another Index.
205 KOKKOS_INLINE_FUNCTION Index grow(int ncells) const;
206
207 // Test to see if there is any overlap between two Indexes.
208 KOKKOS_INLINE_FUNCTION bool touches(const Index& a) const;
209 // Test to see if one contains another (endpoints only)
210 KOKKOS_INLINE_FUNCTION bool contains(const Index& a) const;
211 // Split one into two.
212 KOKKOS_INLINE_FUNCTION bool split(Index& l, Index& r) const;
213 // Split one into two at index i.
214 KOKKOS_INLINE_FUNCTION bool split(Index& l, Index& r, int i) const;
215 // Split index into two with a ratio between 0 and 1.
216 KOKKOS_INLINE_FUNCTION bool split(Index& l, Index& r, double a) const;
217
218 // iterator begin
220 // iterator end
222
223 // An operator< so we can impose some sort of ordering.
224 KOKKOS_INLINE_FUNCTION bool operator<(const Index& r) const {
225 return (
226 (length_m < r.length_m)
227 || ((length_m == r.length_m)
228 && ((first_m < r.first_m)
229 || ((first_m == r.first_m) && (length_m > 0) && (stride_m < r.stride_m)))));
230 }
231 // Test for equality.
232 KOKKOS_INLINE_FUNCTION bool operator==(const Index& r) const noexcept {
233 return (length_m == r.length_m) && (first_m == r.first_m) && (stride_m == r.stride_m);
234 }
235
236 private:
239 size_t length_m;
240
241 // Make an Index that interally counts the other direction.
242 KOKKOS_INLINE_FUNCTION Index reverse() const;
243
244 // Construct with a given base. This is private because
245 // the interface shouldn't depend on how this is done.
246
247 KOKKOS_INLINE_FUNCTION Index(int m, int a, const Index& b);
248
249 KOKKOS_INLINE_FUNCTION Index(int f, int s, const Index* b);
250
251 // Do a general intersect if the strides are not both 1.
252 KOKKOS_INLINE_FUNCTION Index general_intersect(const Index&) const;
253 };
254
255 inline std::ostream& operator<<(std::ostream& out, const Index& I) {
256 out << '[' << I.first() << ':' << I.last() << ':' << I.stride() << ']';
257 return out;
258 }
259} // namespace ippl
260
261#include "Index/Index.hpp"
262
263#endif
Definition Archive.h:20
std::ostream & operator<<(std::ostream &os, const BConds< Field, Dim > &bc)
Definition BConds.h:49
KOKKOS_INLINE_FUNCTION Index general_intersect(const Index &) const
Definition Index.hpp:308
KOKKOS_INLINE_FUNCTION Index intersect(const Index &) const
Definition Index.hpp:244
int stride_m
First index element.
Definition Index.h:238
KOKKOS_INLINE_FUNCTION bool touches(const Index &a) const
Definition Index.hpp:154
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 bool contains(const Index &a) const
Definition Index.hpp:158
KOKKOS_INLINE_FUNCTION Index reverse() const
The number of elements.
Definition Index.hpp:146
int first_m
Definition Index.h:237
KOKKOS_INLINE_FUNCTION bool operator==(const Index &r) const noexcept
Definition Index.h:232
KOKKOS_INLINE_FUNCTION bool split(Index &l, Index &r) const
Definition Index.hpp:162
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 Index grow(int ncells) const
Definition Index.hpp:261
iterator begin()
Definition Index.h:219
KOKKOS_DEFAULTED_FUNCTION ~Index()=default
iterator end()
Definition Index.h:221
KOKKOS_INLINE_FUNCTION int max() const noexcept
Definition Index.hpp:100
KOKKOS_INLINE_FUNCTION bool operator<(const Index &r) const
Definition Index.h:224
size_t length_m
Definition Index.h:239
KOKKOS_INLINE_FUNCTION int last() const noexcept
Definition Index.hpp:92
iterator operator++(int)
Definition Index.h:65
iterator(int current, int stride=1)
Definition Index.h:48
iterator operator--(int)
Definition Index.h:54
iterator & operator+=(int i)
Definition Index.h:76
bool operator<(const iterator &y) const
Definition Index.h:96
bool operator==(const iterator &y) const
Definition Index.h:92
iterator operator+(int i) const
Definition Index.h:86
int operator[](int i) const
Definition Index.h:90
iterator operator-(int i) const
Definition Index.h:88
bool operator!=(const iterator &y) const
Definition Index.h:101
iterator & operator--()
Definition Index.h:60
iterator & operator++()
Definition Index.h:71
bool operator<=(const iterator &y) const
Definition Index.h:105
iterator & operator-=(int i)
Definition Index.h:81
bool operator>(const iterator &y) const
Definition Index.h:103
bool operator>=(const iterator &y) const
Definition Index.h:107