source: Sophya/trunk/SophyaExt/Blitz/blitz/array/map.h@ 221

Last change on this file since 221 was 221, checked in by ansari, 26 years ago

Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99

File size: 12.8 KB
Line 
1/***************************************************************************
2 * blitz/array/map.h Declaration of the ArrayIndexMapping class
3 *
4 * $Id: map.h,v 1.1.1.1 1999-04-09 17:59:03 ansari Exp $
5 *
6 * Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * Suggestions: blitz-suggest@cybervision.com
19 * Bugs: blitz-bugs@cybervision.com
20 *
21 * For more information, please see the Blitz++ Home Page:
22 * http://seurat.uwaterloo.ca/blitz/
23 *
24 ***************************************************************************
25 * $Log: not supported by cvs2svn $
26 * Revision 1.2 1998/03/14 00:04:47 tveldhui
27 * 0.2-alpha-05
28 *
29 * Revision 1.1 1997/07/16 14:51:20 tveldhui
30 * Update: Alpha release 0.2 (Arrays)
31 *
32 */
33
34/*
35 * ArrayIndexMapping is used to implement tensor array notation. For
36 * example:
37 *
38 * Array<float, 2> A, B;
39 * firstIndex i;
40 * secondIndex j;
41 * thirdIndex k;
42 * Array<float, 3> C = A(i,j) * B(j,k);
43 *
44 * For expression templates purposes, something like B(j,k) is represented
45 * by an instance of class ArrayIndexMapping. This class maps an array onto
46 * the destination array coordinate system, e.g. B(j,k) -> C(i,j,k)
47 */
48
49#ifndef BZ_ARRAYMAP_H
50#define BZ_ARRAYMAP_H
51
52#ifndef BZ_ARRAY_H
53 #error <blitz/array/map.h> must be included via <blitz/array.h>
54#endif
55
56BZ_NAMESPACE(blitz)
57
58/*
59 * _bz_doArrayIndexMapping is a helper class. It is specialized for
60 * ranks 1, 2, 3, ..., 11.
61 */
62
63template<int N_rank>
64struct _bz_doArrayIndexMapping {
65 template<class T_numtype, int N_destRank>
66 static T_numtype map(const Array<T_numtype, N_rank>&,
67 const TinyVector<int,N_destRank>&, int, int, int, int, int, int,
68 int, int, int, int, int)
69 {
70 // If you try to use an array index mapping on an array with
71 // rank greater than 11, then you'll get a precondition failure
72 // here.
73 BZPRECONDITION(0);
74 }
75};
76
77template<>
78struct _bz_doArrayIndexMapping<1> {
79 template<class T_numtype, int N_destRank>
80 static T_numtype map(const Array<T_numtype, 1>& array,
81 const TinyVector<int,N_destRank>& index, int i0, int, int, int, int,
82 int, int, int, int, int, int)
83 {
84 return array(index[i0]);
85 }
86};
87
88
89template<>
90struct _bz_doArrayIndexMapping<2> {
91 template<class T_numtype, int N_destRank>
92 static T_numtype map(const Array<T_numtype, 2>& array,
93 const TinyVector<int,N_destRank>& index, int i0, int i1, int,
94 int, int, int, int, int, int, int, int)
95 {
96 return array(index[i0], index[i1]);
97 }
98};
99
100template<>
101struct _bz_doArrayIndexMapping<3> {
102 template<class T_numtype, int N_destRank>
103 static T_numtype map(const Array<T_numtype, 3>& array,
104 const TinyVector<int,N_destRank>& index, int i0, int i1, int i2,
105 int, int, int, int, int, int, int, int)
106 {
107 return array(index[i0], index[i1], index[i2]);
108 }
109};
110
111template<>
112struct _bz_doArrayIndexMapping<4> {
113 template<class T_numtype, int N_destRank>
114 static T_numtype map(const Array<T_numtype, 4>& array,
115 const TinyVector<int,N_destRank>& index, int i0, int i1, int i2,
116 int i3, int, int, int, int, int, int, int)
117 {
118 return array(index[i0], index[i1], index[i2], index[i3]);
119 }
120};
121
122template<>
123struct _bz_doArrayIndexMapping<5> {
124 template<class T_numtype, int N_destRank>
125 static T_numtype map(const Array<T_numtype, 5>& array,
126 const TinyVector<int,N_destRank>& index, int i0, int i1, int i2,
127 int i3, int i4, int, int, int, int, int, int)
128 {
129 return array(index[i0], index[i1], index[i2], index[i3], index[i4]);
130 }
131};
132
133template<>
134struct _bz_doArrayIndexMapping<6> {
135 template<class T_numtype, int N_destRank>
136 static T_numtype map(const Array<T_numtype, 6>& array,
137 const TinyVector<int,N_destRank>& index, int i0, int i1, int i2,
138 int i3, int i4, int i5, int, int, int, int, int)
139 {
140 return array(index[i0], index[i1], index[i2], index[i3], index[i4],
141 index[i5]);
142 }
143};
144
145template<>
146struct _bz_doArrayIndexMapping<7> {
147 template<class T_numtype, int N_destRank>
148 static T_numtype map(const Array<T_numtype, 7>& array,
149 const TinyVector<int,N_destRank>& index, int i0, int i1, int i2,
150 int i3, int i4, int i5, int i6, int, int, int, int)
151 {
152 return array(index[i0], index[i1], index[i2], index[i3], index[i4],
153 index[i5], index[i6]);
154 }
155};
156
157template<>
158struct _bz_doArrayIndexMapping<8> {
159 template<class T_numtype, int N_destRank>
160 static T_numtype map(const Array<T_numtype, 8>& array,
161 const TinyVector<int,N_destRank>& index, int i0, int i1, int i2,
162 int i3, int i4, int i5, int i6, int i7, int, int, int)
163 {
164 return array(index[i0], index[i1], index[i2], index[i3], index[i4],
165 index[i5], index[i6], index[i7]);
166 }
167};
168
169template<>
170struct _bz_doArrayIndexMapping<9> {
171 template<class T_numtype, int N_destRank>
172 static T_numtype map(const Array<T_numtype, 9>& array,
173 const TinyVector<int,N_destRank>& index, int i0, int i1, int i2,
174 int i3, int i4, int i5, int i6, int i7, int i8, int, int)
175 {
176 return array(index[i0], index[i1], index[i2], index[i3], index[i4],
177 index[i5], index[i6], index[i7], index[i8]);
178 }
179};
180
181template<>
182struct _bz_doArrayIndexMapping<10> {
183 template<class T_numtype, int N_destRank>
184 static T_numtype map(const Array<T_numtype, 10>& array,
185 const TinyVector<int,N_destRank>& index, int i0, int i1, int i2,
186 int i3, int i4, int i5, int i6, int i7, int i8, int i9, int)
187 {
188 return array(index[i0], index[i1], index[i2], index[i3], index[i4],
189 index[i5], index[i6], index[i7], index[i8], index[i9]);
190 }
191};
192
193template<>
194struct _bz_doArrayIndexMapping<11> {
195 template<class T_numtype, int N_destRank>
196 static T_numtype map(const Array<T_numtype, 11>& array,
197 const TinyVector<int,N_destRank>& index, int i0, int i1, int i2,
198 int i3, int i4, int i5, int i6, int i7, int i8, int i9, int i10)
199 {
200 return array(index[i0], index[i1], index[i2], index[i3], index[i4],
201 index[i5], index[i6], index[i7], index[i8], index[i9],
202 index[i10]);
203 }
204};
205
206template<class P_numtype, int N_rank, int N_map0, int N_map1=0, int N_map2=0,
207 int N_map3=0, int N_map4=0, int N_map5=0, int N_map6=0, int N_map7=0,
208 int N_map8=0, int N_map9=0, int N_map10=0>
209class ArrayIndexMapping {
210public:
211 typedef P_numtype T_numtype;
212 typedef const Array<T_numtype,N_rank>& T_ctorArg1;
213 typedef int T_ctorArg2; // dummy
214
215 /*
216 * This enum block finds the maximum of the N_map0, N_map1, ..., N_map10
217 * parameters and stores it in maxRank10. The rank of the expression is
218 * then maxRank10 + 1, since the IndexPlaceholders start at 0 rather than
219 * 1.
220 */
221 enum {
222 maxRank1 = (N_map0 > N_map1) ? N_map0 : N_map1,
223 maxRank2 = (N_map2 > maxRank1) ? N_map2 : maxRank1,
224 maxRank3 = (N_map3 > maxRank2) ? N_map3 : maxRank2,
225 maxRank4 = (N_map4 > maxRank3) ? N_map4 : maxRank3,
226 maxRank5 = (N_map5 > maxRank4) ? N_map5 : maxRank4,
227 maxRank6 = (N_map6 > maxRank5) ? N_map6 : maxRank5,
228 maxRank7 = (N_map7 > maxRank6) ? N_map7 : maxRank6,
229 maxRank8 = (N_map8 > maxRank7) ? N_map8 : maxRank7,
230 maxRank9 = (N_map9 > maxRank8) ? N_map9 : maxRank8,
231 maxRank10 = (N_map10 > maxRank9) ? N_map10 : maxRank9
232 };
233
234 enum { numArrayOperands = 1, numIndexPlaceholders = 1,
235 rank = maxRank10 + 1 };
236
237 ArrayIndexMapping(const Array<T_numtype, N_rank>& array)
238 : array_(array)
239 {
240 }
241
242 ArrayIndexMapping(const ArrayIndexMapping<T_numtype,N_rank,N_map0,
243 N_map1,N_map2,N_map3,N_map4,N_map5,N_map6,N_map7,N_map8,N_map9,
244 N_map10>& z)
245 : array_(z.array_)
246 {
247 }
248
249#ifdef BZ_ARRAY_EXPR_PASS_INDEX_BY_VALUE
250 template<int N_destRank>
251 T_numtype operator()(TinyVector<int, N_destRank> i)
252 {
253 return _bz_doArrayIndexMapping<N_rank>::map(array_, i,
254 N_map0, N_map1, N_map2, N_map3, N_map4, N_map5, N_map6,
255 N_map7, N_map8, N_map9, N_map10);
256 }
257#else
258 template<int N_destRank>
259 T_numtype operator()(const TinyVector<int, N_destRank>& i)
260 {
261 return _bz_doArrayIndexMapping<N_rank>::map(array_, i,
262 N_map0, N_map1, N_map2, N_map3, N_map4, N_map5, N_map6,
263 N_map7, N_map8, N_map9, N_map10);
264 }
265#endif
266
267 int lbound(int rank)
268 {
269 if (N_map0 == rank)
270 return array_.lbound(0);
271 else if ((N_map1 == rank) && (N_rank > 1))
272 return array_.lbound(1);
273 else if ((N_map2 == rank) && (N_rank > 2))
274 return array_.lbound(2);
275 else if ((N_map3 == rank) && (N_rank > 3))
276 return array_.lbound(3);
277 else if ((N_map4 == rank) && (N_rank > 4))
278 return array_.lbound(4);
279 else if ((N_map5 == rank) && (N_rank > 5))
280 return array_.lbound(5);
281 else if ((N_map6 == rank) && (N_rank > 6))
282 return array_.lbound(6);
283 else if ((N_map7 == rank) && (N_rank > 7))
284 return array_.lbound(7);
285 else if ((N_map8 == rank) && (N_rank > 8))
286 return array_.lbound(8);
287 else if ((N_map9 == rank) && (N_rank > 9))
288 return array_.lbound(9);
289 else if ((N_map10 == rank) && (N_rank > 10))
290 return array_.lbound(10);
291 else
292 return INT_MIN; // tiny(int());
293 }
294
295 int ubound(int rank)
296 {
297 if (N_map0 == rank)
298 return array_.ubound(0);
299 else if ((N_map1 == rank) && (N_rank > 1))
300 return array_.ubound(1);
301 else if ((N_map2 == rank) && (N_rank > 2))
302 return array_.ubound(2);
303 else if ((N_map3 == rank) && (N_rank > 3))
304 return array_.ubound(3);
305 else if ((N_map4 == rank) && (N_rank > 4))
306 return array_.ubound(4);
307 else if ((N_map5 == rank) && (N_rank > 5))
308 return array_.ubound(5);
309 else if ((N_map6 == rank) && (N_rank > 6))
310 return array_.ubound(6);
311 else if ((N_map7 == rank) && (N_rank > 7))
312 return array_.ubound(7);
313 else if ((N_map8 == rank) && (N_rank > 8))
314 return array_.ubound(8);
315 else if ((N_map9 == rank) && (N_rank > 9))
316 return array_.ubound(9);
317 else if ((N_map10 == rank) && (N_rank > 10))
318 return array_.ubound(10);
319 else
320 return INT_MAX; // huge(int());
321 }
322
323 // If you have a precondition failure on this routine, it means
324 // you are trying to use stack iteration mode on an expression
325 // which contains an index placeholder. You must use index
326 // iteration mode instead.
327 int operator*()
328 {
329 BZPRECONDITION(0);
330 return 0;
331 }
332
333 // See operator*() note
334 void push(int)
335 {
336 BZPRECONDITION(0);
337 }
338
339 // See operator*() note
340 void pop(int)
341 {
342 BZPRECONDITION(0);
343 }
344
345 // See operator*() note
346 void advance()
347 {
348 BZPRECONDITION(0);
349 }
350
351 // See operator*() note
352 void advance(int)
353 {
354 BZPRECONDITION(0);
355 }
356
357 // See operator*() note
358 void loadStride(int)
359 {
360 BZPRECONDITION(0);
361 }
362
363 _bz_bool isUnitStride(int rank) const
364 {
365 BZPRECONDITION(0);
366 return false;
367 }
368
369 void advanceUnitStride()
370 {
371 BZPRECONDITION(0);
372 }
373
374 _bz_bool canCollapse(int,int) const
375 { BZPRECONDITION(0); return _bz_false; }
376
377 T_numtype operator[](int)
378 {
379 BZPRECONDITION(0);
380 return T_numtype();
381 }
382
383 T_numtype fastRead(int)
384 {
385 BZPRECONDITION(0);
386 return T_numtype();
387 }
388
389 int suggestStride(int) const
390 {
391 BZPRECONDITION(0);
392 return 0;
393 }
394
395 _bz_bool isStride(int,int) const
396 {
397 BZPRECONDITION(0);
398 return _bz_true;
399 }
400
401 template<int N_rank2>
402 void moveTo(const TinyVector<int,N_rank2>& i)
403 {
404 BZPRECONDITION(0);
405 return ;
406 }
407
408 void prettyPrint(string& str, prettyPrintFormat& format) const
409 {
410 // NEEDS_WORK-- do real formatting for reductions
411 str += "map[NEEDS_WORK]";
412 }
413
414 template<class T_shape>
415 _bz_bool shapeCheck(const T_shape& shape) const
416 {
417 // NEEDS_WORK-- do a real shape check (tricky)
418 return _bz_true;
419 }
420
421private:
422 ArrayIndexMapping() { }
423
424 const Array<T_numtype, N_rank>& array_;
425};
426
427BZ_NAMESPACE_END
428
429#endif // BZ_ARRAYMAP_H
430
Note: See TracBrowser for help on using the repository browser.