00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef INDEX_H
00012 #define INDEX_H
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 #include "PETE/IpplExpressions.h"
00229
00230 #ifdef IPPL_USE_STANDARD_HEADERS
00231 #include <iostream>
00232 using namespace std;
00233 #else
00234 #include <iostream.h>
00235 #endif
00236
00237
00238 class Index;
00239 ostream& operator<<(ostream& out, const Index& I);
00240
00241
00242 class Index : public PETE_Expr<Index>
00243 {
00244
00245 public:
00246 class iterator
00247 {
00248 public:
00249
00250 iterator() : Current(0), Stride(0) {}
00251 iterator(int current, int stride=1) : Current(current), Stride(stride) {}
00252
00253 int operator*() { return Current ; }
00254 iterator operator--(int)
00255 {
00256 iterator tmp = *this;
00257 Current -= Stride;
00258 return tmp;
00259 }
00260 iterator& operator--()
00261 {
00262 Current -= Stride;
00263 return (*this);
00264 }
00265 iterator operator++(int)
00266 {
00267 iterator tmp = *this;
00268 Current += Stride;
00269 return tmp;
00270 }
00271 iterator& operator++()
00272 {
00273 Current += Stride;
00274 return (*this);
00275 }
00276 iterator& operator+=(int i)
00277 {
00278 Current += (Stride * i);
00279 return *this;
00280 }
00281 iterator& operator-=(int i)
00282 {
00283 Current -= (Stride * i);
00284 return *this;
00285 }
00286 iterator operator+(int i) const
00287 {
00288 return iterator(Current+i*Stride,Stride);
00289 }
00290 iterator operator-(int i) const
00291 {
00292 return iterator(Current-i*Stride,Stride);
00293 }
00294 int operator[](int i) const
00295 {
00296 return Current + i * Stride;
00297 }
00298 bool operator==(const iterator &y) const
00299 {
00300 return (Current == y.Current) && (Stride == y.Stride);
00301 }
00302 bool operator<(const iterator &y) const
00303 {
00304 return (Current < y.Current)||
00305 ((Current==y.Current)&&(Stride<y.Stride));
00306 }
00307 bool operator!=(const iterator &y) const { return !((*this) == y); }
00308 bool operator> (const iterator &y) const { return y < (*this); }
00309 bool operator<=(const iterator &y) const { return !(y < (*this)); }
00310 bool operator>=(const iterator &y) const { return !((*this) < y); }
00311 private:
00312
00313 int Stride;
00314 int Current;
00315 };
00316
00317 class cursor : public PETE_Expr<cursor>
00318 {
00319 private:
00320 int Current;
00321 int Stride;
00322 int First;
00323 unsigned Dim;
00324 const Index* I;
00325 public:
00326 cursor() {}
00327 cursor(const Index& i)
00328 : Current(i.first()),
00329 Stride(i.stride()),
00330 First(i.first()),
00331 Dim(0),
00332 I(&i)
00333 {
00334 }
00335
00336 #ifdef IPPL_PURIFY
00337 cursor(const cursor &model)
00338 : Current(model.Current), Stride(model.Stride),
00339 First(model.First), Dim(model.Dim), I(model.I) {}
00340 cursor& operator=(const cursor &rhs)
00341 {
00342 Current = rhs.Current;
00343 Stride = rhs.Stride;
00344 First = rhs.First;
00345 Dim = rhs.Dim;
00346 I = rhs.I;
00347 return *this;
00348 }
00349 #endif
00350
00351 int operator*() const { return Current; }
00352 int offset() const { return Current; }
00353 int offset(int i) const
00354 {
00355 return Current +
00356 ( Dim==0 ? i*Stride : 0 );
00357 }
00358 int offset(int i, int j) const
00359 {
00360 return Current +
00361 ( Dim==0 ? i*Stride : 0 ) +
00362 ( Dim==1 ? j*Stride : 0 );
00363 }
00364 int offset(int i, int j, int k) const
00365 {
00366 return Current +
00367 ( Dim==0 ? i*Stride : 0 ) +
00368 ( Dim==1 ? j*Stride : 0 ) +
00369 ( Dim==2 ? k*Stride : 0 );
00370 }
00371 void step(unsigned d)
00372 {
00373 if ( d==Dim )
00374 Current += Stride;
00375 }
00376 void rewind(unsigned d)
00377 {
00378 if ( d==Dim )
00379 Current = First;
00380 }
00381 bool plugBase(const Index& i, unsigned d=0)
00382 {
00383 Index plugged( I->plugBase(i) );
00384 Current = First = plugged.first();
00385 Stride = plugged.stride();
00386 Dim = d;
00387 return true;
00388 }
00389 int id() const { return I->id(); }
00390
00391
00392 enum { IsExpr = 1 };
00393 typedef cursor PETE_Expr_t;
00394 typedef int PETE_Return_t;
00395 cursor MakeExpression() const { return *this; }
00396 };
00397
00398
00399
00400 Index();
00401 inline Index(unsigned n);
00402 inline Index(int f, int l);
00403 inline Index(int f, int l, int s);
00404
00405 ~Index() {};
00406 int id() const { return Base; }
00407
00408 inline int min() const;
00409 inline int max() const;
00410 inline int length() const;
00411 inline int stride() const;
00412 inline int first() const;
00413 inline int last() const;
00414 inline bool empty() const;
00415 inline int getBase() const;
00416
00417
00418 friend inline Index operator+(const Index&,int);
00419 friend inline Index operator+(int,const Index&);
00420 friend inline Index operator-(const Index&,int);
00421 friend inline Index operator-(int,const Index&);
00422
00423
00424 friend inline Index operator-(const Index&);
00425 friend inline Index operator*(const Index&,int);
00426 friend inline Index operator*(int,const Index&);
00427 friend inline Index operator/(const Index&,int);
00428
00429
00430 Index intersect(const Index &) const;
00431
00432
00433 inline Index plugBase(const Index &) const;
00434
00435
00436 inline bool sameBase(const Index&) const;
00437
00438
00439 inline bool touches (const Index&a) const;
00440
00441 inline bool contains(const Index&a) const;
00442
00443 inline bool containsAllPoints(const Index &b) const;
00444
00445 inline bool split(Index& l, Index& r) const;
00446
00447 inline bool split(Index& l, Index& r, double a) const;
00448
00449
00450 iterator begin() { return iterator(First,Stride); }
00451
00452 iterator end() { return iterator(First+Stride*Length,Stride); }
00453
00454
00455 bool operator<(const Index& r) const
00456 {
00457 return ((Length<r.Length) ||
00458 (Length==r.Length) && ((First<r.First) ||
00459 (First==r.First) &&
00460 ((Length>0)&&(Stride<r.Stride))));
00461 }
00462
00463 bool operator==(const Index& r) const
00464 {
00465 return (Length==r.Length) && (First==r.First) && (Stride==r.Stride);
00466 }
00467
00468 static void findPut(const Index&,const Index&, const Index&,Index&,Index&);
00469
00470
00471 Message& putMessage(Message& m) const {
00472 int dbuf[3];
00473 int *d = dbuf;
00474 d[0] = first();
00475 d[1] = stride();
00476 d[2] = length();
00477 m.put(d, d + 3);
00478 return m;
00479 }
00480
00481
00482 Message& getMessage(Message& m) {
00483 int dbuf[3];
00484 int *d = dbuf;
00485 m.get(d);
00486 *this = Index(d[0], d[0] + (d[2] - 1)*d[1], d[1]);
00487 return m;
00488 }
00489
00490
00491 typedef cursor PETE_Expr_t;
00492 cursor MakeExpression() const { return cursor(*this); }
00493
00494 private:
00495
00496
00497 int First;
00498 int Stride;
00499 unsigned Length;
00500
00501
00502
00503
00504 unsigned BaseFirst;
00505
00506
00507
00508 int Base;
00509
00510
00511 inline Index reverse() const;
00512
00513
00514
00515 inline Index(int m, int a, const Index &b);
00516 inline Index(int f, int s, const Index *b);
00517
00518
00519 Index general_intersect(const Index&) const;
00520
00521
00522 class DontInitialize {};
00523 Index(DontInitialize) {}
00524 };
00525
00527
00528 #include "Index/IndexInlines.h"
00529
00531
00532 #endif // INDEX_H
00533
00534
00535
00536
00537
00538