array.h 21.9 KB
Newer Older
1
2
3
4
5
/*! \file array.h
 * \brief full template array class headers (prototypes)
 * 
 * ----------------------------------------------------------------------------
 * 
thomas.forbriger's avatar
thomas.forbriger committed
6
 * $Id$
7
 * \author Thomas Forbriger
8
 * \since 08/12/2002
9
10
11
 * 
 * full template array class headers (prototypes)
 *
thomas.forbriger's avatar
thomas.forbriger committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 * ----
 * 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.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * ----
 *
thomas.forbriger's avatar
thomas.forbriger committed
28
29
 * Include this file to have access to the basic modules in libaff.a.
 *
30
31
 * \sa aff
 * \sa aff::Array
thomas.forbriger's avatar
thomas.forbriger committed
32
33
 * \sa \ref page_using
 * \sa \ref sec_main_modules
34
35
36
 * 
 * Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt) 
 * 
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
 * ============================================================================
 * 
 * REVISIONS and CHANGES from former array_dec.h
 *  - 08/12/2002   V1.0   copied from libcontxx
 *  - 16/12/2002   V1.1   (thof)
 *                        - introduced new concept of const-correctness;
 *                          this should work for passing arrays as intrinsic
 *                          references for const element type
 *  - 17/12/2002   V1.2   (thof)
 *                        - introduced access declarations
 *  - 19/12/2002   V1.3   (thof)
 *                        - provide read access to base classes
 *                          and read/write access to representation of
 *                          non-const array
 *                        - replaced four size-argument constructors by one
 *                          constructor with default arguments
 *                        - distinguish between mutable and non-mutable
 *                          representation
 *  - 20/12/2002   V1.4   (thof)
 *                        - Access declarations are nor useable with functions
 *                          that return a reference to *this. They must be
 *                          reimplemented.
 *  - 23/12/2002   V1.5   (thof)
 *                        - copyin() and copyout() work
 *  - 28/12/2002   V1.6   (thof)
 *                        - changed base class from specialization to
 *                          independent class template
 *  - 29/12/2002   V1.7   (thof)
 *                        - ConstArray now inherits from ConstSharedHeap
 *                          (see "\ref sec_design_replicated")
 *                        - use "using" syntax for access declarations
 *                        - arrays do no longer derive from shared heap
 *                          classes
 *                        - reworked shared heap access concept fundamentally;
 *                          code looks much simpler now 
 *                        - member template must be defined here
 *                        - factored out copyin code
 *  - 31/12/2002   V1.8   (thof)
 *                        - Removed non-initializing constructors in
 *                          ConstArray (except default constructor, which is
 *                          needed, when object is a container element) as
 *                          suggested by Wolfgang.
 *  - 02/01/2003   V1.9   (thof)
 *                        - type TIndexVec was not declared
 *  - 03/01/2003   V1.10  (thof)
 *                        - copyout function now returns an aff::Array rather
 *                          than an aff::ConstArray
 *                        - aff::Array inherits copyout from aff::ConstArray
 *                        - deepcopy is now in namespace aff
 *  - 04/07/2005   V1.11  (thof)
 *                        - provide data modification through const Array
 *  - 05/07/2005   V1.12  (thof)
 *                        - expose const representation too if declared const 
 *  - 19/06/2006   V1.13  (thof)
 *                        - offer Tvalue and friends as non-const types
 *
 * ============================================================================
 * 
 * REVISIONS and CHANGES from former array_def.h
 *  - 08/12/2002   V1.0   copied from libcontxx
 *  - 16/12/2002   V1.1   (thof)
 *                        - definitions are now for base class
 *                          (i.e. spcialization for const T)
 *                        - added standard constructors
 *  - 18/12/2002   V1.2   (thof)
 *                        - added stepper code to scalar assignment operator
 *  - 19/12/2002   V1.3   (thof)
 *                        - size constructors did not work
 *                        - replaced four size-argument constructors by one
 *                          constructor with default arguments
 *                        - distinguish between mutable and non-mutable
 *                          representation
 *  - 23/12/2002   V1.4   (thof)
 *                        - copyin() and copyout() work
 *  - 28/12/2002   V1.5   (thof)
 *                        - changed base class from specialization to
 *                          independent class template
 *  - 29/12/2002   V1.6   (thof)
 *                        - due to clearer concept of representation access
 *                          constructors became more simple and are defined in
 *                          declaration - only copy operations remain here
 *                        - member template cannot be explicitely instantiated
 *  - 03/01/2003   V1.7   (thof)
 *                        - copyout function now returns an aff::Array rather
 *                          than an aff::ConstArray
 *
 * ============================================================================
 * 
125
 * REVISIONS and CHANGES 
126
 *  - 08/12/2002   V1.0   copied from libcontxx
127
 *  - 10/11/2010   V1.1   merged array_def.h and array_dec.h into this file
128
129
130
131
132
 * 
 * ============================================================================
 */

// include guard
thomas.forbriger's avatar
thomas.forbriger committed
133
#ifndef AFF_ARRAY_H_VERSION
134

thomas.forbriger's avatar
thomas.forbriger committed
135
#define AFF_ARRAY_H_VERSION \
136
  "AFF_ARRAY_H   V1.1"
thomas.forbriger's avatar
thomas.forbriger committed
137
#define AFF_ARRAY_H_CVSID \
thomas.forbriger's avatar
thomas.forbriger committed
138
  "$Id$"
139

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <aff/lib/sharedheap.h>
#include <aff/lib/strided.h>
#include <aff/lib/stridedstepper.h>
#include <aff/lib/deepcopy.h>
#include <aff/lib/error.h>

/*======================================================================*/
// declaration part
// ===============

namespace aff {

  // forward declaration
  template<class T> class Array;

  /*! \brief Array base class
156
   * \ingroup group_array
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
   *
   * This is a multidimensional (array) container that uses a strided memory
   * layout (Fortran shape) and counted references to data in global memory.
   * For examples see the various test programs.
   *
   * \sa \ref sec_design_multidimensional
   * \sa \ref sec_design_interface
   * \sa \ref sec_design_const
   * \sa \ref page_representation
   * \sa \ref sec_design_replicated
   * \sa aff::Strided
   * \sa aff::SharedHeap
   * \sa tests/arraytest.cc
   * \sa tests/f77test.cc
   * \sa tests/helpertest.cc
   */
  template<class T>
    class ConstArray:
      private aff::Strided
    {
      public:
        /*! \name Various types
         *
         * In particular due to our concept of const-correctness we need
         * several typedefs to declare types derived from the element type of
         * the array.
         *
         * This is in particular usefull for function templates that may take
         * any container class as argument (e.g. aff::subarray) or for other
         * classes dealing with any container (e.g. aff::Iterator).
         * By means of these typedefs they may retrieve appropriate types they
         * have to deal with.
         *
         * \sa \ref sec_design_interface_typedef
         * \sa \ref sec_design_const
         */
        //@{
        //! Type of representation
        typedef aff::ConstSharedHeap<T> Trepresentation;
        //! Type of shape
        typedef aff::Strided Tshape;
        //! Type of shape stepper
        typedef Tshape::Tstepper Tstepper;
        //! we use this for one of the access operators
        typedef Tshape::TIndexVec TIndexVec;
        //! Element type
        typedef T Tvalue;
        //! Type of pointer to element
        typedef T* Tpointer;
        //! Type of reference to element
        typedef T& Treference;
        //! const element type
        typedef const T Tconst_value;
        //! Type of pointer to const element
        typedef const T* Tconst_pointer;
        //! Type of reference to const element
        typedef const T& Tconst_reference;
        //! Type of this array
        typedef ConstArray<T> Tcontainer;
        //! Type of the array of const values
        typedef Tcontainer Tcontainer_of_const;
        //! Short for Tcontainer_of_const
        typedef Tcontainer Tcoc;
        //@}

        /*-----------------------------------------------------------------*/
        
        /*! \name Constructors
         *
         * \note
         * We use the default copy constructor, which automatically invokes
         * the copy constructors of the base classes aff::Strided and
         * aff::SharedHeap<T>. This essentially is a shallow copy of the
         * array, i.e. the copy will reference to the same elements in memory.
         * See aff::Array<T>::copyin() and aff::Array<T>::copyout() for deep
         * copy operations.
         */
        //@{
        //! construct from nothing (empty)
        ConstArray() { }
        //! construct from shape and representation
        ConstArray(const Tshape& shape, 
                   const Trepresentation& representation):
          Tshape(shape), Mrepresentation(representation)
          { check_consistency(); }
        //@}

        /*-----------------------------------------------------------------*/
      
        /*! \name Const access operators
         *
         * Although we generally distinguish between the constness of the
         * container and the constness of the contained data (see 
         * \ref sec_design_const), we provide const element access operators
         * the prohibit element modification. We assume that a const version
         * of the array is usually meant to be used only for reading.
         */
        //@{
        //! full dimensionality access
        const T& operator()(const TIndexVec& index) const
        { return(Mrepresentation[offset(index)]); }
        //! access from 1 index value
        const T& operator()(const Tsubscript& i0) const
        { return(Mrepresentation[offset(i0)]); }
        //! access from 2 index values
        const T& operator()(const Tsubscript& i0,
                            const Tsubscript& i1) const
        { return(Mrepresentation[offset(i0, i1)]); }
        //! access from 3 index values
        const T& operator()(const Tsubscript& i0,
                            const Tsubscript& i1,
                            const Tsubscript& i2) const
        { return(Mrepresentation[offset(i0, i1, i2)]); }
        //! access from 4 index values
        const T& operator()(const Tsubscript& i0,
                            const Tsubscript& i1,
                            const Tsubscript& i2,
                            const Tsubscript& i3) const
        { return(Mrepresentation[offset(i0, i1, i2, i3)]); }
        //@}

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

        /*! \name Shape access
         */
        //@{
        //! return first index of dimension i
        const Tsubscript& f(const Tsubscript& i) const
        { return(this->Tshape::first(i)); }
        //! return last index of dimension i
        const Tsubscript& l(const Tsubscript& i) const
        { return(this->Tshape::last(i)); }
        //@}

        /*! \brief create an identical copy (deep copy) of this array
         *
         * This is mainly used to create a copy of a truely identical array
         * (i.e. array with same element type or at least const version of
         * same element type). Use this function in conjunction with the
         * assignment operator. E.g.:
         * \code
         *   aff::Array<int> A(3,4);
         *   A=5.;
         *   aff::Array<int> B, C;
         *   B=A.copyout();
         *   C=A;
         * \endcode
         * Here arrays \c A and \c B have exactly the same contents but use
         * different memory. While changes to elements of \c C will also
         * affect elements of \c A, this not the case for changes applied to
         * \c B.
         */
        Array<T> copyout() const;
      
        //! \name access declarations
        //@{
        //! access to base class function
        using Tshape::first;
        using Tshape::last;
        using Tshape::size;
        //@}
     
        //! provide access to const shape
        const Tshape& shape() const { return(*this); }
     
        //! provide restricted access representation
        const Trepresentation& representation() const
        { return (Mrepresentation); }

      protected:
        //! provide access to shape offset functions for derived class
        using Tshape::offset;
          
      private:
        //! check consitency between representation and shape
        void check_consistency() const;
        
        //! representation member
        Trepresentation Mrepresentation;

    }; // class ConstArray

  /*======================================================================*/

  /*! \brief Full multi-dimensional array functionality.
342
   * \ingroup group_array
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
   *
   * This is the full array class template. It adds no additional
   * functionality to its base class aff::ConstArray. But it provied acess to
   * all member functions of the base, also to functions that allow data
   * modification.
   *
   * \sa aff::ConstArray
   * \sa \ref sec_design_const
   * \sa \ref sec_design_replicated
   */
  template<class T>
    class Array:
      public ConstArray<T>
    {
      public:
        /*! \name Various types
         *
         * In particular due to our concept of const-correctness we need
         * several typedefs to declare types derived from the element type of
         * the array.
         *
         * \sa \ref sec_design_interface_typedef
         * \sa \ref sec_design_const
         */
        //@{
        //! Type of this array
        typedef Array<T> Tcontainer;
        //! base is container of const (see specialization below)
        typedef aff::ConstArray<T> Tbase;
        //! Type of the array of const values
        typedef Tbase Tcontainer_of_const;
        /*! \brief short for Tcontainer_of_const
         *
         * We generally distinguish between constness of the array and
         * constness of the contained data (see \ref sec_design_const).
         * There will be situations, when you want to promise that a function
         * will not change the contents of an array. In this case you may use
         * a declaration (prototype) like
         * \code
         *   typedef aff::Array<int> Tmyarray;
         *   void myfunction(const Tmyarray::Tcoc& array);
         * \endcode
         * and you may use this function like
         * \code
         *   Tmyarray A(6,7);
         *   A=4;
         *   myfunction(A);
         * \endcode
         *
         * \sa \ref sec_design_const
         */
        typedef Tbase Tcoc;
        //! Type of representation
        typedef aff::SharedHeap<T> Trepresentation;
        //! Type of subscriptor
        typedef aff::Strided Tshape;
        //! we use this for one of the access operators
        typedef Tshape::TIndexVec TIndexVec;
        //! Element type
        typedef T Tvalue;
        //! Type of pointer to element
        typedef T* Tpointer;
        //! Type of reference to element
        typedef T& Treference;
        //! const element type
        typedef const T Tconst_value;
        //! Type of pointer to const element
        typedef const T* Tconst_pointer;
        //! Type of reference to const element
        typedef const T& Tconst_reference;
        //@}

        /*-----------------------------------------------------------------*/
        
        /*! \name Constructors
         *
         * \note
         * We use the default copy constructor, which automatically invokes
         * the copy constructors of the base classes aff::Strided and
         * aff::SharedHeap<T>. This essentially is a shallow copy of the
         * array, i.e. the copy will reference to the same elements in memory.
         * See aff::Array<T>::copyin() and aff::Array<T>::copyout() for deep
         * copy operations.
         */
        //@{
        //! construct from nothing (empty)
        Array() { }
        //! construct from shape and representation
        Array(const Tshape& shape, 
              const Trepresentation& representation):
          Tbase(shape, representation),
          Mrepresentation(representation) { }
        //! construct from shape (defines size and layout)
        explicit Array(const Tshape& shape)
          { 
            Tshape newshape(shape.first(), shape.last());
            Mrepresentation=Trepresentation(newshape.memory_size());
            this->Tbase::operator=(Tbase(newshape, Mrepresentation));
          }
        //! construct from dimension sizes
        explicit Array(const Tsize& s0, const Tsize& s1=1, 
                       const Tsize& s2=1, const Tsize& s3=1)
          { 
            Tshape newshape(s0, s1, s2, s3);
            Mrepresentation=Trepresentation(newshape.memory_size());
            this->Tbase::operator=(Tbase(newshape, Mrepresentation));
          }
        //@}

        /*-----------------------------------------------------------------*/
      
        //! \name access declarations
        //@{
        //! access to base class function
        using Tbase::operator();
        using Tbase::shape;
        using Tbase::copyout;
        //@}

        /*! \brief copy values (deep copy) from other array of convertible type
         *
         * This member function reads the element values of another array of
         * same shape and applies them to this array. In fact the shape needs
         * not be the same. The copy is done through sequential access and as
         * most number as possible will be copied in increasing memory address
         * order.
         *
         * Example:
         * \code
         *   aff::Array<float> A(24);
         *   A=15.
         *   aff::Array<double> B(3,8);
         *   B.copyin(A);
         * \endcode
         * \c B will preserve its shape but is filled with the contents of 
         * \c A (which are not where interesting in this example). Changes
         * applied to the contents of B will not affect the contents of A.
         *
         * \param a other container with element type convertible to element
         *        type of this array and appropriate stepper class
         * \return itself
         */
        template<class C>
          Array& copyin(const C& a)
          {
            aff::deepcopy(a, *this);
            return(*this);
          }
     
        //! return full access representation
        const Trepresentation& representation() const
        { return (Mrepresentation); }

        /*-----------------------------------------------------------------*/
          
        //! set whole array to value
        Tcontainer& operator=(const T& value);

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

        //! full dimensionality access
        T& operator()(const TIndexVec& index) const
        { return(Mrepresentation[this->Tbase::offset(index)]); }
        //! access from 1 index value
        T& operator()(const Tsubscript& i0) const
        { return(Mrepresentation[this->Tbase::offset(i0)]); }
        //! access from 2 index values
        T& operator()(const Tsubscript& i0,
                      const Tsubscript& i1) const
        { return(Mrepresentation[this->Tbase::offset(i0, i1)]); }
        //! access from 3 index values
        T& operator()(const Tsubscript& i0,
                      const Tsubscript& i1,
                      const Tsubscript& i2) const
        { return(Mrepresentation[this->Tbase::offset(i0, i1, i2)]); }
        //! access from 4 index values
        T& operator()(const Tsubscript& i0,
                      const Tsubscript& i1,
                      const Tsubscript& i2,
                      const Tsubscript& i3) const
        { return(Mrepresentation[this->Tbase::offset(i0, i1, i2, i3)]); }

      private:
        //! my (mutable) data representation
        Trepresentation Mrepresentation;

    }; // class Array

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

} // namespace aff

/*======================================================================*/
// definition part
// ===============

namespace aff {

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

  //! check shape and representation
  template<class T>
    void ConstArray<T>::check_consistency() const
    {
      AFF_assert((this->Tshape::first_offset()>=0),
                 "ERROR (ConstArray): invalid shape");
      AFF_assert((this->Tshape::last_offset()<
                  Tsubscript(Mrepresentation.size())),
              "ERROR (ConstArray): shape and representation are inconsistent");
    }

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

  //! create a value (deep) copy
  template<class T>
    Array<T> ConstArray<T>::copyout() const
    {
      aff::Array<T> copy(Tshape(this->first(),this->last()));
      copy.copyin(*this);
      return(copy);
    }

/*======================================================================*/

  //! set whole array to scalar value
  template<class T>
    Array<T>& Array<T>::operator=(const T& value)
    {
      Tshape::Tstepper st(this->shape());
      for(st.tofirst(); st.valid(); st.incr())
      { Mrepresentation[st.current()]=value; } 
      return(*this);
    }
576

577
} // namespace aff
578

thomas.forbriger's avatar
thomas.forbriger committed
579
#endif // AFF_ARRAY_H_VERSION (includeguard)
580
581

/* ----- END OF array.h ----- */