-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.c
116 lines (86 loc) · 2.22 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#define FREE_ARG char*
double *dvector(long n)
/* allocate a double vector with subscript range v[0..n]
initialize with zeros
*/
{
double *v;
v= (double *)malloc((size_t) (n*sizeof(double)));
/* long i; */
/* for (i=0;i<n;i++) v[i]=0; */
return v;
}
int *ivector(long n)
/* allocate a double vector with subscript range v[0..n] */
{
int *v;
v=(int *)malloc((size_t) (n*sizeof(int)));
/* long i; */
/* for (i=0;i<n;i++) v[i]=0; */
return v;
}
unsigned int *uivector(long n)
/* allocate a double vector with subscript range v[0..n] */
{
unsigned int *v;
v=(unsigned int *)malloc((size_t) (n*sizeof(unsigned int)));
/* long i; */
/* for (i=0;i<n;i++) v[i]=0; */
return v;
}
double **dmatrix(long nrow, long ncol)
/* allocate an int matrix with subscript range m[0..nr][0..nc] */
{
long i;
double **m;
/* allocate pointers to rows */
m=(double **) malloc((size_t)((nrow)*sizeof(double*)));
/* allocate rows and set pointers to them */
m[0]=(double *) malloc((size_t)((nrow*ncol)*sizeof(double)));
for(i=1;i<nrow;i++) m[i]=m[i-1]+ncol;
/* for (i=0;i<nrow;i++) */
/* for (j=0;j<ncol;j++) */
/* m[i][j]=0; */
/* return pointer to array of pointers to rows */
return m;
}
int **imatrix(long nrow, long ncol)
/* allocate an int matrix with subscript range m[0..nr][0..nc] */
{
long i;
int **m;
/* allocate pointers to rows */
m=(int **) malloc((size_t)((nrow)*sizeof(int*)));
/* allocate rows and set pointers to them */
m[0]=(int *) malloc((size_t)((nrow*ncol)*sizeof(int)));
for(i=1;i<nrow;i++) m[i]=m[i-1]+ncol;
/* return pointer to array of pointers to rows */
return m;
}
void free_ivector(int *v)
/* free an int vector allocated with ivector() */
{
free((FREE_ARG) (v));
}
void free_uivector(unsigned int *v)
/* free an int vector allocated with uivector() */
{
free((FREE_ARG) (v));
}
void free_dvector(double *v)
/* free a double vector allocated with dvector() */
{
free((FREE_ARG) (v));
}
void free_dmatrix(double **m)
/* free a double matrix allocated by dmatrix() */
{
free((FREE_ARG) (m[0]));
free((FREE_ARG) (m));
}
void free_imatrix(int **m)
/* free an int matrix allocated by imatrix() */
{
free((FREE_ARG) (m[0]));
free((FREE_ARG) (m));
}