[2b666a]: blitz / array / slicing.cc  Maximize  Restore  History

Download this file

357 lines (318 with data), 10.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/***************************************************************************
* blitz/array/slicing.cc Slicing of arrays
*
* Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Suggestions: blitz-dev@oonumerics.org
* Bugs: blitz-bugs@oonumerics.org
*
* For more information, please see the Blitz++ Home Page:
* http://oonumerics.org/blitz/
*
****************************************************************************/
#ifndef BZ_ARRAYSLICING_CC
#define BZ_ARRAYSLICING_CC
#ifndef BZ_ARRAY_H
#error <blitz/array/slicing.cc> must be included via <blitz/array.h>
#endif
BZ_NAMESPACE(blitz)
/*
* These routines make the array a view of a portion of another array.
* They all work by first referencing the other array, and then slicing.
*/
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::constructSubarray(
Array<T_numtype, N_rank>& array, const RectDomain<N_rank>& subdomain)
{
reference(array);
for (int i=0; i < N_rank; ++i)
slice(i, subdomain[i]);
}
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::constructSubarray(
Array<T_numtype, N_rank>& array, const StridedDomain<N_rank>& subdomain)
{
reference(array);
for (int i=0; i < N_rank; ++i)
slice(i, subdomain[i]);
}
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::constructSubarray(
Array<T_numtype, N_rank>& array, Range r0)
{
reference(array);
slice(0, r0);
}
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::constructSubarray(
Array<T_numtype, N_rank>& array, Range r0, Range r1)
{
reference(array);
slice(0, r0);
slice(1, r1);
}
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::constructSubarray(
Array<T_numtype, N_rank>& array, Range r0, Range r1, Range r2)
{
reference(array);
slice(0, r0);
slice(1, r1);
slice(2, r2);
}
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::constructSubarray(
Array<T_numtype, N_rank>& array, Range r0, Range r1, Range r2, Range r3)
{
reference(array);
slice(0, r0);
slice(1, r1);
slice(2, r2);
slice(3, r3);
}
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::constructSubarray(
Array<T_numtype, N_rank>& array, Range r0, Range r1, Range r2, Range r3,
Range r4)
{
reference(array);
slice(0, r0);
slice(1, r1);
slice(2, r2);
slice(3, r3);
slice(4, r4);
}
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::constructSubarray(
Array<T_numtype, N_rank>& array, Range r0, Range r1, Range r2, Range r3,
Range r4, Range r5)
{
reference(array);
slice(0, r0);
slice(1, r1);
slice(2, r2);
slice(3, r3);
slice(4, r4);
slice(5, r5);
}
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::constructSubarray(
Array<T_numtype, N_rank>& array, Range r0, Range r1, Range r2, Range r3,
Range r4, Range r5, Range r6)
{
reference(array);
slice(0, r0);
slice(1, r1);
slice(2, r2);
slice(3, r3);
slice(4, r4);
slice(5, r5);
slice(6, r6);
}
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::constructSubarray(
Array<T_numtype, N_rank>& array, Range r0, Range r1, Range r2, Range r3,
Range r4, Range r5, Range r6, Range r7)
{
reference(array);
slice(0, r0);
slice(1, r1);
slice(2, r2);
slice(3, r3);
slice(4, r4);
slice(5, r5);
slice(6, r6);
slice(7, r7);
}
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::constructSubarray(
Array<T_numtype, N_rank>& array, Range r0, Range r1, Range r2, Range r3,
Range r4, Range r5, Range r6, Range r7, Range r8)
{
reference(array);
slice(0, r0);
slice(1, r1);
slice(2, r2);
slice(3, r3);
slice(4, r4);
slice(5, r5);
slice(6, r6);
slice(7, r7);
slice(8, r8);
}
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::constructSubarray(
Array<T_numtype, N_rank>& array, Range r0, Range r1, Range r2, Range r3,
Range r4, Range r5, Range r6, Range r7, Range r8, Range r9)
{
reference(array);
slice(0, r0);
slice(1, r1);
slice(2, r2);
slice(3, r3);
slice(4, r4);
slice(5, r5);
slice(6, r6);
slice(7, r7);
slice(8, r8);
slice(9, r9);
}
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::constructSubarray(
Array<T_numtype, N_rank>& array, Range r0, Range r1, Range r2, Range r3,
Range r4, Range r5, Range r6, Range r7, Range r8, Range r9, Range r10)
{
reference(array);
slice(0, r0);
slice(1, r1);
slice(2, r2);
slice(3, r3);
slice(4, r4);
slice(5, r5);
slice(6, r6);
slice(7, r7);
slice(8, r8);
slice(9, r9);
slice(10, r10);
}
/*
* This member template is used to implement operator() with any
* combination of int and Range parameters. There's room for up
* to 11 parameters, but any unused parameters have no effect.
*/
template<typename P_numtype, int N_rank> template<int N_rank2, typename R0,
class R1, typename R2, typename R3, typename R4, typename R5, typename R6, typename R7,
class R8, typename R9, typename R10>
void Array<P_numtype, N_rank>::constructSlice(Array<T_numtype, N_rank2>& array,
R0 r0, R1 r1, R2 r2, R3 r3, R4 r4, R5 r5, R6 r6, R7 r7, R8 r8, R9 r9,
R10 r10)
{
MemoryBlockReference<T_numtype>::changeBlock(array);
int setRank = 0;
TinyVector<int, N_rank2> rankMap;
slice(setRank, r0, array, rankMap, 0);
slice(setRank, r1, array, rankMap, 1);
slice(setRank, r2, array, rankMap, 2);
slice(setRank, r3, array, rankMap, 3);
slice(setRank, r4, array, rankMap, 4);
slice(setRank, r5, array, rankMap, 5);
slice(setRank, r6, array, rankMap, 6);
slice(setRank, r7, array, rankMap, 7);
slice(setRank, r8, array, rankMap, 8);
slice(setRank, r9, array, rankMap, 9);
slice(setRank, r10, array, rankMap, 10);
// Redo the ordering_ array to account for dimensions which
// have been sliced away.
int j = 0;
for (int i=0; i < N_rank2; ++i)
{
if (rankMap[array.ordering(i)] != -1)
storage_.setOrdering(j++, rankMap[array.ordering(i)]);
}
calculateZeroOffset();
}
/*
* This member template is also used in the implementation of
* operator() with any combination of int and Rank parameters.
* It's called by constructSlice(), above. This version handles
* Range parameters.
*/
template<typename P_numtype, int N_rank> template<int N_rank2>
void Array<P_numtype, N_rank>::slice(int& setRank, Range r,
Array<T_numtype,N_rank2>& array, TinyVector<int,N_rank2>& rankMap,
int sourceRank)
{
// NEEDS WORK: ordering will change completely when some ranks
// are deleted.
#ifdef BZ_DEBUG_SLICE
cout << "slice(" << setRank << ", [" << r.first(array.lbound(sourceRank))
<< ", " << r.last(array.ubound(sourceRank)) << "], Array<T,"
<< N_rank2 << ">, " << sourceRank << ")" << endl;
#endif
rankMap[sourceRank] = setRank;
length_[setRank] = array.length(sourceRank);
stride_[setRank] = array.stride(sourceRank);
storage_.setAscendingFlag(setRank, array.isRankStoredAscending(sourceRank));
storage_.setBase(setRank, array.base(sourceRank));
slice(setRank, r);
++setRank;
}
/*
* This member template is also used in the implementation of
* operator() with any combination of int and Rank parameters.
* It's called by constructSlice(), above. This version handles
* int parameters, which reduce the dimensionality by one.
*/
template<typename P_numtype, int N_rank> template<int N_rank2>
void Array<P_numtype, N_rank>::slice(int&, int i,
Array<T_numtype,N_rank2>& array, TinyVector<int,N_rank2>& rankMap,
int sourceRank)
{
#ifdef BZ_DEBUG_SLICE
cout << "slice(" << i
<< ", Array<T," << N_rank2 << ">, " << sourceRank << ")" << endl;
cout << "Offset by " << (i * array.stride(sourceRank))
<< endl;
#endif
BZPRECHECK(array.isInRangeForDim(i, sourceRank),
"Slice is out of range for array: index=" << i << " rank=" << sourceRank
<< endl << "Possible range for index: [" << array.lbound(sourceRank)
<< ", " << array.ubound(sourceRank) << "]");
rankMap[sourceRank] = -1;
data_ += i * array.stride(sourceRank);
#ifdef BZ_DEBUG_SLICE
cout << "data_ = " << data_ << endl;
#endif
}
/*
* After calling slice(int rank, Range r), the array refers only to the
* Range r of the original array.
* e.g. Array<int,1> x(100);
* x.slice(firstRank, Range(25,50));
* x = 0; // Sets elements 25..50 of the original array to 0
*/
template<typename P_numtype, int N_rank>
void Array<P_numtype, N_rank>::slice(int rank, Range r)
{
BZPRECONDITION((rank >= 0) && (rank < N_rank));
int first = r.first(lbound(rank));
int last = r.last(ubound(rank));
diffType stride = r.stride();
#ifdef BZ_DEBUG_SLICE
cout << "slice(" << rank << ", Range):" << endl
<< "first = " << first << " last = " << last << "stride = " << stride
<< endl << "length_[rank] = " << length_[rank] << endl;
#endif
BZPRECHECK((((first <= last) && (stride > 0)) ||
((first >= last) && (stride < 0))) &&
(first >= base(rank) && (first - base(rank)) < length_[rank]) &&
(last >= base(rank) && (last - base(rank)) < length_[rank]),
"Bad array slice: Range(" << first << ", " << last << ", "
<< stride << "). Array is Range(" << lbound(rank) << ", "
<< ubound(rank) << ")");
// Will the storage be non-contiguous?
// (1) Slice in the minor dimension and the range does not span
// the entire index interval (NB: non-unit strides are possible)
// (2) Slice in a middle dimension and the range is not Range::all()
length_[rank] = (last - first) / stride + 1;
// TV 20000312: added second term here, for testsuite/Josef-Wagenhuber
diffType offset = (first - base(rank) * stride) * stride_[rank];
data_ += offset;
zeroOffset_ += offset;
stride_[rank] *= stride;
// JCC: adjust ascending flag if slicing with backwards Range
if (stride<0)
storage_.setAscendingFlag(rank, !isRankStoredAscending(rank));
}
BZ_NAMESPACE_END
#endif // BZ_ARRAYSLICING_CC