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 <iomanip>
00032 #include <iostream>
00033 #include <climits>
00034 #include "arts.h"
00035
00046 template<class base>
00047 class Array : public std::vector<base>
00048 {
00049 public:
00050
00051 Array() : std::vector<base>() { };
00052 explicit Array(Index n) : std::vector<base>(n) { };
00053 Array(Index n, const base& fill);
00054 Array(const Array<base>& A) : std::vector<base>(A) { };
00055
00056
00057 Array& operator=(base x);
00058 Array& operator=(const Array<base>& A);
00059
00060
00061 Index nelem() const;
00062
00063
00064 const base& operator[](Index n) const;
00065 base& operator[](Index n);
00066 };
00067
00068
00069
00070
00071
00073 template<class base>
00074 inline Array<base>::Array(Index n, const base& fill) :
00075 std::vector<base>(n)
00076 {
00077
00078 std::fill(this->begin(),this->end(),fill);
00079 };
00080
00081
00083 template<class base>
00084 inline Array<base>& Array<base>::operator=(base x)
00085 {
00086 std::fill(this->begin(),this->end(),x);
00087 return *this;
00088 }
00089
00100 template<class base>
00101 inline Array<base>& Array<base>::operator=(const Array<base>& A)
00102 {
00103
00104 if ( 0==this->size() )
00105 resize(A.size());
00106 else
00107 assert( this->size()==A.size() );
00108
00109 std::copy( A.begin(), A.end(), this->begin() );
00110 return *this;
00111 }
00112
00114 template<class base>
00115 inline Index Array<base>::nelem() const
00116 {
00117 size_t s = this->size();
00118 assert(s<LONG_MAX);
00119 return static_cast<long>(s);
00120 }
00121
00124 template<class base>
00125 inline const base& Array<base>::operator[](Index n) const
00126 {
00127 assert(0<=n);
00128 assert(n<nelem());
00129 return std::vector<base>::operator[](n);
00130 }
00131
00134 template<class base>
00135 inline base& Array<base>::operator[](Index n)
00136 {
00137 assert(0<=n);
00138 assert(n<nelem());
00139 return std::vector<base>::operator[](n);
00140 }
00141
00142
00143
00145 template<class base>
00146 inline std::ostream& operator<<(std::ostream& os, const Array<base>& v)
00147 {
00148 typename Array<base>::const_iterator i = v.begin();
00149 const typename Array<base>::const_iterator end = v.end();
00150
00151 if ( i!=end )
00152 {
00153 os << setw(3) << *i;
00154 ++i;
00155 }
00156
00157 for ( ; i!=end; ++i )
00158 {
00159 os << "\n" << setw(3) << *i;
00160 }
00161
00162 return os;
00163 }
00164
00166 template<class base>
00167 inline base max(const Array<base>& x)
00168 {
00169
00170 base max = x[0];
00171
00172 typename Array<base>::const_iterator xi = x.begin();
00173 const typename Array<base>::const_iterator xe = x.end();
00174
00175 for ( ; xi!=xe ; ++xi )
00176 {
00177 if ( *xi > max )
00178 max = *xi;
00179 }
00180
00181 return max;
00182 }
00183
00185 template<class base>
00186 inline base min(const Array<base>& x)
00187 {
00188
00189 base min = x[0];
00190
00191 typename Array<base>::const_iterator xi = x.begin();
00192 const typename Array<base>::const_iterator xe = x.end();
00193
00194 for ( ; xi!=xe ; ++xi )
00195 {
00196 if ( *xi < min )
00197 min = *xi;
00198 }
00199
00200 return min;
00201 }
00202
00203
00204
00205
00206
00207
00208 #endif // array_h