Overture  Version 25
TrivialArray.h
Go to the documentation of this file.
1 #ifndef TrivialArray_
2 #define TrivialArray_
3 
4 #include <assert.h>
5 
6 template<class Type, class Dim>
7 class TrivialArray {
8  public:
9 //
10 // Change this section to allow for any maximum number of dimensions:
11 // Also add more array indexing functions using operator().
12 //
13 // Allow up to ten-dimensional arrays.
14  enum { MAXDIM = 10 };
15 #define DIM_ARGUMENTS \
16  const Dim& dim0 = 0, const Dim& dim1 = 0, \
17  const Dim& dim2 = 0, const Dim& dim3 = 0, \
18  const Dim& dim4 = 0, const Dim& dim5 = 0, \
19  const Dim& dim6 = 0, const Dim& dim7 = 0, \
20  const Dim& dim8 = 0, const Dim& dim9 = 0
21 #define DIM_ARGS dim0, dim1, dim2, dim3, dim4, dim5, dim6, dim7, dim8, dim9
22 #define X_DIM_ARGS \
23  x.dimension[0], x.dimension[1], x.dimension[2], \
24  x.dimension[3], x.dimension[4], x.dimension[5], \
25  x.dimension[6], x.dimension[7], x.dimension[8], \
26  x.dimension[9]
27 #define DIM_DECLARATIONS \
28  Dim dim[MAXDIM]; \
29  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; \
30  dim[3] = dim3; dim[4] = dim4; dim[5] = dim5; \
31  dim[6] = dim6; dim[7] = dim7; dim[8] = dim8; \
32  dim[9] = dim9
33 //
34 // Public member functions:
35 //
36 // Default constructor, with optional arguments of type Dim.
37 //
38 #ifdef USE_DEFAULT_ARGUMENTS
40  numberOfElements = numberOfDimensions = 0;
41  dataPointer = basePointer = 0;
42  for (int i=0; i<MAXDIM; i++)
43  { dimension[i] = Dim(0); stride[i] = 1; }
44  redim(DIM_ARGS);
45  }
46 #else
47 #define TrivialArray_constructor \
48  numberOfElements = numberOfDimensions = 0; \
49  dataPointer = basePointer = 0; \
50  for (int i=0; i<MAXDIM; i++) \
51  { dimension[i] = Dim(0); stride[i] = 1; }
52 
55  redim();
56  }
57  TrivialArray(const Dim& dim0) {
59  redim(dim0);
60  }
61  TrivialArray(const Dim& dim0, const Dim& dim1) {
63  redim(dim0, dim1);
64  }
65  TrivialArray(const Dim& dim0, const Dim& dim1, const Dim& dim2) {
67  redim(dim0, dim1, dim2);
68  }
69  TrivialArray(const Dim& dim0, const Dim& dim1, const Dim& dim2,
70  const Dim& dim3) {
72  redim(dim0, dim1, dim2, dim3);
73  }
74  TrivialArray(const Dim& dim0, const Dim& dim1, const Dim& dim2,
75  const Dim& dim3, const Dim& dim4) {
77  redim(dim0, dim1, dim2, dim3, dim4);
78  }
79  TrivialArray(const Dim& dim0, const Dim& dim1, const Dim& dim2,
80  const Dim& dim3, const Dim& dim4, const Dim& dim5) {
82  redim(dim0, dim1, dim2, dim3, dim4, dim5);
83  }
84  TrivialArray(const Dim& dim0, const Dim& dim1, const Dim& dim2,
85  const Dim& dim3, const Dim& dim4, const Dim& dim5, const Dim& dim6) {
87  redim(dim0, dim1, dim2, dim3, dim4, dim5, dim6);
88  }
89  TrivialArray(const Dim& dim0, const Dim& dim1, const Dim& dim2,
90  const Dim& dim3, const Dim& dim4, const Dim& dim5, const Dim& dim6,
91  const Dim& dim7) {
93  redim(dim0, dim1, dim2, dim3, dim4, dim5, dim6, dim7);
94  }
95  TrivialArray(const Dim& dim0, const Dim& dim1, const Dim& dim2,
96  const Dim& dim3, const Dim& dim4, const Dim& dim5, const Dim& dim6,
97  const Dim& dim7, const Dim& dim8) {
99  redim(dim0, dim1, dim2, dim3, dim4, dim5, dim6, dim7, dim8);
100  }
101  TrivialArray(const Dim& dim0, const Dim& dim1, const Dim& dim2,
102  const Dim& dim3, const Dim& dim4, const Dim& dim5, const Dim& dim6,
103  const Dim& dim7, const Dim& dim8, const Dim& dim9) {
105  redim(dim0, dim1, dim2, dim3, dim4, dim5, dim6, dim7, dim8, dim9);
106  }
107 #undef TrivialArray_constructor
108 #endif // USE_DEFAULT_ARGUMENTS
109 //
110 // Copy constructor.
111 //
113  numberOfElements = numberOfDimensions = 0;
114  dataPointer = basePointer = 0;
115  for (int i=0; i<MAXDIM; i++)
116  { dimension[i] = Dim(0); stride[i] = 1; }
117  operator=(x);
118  }
119 //
120 // Destructor.
121 //
123  { if (dataPointer) delete [] dataPointer; }
124 //
125 // Reshape. Arguments of type Dim are optional.
126 //
127 #ifdef USE_DEFAULT_ARGUMENTS
128  int reshape(DIM_ARGUMENTS) {
130  int numberOfElements_, numberOfDimensions_, dimsChanged, i;
131  for (numberOfElements_ = numberOfDimensions_ = dimsChanged = 0;
132  numberOfDimensions_ < MAXDIM && dim[numberOfDimensions_] != Dim(0);
133  numberOfDimensions_++)
134  if (numberOfDimensions_)
135  numberOfElements_ *= dim[numberOfDimensions_].getCount();
136  else numberOfElements_ = dim[numberOfDimensions_].getCount();
137  assert(numberOfElements_ == numberOfElements);
138  dimsChanged = numberOfDimensions_ != numberOfDimensions;
139  numberOfDimensions = numberOfDimensions_;
140  basePointer = dataPointer;
141  for (i=0; i<numberOfDimensions; i++) {
142  dimsChanged = dimsChanged || dim[i] != dimension[i];
143  dimension[i] = dim[i];
144  stride[i] = i ? stride[i-1] * dimension[i-1].getCount() : 1;
145  basePointer -= stride[i] * dimension[i].getBase();
146  } // end for
147  for (i=numberOfDimensions; i<MAXDIM; i++) {
148  dimension[i] = Dim(0);
149  stride[i] = i ? stride[i-1] : 1;
150  } // end for
151  return dimsChanged;
152  }
153 #else
154 #define TrivialArray_reshape \
155  int numberOfElements_, numberOfDimensions_, dimsChanged, i; \
156  for (numberOfElements_ = numberOfDimensions_ = dimsChanged = 0; \
157  numberOfDimensions_ < MAXDIM && !(dim[numberOfDimensions_] == Dim(0)); \
158  numberOfDimensions_++) \
159  if (numberOfDimensions_) \
160  numberOfElements_ *= dim[numberOfDimensions_].getCount(); \
161  else numberOfElements_ = dim[numberOfDimensions_].getCount(); \
162  assert(numberOfElements_ == numberOfElements); \
163  dimsChanged = numberOfDimensions_ != numberOfDimensions; \
164  numberOfDimensions = numberOfDimensions_; \
165  basePointer = dataPointer; \
166  for (i=0; i<numberOfDimensions; i++) { \
167  dimsChanged = dimsChanged || !(dim[i] == dimension[i]); \
168  dimension[i] = dim[i]; \
169  stride[i] = i ? stride[i-1] * dimension[i-1].getCount() : 1; \
170  basePointer -= stride[i] * dimension[i].getBase(); \
171  } \
172  for (i=numberOfDimensions; i<MAXDIM; i++) { \
173  dimension[i] = Dim(0); \
174  stride[i] = i ? stride[i-1] : 1; \
175  } \
176  return dimsChanged
177  int reshape() {
178  Dim dim[MAXDIM];
179  dim[0] = 0; dim[1] = 0; dim[2] = 0; dim[3] = 0; dim[4] = 0;
180  dim[5] = 0; dim[6] = 0; dim[7] = 0; dim[8] = 0; dim[9] = 0;
182  }
183  int reshape(const Dim& dim0) {
184  Dim dim[MAXDIM];
185  dim[0] = dim0; dim[1] = 0; dim[2] = 0; dim[3] = 0; dim[4] = 0;
186  dim[5] = 0; dim[6] = 0; dim[7] = 0; dim[8] = 0; dim[9] = 0;
188  }
189  int reshape(const Dim& dim0, const Dim& dim1) {
190  Dim dim[MAXDIM];
191  dim[0] = dim0; dim[1] = dim1; dim[2] = 0; dim[3] = 0; dim[4] = 0;
192  dim[5] = 0; dim[6] = 0; dim[7] = 0; dim[8] = 0; dim[9] = 0;
194  }
195  int reshape(const Dim& dim0, const Dim& dim1, const Dim& dim2) {
196  Dim dim[MAXDIM];
197  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = 0; dim[4] = 0;
198  dim[5] = 0; dim[6] = 0; dim[7] = 0; dim[8] = 0; dim[9] = 0;
200  }
201  int reshape(const Dim& dim0, const Dim& dim1, const Dim& dim2,
202  const Dim& dim3) {
203  Dim dim[MAXDIM];
204  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3; dim[4] = 0;
205  dim[5] = 0; dim[6] = 0; dim[7] = 0; dim[8] = 0; dim[9] = 0;
207  }
208  int reshape(const Dim& dim0, const Dim& dim1, const Dim& dim2,
209  const Dim& dim3, const Dim& dim4) {
210  Dim dim[MAXDIM];
211  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3;
212  dim[4] = dim4; dim[5] = 0; dim[6] = 0; dim[7] = 0;
213  dim[8] = 0; dim[9] = 0;
215  }
216  int reshape(const Dim& dim0, const Dim& dim1, const Dim& dim2,
217  const Dim& dim3, const Dim& dim4, const Dim& dim5) {
218  Dim dim[MAXDIM];
219  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3;
220  dim[4] = dim4; dim[5] = dim5; dim[6] = 0; dim[7] = 0;
221  dim[8] = 0; dim[9] = 0;
223  }
224  int reshape(const Dim& dim0, const Dim& dim1, const Dim& dim2,
225  const Dim& dim3, const Dim& dim4, const Dim& dim5, const Dim& dim6) {
226  Dim dim[MAXDIM];
227  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3;
228  dim[4] = dim4; dim[5] = dim5; dim[6] = dim6; dim[7] = 0;
229  dim[8] = 0; dim[9] = 0;
231  }
232  int reshape(const Dim& dim0, const Dim& dim1, const Dim& dim2,
233  const Dim& dim3, const Dim& dim4, const Dim& dim5, const Dim& dim6,
234  const Dim& dim7) {
235  Dim dim[MAXDIM];
236  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3;
237  dim[4] = dim4; dim[5] = dim5; dim[6] = dim6; dim[7] = dim7;
238  dim[8] = 0; dim[9] = 0;
240  }
241  int reshape(const Dim& dim0, const Dim& dim1, const Dim& dim2,
242  const Dim& dim3, const Dim& dim4, const Dim& dim5, const Dim& dim6,
243  const Dim& dim7, const Dim& dim8) {
244  Dim dim[MAXDIM];
245  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3;
246  dim[4] = dim4; dim[5] = dim5; dim[6] = dim6; dim[7] = dim7;
247  dim[8] = dim8; dim[9] = 0;
249  }
250  int reshape(const Dim& dim0, const Dim& dim1, const Dim& dim2,
251  const Dim& dim3, const Dim& dim4, const Dim& dim5, const Dim& dim6,
252  const Dim& dim7, const Dim& dim8, const Dim& dim9) {
253  Dim dim[MAXDIM];
254  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3;
255  dim[4] = dim4; dim[5] = dim5; dim[6] = dim6; dim[7] = dim7;
256  dim[8] = dim8; dim[9] = dim9;
258  }
259 #undef TrivialArray_reshape
260 #endif // USE_DEFAULT_ARGUMENTS
261  int reshape(const TrivialArray& x) { return reshape(X_DIM_ARGS); }
262 //
263 // Redimension (does not copy data). Arguments of type Dim are optional.
264 //
265 #ifdef USE_DEFAULT_ARGUMENTS
266  int redim(DIM_ARGUMENTS) {
268  int numberOfElements_, numberOfDimensions_, dimsChanged, i;
269  for (numberOfElements_ = numberOfDimensions_ = dimsChanged = 0;
270  numberOfDimensions_ < MAXDIM && dim[numberOfDimensions_] != Dim(0);
271  numberOfDimensions_++) {
272  if (numberOfDimensions_)
273  numberOfElements_ *= dim[numberOfDimensions_].getCount();
274  else numberOfElements_ = dim[numberOfDimensions_].getCount();
275  dimsChanged = dimsChanged ||
276  numberOfDimensions_ >= numberOfDimensions ||
277  dim[numberOfDimensions_] != dimension[numberOfDimensions_];
278  } // end for
279  dimsChanged = dimsChanged || numberOfDimensions_ != numberOfDimensions;
280  if (numberOfElements != numberOfElements_) {
281  assert(dimsChanged);
282  if (dataPointer) delete [] dataPointer;
283  dataPointer = (numberOfElements = numberOfElements_) ?
284  new Type[numberOfElements] : 0;
285  } // end if
286  if (dimsChanged) {
287  numberOfDimensions = numberOfDimensions_;
288  basePointer = dataPointer;
289  for (i=0; i<numberOfDimensions; i++) {
290  dimension[i] = dim[i];
291  stride[i] = i ? stride[i-1] * dimension[i-1].getCount() : 1;
292  basePointer -= stride[i] * dimension[i].getBase();
293  } // end for
294  for (i=numberOfDimensions; i<MAXDIM; i++)
295  { dimension[i] = Dim(0); stride[i] = i ? stride[i-1] : 1; }
296  } // end if
297  return dimsChanged;
298  }
299 #else
300 #define TrivialArray_redim \
301  int numberOfElements_, numberOfDimensions_, dimsChanged, i; \
302  for (numberOfElements_ = numberOfDimensions_ = dimsChanged = 0; \
303  numberOfDimensions_ < MAXDIM && !(dim[numberOfDimensions_] == Dim(0)); \
304  numberOfDimensions_++) { \
305  if (numberOfDimensions_) \
306  numberOfElements_ *= dim[numberOfDimensions_].getCount(); \
307  else numberOfElements_ = dim[numberOfDimensions_].getCount(); \
308  dimsChanged = dimsChanged || \
309  numberOfDimensions_ >= numberOfDimensions || \
310  !(dim[numberOfDimensions_] == dimension[numberOfDimensions_]); \
311  } \
312  dimsChanged = dimsChanged || numberOfDimensions_ != numberOfDimensions; \
313  if (numberOfElements != numberOfElements_) { \
314  assert(dimsChanged); \
315  if (dataPointer) delete [] dataPointer; \
316  dataPointer = (numberOfElements = numberOfElements_) ? \
317  new Type[numberOfElements] : 0; \
318  } \
319  if (dimsChanged) { \
320  numberOfDimensions = numberOfDimensions_; \
321  basePointer = dataPointer; \
322  for (i=0; i<numberOfDimensions; i++) { \
323  dimension[i] = dim[i]; \
324  stride[i] = i ? stride[i-1] * dimension[i-1].getCount() : 1; \
325  basePointer -= stride[i] * dimension[i].getBase(); \
326  } \
327  for (i=numberOfDimensions; i<MAXDIM; i++) \
328  { dimension[i] = Dim(0); stride[i] = i ? stride[i-1] : 1; } \
329  } \
330  return dimsChanged
331  int redim() {
332  Dim dim[MAXDIM];
333  dim[0] = 0; dim[1] = 0; dim[2] = 0; dim[3] = 0; dim[4] = 0;
334  dim[5] = 0; dim[6] = 0; dim[7] = 0; dim[8] = 0; dim[9] = 0;
336  }
337  int redim(const Dim& dim0) {
338  Dim dim[MAXDIM];
339  dim[0] = dim0; dim[1] = 0; dim[2] = 0; dim[3] = 0; dim[4] = 0;
340  dim[5] = 0; dim[6] = 0; dim[7] = 0; dim[8] = 0; dim[9] = 0;
342  }
343  int redim(const Dim& dim0, const Dim& dim1) {
344  Dim dim[MAXDIM];
345  dim[0] = dim0; dim[1] = dim1; dim[2] = 0; dim[3] = 0; dim[4] = 0;
346  dim[5] = 0; dim[6] = 0; dim[7] = 0; dim[8] = 0; dim[9] = 0;
348  }
349  int redim(const Dim& dim0, const Dim& dim1, const Dim& dim2) {
350  Dim dim[MAXDIM];
351  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = 0; dim[4] = 0;
352  dim[5] = 0; dim[6] = 0; dim[7] = 0; dim[8] = 0; dim[9] = 0;
354  }
355  int redim(const Dim& dim0, const Dim& dim1, const Dim& dim2,
356  const Dim& dim3) {
357  Dim dim[MAXDIM];
358  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3; dim[4] = 0;
359  dim[5] = 0; dim[6] = 0; dim[7] = 0; dim[8] = 0; dim[9] = 0;
361  }
362  int redim(const Dim& dim0, const Dim& dim1, const Dim& dim2,
363  const Dim& dim3, const Dim& dim4) {
364  Dim dim[MAXDIM];
365  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3;
366  dim[4] = dim4; dim[5] = 0; dim[6] = 0; dim[7] = 0;
367  dim[8] = 0; dim[9] = 0;
369  }
370  int redim(const Dim& dim0, const Dim& dim1, const Dim& dim2,
371  const Dim& dim3, const Dim& dim4, const Dim& dim5) {
372  Dim dim[MAXDIM];
373  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3;
374  dim[4] = dim4; dim[5] = dim5; dim[6] = 0; dim[7] = 0;
375  dim[8] = 0; dim[9] = 0;
377  }
378  int redim(const Dim& dim0, const Dim& dim1, const Dim& dim2,
379  const Dim& dim3, const Dim& dim4, const Dim& dim5, const Dim& dim6) {
380  Dim dim[MAXDIM];
381  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3;
382  dim[4] = dim4; dim[5] = dim5; dim[6] = dim6; dim[7] = 0;
383  dim[8] = 0; dim[9] = 0;
385  }
386  int redim(const Dim& dim0, const Dim& dim1, const Dim& dim2,
387  const Dim& dim3, const Dim& dim4, const Dim& dim5, const Dim& dim6,
388  const Dim& dim7) {
389  Dim dim[MAXDIM];
390  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3;
391  dim[4] = dim4; dim[5] = dim5; dim[6] = dim6; dim[7] = dim7;
392  dim[8] = 0; dim[9] = 0;
394  }
395  int redim(const Dim& dim0, const Dim& dim1, const Dim& dim2,
396  const Dim& dim3, const Dim& dim4, const Dim& dim5, const Dim& dim6,
397  const Dim& dim7, const Dim& dim8) {
398  Dim dim[MAXDIM];
399  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3;
400  dim[4] = dim4; dim[5] = dim5; dim[6] = dim6; dim[7] = dim7;
401  dim[8] = dim8; dim[9] = 0;
403  }
404  int redim(const Dim& dim0, const Dim& dim1, const Dim& dim2,
405  const Dim& dim3, const Dim& dim4, const Dim& dim5, const Dim& dim6,
406  const Dim& dim7, const Dim& dim8, const Dim& dim9) {
407  Dim dim[MAXDIM];
408  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3;
409  dim[4] = dim4; dim[5] = dim5; dim[6] = dim6; dim[7] = dim7;
410  dim[8] = dim8; dim[9] = dim9;
412  }
413 #undef TrivialArray_redim
414 #endif // USE_DEFAULT_ARGUMENTS
415  int redim(const TrivialArray& x) { return redim(X_DIM_ARGS); }
416 //
417 // Resize (Copies data). Arguments of type Dim are optional.
418 //
419 #ifdef USE_DEFAULT_ARGUMENTS
420  int resize(DIM_ARGUMENTS) {
422  Dim dimension_[MAXDIM];
423  int stride_[MAXDIM], base[MAXDIM], bound[MAXDIM];
424  int numberOfElements_, numberOfDimensions_, dimsChanged,
425  dimsOverlap = 1, i;
426  for (numberOfElements_ = numberOfDimensions_ = dimsChanged = 0;
427  numberOfDimensions_ < MAXDIM && dim[numberOfDimensions_] != Dim(0);
428  numberOfDimensions_++) {
429  dimension_[numberOfDimensions_] = dimension[numberOfDimensions_];
430  if ((base[numberOfDimensions_] = max0(
431  dimension[numberOfDimensions_].getBase(),
432  dim[numberOfDimensions_] .getBase())) >
433  (bound[numberOfDimensions_] = min0(
434  dimension[numberOfDimensions_].getBound(),
435  dim[numberOfDimensions_] .getBound())))
436  dimsOverlap = 0;
437  stride_[numberOfDimensions_] = stride[numberOfDimensions_];
438  if (numberOfDimensions_)
439  numberOfElements_ *= dim[numberOfDimensions_].getCount();
440  else numberOfElements_ = dim[numberOfDimensions_].getCount();
441  dimsChanged = dimsChanged ||
442  numberOfDimensions_ >= numberOfDimensions ||
443  dim[numberOfDimensions_] != dimension[numberOfDimensions_];
444  } // end for
445  assert (numberOfDimensions_ == numberOfDimensions);
446  if (dimsChanged) {
447  Type *dataPointer_ = dataPointer, *basePointer_ = basePointer;
448  dataPointer = (numberOfElements = numberOfElements_) ?
449  new Type[numberOfElements] : 0;
450  basePointer = dataPointer;
451  for (i=0; i<numberOfDimensions; i++) {
452  dimension[i] = dim[i];
453  stride[i] = i ? stride[i-1] * dimension[i-1].getCount() : 1;
454  basePointer -= stride[i] * dimension[i].getBase();
455  } // end for
456  for (i=numberOfDimensions; i<MAXDIM; i++) {
457  dimension[i] = Dim(0); stride[i] = i ? stride[i-1] : 1;
458  base[i] = bound[i] = dimension[i].getBase();
459  } // end for
460  if (dimsOverlap)
461  for (int i9=base[9]; i9<=bound[9]; i9++)
462  for (int i8=base[8]; i8<=bound[8]; i8++)
463  for (int i7=base[7]; i7<=bound[7]; i7++)
464  for (int i6=base[6]; i6<=bound[6]; i6++)
465  for (int i5=base[5]; i5<=bound[5]; i5++)
466  for (int i4=base[4]; i4<=bound[4]; i4++)
467  for (int i3=base[3]; i3<=bound[3]; i3++)
468  for (int i2=base[2]; i2<=bound[2]; i2++)
469  for (int i1=base[1]; i1<=bound[1]; i1++)
470  for (int i0=base[0]; i0<=bound[0]; i0++)
471  basePointer[ i0 +
472  stride[1] * i1 + stride[2] * i2 + stride[3] * i3 +
473  stride[4] * i4 + stride[5] * i5 + stride[6] * i6 +
474  stride[7] * i7 + stride[8] * i8 + stride[9] * i9] =
475  basePointer_[ i0 +
476  stride_[1] * i1 + stride_[2] * i2 + stride_[3] * i3 +
477  stride_[4] * i4 + stride_[5] * i5 + stride_[6] * i6 +
478  stride_[7] * i7 + stride_[8] * i8 + stride_[9] * i9];
479  if (dataPointer_) delete [] dataPointer_;
480  } // end if
481  return dimsChanged;
482  }
483 #else
484 #define TrivialArray_resize \
485  Dim dimension_[MAXDIM]; \
486  int stride_[MAXDIM], base[MAXDIM], bound[MAXDIM]; \
487  int numberOfElements_, numberOfDimensions_, dimsChanged, \
488  dimsOverlap = 1, i; \
489  for (numberOfElements_ = numberOfDimensions_ = dimsChanged = 0; \
490  numberOfDimensions_ < MAXDIM && !(dim[numberOfDimensions_] == Dim(0)); \
491  numberOfDimensions_++) { \
492  dimension_[numberOfDimensions_] = dimension[numberOfDimensions_]; \
493  if ((base[numberOfDimensions_] = max0( \
494  dimension[numberOfDimensions_].getBase(), \
495  dim[numberOfDimensions_] .getBase())) > \
496  (bound[numberOfDimensions_] = min0( \
497  dimension[numberOfDimensions_].getBound(), \
498  dim[numberOfDimensions_] .getBound()))) \
499  dimsOverlap = 0; \
500  stride_[numberOfDimensions_] = stride[numberOfDimensions_]; \
501  if (numberOfDimensions_) \
502  numberOfElements_ *= dim[numberOfDimensions_].getCount(); \
503  else numberOfElements_ = dim[numberOfDimensions_].getCount(); \
504  dimsChanged = dimsChanged || \
505  numberOfDimensions_ >= numberOfDimensions || \
506  !(dim[numberOfDimensions_] == dimension[numberOfDimensions_]); \
507  } \
508  assert (numberOfDimensions_ == numberOfDimensions); \
509  if (dimsChanged) { \
510  Type *dataPointer_ = dataPointer, *basePointer_ = basePointer; \
511  dataPointer = (numberOfElements = numberOfElements_) ? \
512  new Type[numberOfElements] : 0; \
513  basePointer = dataPointer; \
514  for (i=0; i<numberOfDimensions; i++) { \
515  dimension[i] = dim[i]; \
516  stride[i] = i ? stride[i-1] * dimension[i-1].getCount() : 1; \
517  basePointer -= stride[i] * dimension[i].getBase(); \
518  } \
519  for (i=numberOfDimensions; i<MAXDIM; i++) { \
520  dimension[i] = Dim(0); stride[i] = i ? stride[i-1] : 1; \
521  base[i] = bound[i] = dimension[i].getBase(); \
522  } \
523  if (dimsOverlap) \
524  for (int i9=base[9]; i9<=bound[9]; i9++) \
525  for (int i8=base[8]; i8<=bound[8]; i8++) \
526  for (int i7=base[7]; i7<=bound[7]; i7++) \
527  for (int i6=base[6]; i6<=bound[6]; i6++) \
528  for (int i5=base[5]; i5<=bound[5]; i5++) \
529  for (int i4=base[4]; i4<=bound[4]; i4++) \
530  for (int i3=base[3]; i3<=bound[3]; i3++) \
531  for (int i2=base[2]; i2<=bound[2]; i2++) \
532  for (int i1=base[1]; i1<=bound[1]; i1++) \
533  for (int i0=base[0]; i0<=bound[0]; i0++) \
534  basePointer[ i0 + \
535  stride[1] * i1 + stride[2] * i2 + stride[3] * i3 + \
536  stride[4] * i4 + stride[5] * i5 + stride[6] * i6 + \
537  stride[7] * i7 + stride[8] * i8 + stride[9] * i9] = \
538  basePointer_[ i0 + \
539  stride_[1] * i1 + stride_[2] * i2 + stride_[3] * i3 + \
540  stride_[4] * i4 + stride_[5] * i5 + stride_[6] * i6 + \
541  stride_[7] * i7 + stride_[8] * i8 + stride_[9] * i9]; \
542  if (dataPointer_) delete [] dataPointer_; \
543  } \
544  return dimsChanged
545  int resize() {
546  Dim dim[MAXDIM];
547  dim[0] = 0; dim[1] = 0; dim[2] = 0; dim[3] = 0; dim[4] = 0;
548  dim[5] = 0; dim[6] = 0; dim[7] = 0; dim[8] = 0; dim[9] = 0;
550  }
551  int resize(const Dim& dim0) {
552  Dim dim[MAXDIM];
553  dim[0] = dim0; dim[1] = 0; dim[2] = 0; dim[3] = 0; dim[4] = 0;
554  dim[5] = 0; dim[6] = 0; dim[7] = 0; dim[8] = 0; dim[9] = 0;
556  }
557  int resize(const Dim& dim0, const Dim& dim1) {
558  Dim dim[MAXDIM];
559  dim[0] = dim0; dim[1] = dim1; dim[2] = 0; dim[3] = 0; dim[4] = 0;
560  dim[5] = 0; dim[6] = 0; dim[7] = 0; dim[8] = 0; dim[9] = 0;
562  }
563  int resize(const Dim& dim0, const Dim& dim1, const Dim& dim2) {
564  Dim dim[MAXDIM];
565  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = 0; dim[4] = 0;
566  dim[5] = 0; dim[6] = 0; dim[7] = 0; dim[8] = 0; dim[9] = 0;
568  }
569  int resize(const Dim& dim0, const Dim& dim1, const Dim& dim2,
570  const Dim& dim3) {
571  Dim dim[MAXDIM];
572  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3; dim[4] = 0;
573  dim[5] = 0; dim[6] = 0; dim[7] = 0; dim[8] = 0; dim[9] = 0;
575  }
576  int resize(const Dim& dim0, const Dim& dim1, const Dim& dim2,
577  const Dim& dim3, const Dim& dim4) {
578  Dim dim[MAXDIM];
579  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3;
580  dim[4] = dim4; dim[5] = 0; dim[6] = 0; dim[7] = 0;
581  dim[8] = 0; dim[9] = 0;
583  }
584  int resize(const Dim& dim0, const Dim& dim1, const Dim& dim2,
585  const Dim& dim3, const Dim& dim4, const Dim& dim5) {
586  Dim dim[MAXDIM];
587  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3;
588  dim[4] = dim4; dim[5] = dim5; dim[6] = 0; dim[7] = 0;
589  dim[8] = 0; dim[9] = 0;
591  }
592  int resize(const Dim& dim0, const Dim& dim1, const Dim& dim2,
593  const Dim& dim3, const Dim& dim4, const Dim& dim5, const Dim& dim6) {
594  Dim dim[MAXDIM];
595  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3;
596  dim[4] = dim4; dim[5] = dim5; dim[6] = dim6; dim[7] = 0;
597  dim[8] = 0; dim[9] = 0;
599  }
600  int resize(const Dim& dim0, const Dim& dim1, const Dim& dim2,
601  const Dim& dim3, const Dim& dim4, const Dim& dim5, const Dim& dim6,
602  const Dim& dim7) {
603  Dim dim[MAXDIM];
604  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3;
605  dim[4] = dim4; dim[5] = dim5; dim[6] = dim6; dim[7] = dim7;
606  dim[8] = 0; dim[9] = 0;
608  }
609  int resize(const Dim& dim0, const Dim& dim1, const Dim& dim2,
610  const Dim& dim3, const Dim& dim4, const Dim& dim5, const Dim& dim6,
611  const Dim& dim7, const Dim& dim8) {
612  Dim dim[MAXDIM];
613  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3;
614  dim[4] = dim4; dim[5] = dim5; dim[6] = dim6; dim[7] = dim7;
615  dim[8] = dim8; dim[9] = 0;
617  }
618  int resize(const Dim& dim0, const Dim& dim1, const Dim& dim2,
619  const Dim& dim3, const Dim& dim4, const Dim& dim5, const Dim& dim6,
620  const Dim& dim7, const Dim& dim8, const Dim& dim9) {
621  Dim dim[MAXDIM];
622  dim[0] = dim0; dim[1] = dim1; dim[2] = dim2; dim[3] = dim3;
623  dim[4] = dim4; dim[5] = dim5; dim[6] = dim6; dim[7] = dim7;
624  dim[8] = dim8; dim[9] = dim9;
626  }
627 #undef TrivialArray_resize
628 #endif // USE_DEFAULT_ARGUMENTS
629  int resize(const TrivialArray& x) { return resize(X_DIM_ARGS); }
630 //
631 // Operator =.
632 //
634  if (this != &x) {
635  if (numberOfElements == 0) {
636  redim(x);
637  } else {
638  assert(numberOfElements == x.numberOfElements);
639  assert(numberOfDimensions == x.numberOfDimensions);
640  for (int i=0; i<MAXDIM; i++)
641  assert(dimension[i] == x.dimension[i]);
642  } // end if
643  for (int i=0; i<numberOfElements; i++)
644  dataPointer[i] = x.dataPointer[i];
645  } // end if
646  return *this;
647  }
648  TrivialArray& operator=(const Type& x) {
649  for (int i=0; i<numberOfElements; i++) dataPointer[i] = x;
650  return *this;
651  }
652 //
653 // The number of array dimensions, elements, etc.
654 //
655  int getNumberOfElements() const { return numberOfElements; }
656  int getNumberOfDimensions() const { return numberOfDimensions; }
657  int getBase(const int i) const { return dimension[i].getBase(); }
658  int getBound(const int i) const { return dimension[i].getBound(); }
659  void consistencyCheck() const { }
660 //
661 // Use operator[] as if the array were one-dimensional, starting at index 0.
662 //
663  Type& operator[](const int i) { return dataPointer[i]; }
664  const Type& operator[](const int i) const { return ((TrivialArray&)*this)[i]; }
665 //
666 // Index the array using operator().
667 //
668  Type& operator()(const int i0)
669  { assert(numberOfDimensions == 1); return basePointer[i0]; }
670  Type& operator()(const int i0, const int i1)
671  { assert(numberOfDimensions == 2); return basePointer[i0 + stride[1] * i1]; }
672  Type& operator()(const int i0, const int i1, const int i2)
673  { assert(numberOfDimensions == 3); return basePointer[i0 + stride[1] * i1 + stride[2] * i2]; }
674  Type& operator()(const int i0, const int i1, const int i2, const int i3)
675  { assert(numberOfDimensions == 4); return basePointer[i0 + stride[1] * i1 + stride[2] * i2 + stride[3] * i3]; }
676  Type& operator()(const int i0, const int i1, const int i2, const int i3, const int i4)
677  { assert(numberOfDimensions == 5); return basePointer[i0 + stride[1] * i1 + stride[2] * i2 + stride[3] * i3 + stride[4] * i4]; }
678  Type& operator()(const int i0, const int i1, const int i2, const int i3, const int i4, const int i5)
679  { assert(numberOfDimensions == 6); return basePointer[i0 + stride[1] * i1 + stride[2] * i2 + stride[3] * i3 + stride[4] * i4 + stride[5] * i5]; }
680  Type& operator()(const int i0, const int i1, const int i2, const int i3, const int i4, const int i5, const int i6)
681  { assert(numberOfDimensions == 7); return basePointer[i0 + stride[1] * i1 + stride[2] * i2 + stride[3] * i3 + stride[4] * i4 + stride[5] * i5
682  + stride[6] * i6]; }
683  Type& operator()(const int i0, const int i1, const int i2, const int i3, const int i4, const int i5, const int i6, const int i7)
684  { assert(numberOfDimensions == 8); return basePointer[i0 + stride[1] * i1 + stride[2] * i2 + stride[3] * i3 + stride[4] * i4 + stride[5] * i5
685  + stride[6] * i6 + stride[7] * i7]; }
686  Type& operator()(const int i0, const int i1, const int i2, const int i3, const int i4, const int i5, const int i6, const int i7, const int i8)
687  { assert(numberOfDimensions == 9); return basePointer[i0 + stride[1] * i1 + stride[2] * i2 + stride[3] * i3 + stride[4] * i4 + stride[5] * i5
688  + stride[6] * i6 + stride[7] * i7 + stride[8] * i8]; }
689  Type& operator()(const int i0, const int i1, const int i2, const int i3, const int i4, const int i5, const int i6, const int i7, const int i8, const int i9)
690  { assert(numberOfDimensions ==10); return basePointer[i0 + stride[1] * i1 + stride[2] * i2 + stride[3] * i3 + stride[4] * i4 + stride[5] * i5
691  + stride[6] * i6 + stride[7] * i7 + stride[8] * i8 + stride[9] * i9]; }
692  const Type& operator()(const int i0) const
693  { return ((TrivialArray&)*this)(i0); }
694  const Type& operator()(const int i0, const int i1) const
695  { return ((TrivialArray&)*this)(i0, i1); }
696  const Type& operator()(const int i0, const int i1, const int i2) const
697  { return ((TrivialArray&)*this)(i0, i1, i2); }
698  const Type& operator()(const int i0, const int i1, const int i2, const int i3) const
699  { return ((TrivialArray&)*this)(i0, i1, i2, i3); }
700  const Type& operator()(const int i0, const int i1, const int i2, const int i3, const int i4) const
701  { return ((TrivialArray&)*this)(i0, i1, i2, i3, i4); }
702  const Type& operator()(const int i0, const int i1, const int i2, const int i3, const int i4, const int i5) const
703  { return ((TrivialArray&)*this)(i0, i1, i2, i3, i4, i5); }
704  const Type& operator()(const int i0, const int i1, const int i2, const int i3, const int i4, const int i5, const int i6) const
705  { return ((TrivialArray&)*this)(i0, i1, i2, i3, i4, i5, i6); }
706  const Type& operator()(const int i0, const int i1, const int i2, const int i3, const int i4, const int i5, const int i6, const int i7) const
707  { return ((TrivialArray&)*this)(i0, i1, i2, i3, i4, i5, i6, i7); }
708  const Type& operator()(const int i0, const int i1, const int i2, const int i3, const int i4, const int i5, const int i6, const int i7, const int i8) const
709  { return ((TrivialArray&)*this)(i0, i1, i2, i3, i4, i5, i6, i7, i8); }
710  const Type& operator()(const int i0, const int i1, const int i2, const int i3, const int i4, const int i5, const int i6, const int i7, const int i8,
711  const int i9) const
712  { return ((TrivialArray&)*this)(i0, i1, i2, i3, i4, i5, i6, i7, i8, i9); }
713  private:
714  static int min0(const int i, const int j) { return i < j ? i : j; }
715  static int max0(const int i, const int j) { return i > j ? i : j; }
716 //
717 // Private data:
718 //
719  int numberOfElements;
720  int numberOfDimensions;
721  Dim dimension[MAXDIM];
722  int stride[MAXDIM];
723  Type *dataPointer, *basePointer;
724 #undef DIM_ARGUMENTS
725 #undef DIM_ARGS
726 #undef X_DIM_ARGS
727 #undef DIM_DECLARATIONS
728 };
729 
730 #endif // TrivialArray_