OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
IndexInlines.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  *
7  * Visit http://people.web.psi.ch/adelmann/ for more details
8  *
9  ***************************************************************************/
10 
11 #ifndef INDEX_INLINES_H
12 #define INDEX_INLINES_H
13 
14 // include files
15 #include "Utility/Unique.h"
16 #include "Utility/PAssert.h"
17 
19 // Null ctor.
21 inline
23 : First(0),
24  Stride(0),
25  Length(0),
26  BaseFirst(0),
27  Base(Unique::get())
28 {
29 }
30 
32 // Ctor for [0..n-1]
34 
35 inline
36 Index::Index(unsigned n)
37 : First(0),
38  Stride(1),
39  Length(n),
40  BaseFirst(0),
41  Base(Unique::get())
42 {
43 }
44 
45 
47 // ctor for range.
49 inline
50 Index::Index(int f, int l)
51 : First(f),
52  Stride(1),
53  Length(l-f+1),
54  BaseFirst(0),
55  Base(Unique::get())
56 {
57  PAssert_GE(l - f + 1, 0);
58 }
59 
60 inline
61 Index::Index(int f, int l, int s)
62 : First(f),
63  Stride(s),
64  BaseFirst(0),
65  Base(Unique::get())
66 {
67  PAssert_NE(s, 0);
68  if ( f==l ) {
69  Length = 1;
70  }
71  else if ( (l>f) ^ (s<0) ) {
72  Length = (l-f)/s + 1;
73  }
74  else {
75  Length = 0;
76  }
77 }
78 
80 // Some ctors for internal use.
82 
83 inline
84 Index::Index(int m, int a, const Index &b)
85 : First(b.First*m+a),
86  Stride(b.Stride*m),
87  Length(b.Length),
88  BaseFirst( b.BaseFirst ),
89  Base(b.Base)
90 {
91 }
92 
93 inline
94 Index::Index(int f, int s, const Index *b)
95 : First(f),
96  Stride(s),
97  Length(b->Length),
98  BaseFirst(b->BaseFirst),
99  Base(b->Base)
100 {
101 }
102 
104 //
105 // Informational functions about Index.
106 //
107 // first: return the first index.
108 // length: return the number of elements.
109 // stride: the stride between elements.
110 //
111 // min: the minimum value.
112 // max: the maximum value.
113 //
115 
116 inline int Index::first() const
117 {
118  return First;
119 }
120 
121 inline int Index::stride() const
122 {
123  return Stride;
124 }
125 
126 inline bool Index::empty() const
127 {
128  return Length==0;
129 }
130 
131 inline unsigned int Index::length() const
132 {
133  return Length;
134 }
135 
136 inline int Index::last() const
137 {
138  return Length==0 ? First : First + Stride*(Length-1);
139 }
140 
141 inline int Index::min() const
142 {
143  return Stride>=0 ? First : First+Stride*(Length-1);
144 }
145 
146 inline int Index::max() const
147 {
148  return Stride>=0 ? First+Stride*(Length-1) : First;
149 }
150 
151 inline int Index::getBase() const
152 {
153  return Base;
154 }
155 
157 //
158 // Operations on Index's.
159 //
161 
162 inline Index operator+(const Index& i, int off)
163 {
164  return Index(1,off,i);
165 }
166 
167 inline Index operator+(int off, const Index& i)
168 {
169  return Index(1,off,i);
170 }
171 
172 inline Index operator-(const Index& i, int off)
173 {
174  return Index(1,-off,i);
175 }
176 
177 inline Index operator-(int off, const Index& i)
178 {
179  return Index(-1,off,i);
180 }
181 
182 inline Index operator-(const Index& i)
183 {
184  return Index(-1,0,i);
185 }
186 
187 inline Index operator*(const Index& i, int m)
188 {
189  return Index(m,0,i);
190 }
191 
192 inline Index operator*(int m, const Index& i)
193 {
194  return Index(m,0,i);
195 }
196 
197 inline Index operator/(const Index& i, int d)
198 {
199  return Index(i.First/d, i.Stride/d, &i);
200 }
201 
203 //
204 // Comparison operators.
205 //
207 
208 inline bool Index::sameBase(const Index& i) const
209 {
210  return Base == i.Base;
211 }
212 
214 
215 inline Index Index::plugBase(const Index &a) const
216 {
217  Index ret;
218  ret.BaseFirst = a.BaseFirst;
219  ret.Length = a.Length;
220  ret.Stride = Stride;
221  ret.First = First + Stride*(a.BaseFirst-BaseFirst);
222  ret.Base = Base;
223  return ret;
224 }
225 
227 
228 inline Index Index::reverse() const
229 {
230  Index j;
231  j.First = last();
232  j.Length = Length;
233  j.Stride = -Stride;
234  j.Base = Base;
235  j.BaseFirst = BaseFirst;
236  return j;
237 }
238 
240 
241 // Test to see if there is any overlap between two Indexes.
242 inline bool Index::touches(const Index&a) const
243 {
244  return (min()<=a.max())&&(max()>=a.min());
245 }
246 
247 // Test to see if one index completely contains another.
248 inline bool Index::contains(const Index&a) const
249 {
250  return (min()<=a.min())&&(max()>=a.max());
251 }
252 
253 inline bool Index::containsAllPoints(const Index &b) const
254 {
255  // Find min and max values of type domains
256  int a0 = min();
257  int a1 = max();
258  int s = stride();
259  int b0 = b.min();
260  int b1 = b.max();
261  int t = b.stride();
262  if (s < 0)
263  s = -s;
264  if (t < 0)
265  t = -t;
266 
267  // We can do a quick short-circuit check to make sure they do not overlap
268  // at all just from their endpoints. If they don't even do this, we can
269  // quit and say they do not touch.
270  bool quicktest = (a0 <= b0 && a1 >= b1);
271  if (!quicktest || s == 1)
272  return quicktest;
273 
274  // OK, the endpoints of a contain those of b, and we must find out if
275  // all the points in b are found in a. This will be true if:
276  // 1. The stride of b is a multipple of the stride of a
277  // 2. The endpoints of b are found in a
278  // If either of these conditions are false, a does not contain b
279  return (t % s == 0) && ((b0-a0) % s == 0) && ((a1-b1) % s == 0);
280 }
281 
282 
283 
284 // Split an index into equal parts
285 inline bool Index::split(Index& l, Index& r) const
286 {
287  PAssert_EQ(Stride, 1);
288  PAssert_GT(Length, 1);
289  //if ( Length <= 1 )
290  // return false;
291  //else
292  // {
293  int first = First;
294  int length = Length;
295  int mid = first + length/2 - 1;
296  l = Index(first, mid);
297  r = Index(mid+1,first+length-1);
298  return true;
299  // }
300 }
301 
302 // Split an index with the given ratio
303 inline bool Index::split(Index& l, Index& r, double a) const
304 {
305  PAssert_EQ(Stride, 1);
306  PAssert_GT(Length, 1);
307  PAssert_LT(a, 1.0);
308  PAssert_GT(a, 0.0);
309  //if ( Length <= 1 )
310  // return false;
311  //else
312  // {
313  int first = First;
314  int length = Length;
315  int mid = first + static_cast<int>(length*a+0.5) - 1;
316  l = Index(first, mid);
317  r = Index(mid+1,first+length-1);
318  return true;
319  // }
320 }
321 
323 
324 #define INDEX_PETE_DOUBLE_OPERATOR(OP,APP) \
325  \
326 inline \
327 PETE_TBTree< APP , Index::PETE_Expr_t , PETE_Scalar<double> > \
328 OP ( const Index& idx, double x ) \
329 { \
330  typedef \
331  PETE_TBTree< APP , Index::PETE_Expr_t , PETE_Scalar<double> > \
332  Return_t; \
333  return Return_t( idx.MakeExpression(), PETE_Scalar<double>(x) ); \
334 } \
335  \
336 inline \
337 PETE_TBTree< APP , PETE_Scalar<double> , Index::PETE_Expr_t > \
338 OP ( double x , const Index& idx ) \
339 { \
340  typedef \
341  PETE_TBTree< APP , PETE_Scalar<double> , Index::PETE_Expr_t > \
342  Return_t; \
343  return Return_t( PETE_Scalar<double>(x) , idx.MakeExpression()); \
344 }
345 
351 
358 
361 
362 #undef INDEX_PETE_DOUBLE_OPERATOR
363 
365 
366 #define INDEX_PETE_FLOAT_OPERATOR(OP,APP) \
367  \
368 inline \
369 PETE_TBTree< APP , Index::PETE_Expr_t , PETE_Scalar<float> > \
370 OP ( const Index& idx, float x ) \
371 { \
372  typedef \
373  PETE_TBTree< APP , Index::PETE_Expr_t , PETE_Scalar<float> > \
374  Return_t; \
375  return Return_t( idx.MakeExpression(), PETE_Scalar<float>(x) ); \
376 } \
377  \
378 inline \
379 PETE_TBTree< APP , PETE_Scalar<float> , Index::PETE_Expr_t > \
380 OP ( float x , const Index& idx ) \
381 { \
382  typedef \
383  PETE_TBTree< APP , PETE_Scalar<float> , Index::PETE_Expr_t > \
384  Return_t; \
385  return Return_t( PETE_Scalar<float>(x) , idx.MakeExpression()); \
386 }
387 
393 
400 
403 
404 #undef INDEX_PETE_FLOAT_OPERATOR
405 
407 
408 #define INDEX_PETE_INT_OPERATOR(OP,APP) \
409  \
410 inline \
411 PETE_TBTree< APP , Index::PETE_Expr_t , PETE_Scalar<int> > \
412 OP ( const Index& idx, int x ) \
413 { \
414  typedef \
415  PETE_TBTree< APP , Index::PETE_Expr_t , PETE_Scalar<int> > \
416  Return_t; \
417  return Return_t( idx.MakeExpression(), PETE_Scalar<int>(x) ); \
418 } \
419  \
420 inline \
421 PETE_TBTree< APP , PETE_Scalar<int> , Index::PETE_Expr_t > \
422 OP ( int x , const Index& idx ) \
423 { \
424  typedef \
425  PETE_TBTree< APP , PETE_Scalar<int> , Index::PETE_Expr_t > \
426  Return_t; \
427  return Return_t( PETE_Scalar<int>(x) , idx.MakeExpression()); \
428 }
429 
431 
438 
441 
442 #undef INDEX_PETE_INT_OPERATOR
443 
445 
446 #endif // INDEX_INLINES_H
447 
448 /***************************************************************************
449  * $RCSfile: IndexInlines.h,v $ $Author: adelmann $
450  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:27 $
451  * IPPL_VERSION_ID: $Id: IndexInlines.h,v 1.1.1.1 2003/01/23 07:40:27 adelmann Exp $
452  ***************************************************************************/
Matrix< T > operator+(const Matrix< T > &, const Matrix< T > &)
Matrix addition.
Definition: Matrix.h:275
Matrix< T > operator/(const Matrix< T > &, const T &)
Matrix divided by scalar.
Definition: Matrix.h:329
#define INDEX_PETE_DOUBLE_OPERATOR(OP, APP)
Definition: IndexInlines.h:324
PETE_TBTree< OpGT, Index::PETE_Expr_t, PETE_Scalar< double > > gt(const Index &idx, double x)
Definition: IndexInlines.h:354
bool containsAllPoints(const Index &b) const
Definition: IndexInlines.h:253
#define INDEX_PETE_INT_OPERATOR(OP, APP)
Definition: IndexInlines.h:408
PETE_TBTree< OpEQ, Index::PETE_Expr_t, PETE_Scalar< double > > eq(const Index &idx, double x)
Definition: IndexInlines.h:356
int Base
Definition: Index.h:501
PETE_TBTree< OpLE, Index::PETE_Expr_t, PETE_Scalar< double > > le(const Index &idx, double x)
Definition: IndexInlines.h:353
#define INDEX_PETE_FLOAT_OPERATOR(OP, APP)
Definition: IndexInlines.h:366
Index reverse() const
Definition: IndexInlines.h:228
Definition: Unique.h:28
Matrix< T > operator*(const Matrix< T > &, const Matrix< T > &)
Matrix multiply.
Definition: Matrix.h:297
bool split(Index &l, Index &r) const
Definition: IndexInlines.h:285
PETE_TBTree< OpNE, Index::PETE_Expr_t, PETE_Scalar< double > > ne(const Index &idx, double x)
Definition: IndexInlines.h:357
unsigned int length() const
Definition: IndexInlines.h:131
bool sameBase(const Index &) const
Definition: IndexInlines.h:208
#define PAssert_LT(a, b)
Definition: PAssert.h:121
PETE_TBTree< OpGE, Index::PETE_Expr_t, PETE_Scalar< double > > ge(const Index &idx, double x)
Definition: IndexInlines.h:355
int getBase() const
Definition: IndexInlines.h:151
unsigned Length
Definition: Index.h:492
int First
Definition: Index.h:490
#define PAssert_GE(a, b)
Definition: PAssert.h:124
#define PAssert_EQ(a, b)
Definition: PAssert.h:119
Definition: Index.h:236
int last() const
Definition: IndexInlines.h:136
double Max(double a, double b)
int Stride
Definition: Index.h:491
constexpr double a0
Bohr radius in m.
Definition: Physics.h:73
PETE_TBTree< OpLT, Index::PETE_Expr_t, PETE_Scalar< double > > lt(const Index &idx, double x)
Definition: IndexInlines.h:352
bool touches(const Index &a) const
Definition: IndexInlines.h:242
#define PAssert_GT(a, b)
Definition: PAssert.h:123
double Min(double a, double b)
#define PAssert_NE(a, b)
Definition: PAssert.h:120
int max() const
Definition: IndexInlines.h:146
Index plugBase(const Index &) const
Definition: IndexInlines.h:215
bool empty() const
Definition: IndexInlines.h:126
int stride() const
Definition: IndexInlines.h:121
int first() const
Definition: IndexInlines.h:116
Matrix< T > operator-(const Matrix< T > &, const Matrix< T > &)
Matrix subtraction.
Definition: Matrix.h:282
bool contains(const Index &a) const
Definition: IndexInlines.h:248
int min() const
Definition: IndexInlines.h:141
unsigned BaseFirst
Definition: Index.h:497