libStatGen Software
1
|
00001 /* 00002 * Copyright (C) 2010 Regents of the University of Michigan 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation, either version 3 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 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 // Template definitions follow ... 00047 // 00048 00049 template <class T> T** AllocateMatrix(int rows, int cols) 00050 { 00051 T ** matrix = new T * [rows]; 00052 00053 // Stop early if we are out of memory 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 // Safely unravel allocation if we run out of memory 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