00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00027 #ifndef array_h
00028 #define array_h
00029
00030 #include <vector>
00031 #include <iostream>
00032 #include <iomanip>
00033 #include <cassert>
00034 #include <climits>
00035 #include "matpack.h"
00036
00037
00038
00039 template<class base>
00040 class Array;
00041
00043 typedef Array<Index> ArrayOfIndex;
00044
00045 typedef Array<ArrayOfIndex> ArrayOfArrayOfIndex;
00046
00048 typedef Array<Numeric> ArrayOfNumeric;
00049
00050
00051 class Vector;
00052 class Matrix;
00053 class Tensor3;
00054 class Tensor4;
00055 class Tensor5;
00056 class Tensor6;
00057 class Tensor7;
00058
00060 typedef Array<Vector> ArrayOfVector;
00061
00063 typedef Array<Matrix> ArrayOfMatrix;
00064
00065 typedef Array<ArrayOfMatrix> ArrayOfArrayOfMatrix;
00066
00068 typedef Array<Tensor3> ArrayOfTensor3;
00069
00070 typedef Array<ArrayOfTensor3> ArrayOfArrayOfTensor3;
00071
00073 typedef Array<Tensor4> ArrayOfTensor4;
00074
00076 typedef Array<Tensor5> ArrayOfTensor5;
00077
00079 typedef Array<Tensor6> ArrayOfTensor6;
00080
00081 typedef Array<ArrayOfTensor6> ArrayOfArrayOfTensor6;
00082
00084 typedef Array<Tensor7> ArrayOfTensor7;
00085
00086
00097 template<class base>
00098 class Array : public vector<base>
00099 {
00100 public:
00101
00102 Array() : vector<base>() { }
00103 explicit Array(Index n) : vector<base>(n) { }
00104 Array(Index n, const base& fillvalue);
00105 Array(const Array<base>& A) : vector<base>(A) { }
00106
00107
00108 Array& operator=(base x);
00109 Array& operator=(const Array<base>& A);
00110
00111
00112 Index nelem() const;
00113
00114
00115 const base& operator[](Index n) const;
00116 base& operator[](Index n);
00117 };
00118
00119
00120
00121
00122
00124 template<class base>
00125 inline Array<base>::Array(Index n, const base& fillvalue) :
00126 vector<base>(n)
00127 {
00128
00129 fill(this->begin(),this->end(),fillvalue);
00130 }
00131
00132
00134 template<class base>
00135 inline Array<base>& Array<base>::operator=(base x)
00136 {
00137 fill(this->begin(),this->end(),x);
00138 return *this;
00139 }
00140
00142
00157 template<class base>
00158 inline Array<base>& Array<base>::operator=(const Array<base>& A)
00159 {
00160
00161 resize(A.size());
00162 copy( A.begin(), A.end(), this->begin() );
00163 return *this;
00164 }
00165
00167 template<class base>
00168 inline Index Array<base>::nelem() const
00169 {
00170 size_t s = this->size();
00171 assert(s<LONG_MAX);
00172 return static_cast<long>(s);
00173 }
00174
00177 template<class base>
00178 inline const base& Array<base>::operator[](Index n) const
00179 {
00180 assert(0<=n);
00181 assert(n<nelem());
00182 return vector<base>::operator[](n);
00183 }
00184
00187 template<class base>
00188 inline base& Array<base>::operator[](Index n)
00189 {
00190 assert(0<=n);
00191 assert(n<nelem());
00192 return vector<base>::operator[](n);
00193 }
00194
00195
00196
00197
00199 template<class base>
00200 inline ostream& operator<<(ostream& os, const Array<base>& v)
00201 {
00202 typename Array<base>::const_iterator i = v.begin();
00203 const typename Array<base>::const_iterator end = v.end();
00204
00205 if ( i!=end )
00206 {
00207 os << setw(3) << *i;
00208 ++i;
00209 }
00210
00211 for ( ; i!=end; ++i )
00212 {
00213 os << " " << setw(3) << *i;
00214 }
00215
00216 return os;
00217 }
00218
00220 template<class base>
00221 inline base max(const Array<base>& x)
00222 {
00223
00224 base max = x[0];
00225
00226 typename Array<base>::const_iterator xi = x.begin();
00227 const typename Array<base>::const_iterator xe = x.end();
00228
00229 for ( ; xi!=xe ; ++xi )
00230 {
00231 if ( *xi > max )
00232 max = *xi;
00233 }
00234
00235 return max;
00236 }
00237
00239 template<class base>
00240 inline base min(const Array<base>& x)
00241 {
00242
00243 base min = x[0];
00244
00245 typename Array<base>::const_iterator xi = x.begin();
00246 const typename Array<base>::const_iterator xe = x.end();
00247
00248 for ( ; xi!=xe ; ++xi )
00249 {
00250 if ( *xi < min )
00251 min = *xi;
00252 }
00253
00254 return min;
00255 }
00256
00257
00259
00272 template <class base>
00273 Index find_first( const Array<base>& x,
00274 const base& w )
00275 {
00276 for ( Index i=0; i<x.nelem(); ++i )
00277 if ( w == x[i] )
00278 return i;
00279
00280 return -1;
00281 }
00282
00284
00297 template <class base>
00298 void find_all( ArrayOfIndex& pos,
00299 const Array<base>& x,
00300 const base& w )
00301 {
00302 pos.resize(0);
00303 for ( Index i=0; i<x.nelem(); ++i )
00304 if ( w == x[i] )
00305 pos.push_back(i);
00306 }
00307
00308
00309
00310
00311
00312
00313
00314 #endif // array_h