AFF --- A container for numbers (array) by Friederich and Forbriger.
strided.h
Go to the documentation of this file.
1 
67 // include guard
68 #ifndef AFF_STRIDED_H_VERSION
69 
70 #define AFF_STRIDED_H_VERSION \
71  "AFF_STRIDED_H V1.8"
72 
73 #include<aff/lib/types.h>
74 #include<aff/lib/simplearray.h>
75 
76 namespace aff {
77 
78  namespace util {
80  class StridedStepper;
81  } // namespace util
82 
117  class Strided {
118  public:
123  static const Tdim Mmax_dimen=4;
124 
131 
132  /*-----------------------------------------------------------------*/
133 
137  Strided(): Mstride(0), Mfirst(0), Mlast(0), Mbase(0) { }
146  explicit Strided(const TSizeVec& sizes, const Tsubscript& first=1,
147  const Tsubscript& shift=0)
148  { setup_from_size(sizes, first, shift); }
161  explicit Strided(const TIndexVec& first, const TIndexVec& last,
162  const Tsubscript& shift=0);
172  explicit Strided(const Tsize& s0, const Tsize& s1=1,
173  const Tsize& s2=1, const Tsize& s3=1);
175 
176  /*-----------------------------------------------------------------*/
177 
182  Tsize memory_size() const
184  { return (offset(Mlast)-offset(Mfirst)+1); }
186  Tsize size() const;
189  { return offset(Mfirst[0]); }
192  { return offset(Mlast); }
194  const Tsubscript& first(const Tsubscript& i) const
195  { return Mfirst[i]; }
197  const Tsubscript& last(const Tsubscript& i) const
198  { return (Mlast[i]); }
200  Tsize size(const Tsubscript& i) const
201  { return (static_cast<Tsize>(Mlast[i]-Mfirst[i]+1)); }
203  const Tsize& stride(const Tsubscript& i) const
204  { return Mstride[i]; }
206  const TIndexVec& first() const { return(Mfirst); }
208  const TIndexVec& last() const { return(Mlast); }
210  const TSizeVec& stride() const { return(Mstride); }
212 
213  /*-----------------------------------------------------------------*/
214 
219  Tsubscript offset(const TIndexVec& index) const
221  { return(inline_innerproduct(index,Mstride)+Mbase[Mmax_dimen-1]); }
223  Tsubscript offset(const Tsubscript& i0) const
224  { return(i0*Mstride[0]+Mbase[0]); }
227  const Tsubscript& i1) const
228  { return(i0*Mstride[0]+i1*Mstride[1]+Mbase[1]); }
231  const Tsubscript& i1,
232  const Tsubscript& i2) const
233  { return(i0*Mstride[0]+i1*Mstride[1]+i2*Mstride[2]+Mbase[2]); }
236  const Tsubscript& i1,
237  const Tsubscript& i2,
238  const Tsubscript& i3) const
239  { return(i0*Mstride[0]+i1*Mstride[1]+i2*Mstride[2]+
240  i3*Mstride[3]+Mbase[3]); }
242 
243  /*-----------------------------------------------------------------*/
244 
256  Strided& shrink(const TIndexVec& last);
259  Strided& shrink(const TIndexVec& first, const TIndexVec& last);
261  Strided& shrink(const Tdim& i,
262  const Tsubscript& first, const Tsubscript& last);
264  Strided& shrink(const Tdim& i,
265  const Tsubscript& last);
267  Strided& collapse(const Tdim& i, const Tsubscript& index);
269  Strided& shift(const Tdim& i, const Tsubscript& index);
271  Strided& shift(const TIndexVec& index);
273  Strided& setfirst(const Tdim& i, const Tsubscript& index);
275  Strided& setfirst(const TIndexVec& index);
277 
278  /*-----------------------------------------------------------------*/
279 
280  private:
281  void calculate_base(const Tsubscript& offset);
282  void setup_from_size(const TSizeVec& size,
283  const Tsubscript& first=1,
284  const Tsubscript& shift=0);
285 
286  TSizeVec Mstride; //<! strides for each dimension
287  TIndexVec Mfirst; //<! first index of each dimension
288  TIndexVec Mlast; //<! last index of each dimension
289  TIndexVec Mbase; //<! base for each dimension (see index operators)
290  };
291 
292  /*======================================================================*/
293  // helper functions
294 
295  namespace util {
296 
304  bool is_dense_1D_array(const aff::Strided& shape);
305 
306  } // namespace util
307 
308 } // namespace aff
309 
310 #endif // AFF_STRIDED_H_VERSION (includeguard)
311 
312 /* ----- END OF strided.h ----- */
TIndexVec Mlast
Definition: strided.h:288
Root namespace of library.
Definition: array.h:148
Shape for a rectangular array layout.
Definition: strided.h:117
some typedefs we refer to
aff::util::StridedStepper Tstepper
type of stepper
Definition: strided.h:130
Strided(const TSizeVec &sizes, const Tsubscript &first=1, const Tsubscript &shift=0)
construct do given size and first index
Definition: strided.h:146
Tsubscript offset(const Tsubscript &i0, const Tsubscript &i1, const Tsubscript &i2, const Tsubscript &i3) const
offset from 4 index values
Definition: strided.h:235
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
Tsize size(const Tsubscript &i) const
size of dimension
Definition: strided.h:200
const TIndexVec & first() const
return vector of first index values
Definition: strided.h:206
TIndexVec Mbase
Definition: strided.h:289
Tsubscript last_offset() const
last mapped position
Definition: strided.h:191
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
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
Tsubscript offset(const Tsubscript &i0, const Tsubscript &i1) const
offset from 2 index values
Definition: strided.h:226
Strided & collapse(const Tdim &i, const Tsubscript &index)
slice
Definition: strided.cc:224
const Tsubscript & first(const Tsubscript &i) const
first index of dimension
Definition: strided.h:194
const Tsize & stride(const Tsubscript &i) const
stride of dimension
Definition: strided.h:203
static const Tdim Mmax_dimen
instantiate static member (otherwise the linker won&#39;t find it)
Definition: strided.h:123
SimpleRigidArray< Tsize, Mmax_dimen > TSizeVec
type of size and stride array (always positive)
Definition: strided.h:126
Tsubscript offset(const Tsubscript &i0, const Tsubscript &i1, const Tsubscript &i2) const
offset from 3 index values
Definition: strided.h:230
Strided()
construct and initialize to zero
Definition: strided.h:138
Tsize memory_size() const
total size of mapped memory range
Definition: strided.h:183
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
a simple rigid array
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
T inline_innerproduct(const SimpleRigidArray< T, N > &A, const SimpleRigidArray< T, N > &B)
Returns inner product.
Definition: simplearray.h:206
A stepper for all strided shapes.
ptrdiff_t Tsubscript
Type to hold an array&#39;s subscript value.
Definition: types.h:53
Tsubscript offset(const Tsubscript &i0) const
offset from 1 index value
Definition: strided.h:223
SimpleRigidArray< Tsubscript, Mmax_dimen > TIndexVec
type of limit arrays (any sign)
Definition: strided.h:128
const Tsubscript & last(const Tsubscript &i) const
last index of dimension
Definition: strided.h:197
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
const TSizeVec & stride() const
return vector of stride values
Definition: strided.h:210