strided.cc 8.41 KB
Newer Older
1
2
/*! \file strided.cc
 * \brief shape of a strided array (definitions)
thomas.forbriger's avatar
thomas.forbriger committed
3
4
5
 * 
 * ----------------------------------------------------------------------------
 * 
6
 * $Id: strided.cc,v 1.4 2002-12-18 18:43:44 uid1201 Exp $
thomas.forbriger's avatar
thomas.forbriger committed
7
8
9
 * \author Thomas Forbriger
 * \since 08/12/2002
 * 
10
 * shape of a strided array (definitions)
thomas.forbriger's avatar
thomas.forbriger committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 * \sa aff::Strided
 * 
 * Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt) 
 * 
 * REVISIONS and CHANGES 
 *  - 08/12/2002   V1.0   copied from libcontxx
 *  - 11/12/2002   V1.1   major revision (thof)
 *                        - removed template parameter
 *                        - calculates base for index operator of
 *                          each dimensionality
 *  - 12/12/2002   V1.2   (thof)
 *                        - added shrink and collapse
 *  - 13/12/2002   V1.3   (thof)
 *                        - reworked shape definition and mapping
 *                        - added straightforward implementations for
 *                          subarrays and slicing
27
28
29
30
 *  - 14/12/2002   V1.4   (thof)
 *                        - this now is a compilable source file (no longer a
 *                          header) the code contains not template parameter
 *                          and fits well in the binary library
thomas.forbriger's avatar
thomas.forbriger committed
31
32
 *  - 16/12/2002   V1.5   (thof)
 *                        - wolle asked for shift and setfirst functions...
33
34
 *  - 18/12/2002   V1.6   (thof)
 *                        - essential correction in collapse
thomas.forbriger's avatar
thomas.forbriger committed
35
36
37
38
 * 
 * ============================================================================
 */

39
#define AFF_STRIDED_CC_VERSION \
40
  "AFF_STRIDED_DEF_H   V1.6"
41
#define AFF_STRIDED_CC_CVSID \
42
  "$Id: strided.cc,v 1.4 2002-12-18 18:43:44 uid1201 Exp $"
thomas.forbriger's avatar
thomas.forbriger committed
43

44
#include<aff/lib/strided.h>
thomas.forbriger's avatar
thomas.forbriger committed
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
#include<aff/lib/error.h>

namespace aff {

  /*----------------------------------------------------------------------*/

  //! instantiate static member (otherwise the linker won't find it)
  const Tdim Strided::Mmax_dimen; 
  
  /*----------------------------------------------------------------------*/

  //! construct do given size and first index
  Strided::Strided(const TSizeVec& size, 
                   const Tsubscript& first,
                   const Tsubscript& shift)
  {
    TSizeVec one(1);
    AFF_assert((!inline_anylarger(one,size)),
               "ERROR (Strided): size must be at least one");
    Mfirst=first;
    Mstride[0]=1;
    Mlast[0]=Mfirst[0]+size[0]-1;
    for(Tdim i=1; i<Mmax_dimen; i++) 
    { 
      Mstride[i]=Mstride[i-1]*size[i-1]; 
      Mlast[i]=Mfirst[i]+size[i]-1;
    }
    calculate_base(shift);
  }

  /*----------------------------------------------------------------------*/

  //! construct do given first and last index
  Strided::Strided(const TIndexVec& first, const TIndexVec& last, 
                   const Tsubscript& shift)
  {
    AFF_assert((!inline_anylarger(first,last)),
               "ERROR (Strided): first index must be smaller than last");
    Mfirst=first;
    Mlast=last;
    Mstride[0]=1;
    for(Tdim i=1; i<Mmax_dimen; i++) 
    { 
      Mstride[i]=Mstride[i-1]*(1+Mlast[i-1]-Mfirst[i-1]); 
    }
    calculate_base(shift);
  }

  /*----------------------------------------------------------------------*/

  //! construct do given first and last index
  void Strided::calculate_base(const Tsubscript& offset) 
  {
    Mbase[0]=offset-Mfirst[0]*Mstride[0];
    for (Tdim i=1; i<Mmax_dimen; i++) 
    {
      Mbase[i]=Mbase[i-1]-Mfirst[i]*Mstride[i];
    }
  }

  /*----------------------------------------------------------------------*/

  //! calculate total size
  Tsize Strided::size() const
  {
    // could be done more elegant by inlining - but this will do it here
    TSizeVec size(Mlast);
    for (int i=0; i<Mmax_dimen; i++) { size[i]-=(Mfirst[i]-1); }
    return (inline_product(size));
  }

  /*----------------------------------------------------------------------*/
  
  //! subarray
  Strided& Strided::shrink(const TIndexVec& last)
  { 
    AFF_assert((!inline_anysmaller(last,Mfirst)),
               "ERROR (Strided::shrink): index must not be smaller than first");
    AFF_assert((!inline_anylarger(last,Mlast)),
               "ERROR (Strided::shrink): index must not be larger than last");
    Mlast=last;
    return(*this);
  }

  /*----------------------------------------------------------------------*/
  
  //! subarray
  Strided& Strided::shrink(const TIndexVec& first, const TIndexVec& last)
  {
    AFF_assert((!(inline_anysmaller(last,Mfirst)
                  ||inline_anylarger(last,Mlast))),
               "ERROR (Strided::shrink): illegal last index vector");
    AFF_assert((!(inline_anysmaller(first,Mfirst)
                  ||inline_anylarger(first,Mlast))),
               "ERROR (Strided::shrink): illegal first index vector");
    AFF_assert((!inline_anysmaller(last,first)),
               "ERROR (Strided::shrink): last must not be smaller than first");
    Tsubscript offset=this->offset(first);
    Mfirst=first;
    Mlast=last;
    this->calculate_base(offset);
    return(*this);
  }

  /*----------------------------------------------------------------------*/
  
  //! subarray
  Strided& Strided::shrink(const Tdim& i, 
                           const Tsubscript& first, 
                           const Tsubscript& last)
  {
    AFF_assert((i<Mmax_dimen),
               "ERROR (Strided::shrink): illegal dimension");
    AFF_assert(((Mfirst[i]<=first)&&(first<=last)&&(last<=Mlast[i])),
               "ERROR (Strided::shrink): index range erres");
    // take the long way home
    TIndexVec newfirst(Mfirst);
    newfirst[i]=first;
    Tsubscript offset=this->offset(newfirst);
    Mfirst[i]=first;
    Mlast[i]=last;
    this->calculate_base(offset);
    return(*this);
  }

  /*----------------------------------------------------------------------*/
  
  //! subarray
  Strided& Strided::shrink(const Tdim& i, 
                           const Tsubscript& last)
  {
    AFF_assert((i<Mmax_dimen),
               "ERROR (Strided::shrink): illegal dimension");
    AFF_assert(((Mfirst[i]<=last)&&(last<=Mlast[i])),
               "ERROR (Strided::shrink): illegal index value");
    Mlast[i]=last;
    return(*this);
  }

  /*----------------------------------------------------------------------*/
  
  /*! \brief slice
   * \todo
   * This is implementation of slicing is straightforward but rather
   * inefficient. If you do a lot of slicing it might be worth to review and
   * rework the code.
   */
  Strided& Strided::collapse(const Tdim& i, const Tsubscript& index)
  {
    AFF_assert((i<Mmax_dimen),
               "ERROR (Strided::collapse): illegal dimension");
    AFF_assert(((Mfirst[i]<=index)&&(index<=Mlast[i])),
               "ERROR (Strided::collapse): index range error");
    this->shrink(i,index,index); 
    Tsubscript offset=this->first_offset();
    Tdim j=i+1;
    while (j<Mmax_dimen)
    {
      Mfirst[j-1]=Mfirst[j];
      Mlast[j-1]=Mlast[j];
      Mstride[j-1]=Mstride[j];
      j++;
    }
208
209
210
    Mfirst[Mmax_dimen-1]=1;
    Mlast[Mmax_dimen-1]=1;
    Mstride[Mmax_dimen-1]=this->size(Mmax_dimen-2)*Mstride[Mmax_dimen-2];
thomas.forbriger's avatar
thomas.forbriger committed
211
212
213
214
    this->calculate_base(offset);
    return(*this);
  }

thomas.forbriger's avatar
thomas.forbriger committed
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
  /*----------------------------------------------------------------------*/
  
  //! shift
  Strided& Strided::shift(const Tdim& i, const Tsubscript& index)
  {
    AFF_assert((i<Mmax_dimen),
               "ERROR (Strided::shift): illegal dimension");
    Tsubscript offset=this->first_offset();
    Mfirst[i]+=index;
    Mlast[i]+=index;
    this->calculate_base(offset);
    return(*this);
  }

  /*----------------------------------------------------------------------*/
  
  //! shift
  Strided& Strided::shift(const TIndexVec& index)
  {
    Tsubscript offset=this->first_offset();
    for (Tdim i=0; i<Mmax_dimen; i++)
    {
      Mfirst[i]+=index[i];
      Mlast[i]+=index[i];
    }
    this->calculate_base(offset);
    return(*this);
  }

  /*----------------------------------------------------------------------*/
  
  //! setfirst
  Strided& Strided::setfirst(const Tdim& i, const Tsubscript& index)
  {
    AFF_assert((i<Mmax_dimen),
               "ERROR (Strided::setfirst): illegal dimension");
    Tsubscript ishift=index-Mfirst[i];
    return(this->shift(i, ishift));
  }

  /*----------------------------------------------------------------------*/
  
  //! setfirst
  Strided& Strided::setfirst(const TIndexVec& index)
  {
    TIndexVec ishift(index);
    for (Tdim i=0; i<Mmax_dimen; i++)
    {
      ishift[i]-=Mfirst[i];
    }
    return(this->shift(ishift));
  }

thomas.forbriger's avatar
thomas.forbriger committed
268
269
} // namespace aff

270
/* ----- END OF strided.cc ----- */