1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
2 | // Base array class - Memory organisation management
|
---|
3 | // R. Ansari, C.Magneville 03/2000
|
---|
4 |
|
---|
5 | #ifndef BaseArray_SEEN
|
---|
6 | #define BaseArray_SEEN
|
---|
7 |
|
---|
8 | #include "machdefs.h"
|
---|
9 | #include <math.h>
|
---|
10 | #include <iostream.h>
|
---|
11 | #include "anydataobj.h"
|
---|
12 | #include "dvlist.h"
|
---|
13 |
|
---|
14 |
|
---|
15 | // Maximum number of dimensions for array
|
---|
16 | #define BASEARRAY_MAXNDIMS 5
|
---|
17 |
|
---|
18 | namespace SOPHYA {
|
---|
19 |
|
---|
20 | // ------------ classe template Array -----------
|
---|
21 | class BaseArray : public AnyDataObj {
|
---|
22 | public:
|
---|
23 | // To define Array / Matrix memory mapping
|
---|
24 | enum { AutoMemoryMapping = -1, SameMemoryMapping = 0,
|
---|
25 | CMemoryMapping = 1, FortranMemoryMapping = 2 };
|
---|
26 |
|
---|
27 | // Max Nb of printed elements
|
---|
28 | static void SetMaxPrint(uint_4 nprt=50);
|
---|
29 | // Memory organisation (for matrices)
|
---|
30 | static short SetDefaultMemoryMapping(short mm=CMemoryMapping);
|
---|
31 | static inline short GetDefaultMemoryMapping() { return default_memory_mapping; }
|
---|
32 |
|
---|
33 | // Creation / destruction
|
---|
34 | BaseArray();
|
---|
35 | virtual ~BaseArray();
|
---|
36 |
|
---|
37 | // Returns true if ndim and sizes are equal
|
---|
38 | virtual bool CompareSizes(const BaseArray& a);
|
---|
39 |
|
---|
40 | // Compacts size=1 array dimensions
|
---|
41 | virtual void CompactAllDim(); // suppresses all size==1 dimensions
|
---|
42 | virtual void CompactTrailingDim(); // suppresses size==1 dimensions after the last size>1 dimension
|
---|
43 |
|
---|
44 | // Array dimensions
|
---|
45 | inline uint_4 NbDimensions() const { return( ndim_ ); }
|
---|
46 |
|
---|
47 | inline uint_8 Size() const { return(totsize_); }
|
---|
48 | inline uint_4 SizeX() const { return(size_[0]); }
|
---|
49 | inline uint_4 SizeY() const { return(size_[1]); }
|
---|
50 | inline uint_4 SizeZ() const { return(size_[2]); }
|
---|
51 | inline uint_4 Size(int ka) const { return(size_[CheckDI(ka,1)]); }
|
---|
52 |
|
---|
53 | uint_4 MaxSizeKA() const ;
|
---|
54 |
|
---|
55 | // memory organisation - packing information
|
---|
56 | inline short GetMemoryMapping() const
|
---|
57 | {return ( (marowi_ == 1) ? CMemoryMapping : FortranMemoryMapping) ; }
|
---|
58 |
|
---|
59 | inline uint_4 RowsKA() const {return marowi_; } // Dimension correspondant aux lignes
|
---|
60 | inline uint_4 ColsKA() const {return macoli_; } // Dimension correspondant aux colonnes
|
---|
61 | inline uint_4 VectKA() const {return veceli_; } // Dimension corr. aux elts d'un vecteur
|
---|
62 |
|
---|
63 | inline bool IsPacked() const { return(moystep_ == 1); }
|
---|
64 | inline bool IsPackedX() const { return(step_[0] == 1); }
|
---|
65 | inline bool IsPackedY() const { return(step_[1] == 1); }
|
---|
66 | inline bool IsPackedZ() const { return(step_[2] == 1); }
|
---|
67 | inline bool IsPacked(int ka) const { return(step_[CheckDI(ka,2)] == 1); }
|
---|
68 |
|
---|
69 | inline uint_4 MinStep() const { return(minstep_); }
|
---|
70 | inline uint_4 AvgStep() const { return(moystep_); }
|
---|
71 | inline uint_4 StepX() const { return(step_[0]); }
|
---|
72 | inline uint_4 StepY() const { return(step_[1]); }
|
---|
73 | inline uint_4 StepZ() const { return(step_[2]); }
|
---|
74 | inline uint_4 Step(int ka) const { return(step_[CheckDI(ka,3)]); }
|
---|
75 |
|
---|
76 | uint_4 MinStepKA() const ;
|
---|
77 |
|
---|
78 | uint_8 Offset(uint_8 ip=0) const ;
|
---|
79 | inline uint_8 Offset(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it=0, uint_4 iu=0) const;
|
---|
80 |
|
---|
81 | // a abstract element acces methode
|
---|
82 | virtual double ValueAtPosition(uint_8 ip) const = 0;
|
---|
83 |
|
---|
84 | // Impression, I/O, ...
|
---|
85 | void Show(ostream& os, bool si=false) const;
|
---|
86 | inline void Show() const { Show(cout); }
|
---|
87 | virtual string DataType() const = 0;
|
---|
88 |
|
---|
89 | // Objet DVList info
|
---|
90 | DVList& Info();
|
---|
91 |
|
---|
92 | protected:
|
---|
93 | // Verifie la compatibilite de l'index de dimension
|
---|
94 | inline int CheckDI(int ka, int msg) const ;
|
---|
95 | // Verifie la compatibilite des bornes d'index
|
---|
96 | inline void CheckBound(int ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu, int msg) const ;
|
---|
97 | // Changing Sizes/NDim ... return true if OK
|
---|
98 | bool UpdateSizes(uint_4 ndim, const uint_4 * siz, uint_4 step, uint_8 offset, string & exmsg);
|
---|
99 | bool UpdateSizes(uint_4 ndim, const uint_4 * siz, const uint_4 * step, uint_8 offset, string & exmsg);
|
---|
100 | bool UpdateSizes(const BaseArray& a, string & exmsg);
|
---|
101 | static uint_8 ComputeTotalSize(uint_4 ndim, const uint_4 * siz, uint_4 step, uint_8 offset) ;
|
---|
102 | // Organisation memoire
|
---|
103 | static short SelectMemoryMapping(short mm);
|
---|
104 | void UpdateMemoryMapping(short mm, uint_4 & nx, uint_4 & ny);
|
---|
105 | void UpdateMemoryMapping(BaseArray const & a, short mm);
|
---|
106 |
|
---|
107 | // Pour Extraction de sous-tableau
|
---|
108 | virtual void UpdateSubArraySizes(BaseArray & ra, uint_4 ndim, uint_4 * siz, uint_4 * pos, uint_4 * step) const;
|
---|
109 |
|
---|
110 | uint_4 ndim_; // nb of dimensions
|
---|
111 | uint_4 size_[BASEARRAY_MAXNDIMS]; // array size in each dimension
|
---|
112 | uint_8 totsize_; // Total number of elements
|
---|
113 | uint_4 step_[BASEARRAY_MAXNDIMS]; // two consecutive elements distance in a given dimension
|
---|
114 | uint_4 minstep_; // minimal step (in any axes)
|
---|
115 | uint_4 moystep_; // mean step 0 non regular steps
|
---|
116 | uint_8 offset_; // global offset -> position of elem[0] in DataBlock
|
---|
117 | uint_4 marowi_, macoli_; // For matrices, Row index and column index in dimensions
|
---|
118 | uint_4 veceli_; // For vectors, dimension index = marowi_/macoli_ (Row/Col vectors)
|
---|
119 |
|
---|
120 | DVList* mInfo; // Infos (variables) attachees au tableau
|
---|
121 |
|
---|
122 | static char * ck_op_msg_[6]; // Operation messages for CheckDI() CheckBound()
|
---|
123 | static uint_4 max_nprt_; // Nb maxi d'elements imprimes
|
---|
124 | static short default_memory_mapping; // Default memory mapping
|
---|
125 | };
|
---|
126 |
|
---|
127 | // --------------------------------------------------
|
---|
128 | // Methodes inline de verification
|
---|
129 | // --------------------------------------------------
|
---|
130 | inline int BaseArray::CheckDI(int ka, int msg) const
|
---|
131 | {
|
---|
132 | if ( (ka < 0) || (ka >= ndim_) ) {
|
---|
133 | string txt = "BaseArray::CheckDimensionIndex/Error "; txt += ck_op_msg_[msg];
|
---|
134 | throw(ParmError(txt));
|
---|
135 | }
|
---|
136 | return(ka);
|
---|
137 | }
|
---|
138 |
|
---|
139 | inline void BaseArray::CheckBound(int ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu, int msg) const
|
---|
140 | {
|
---|
141 | if ( (ix >= size_[0]) || (iy >= size_[1]) || (iz > size_[2]) ||
|
---|
142 | (it >= size_[3]) || (iu >= size_[4]) ) {
|
---|
143 | string txt = "BaseArray::CheckArrayBound/Error "; txt += ck_op_msg_[msg];
|
---|
144 | throw(ParmError(txt));
|
---|
145 | }
|
---|
146 | return;
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | // --------------------------------------------------
|
---|
151 | // Position d'un element
|
---|
152 | // --------------------------------------------------
|
---|
153 | inline uint_8 BaseArray::Offset(uint_4 ix, uint_4 iy, uint_4 iz, uint_4 it, uint_4 iu) const
|
---|
154 | {
|
---|
155 | #ifdef SO_BOUNDCHECKING
|
---|
156 | CheckBound(ix, iy, iz, it, iu, 4);
|
---|
157 | #endif
|
---|
158 | return ( offset_+ ix*step_[0] + iy*step_[1] + iz*step_[2] +
|
---|
159 | it*step_[3] + iu*step_[4] );
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | } // Fin du namespace
|
---|
164 |
|
---|
165 | #endif
|
---|