AFF --- A container for numbers (array) by Friederich and Forbriger.
strided.cc
Go to the documentation of this file.
1 
59 #define AFF_STRIDED_CC_VERSION \
60  "AFF_STRIDED_DEF_H V1.8"
61 
62 #include<aff/lib/strided.h>
63 #include<aff/lib/error.h>
64 
65 namespace aff {
66 
67  /*----------------------------------------------------------------------*/
68 
71 
72  /*----------------------------------------------------------------------*/
73 
76  const Tsubscript& first,
77  const Tsubscript& shift)
78  {
79  TSizeVec one(1);
81  "ERROR (Strided::setup_from_size): size must be at least one");
82  Mfirst=first;
83  Mstride[0]=1;
84  Mlast[0]=Mfirst[0]+size[0]-1;
85  for(Tdim i=1; i<Mmax_dimen; i++)
86  {
87  Mstride[i]=Mstride[i-1]*size[i-1];
88  Mlast[i]=Mfirst[i]+size[i]-1;
89  }
91  }
92 
93  /*----------------------------------------------------------------------*/
94 
96  Strided::Strided(const Tsize& s0, const Tsize& s1, const Tsize& s2,
97  const Tsize& s3)
98  {
99  TSizeVec size;
100  size[0]=s0;
101  size[1]=s1;
102  size[2]=s2;
103  size[3]=s3;
104  setup_from_size(size, 1, 0);
105  }
106 
107  /*----------------------------------------------------------------------*/
108 
110  Strided::Strided(const TIndexVec& first, const TIndexVec& last,
111  const Tsubscript& shift)
112  {
114  "ERROR (Strided): first index must be smaller than last");
115  Mfirst=first;
116  Mlast=last;
117  Mstride[0]=1;
118  for(Tdim i=1; i<Mmax_dimen; i++)
119  {
120  Mstride[i]=Mstride[i-1]*(1+Mlast[i-1]-Mfirst[i-1]);
121  }
123  }
124 
125  /*----------------------------------------------------------------------*/
126 
128  void Strided::calculate_base(const Tsubscript& offset)
129  {
130  Mbase[0]=offset-Mfirst[0]*Mstride[0];
131  for (Tdim i=1; i<Mmax_dimen; i++)
132  {
133  Mbase[i]=Mbase[i-1]-Mfirst[i]*Mstride[i];
134  }
135  }
136 
137  /*----------------------------------------------------------------------*/
138 
141  {
142  // could be done more elegant by inlining - but this will do it here
144  for (int i=0; i<Mmax_dimen; i++) { size[i]-=(Mfirst[i]-1); }
145  return (inline_product(size));
146  }
147 
148  /*----------------------------------------------------------------------*/
149 
152  {
154  "ERROR (Strided::shrink): index must not be smaller than first");
156  "ERROR (Strided::shrink): index must not be larger than last");
157  Mlast=last;
158  return(*this);
159  }
160 
161  /*----------------------------------------------------------------------*/
162 
164  Strided& Strided::shrink(const TIndexVec& first, const TIndexVec& last)
165  {
168  "ERROR (Strided::shrink): illegal last index vector");
171  "ERROR (Strided::shrink): illegal first index vector");
173  "ERROR (Strided::shrink): last must not be smaller than first");
174  Tsubscript offset=this->offset(first);
175  Mfirst=first;
176  Mlast=last;
177  this->calculate_base(offset);
178  return(*this);
179  }
180 
181  /*----------------------------------------------------------------------*/
182 
185  const Tsubscript& first,
186  const Tsubscript& last)
187  {
189  "ERROR (Strided::shrink): illegal dimension");
190  AFF_assert(((Mfirst[i]<=first)&&(first<=last)&&(last<=Mlast[i])),
191  "ERROR (Strided::shrink): index range error");
192  // take the long way home
193  TIndexVec newfirst(Mfirst);
194  newfirst[i]=first;
195  Tsubscript offset=this->offset(newfirst);
196  Mfirst[i]=first;
197  Mlast[i]=last;
198  this->calculate_base(offset);
199  return(*this);
200  }
201 
202  /*----------------------------------------------------------------------*/
203 
206  const Tsubscript& last)
207  {
209  "ERROR (Strided::shrink): illegal dimension");
210  AFF_assert(((Mfirst[i]<=last)&&(last<=Mlast[i])),
211  "ERROR (Strided::shrink): illegal index value");
212  Mlast[i]=last;
213  return(*this);
214  }
215 
216  /*----------------------------------------------------------------------*/
217 
224  Strided& Strided::collapse(const Tdim& i, const Tsubscript& index)
225  {
227  "ERROR (Strided::collapse): illegal dimension");
228  AFF_assert(((Mfirst[i]<=index)&&(index<=Mlast[i])),
229  "ERROR (Strided::collapse): index range error");
230  this->shrink(i,index,index);
232  Tdim j=i+1;
233  while (j<Mmax_dimen)
234  {
235  Mfirst[j-1]=Mfirst[j];
236  Mlast[j-1]=Mlast[j];
237  Mstride[j-1]=Mstride[j];
238  j++;
239  }
240  Mfirst[Mmax_dimen-1]=1;
241  Mlast[Mmax_dimen-1]=1;
243  this->calculate_base(offset);
244  return(*this);
245  }
246 
247  /*----------------------------------------------------------------------*/
248 
250  Strided& Strided::shift(const Tdim& i, const Tsubscript& index)
251  {
253  "ERROR (Strided::shift): illegal dimension");
255  Mfirst[i]+=index;
256  Mlast[i]+=index;
257  this->calculate_base(offset);
258  return(*this);
259  }
260 
261  /*----------------------------------------------------------------------*/
262 
265  {
267  for (Tdim i=0; i<Mmax_dimen; i++)
268  {
269  Mfirst[i]+=index[i];
270  Mlast[i]+=index[i];
271  }
272  this->calculate_base(offset);
273  return(*this);
274  }
275 
276  /*----------------------------------------------------------------------*/
277 
279  Strided& Strided::setfirst(const Tdim& i, const Tsubscript& index)
280  {
282  "ERROR (Strided::setfirst): illegal dimension");
283  Tsubscript ishift=index-Mfirst[i];
284  return(this->shift(i, ishift));
285  }
286 
287  /*----------------------------------------------------------------------*/
288 
291  {
292  TIndexVec ishift(index);
293  for (Tdim i=0; i<Mmax_dimen; i++)
294  {
295  ishift[i]-=Mfirst[i];
296  }
297  return(this->shift(ishift));
298  }
299 
300  /*======================================================================*/
301 
303  {
304  bool retval
305  =((shape.size(1)==1)
306  && (shape.size(2)==1)
307  && (shape.size(3)==1)
308  && (shape.stride(0)==1));
309  return retval;
310  } // bool is_dense_1D_array(const aff::Strided& shape)
311 
312 } // namespace aff
313 
314 /* ----- END OF strided.cc ----- */
TIndexVec Mlast
Definition: strided.h:288
Root namespace of library.
Definition: array.h:148
Shape for a rectangular array layout.
Definition: strided.h:117
T inline_product(const SimpleRigidArray< T, N > &array)
Product of all elements.
Definition: simplearray.h:178
bool is_dense_1D_array(const aff::Strided &shape)
check whether array shape describes a 1D array with dense layout in the memory.
Definition: strided.cc:302
const TIndexVec & first() const
return vector of first index values
Definition: strided.h:206
TIndexVec Mbase
Definition: strided.h:289
void setup_from_size(const TSizeVec &size, const Tsubscript &first=1, const Tsubscript &shift=0)
setup to given size and first index
Definition: strided.cc:75
bool inline_anysmaller(const SimpleRigidArray< T, N > &A, const SimpleRigidArray< T, N > &B)
Returns true if any of A is smaller than corresponding B.
Definition: simplearray.h:188
Strided & setfirst(const Tdim &i, const Tsubscript &index)
setfirst
Definition: strided.cc:279
Strided & shift(const Tdim &i, const Tsubscript &index)
shift
Definition: strided.cc:250
bool inline_anylarger(const SimpleRigidArray< T, N > &A, const SimpleRigidArray< T, N > &B)
Returns true if any of A is larger than corresponding B.
Definition: simplearray.h:194
Strided & collapse(const Tdim &i, const Tsubscript &index)
slice
Definition: strided.cc:224
const Tsize & stride(const Tsubscript &i) const
stride of dimension
Definition: strided.h:203
exceptions and error handling macros (prototypes)
static const Tdim Mmax_dimen
instantiate static member (otherwise the linker won&#39;t find it)
Definition: strided.h:123
#define AFF_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:162
shape of s strided array (prototypes)
Strided()
construct and initialize to zero
Definition: strided.h:138
void calculate_base(const Tsubscript &offset)
construct do given first and last index
Definition: strided.cc:128
Tsize size() const
total number of mapped elements
Definition: strided.cc:140
TSizeVec Mstride
Definition: strided.h:286
const TIndexVec & last() const
return vector of last index values
Definition: strided.h:208
Tsubscript first_offset() const
first mapped position
Definition: strided.h:188
unsigned short int Tdim
Type to hold an array dimensionality.
Definition: types.h:49
ptrdiff_t Tsubscript
Type to hold an array&#39;s subscript value.
Definition: types.h:53
TIndexVec Mfirst
Definition: strided.h:287
Tsubscript offset(const TIndexVec &index) const
full dimensionality access
Definition: strided.h:220
Strided & shrink(const TIndexVec &last)
subarray
Definition: strided.cc:151
size_t Tsize
Type to hold the size of an array dimension.
Definition: types.h:51