/vol/vipdata/irtk/common++/include/irtkAllocate.h

00001 /*=========================================================================
00002 
00003   Library   : Image Registration Toolkit (IRTK)
00004   Module    : $Id: irtkAllocate.h 2 2008-12-23 12:40:14Z dr $
00005   Copyright : Imperial College, Department of Computing
00006               Visual Information Processing (VIP), 2008 onwards
00007   Date      : $Date: 2008-12-23 12:40:14 +0000 (Tue, 23 Dec 2008) $
00008   Version   : $Revision: 2 $
00009   Changes   : $Author: dr $
00010 
00011 =========================================================================*/
00012 
00013 #ifndef _IRTKALLOCATE_H
00014 
00015 #define _IRTKALLOCATE_H
00016 
00017 #include <irtkCommon.h>
00018 
00019 template <class Type> inline Type **Allocate(Type **matrix, int x, int y)
00020 {
00021   int i;
00022 
00023   if ((matrix = new Type *[y]) == NULL) {
00024     cerr << "Allocate: malloc failed for " << x << " x " << y << "\n";
00025     exit(1);
00026   }
00027 
00028   if ((matrix[0] = new Type[y*x]) == NULL) {
00029     cerr << "Allocate: malloc failed for " << x << " x " << y << "\n";
00030     exit(1);
00031   }
00032 
00033   for (i = 1; i < y; i++) {
00034     matrix[i] = matrix[i-1] + x;
00035   }
00036 
00037 #ifdef DEBUG
00038   memory_allocated += x*y*sizeof(Type);
00039   cout << "Allocate: Memory allocated is " << memory_allocated << " bytes.\n";
00040 #endif
00041 
00042   return matrix;
00043 }
00044 
00045 template <class Type> inline Type ***Allocate(Type ***matrix, int x, int y, int z)
00046 {
00047   int i, j;
00048 
00049   if ((matrix = new Type **[z]) == NULL) {
00050     cerr << "Allocate: malloc failed for " << x << " x " << y << " x ";
00051     cerr << z << "\n";
00052     exit(1);
00053   }
00054 
00055   if ((matrix[0] = new Type *[z*y]) == NULL) {
00056     cerr << "Allocate: malloc failed for " << x << " x " << y << " x ";
00057     cerr << z << "\n";
00058     exit(1);
00059   }
00060 
00061   for (i = 1; i < z; i++) {
00062     matrix[i] = matrix[i-1] + y;
00063   }
00064 
00065   if ((matrix[0][0] = new Type[z*y*x]) == NULL) {
00066     cerr << "Allocate: malloc failed for " << x << " x " << y << " x ";
00067     cerr << z << "\n";
00068     exit(1);
00069   }
00070 
00071   for (i = 0; i < z; i++) {
00072     for (j = 0; j < y; j++) {
00073       matrix[i][j] = matrix[0][0] + (i*y+j)*x;
00074     }
00075   }
00076 
00077 #ifdef DEBUG
00078   memory_allocated += x*y*z*sizeof(Type);
00079   cout << "Allocate: Memory allocated is " << memory_allocated << " bytes.\n";
00080 #endif
00081 
00082   return matrix;
00083 }
00084 
00085 template <class Type> inline Type ****Allocate(Type ****matrix, int x, int y, int z, int t)
00086 {
00087   int i, j, k;
00088 
00089   if ((matrix = new Type ***[t]) == NULL) {
00090     cerr << "Allocate: malloc failed for " << x << " x " << y << " x ";
00091     cerr << z << " x " << t << "\n";
00092     exit(1);
00093   }
00094 
00095   if ((matrix[0] = new Type **[t*z]) == NULL) {
00096     cerr << "Allocate: malloc failed for " << x << " x " << y << " x ";
00097     cerr << z << " x " << t << "\n";
00098     exit(1);
00099   }
00100 
00101   for (i = 1; i < t; i++) {
00102     matrix[i] = matrix[i-1] + z;
00103   }
00104 
00105   if ((matrix[0][0] = new Type*[t*z*y]) == NULL) {
00106     cerr << "Allocate: malloc failed for " << x << " x " << y << " x ";
00107     cerr << z << " x " << t << "\n";
00108     exit(1);
00109   }
00110 
00111   for (i = 0; i < t; i++) {
00112     for (j = 0; j < z; j++) {
00113       matrix[i][j] = matrix[0][0] + i*z*y + j*y;
00114     }
00115   }
00116 
00117   if ((matrix[0][0][0] = new Type[t*z*y*x]) == NULL) {
00118     cerr << "Allocate: malloc failed for " << x << " x " << y << " x ";
00119     cerr << z << " x " << t << "\n";
00120     exit(1);
00121   }
00122 
00123   for (i = 0; i < t; i++) {
00124     for (j = 0; j < z; j++) {
00125       for (k = 0; k < y; k++) {
00126         matrix[i][j][k] = matrix[0][0][0] + i*z*y*x + j*y*x + k*x;
00127       }
00128     }
00129   }
00130 
00131 #ifdef DEBUG
00132   memory_allocated += x*y*z*t*sizeof(Type);
00133   cout << "Allocate: Memory allocated is " << memory_allocated << " bytes.\n";
00134 #endif
00135 
00136   return matrix;
00137 }
00138 
00139 #endif