AFF --- A container for numbers (array) by Friederich and Forbriger.
array.h
Go to the documentation of this file.
1 
132 // include guard
133 #ifndef AFF_ARRAY_H_VERSION
134 
135 #define AFF_ARRAY_H_VERSION \
136  "AFF_ARRAY_H V1.2"
137 
138 #include <aff/lib/sharedheap.h>
139 #include <aff/lib/strided.h>
140 #include <aff/lib/stridedstepper.h>
141 #include <aff/lib/deepcopy.h>
142 #include <aff/lib/error.h>
143 
144 /*======================================================================*/
145 // declaration part
146 // ===============
147 
148 namespace aff {
149 
150  // forward declaration
151  template<class T> class Array;
152 
171  template<class T>
172  class ConstArray:
173  private aff::Strided
174  {
175  public:
201  typedef T Tvalue;
203  typedef T* Tpointer;
205  typedef T& Treference;
207  typedef const T Tconst_value;
209  typedef const T* Tconst_pointer;
211  typedef const T& Tconst_reference;
217  typedef Tcontainer Tcoc;
219 
220  /*-----------------------------------------------------------------*/
221 
233  ConstArray() { }
239  { check_consistency(); }
241 
242  /*-----------------------------------------------------------------*/
243 
253  const T& operator()(const TIndexVec& index) const
255  { return(Mrepresentation[offset(index)]); }
257  const T& operator()(const Tsubscript& i0) const
258  { return(Mrepresentation[offset(i0)]); }
260  const T& operator()(const Tsubscript& i0,
261  const Tsubscript& i1) const
262  { return(Mrepresentation[offset(i0, i1)]); }
264  const T& operator()(const Tsubscript& i0,
265  const Tsubscript& i1,
266  const Tsubscript& i2) const
267  { return(Mrepresentation[offset(i0, i1, i2)]); }
269  const T& operator()(const Tsubscript& i0,
270  const Tsubscript& i1,
271  const Tsubscript& i2,
272  const Tsubscript& i3) const
273  { return(Mrepresentation[offset(i0, i1, i2, i3)]); }
275 
276  /*-----------------------------------------------------------------*/
277 
281  const Tsubscript& f(const Tsubscript& i) const
283  { return(this->Tshape::first(i)); }
285  const Tsubscript& l(const Tsubscript& i) const
286  { return(this->Tshape::last(i)); }
288 
307  Array<T> copyout() const;
308 
310 
311  using Tshape::first;
313  using Tshape::last;
314  using Tshape::size;
316 
318  const Tshape& shape() const { return(*this); }
319 
321  Tshape& shape() { return(*this); }
322 
325  { return (Mrepresentation); }
326 
327  protected:
329  using Tshape::offset;
330 
331  private:
333  void check_consistency() const;
334 
337 
338  }; // class ConstArray
339 
340  /*======================================================================*/
341 
354  template<class T>
355  class Array:
356  public ConstArray<T>
357  {
358  public:
369  typedef Array<T> Tcontainer;
395  typedef Tbase Tcoc;
403  typedef T Tvalue;
405  typedef T* Tpointer;
407  typedef T& Treference;
409  typedef const T Tconst_value;
411  typedef const T* Tconst_pointer;
413  typedef const T& Tconst_reference;
415 
416  /*-----------------------------------------------------------------*/
417 
429  Array() { }
432  Array(const Tshape& shape,
437  explicit Array(const Tshape& shape)
438  {
439  Tshape newshape(shape.first(), shape.last());
440  Mrepresentation=Trepresentation(newshape.memory_size());
441  this->Tbase::operator=(Tbase(newshape, Mrepresentation));
442  }
444  explicit Array(const Tsize& s0, const Tsize& s1=1,
445  const Tsize& s2=1, const Tsize& s3=1)
446  {
447  Tshape newshape(s0, s1, s2, s3);
449  this->Tbase::operator=(Tbase(newshape, Mrepresentation));
450  }
452 
453  /*-----------------------------------------------------------------*/
454 
456 
457  using Tbase::operator();
459  using Tbase::shape;
460  using Tbase::copyout;
462 
486  template<class C>
487  Array& copyin(const C& a)
488  {
489  aff::deepcopy(a, *this);
490  return(*this);
491  }
492 
495  { return (Mrepresentation); }
496 
497  /*-----------------------------------------------------------------*/
498 
500  Tcontainer& operator=(const T& value);
501 
502  /*-----------------------------------------------------------------*/
503 
505  T& operator()(const TIndexVec& index) const
506  { return(Mrepresentation[this->Tbase::offset(index)]); }
508  T& operator()(const Tsubscript& i0) const
509  { return(Mrepresentation[this->Tbase::offset(i0)]); }
511  T& operator()(const Tsubscript& i0,
512  const Tsubscript& i1) const
513  { return(Mrepresentation[this->Tbase::offset(i0, i1)]); }
515  T& operator()(const Tsubscript& i0,
516  const Tsubscript& i1,
517  const Tsubscript& i2) const
518  { return(Mrepresentation[this->Tbase::offset(i0, i1, i2)]); }
520  T& operator()(const Tsubscript& i0,
521  const Tsubscript& i1,
522  const Tsubscript& i2,
523  const Tsubscript& i3) const
524  { return(Mrepresentation[this->Tbase::offset(i0, i1, i2, i3)]); }
525 
526  private:
529 
530  }; // class Array
531 
532 /*----------------------------------------------------------------------*/
533 
534 } // namespace aff
535 
536 /*======================================================================*/
537 // definition part
538 // ===============
539 
540 namespace aff {
541 
542 /*----------------------------------------------------------------------*/
543 
545  template<class T>
547  {
548  AFF_assert((this->Tshape::first_offset()>=0),
549  "ERROR (ConstArray): invalid shape");
550  AFF_assert((this->Tshape::last_offset()<
551  Tsubscript(Mrepresentation.size())),
552  "ERROR (ConstArray): shape and representation are inconsistent");
553  }
554 
555 /*----------------------------------------------------------------------*/
556 
558  template<class T>
560  {
561  aff::Array<T> copy(Tshape(this->first(),this->last()));
562  copy.copyin(*this);
563  return(copy);
564  }
565 
566 /*======================================================================*/
567 
569  template<class T>
570  Array<T>& Array<T>::operator=(const T& value)
571  {
572  Tshape::Tstepper st(this->shape());
573  for(st.tofirst(); st.valid(); st.incr())
574  { Mrepresentation[st.current()]=value; }
575  return(*this);
576  }
577 
578 } // namespace aff
579 
580 #endif // AFF_ARRAY_H_VERSION (includeguard)
581 
582 /* ----- END OF array.h ----- */
const T & Tconst_reference
Type of reference to const element.
Definition: array.h:413
T & operator()(const Tsubscript &i0, const Tsubscript &i1) const
access from 2 index values
Definition: array.h:511
Array & copyin(const C &a)
copy values (deep copy) from other array of convertible type
Definition: array.h:487
const T Tconst_value
const element type
Definition: array.h:409
Root namespace of library.
Definition: array.h:148
Shape for a rectangular array layout.
Definition: strided.h:117
aff::Strided Tshape
Type of shape.
Definition: array.h:195
T & operator()(const Tsubscript &i0, const Tsubscript &i1, const Tsubscript &i2) const
access from 3 index values
Definition: array.h:515
T & operator()(const Tsubscript &i0) const
access from 1 index value
Definition: array.h:508
T & Treference
Type of reference to element.
Definition: array.h:205
Tshape::TIndexVec TIndexVec
we use this for one of the access operators
Definition: array.h:401
const Trepresentation & representation() const
return full access representation
Definition: array.h:494
const Trepresentation & representation() const
provide restricted access representation
Definition: array.h:324
void deepcopy(const S &source, T &target)
deep copy
Definition: deepcopy.h:65
Tshape::Tstepper Tstepper
Type of shape stepper.
Definition: array.h:197
T & operator()(const TIndexVec &index) const
full dimensionality access
Definition: array.h:505
Tshape::TIndexVec TIndexVec
we use this for one of the access operators
Definition: array.h:199
Tcontainer Tcoc
Short for Tcontainer_of_const.
Definition: array.h:217
const T & operator()(const Tsubscript &i0) const
access from 1 index value
Definition: array.h:257
ConstArray(const Tshape &shape, const Trepresentation &representation)
construct from shape and representation
Definition: array.h:236
const TIndexVec & first() const
return vector of first index values
Definition: strided.h:206
const T & Tconst_reference
Type of reference to const element.
Definition: array.h:211
const T & operator()(const TIndexVec &index) const
full dimensionality access
Definition: array.h:254
StridedStepper & incr()
increment offset - return reference to itself
void check_consistency() const
check consitency between representation and shape
Definition: array.h:546
Array(const Tsize &s0, const Tsize &s1=1, const Tsize &s2=1, const Tsize &s3=1)
construct from dimension sizes
Definition: array.h:444
aff::ConstArray< T > Tbase
base is container of const (see specialization below)
Definition: array.h:372
Stepper class for strided shapes (prototypes)
A template class to share heap memory for different array projections.
Definition: sharedheap.h:252
const Tsubscript & first(const Tsubscript &i) const
first index of dimension
Definition: strided.h:194
Array(const Tshape &shape)
construct from shape (defines size and layout)
Definition: array.h:437
const T * Tconst_pointer
Type of pointer to const element.
Definition: array.h:411
exceptions and error handling macros (prototypes)
aff::ConstSharedHeap< T > Trepresentation
Type of representation.
Definition: array.h:193
const T & operator()(const Tsubscript &i0, const Tsubscript &i1, const Tsubscript &i2, const Tsubscript &i3) const
access from 4 index values
Definition: array.h:269
T * Tpointer
Type of pointer to element.
Definition: array.h:203
Tcontainer Tcontainer_of_const
Type of the array of const values.
Definition: array.h:215
T Tvalue
Element type.
Definition: array.h:201
#define AFF_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:162
const T & operator()(const Tsubscript &i0, const Tsubscript &i1, const Tsubscript &i2) const
access from 3 index values
Definition: array.h:264
shape of s strided array (prototypes)
const T Tconst_value
const element type
Definition: array.h:207
ConstArray()
construct from nothing (empty)
Definition: array.h:234
const Tshape & shape() const
provide access to const shape
Definition: array.h:318
ConstArray< T > Tcontainer
Type of this array.
Definition: array.h:213
shared heap representation (prototypes)
Tsize memory_size() const
total size of mapped memory range
Definition: strided.h:183
external deep copy function (prototypes)
This is the base class for const elements.
Definition: sharedheap.h:139
T & Treference
Type of reference to element.
Definition: array.h:407
T * Tpointer
Type of pointer to element.
Definition: array.h:405
Tsize size() const
total number of mapped elements
Definition: strided.cc:140
const T * Tconst_pointer
Type of pointer to const element.
Definition: array.h:209
Full multi-dimensional array functionality.This is the full array class template. It adds no addition...
Definition: array.h:151
Tcontainer & operator=(const T &value)
set whole array to value
Definition: array.h:570
const TIndexVec & last() const
return vector of last index values
Definition: strided.h:208
T & operator()(const Tsubscript &i0, const Tsubscript &i1, const Tsubscript &i2, const Tsubscript &i3) const
access from 4 index values
Definition: array.h:520
Array(const Tshape &shape, const Trepresentation &representation)
construct from shape and representation
Definition: array.h:432
aff::SharedHeap< T > Trepresentation
Type of representation.
Definition: array.h:397
Array()
construct from nothing (empty)
Definition: array.h:430
const Tsubscript & current() const
return current index value for Representation access
Tbase Tcoc
short for Tcontainer_of_const
Definition: array.h:395
Tshape & shape()
allow shape manipulation
Definition: array.h:321
aff::Strided Tshape
Type of subscriptor.
Definition: array.h:399
T Tvalue
Element type.
Definition: array.h:403
StridedStepper & tofirst()
set current element index to the first - return reference to itself
Tbase Tcontainer_of_const
Type of the array of const values.
Definition: array.h:374
A stepper for all strided shapes.
ptrdiff_t Tsubscript
Type to hold an array&#39;s subscript value.
Definition: types.h:53
const Tsubscript & l(const Tsubscript &i) const
return last index of dimension i
Definition: array.h:285
Trepresentation Mrepresentation
representation member
Definition: array.h:336
const T & operator()(const Tsubscript &i0, const Tsubscript &i1) const
access from 2 index values
Definition: array.h:260
const bool & valid() const
valid if not passed end or beginning
Array base classThis is a multidimensional (array) container that uses a strided memory layout (Fortr...
Definition: array.h:172
const Tsubscript & f(const Tsubscript &i) const
return first index of dimension i
Definition: array.h:282
Trepresentation Mrepresentation
my (mutable) data representation
Definition: array.h:528
const Tsubscript & last(const Tsubscript &i) const
last index of dimension
Definition: strided.h:197
Array< T > copyout() const
create an identical copy (deep copy) of this array
Definition: array.h:559
Tsubscript offset(const TIndexVec &index) const
full dimensionality access
Definition: strided.h:220
size_t Tsize
Type to hold the size of an array dimension.
Definition: types.h:51
Array< T > Tcontainer
Type of this array.
Definition: array.h:370