00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #if !defined (NXS_ALLOCATE_MATRIX_H)
00022 # define NXS_ALLOCATE_MATRIX_H
00023
00024 #include "ncl/nxsdefs.h"
00025
00026 template<typename T>
00027 T *** NewThreeDArray(unsigned f , unsigned s , unsigned t);
00028 template<typename T>
00029 T ** NewTwoDArray(unsigned f , unsigned s);
00030 template<typename T>
00031 void DeleteThreeDArray(T ***& ptr);
00032 template<typename T>
00033 void DeleteTwoDArray(T **& ptr);
00034
00054 template<typename T> T *** NewThreeDArray(unsigned f , unsigned s , unsigned t)
00055 {
00056 NCL_ASSERT(f > 0 && s > 0 && t> 0);
00057 const unsigned twoDStride = s*t;
00058 T ***ptr;
00059 ptr = new T **[f];
00060 ptr[0] = new T *[f * s];
00061 ptr[0][0] = new T[f * s * t];
00062 for (unsigned sIt = 1 ; sIt < s ; sIt++)
00063 ptr[0][sIt] = ptr[0][sIt-1] + t ;
00064 for (unsigned fIt = 1 ; fIt < f ; fIt ++)
00065 {
00066 ptr[fIt] = ptr[fIt -1] + s ;
00067 ptr[fIt][0] = ptr[fIt -1][0] + twoDStride;
00068 for (unsigned sIt = 1 ; sIt < s ; sIt++)
00069 ptr[fIt][sIt] = ptr[fIt][sIt-1] + t ;
00070 }
00071 return ptr;
00072 }
00073
00077 template<typename T> void DeleteThreeDArray (T *** & ptr)
00078 {
00079 if (ptr)
00080 {
00081 if (*ptr)
00082 {
00083 delete [] **ptr;
00084 delete [] * ptr;
00085 }
00086 delete [] ptr;
00087 }
00088 ptr = NULL;
00089 }
00090
00109 template<typename T> T **NewTwoDArray(unsigned f , unsigned s)
00110 {
00111 NCL_ASSERT(f > 0 && s > 0);
00112 T **ptr;
00113 ptr = new T *[f];
00114 *ptr = new T [f * s];
00115 for (unsigned fIt = 1 ; fIt < f ; fIt ++)
00116 ptr[fIt] = ptr[fIt -1] + s ;
00117 return ptr;
00118 }
00119
00123 template<typename T> inline void DeleteTwoDArray (T ** & ptr)
00124 {
00125 if (ptr)
00126 {
00127 delete [] * ptr;
00128 delete [] ptr;
00129 ptr = NULL;
00130 }
00131 }
00132
00133
00134 template<typename T>
00135 class ScopedThreeDMatrix
00136 {
00137 public:
00138 T *** ptr;
00139
00143 T *** GetAlias() const
00144 {
00145 return ptr;
00146 }
00148 ScopedThreeDMatrix(unsigned f = 0, unsigned s = 0, unsigned t = 0)
00149 :ptr(NULL)
00150 {
00151 Initialize(f, s, t);
00152 }
00154 void Initialize(unsigned f = 0, unsigned s = 0, unsigned t = 0)
00155 {
00156 Free();
00157 if (f > 0 && s > 0 && t > 0)
00158 ptr = NewThreeDArray<T>(f, s, t);
00159 }
00164 T ***Surrender()
00165 {
00166 T ***temp = ptr;
00167 ptr = NULL;
00168 return temp;
00169 }
00170 ~ScopedThreeDMatrix()
00171 {
00172 Free();
00173 }
00175 void Free()
00176 {
00177 if (ptr != NULL)
00178 {
00179 DeleteThreeDArray<T>(ptr);
00180 ptr = 0L;
00181 }
00182 }
00183 };
00184
00185
00192 template<typename T>
00193 class ScopedTwoDMatrix
00194 {
00195
00196 public:
00197 T ** ptr;
00198
00202 T ** GetAlias() const
00203 {
00204 return ptr;
00205 }
00207 ScopedTwoDMatrix(unsigned f = 0, unsigned s = 0)
00208 :ptr(NULL)
00209 {
00210 Initialize(f, s);
00211 }
00213 void Initialize(unsigned f, unsigned s)
00214 {
00215 Free();
00216 if (f > 0 && s > 0)
00217 ptr = NewTwoDArray<T>(f, s);
00218 }
00223 T **Surrender()
00224 {
00225 T** temp = ptr;
00226 ptr = NULL;
00227 return temp;
00228 }
00229
00230 ~ScopedTwoDMatrix()
00231 {
00232 Free();
00233 }
00234
00236 void Free()
00237 {
00238 if (ptr != NULL)
00239 {
00240 DeleteTwoDArray<T>(ptr);
00241 ptr = 0L;
00242 }
00243 }
00244
00245 };
00246
00247 typedef ScopedTwoDMatrix<double> ScopedDblTwoDMatrix;
00248 typedef ScopedTwoDMatrix<unsigned> ScopedUIntTwoDMatrix;
00249
00250 typedef ScopedThreeDMatrix<double> ScopedDblThreeDMatrix;
00251 typedef ScopedThreeDMatrix<unsigned> ScopedUIntThreeDMatrix;
00252
00253 #endif