00001
00002
00003
00004
00005 #ifndef _NOBLITZ_H_
00006 #define _NOBLITZ_H_
00007 #include <cassert>
00008 #include <vector>
00009
00010 template <int N>
00011 class VarShape {
00012 private:
00013 std::vector <int> s;
00014 public:
00015 const int& operator[](int i) const {
00016 assert ((i<N)&&(i>=0));
00017 return s[i];
00018 }
00019
00020 int& operator[](int i) {
00021 assert ((i<N)&&(i>=0));
00022 return s[i];
00023 }
00024
00025 VarShape() {
00026 s.resize(N);
00027 }
00028
00029 VarShape(const VarShape& V) {
00030 s.resize(V.N);
00031 for (int i=0; i<N; i++)
00032 s[i] = V.s[i];
00033 }
00034 };
00035
00036
00037 template <class T, int N>
00038 class Array {
00039 };
00040
00041
00042 template <class T>
00043 class Array<T,3> {
00044 private:
00045 T*** array;
00046 T* the_array;
00047 VarShape<3> s;
00048 public:
00049
00050 Array() {
00051 for (int i=0; i<3; i++)
00052 shape()[i]=0;
00053 }
00054
00055 Array(const Array<T,3>& a) {
00056 const int ni = a.shape()[0];
00057 const int nj = a.shape()[1];
00058 const int nk = a.shape()[2];
00059
00060 resize(ni,nj,nk);
00061
00062 for (int i=0; i<ni*nj*nk; i++)
00063 the_array[i] = a.the_array[i];
00064 }
00065
00066 ~Array() {
00067 if (shape()[0]>0) {
00068 for (int i=0; i<shape()[0]; i++)
00069 delete[] array[i];
00070 delete [] array;
00071 delete [] the_array;
00072 }
00073 }
00074
00075 inline T& operator()(const int& i, const int& j, const int& k) {
00076 return array[i][j][k];
00077 }
00078
00079 inline const T& operator()(const int& i, const int& j, const int& k) const {
00080 return array[i][j][k];
00081 }
00082
00083 inline T& operator[](const int& i) {
00084 return the_array[i];
00085 }
00086
00087 inline const T& operator[](const int& i) const {
00088 return the_array[i];
00089 }
00090
00091 inline Array<T,3> operator*(const real& d) const {
00092 Array<T,3> a;
00093 a.resize(shape()[0],shape()[1],shape()[2]);
00094
00095 for (int i=0; i<shape()[0]*shape()[1]*shape()[2]; i++)
00096 a.the_array[i] = the_array[i]*d;
00097
00098 return a;
00099 }
00100
00101 inline Array<T,3> operator-(const Array<T,3>& a) const {
00102 assert((shape()[0]==a.shape()[0])
00103 &&(shape()[1]==a.shape()[1])
00104 &&(shape()[2]==a.shape()[2]));
00105
00106 Array<T,3> temp;
00107 temp.resize(shape()[0],shape()[1],shape()[2]);
00108 for (int i=0; i<shape()[0]*shape()[1]*shape()[2]; i++) {
00109 temp.the_array[i]
00110 = the_array[i] - a.the_array[i];
00111 }
00112 return temp;
00113 }
00114
00115 inline Array<T,3>& operator-=(const Array<T,3>& a) {
00116 assert((shape()[0]==a.shape()[0])
00117 &&(shape()[1]==a.shape()[1])
00118 &&(shape()[2]==a.shape()[2]));
00119
00120 for (int i=0; i<shape()[0]*shape()[1]*shape()[2]; i++){
00121 the_array[i] -= a.the_array[i];
00122 }
00123 return *this;
00124 }
00125
00126 inline Array<T,3> operator+(const Array<T,3>& a) const {
00127 assert((shape()[0]==a.shape()[0])
00128 &&(shape()[1]==a.shape()[1])
00129 &&(shape()[2]==a.shape()[2]));
00130
00131 Array<T,3> temp;
00132 temp.resize(shape()[0],shape()[1],shape()[2]);
00133 for (int i=0; i<shape()[0]*shape()[1]*shape()[2]; i++) {
00134 temp.the_array[i]
00135 = the_array[i] + a.the_array[i];
00136 }
00137 return temp;
00138 }
00139
00140 inline Array<T,3>& operator*=(const real& d) {
00141
00142 for (int i=0; i<shape()[0]*shape()[1]*shape()[2]; i++) {
00143 the_array[i] *= d;
00144 }
00145 return *this;
00146 }
00147
00148 inline Array<T,3>& operator+=(const Array<T,3>& a) {
00149 assert((shape()[0]==a.shape()[0])
00150 &&(shape()[1]==a.shape()[1])
00151 &&(shape()[2]==a.shape()[2]));
00152
00153 for (int i=0; i<shape()[0]*shape()[1]*shape()[2]; i++) {
00154 the_array[i] += a.the_array[i];
00155 }
00156 return *this;
00157 }
00158
00159 inline VarShape<3>& shape() {
00160 return s;
00161 }
00162
00163 inline void resize(const int& ni, const int& nj, const int& nk) {
00164 if ((ni != shape()[0])&&(nj != shape()[1])&&(nk != shape()[2])) {
00165
00166
00167 if (shape()[0] > 0) {
00168 for (int i=0; i<shape()[0]; i++)
00169 if (shape()[1] > 0) {
00170 delete [] array[i];
00171 }
00172 delete [] array;
00173 delete [] the_array;
00174 }
00175
00176 shape()[0] = ni;
00177 shape()[1] = nj;
00178 shape()[2] = nk;
00179 array = new T**[ni];
00180
00181 the_array = new T[ni*nj*nk];
00182
00183 for (int i=0; i<ni; i++)
00184 array[i] = new T*[nj];
00185 for (int i=0; i<ni; i++)
00186 for (int j=0; j<nj; j++)
00187 array[i][j] = &the_array[i*nj*nk + j*nk];
00188 }
00189 }
00190
00191 inline const VarShape<3>& shape() const {
00192 return s;
00193 }
00194
00195 inline Array<T,3>& operator=(const Array<T,3>& a) {
00196
00197 if (this != &a) {
00198 resize(a.shape()[0], a.shape()[1], a.shape()[2]);
00199 for (int i=0; i<a.shape()[0]*shape()[1]*shape()[2]; i++)
00200 the_array[i] = a.the_array[i];
00201 }
00202
00203 return *this;
00204 }
00205
00206
00207 inline Array<T,3>& operator=(const real& d) {
00208 for (int i=0; i<shape()[0]*shape()[1]*shape()[2]; i++)
00209 the_array[i] = d;
00210
00211 return *this;
00212 }
00213
00214 };
00215
00216 #endif // _NOBLITZ_H_
00217