home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / mesch12a.zip / tutadv.c < prev    next >
C/C++ Source or Header  |  1994-01-14  |  5KB  |  193 lines

  1.  
  2. /* routines from the section 8 of tutorial.txt */
  3.  
  4. #include "matrix.h"
  5.  
  6. #define M3D_LIST    3      /* list number */
  7. #define TYPE_MAT3D  0      /* the number of a type */
  8.  
  9. /* type for 3 dimensional matrices */
  10. typedef struct {
  11.     int l,m,n;    /* actual dimensions */
  12.     int max_l, max_m, max_n;    /* maximal dimensions */
  13.     Real ***me;    /* pointer to matrix elements */
  14.                    /* we do not consider segmented memory */
  15.         Real *base, **me2d;  /* me and me2d are additional pointers 
  16.                 to base */
  17. } MAT3D;
  18.  
  19.  
  20. /* function for creating a variable of MAT3D type */
  21.  
  22. MAT3D *m3d_get(l,m,n)
  23. int l,m,n;
  24. {
  25.   MAT3D *mat;
  26.   int i,j,k;
  27.  
  28.   /* check if arguments are positive */
  29.   if (l <= 0 || m <= 0 || n <= 0)
  30.     error(E_NEG,"m3d_get");
  31.  
  32.     /* new structure */
  33.   if ((mat = NEW(MAT3D)) == (MAT3D *)NULL)
  34.     error(E_MEM,"m3d_get");
  35.   else if (mem_info_is_on()) {
  36.     /* record how many bytes is allocated */
  37.     mem_bytes_list(TYPE_MAT3D,0,sizeof(MAT3D),M3D_LIST);
  38.     /* record a new allocated variable */
  39.     mem_numvar_list(TYPE_MAT3D,1,M3D_LIST);
  40.   }
  41.  
  42.   mat->l = mat->max_l = l;
  43.   mat->m = mat->max_m = m;
  44.   mat->n = mat->max_n = n;
  45.  
  46.     /* allocate memory for 3D array */
  47.   if ((mat->base = NEW_A(l*m*n,Real)) == (Real *)NULL) 
  48.     error(E_MEM,"m3d_get");
  49.   else if (mem_info_is_on())
  50.     mem_bytes_list(TYPE_MAT3D,0,l*m*n*sizeof(Real),M3D_LIST);
  51.  
  52.     /* allocate memory for 2D pointers */
  53.   if ((mat->me2d = NEW_A(l*m,Real *)) == (Real **)NULL)
  54.     error(E_MEM,"m3d_get");
  55.   else if (mem_info_is_on())
  56.     mem_bytes_list(TYPE_MAT3D,0,l*m*sizeof(Real *),M3D_LIST);      
  57.  
  58.     /* allocate  memory for 1D pointers */
  59.   if ((mat->me = NEW_A(l,Real **)) == (Real ***)NULL)
  60.     error(E_MEM,"m3d_get");
  61.   else if (mem_info_is_on())
  62.     mem_bytes_list(TYPE_MAT3D,0,l*sizeof(Real **),M3D_LIST);
  63.  
  64.       /* pointers to 2D matrices */
  65.   for (i=0,k=0; i < l; i++)
  66.     for (j=0; j < m; j++)
  67.       mat->me2d[k++] = &mat->base[(i*m+j)*n];
  68.  
  69.        /* pointers to rows */
  70.   for (i=0; i < l; i++)
  71.     mat->me[i] = &mat->me2d[i*m];
  72.  
  73.   return mat;
  74. }
  75.  
  76.  
  77. /* deallocate a variable of type MAT3D */
  78.  
  79. int m3d_free(mat)
  80. MAT3D *mat;
  81. {
  82.        /* do not try to deallocate the NULL pointer */
  83.   if (mat == (MAT3D *)NULL)
  84.     return -1;
  85.     
  86.       /* first deallocate base */
  87.   if (mat->base != (Real *)NULL) {
  88.     if (mem_info_is_on())
  89.     /* record how many bytes is deallocated */
  90.       mem_bytes_list(TYPE_MAT3D,mat->max_l*mat->max_m*mat->max_n*sizeof(Real),
  91.              0,M3D_LIST);
  92.     free((char *)mat->base);
  93.   }
  94.  
  95.      /* deallocate array of 2D pointers */
  96.   if (mat->me2d != (Real **)NULL) {
  97.     if (mem_info_is_on())
  98.     /* record how many bytes is deallocated */
  99.       mem_bytes_list(TYPE_MAT3D,mat->max_l*mat->max_m*sizeof(Real *),
  100.              0,M3D_LIST);
  101.     free((char *)mat->me2d);
  102.   }
  103.  
  104.      /* deallocate array of 1D pointers */
  105.   if (mat->me != (Real ***)NULL) {
  106.     if (mem_info_is_on())
  107.     /* record how many bytes is deallocated */
  108.       mem_bytes_list(TYPE_MAT3D,mat->max_l*sizeof(Real **),0,M3D_LIST);
  109.     free((char *)mat->me);
  110.   }
  111.  
  112.     /* deallocate  MAT3D structure */
  113.   if (mem_info_is_on()) {
  114.     mem_bytes_list(TYPE_MAT3D,sizeof(MAT3D),0,M3D_LIST);
  115.     mem_numvar_list(TYPE_MAT3D,-1,M3D_LIST);
  116.   }
  117.   free((char *)mat);
  118.  
  119.   return 0;
  120. }
  121.  
  122. /*=============================================*/
  123.  
  124. char *m3d_names[] = {
  125.   "MAT3D"
  126. };
  127.  
  128.  
  129. #define M3D_NUM  (sizeof(m3d_names)/sizeof(*m3d_names))
  130.  
  131. int (*m3d_free_funcs[M3D_NUM])() = {
  132.   m3d_free
  133. };
  134.  
  135. static MEM_ARRAY m3d_sum[M3D_NUM];
  136.  
  137.  
  138. /* test routing for allocating/deallocating static variables */
  139. void test_stat(k)
  140. int k;
  141. {
  142.    static MAT3D *work;
  143.  
  144.    if (!work) {
  145.       work = m3d_get(10,10,10);
  146.       mem_stat_reg_list(&work,TYPE_MAT3D,M3D_LIST);
  147.       work->me[9][9][9] = -3.14;
  148.    }
  149.    
  150.    if (k == 9) 
  151.      printf(" work[9][9][9] = %g\n",work->me[9][9][9]);
  152. }
  153.  
  154.  
  155. void main()
  156. {
  157.   MAT3D *M;
  158.   int i,j,k;
  159.  
  160.   mem_info_on(TRUE);
  161.   /* can be the first command */
  162.   mem_attach_list(M3D_LIST,M3D_NUM,m3d_names,m3d_free_funcs,m3d_sum);
  163.  
  164.   M = m3d_get(3,4,5);
  165.   mem_info_file(stdout,M3D_LIST);
  166.  
  167.   /* make use of M->me[i][j][k], where i,j,k are non-negative and 
  168.     i < 3, j < 4, k < 5 */
  169.  
  170.   mem_stat_mark(1);
  171.   for (i=0; i < 3; i++)
  172.     for (j=0; j < 4; j++)
  173.       for (k=0; k < 5; k++) {
  174.      test_stat(i+j+k);
  175.      M->me[i][j][k] = i+j+k;
  176.       }
  177.   mem_stat_free_list(1,M3D_LIST);
  178.   mem_info_file(stdout,M3D_LIST);
  179.  
  180.   printf(" M[%d][%d][%d] = %g\n",2,3,4,M->me[2][3][4]);
  181.  
  182.   mem_stat_mark(2);
  183.   test_stat(9);
  184.   mem_stat_free_list(2,M3D_LIST);
  185.  
  186.   m3d_free(M);  /* if M is not necessary */
  187.   mem_info_file(stdout,M3D_LIST);
  188.  
  189. }
  190.  
  191.  
  192.  
  193.