[project @ 2000-04-05 10:06:36 by simonmar]
[ghc-hetmet.git] / ghc / utils / prof / cgprof / matrix.c
1 /* ------------------------------------------------------------------------
2  * $Id: matrix.c,v 1.1 2000/04/05 10:06:36 simonmar Exp $
3  *                                                                      
4  *      Copyright (C) 1995-2000 University of Oxford
5  *                                                                      
6  * Permission to use, copy, modify, and distribute this software,
7  * and to incorporate it, in whole or in part, into other software,
8  * is hereby granted without fee, provided that
9  *   (1) the above copyright notice and this permission notice appear in
10  *       all copies of the source code, and the above copyright notice
11  *       appear in clearly visible form on all supporting documentation
12  *       and distribution media;
13  *   (2) modified versions of this software be accompanied by a complete
14  *       change history describing author, date, and modifications made;
15  *       and
16  *   (3) any redistribution of the software, in original or modified
17  *       form, be without fee and subject to these same conditions.
18  * --------------------------------------------------------------------- */
19
20 /* Not very clever sparse representation of a matrix. However, it will do
21  * for the call graph profiler. 
22  */
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include "matrix.h"
27
28 Matrix newMat(int rows,int cols, int elsize, void *zero) {
29   Matrix res;
30
31   res.elsize= elsize;
32   res.zero  = malloc(elsize);
33   if (res.zero==NULL) {
34     fprintf(stderr,"{newMat} unable to allocate storage\n");
35     exit(1);
36   }
37   memcpy(res.zero,zero,elsize);
38   res.rows  = rows;
39   res.cols  = cols;
40   res.mat=NULL;
41   return res;
42 }
43
44 void freeMat(Matrix *mat) {
45   Matrix_element *tmp_ptr, *ptr=mat->mat;
46   free(mat->zero);
47
48   while(ptr!=NULL) {
49     free(ptr->data);
50     tmp_ptr = ptr->next;
51     free(ptr);
52     ptr=tmp_ptr;
53   }
54 }
55
56 void *_Mat(Matrix *mat,int x, int y,int lineno, char *filename) {
57   Matrix_element *ptr= mat->mat;
58   if (x<0 || x>=mat->rows || y<0 || y>=mat->cols) {
59     fprintf(stderr,"Mat[%d,%d] out of bound index at line %d of \"%s\"\n",
60             x,y,lineno,filename);
61     exit(1);
62   }
63   while(ptr) {
64     if ((x==ptr->x) && (y==ptr->y)) {
65       return ptr->data;
66     }
67     ptr=ptr->next;
68   }
69   /* Not in list */
70   ptr = (Matrix_element*) malloc(sizeof(Matrix_element));
71   if (ptr==NULL) {
72     fprintf(stderr,"{_Mat} failed to allocate %d bytes\n",
73             sizeof(Matrix_element));
74     exit(1);
75   }
76   ptr->data = (void*) malloc(mat->elsize);
77   if (ptr->data==NULL) {
78     fprintf(stderr,"{_Mat} failed to allocate element of size %d bytes\n",
79             mat->elsize);
80     exit(1);
81   }
82   ptr->x=x;
83   ptr->y=y;
84   memcpy(ptr->data,mat->zero,mat->elsize);
85   ptr->next=mat->mat;
86   mat->mat=ptr;
87   return ptr->data;
88 }
89
90 int Mat_dense(Matrix mat,int x,int y) {
91   Matrix_element *ptr= mat.mat;
92   while (ptr) {
93     if ((x==ptr->x) && (y==ptr->y)) return 1;
94     ptr=ptr->next;
95   }
96   return 0;
97 }