MemoryAllocators.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __MEMORY_ALLOCATORS_H__
00019 #define __MEMORY_ALLOCATORS_H__
00020
00021 #include <stdlib.h>
00022
00023 template <class T> T** AllocateMatrix(int rows, int cols);
00024 template <class T> T** AllocateMatrix(int rows, int cols, T value);
00025 template <class T> void FreeMatrix(T ** & matrix, int rows);
00026
00027 char ** AllocateCharMatrix(int rows, int cols);
00028 void FreeCharMatrix(char ** & matrix, int rows);
00029
00030 float ** AllocateFloatMatrix(int rows, int cols);
00031 void FreeFloatMatrix(float ** & matrix, int rows);
00032
00033 double ** AllocateDoubleMatrix(int rows, int cols);
00034 void FreeDoubleMatrix(double ** & matrix, int rows);
00035
00036 int ** AllocateIntMatrix(int rows, int cols);
00037 void FreeIntMatrix(int ** & matrix, int rows);
00038
00039 short ** AllocateShortMatrix(int rows, int cols);
00040 void FreeShortMatrix(short ** & matrix, int rows);
00041
00042 char *** AllocateCharCube(int n, int rows, int cols);
00043 void FreeCharCube(char *** & matrix, int n, int rows);
00044
00045
00046
00047
00048
00049 template <class T> T** AllocateMatrix(int rows, int cols)
00050 {
00051 T ** matrix = new T * [rows];
00052
00053
00054 if (matrix == NULL)
00055 return NULL;
00056
00057 for (int i = 0; i < rows; i++)
00058 {
00059 matrix[i] = new T [cols];
00060
00061
00062 if (matrix[i] == NULL)
00063 {
00064 while (i--)
00065 delete [] matrix[i];
00066
00067 delete [] matrix;
00068
00069 return NULL;
00070 }
00071 }
00072
00073 return matrix;
00074 };
00075
00076 template <class T> T** AllocateMatrix(int rows, int cols, T value)
00077 {
00078 T ** matrix = AllocateMatrix<T>(rows, cols);
00079
00080 if (matrix != NULL)
00081 for (int i = 0; i < rows; i++)
00082 for (int j = 0; j < cols; j++)
00083 matrix[i][j] = value;
00084
00085 return matrix;
00086 };
00087
00088 template <class T> void FreeMatrix(T ** & matrix, int rows)
00089 {
00090 if (matrix == NULL)
00091 return;
00092
00093 for (int i = 0; i < rows; i++)
00094 delete [] matrix[i];
00095
00096 delete [] matrix;
00097
00098 matrix = NULL;
00099 };
00100
00101 #endif
00102