📄 alloc.h
字号:
//// spectral toolkit // copyright (c) 2005 university corporation for atmospheric research// licensed under the gnu general public license//#ifndef __alloc__#define __alloc__namespace spectral{ /// Function template for 1-d array allocation. /// Use the 1-d dealloc function template to free memory allocated by alloc. /// \param n length of array /// \return pointer to allocated array template <typename T> T *alloc(int n) { T *p=new T[n]; return(p); } /// Function template for 1-d array deallocation. /// \param p pointer to array template <typename T> void dealloc(T *p) { delete [] p; } /// Function template for 2-d array allocation. Allocates a 2-d array as a /// double C-style pointer or array of pointers, contiguous in memory. /// Use the 2-d dealloc function template to free memory allocated by alloc. /// \param n2 length of first array dimension /// \param n1 length of second array dimension /// \return pointer to allocated 2-d array template <typename T> T **alloc(int n2,int n1) { int i; T **p = new T*[n2]; p[0] = new T[n2*n1]; for(i=1;i<n2;i++) p[i]=p[i-1]+n1; return(p); } /// Function template for 2-d triangular array allocation. Allocates a 2-d triangular array as a /// double C-style pointer or array of pointers, contiguous in memory. /// This array is useful for storing spherical harmonic spectral coefficients. /// Use the 2-d dealloc function template to free memory allocated by alloc. /// \param n length of triangular array dimension /// \return pointer to allocated 2-d triangular array accessed as [i][j] where i=0,..,n-1 and j=i,..,n-1 /// \sa sphere alf template <typename T> T **alloct(int n) { int i; T **p = new T*[n]; p[0] = new T[n*(n+1)/2]; for(i=1;i<n;i++) p[i]=p[i-1]+n-i; return(p); } /// Function template for 2-d array deallocation. /// \param p pointer to array template <typename T> void dealloc(T **p) { delete [] p[0]; delete [] p; } /// Function template for 3-d array allocation. Allocates a 3-d array as a /// triple C-style pointer, contiguous in memory. /// Use the 3-d dealloc function template to free memory allocated by alloc. /// \param n3 length of first array dimension /// \param n2 length of second array dimension /// \param n1 length of third array dimension /// \return pointer to allocated 3-d array template <typename T> T ***alloc(int n3,int n2,int n1) { int i,j,k; T ***p = new T**[n3]; p[0]= new T*[n3*n2]; for(i=1;i<n3;i++) p[i]=p[i-1]+n2; p[0][0] = new T[n3*n2*n1]; for(k=1;k<n3;k++) p[k][0]=p[k-1][0]+n1*n2; for(k=0;k<n3;k++) for(j=1;j<n2;j++) p[k][j] = p[k][j-1]+n1; return(p); } /// Function template for 3-d triangular array allocation. Allocates a 3-d triangular /// array as a triple C-style pointer, contiguous in memory. /// Use the 3-d dealloc function template to free memory allocated by alloc. /// \param m length of first array dimension /// \param n length of triangle /// \return pointer to allocated 3-d triangular array accessed as [k][i][j] where k=0,..,m-1; i=0,..,n-1 and j=i,..,n-1 template <typename T> T ***alloct(int m,int n) { T ***p = new T**[m]; p[0] = new T*[m*n]; for(int i=1;i<m;i++) p[i]=p[i-1]+n; p[0][0] = new T[m*(n*(n+1)/2)]; for(int k=1;k<m;k++) p[k][0]=p[k-1][0]+n*(n+1)/2; for(int j=0;j<m;j++) for(int i=1;i<n;i++) p[j][i]=p[j][i-1]+n-i; return(p); } /// Function template for 3-d array deallocation. /// \param p pointer to array template <typename T> void dealloc(T ***p) { delete [] p[0][0]; delete [] p[0]; delete [] p; } /// Function template for 4-d array allocation. Allocates a 4-d array as a /// quadruple C-style pointer, contiguous in memory. /// Use the 4-d dealloc function template to free memory allocated by alloc. /// \param n4 length of first array dimension /// \param n3 length of second array dimension /// \param n2 length of thrid array dimension /// \param n1 length of fourth array dimension /// \return pointer to allocated 4-d array template <typename T> T ****alloc(int n4,int n3,int n2,int n1) { int i,j,k,l; T ****p = new T***[n4]; p[0]= new T**[n4*n3]; for(i=1;i<n4;i++) p[i]=p[i-1]+n3; p[0][0] = new T*[n4*n3*n2]; for(k=1;k<n4;k++) p[k][0]=p[k-1][0]+n2*n3; for(k=0;k<n4;k++) for(j=1;j<n3;j++) p[k][j] = p[k][j-1]+n2; p[0][0][0] = new T[n4*n3*n2*n1]; for(l=1;l<n4;l++) p[l][0][0]=p[l-1][0][0]+n1*n2*n3; for(l=0;l<n4;l++) for(k=1;k<n3;k++) p[l][k][0]=p[l][k-1][0]+n1*n2; for(l=0;l<n4;l++) for(k=0;k<n3;k++) for(j=1;j<n2;j++) p[l][k][j] = p[l][k][j-1]+n1; return(p); } /// Function template for 4-d array deallocation. /// \param p pointer to array template <typename T> void dealloc(T ****p) { delete [] p[0][0][0]; delete [] p[0][0]; delete [] p[0]; delete [] p; }}#endif// Local Variables:// mode:C++// End:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -