ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/freemol/trunk/src/mengine/src/utility.c
Revision: 110
Committed: Thu Mar 12 01:43:50 2009 UTC (12 years, 3 months ago) by gilbertke
File size: 5838 byte(s)
Log Message:
further cleanup and localization of atom data
Line File contents
1 #define EXTERN extern
2 #include "pcwin.h"
3 #include "utility.h"
4
5 void message_alert(char *, char *);
6
7 /* Routines to allocate and free space */
8 /* ---------------------------------- */
9 float *vector( int nl, int nh)
10 {
11 float *v;
12
13 v = (float *)malloc((unsigned) (nh-nl+1)*sizeof(float));
14 if (!v)
15 {
16 message_alert("Error allocation fmemory. Please contact Serena Software. The program will now exit!","Utility");
17 fprintf(pcmlogfile,"error allocating float vector\n");
18 exit(0);
19 }
20 return v-nl;
21 }
22 /* ---------------------------------- */
23 int *ivector( int nl, int nh)
24 {
25 int *v;
26
27 v = ( int *)malloc((unsigned) (nh-nl+1)*sizeof( int));
28 if (!v)
29 {
30 message_alert("Error allocation imemory. Please contact Serena Software. The program will now exit!","Utility");
31 fprintf(pcmlogfile,"error allocating int vector\n");
32 exit(0);
33 }
34 return v-nl;
35 }
36 /* ---------------------------------- */
37 long int *ilvector( int nl, int nh)
38 {
39 long int *v;
40
41 v = (long int *)malloc((unsigned) (nh-nl+1)*sizeof(long int));
42 if (!v)
43 {
44 message_alert("Error allocation imemory. Please contact Serena Software. The program will now exit!","Utility");
45 fprintf(pcmlogfile,"error allocating int vector\n");
46 exit(0);
47 }
48 return v-nl;
49 }
50 /* ---------------------------------- */
51 double *dvector( int nl, int nh)
52 {
53 double *v;
54
55 v = (double *)malloc((unsigned) (nh-nl+1)*sizeof(double));
56 if (!v)
57 {
58 char hatext[128];
59 sprintf(hatext,"Error allocation dmemory. Size %d\n",nh);
60 message_alert(hatext,"Utility");
61 fprintf(pcmlogfile,"error allocating double vector\n");
62 exit(0);
63 }
64 return v-nl;
65 }
66 /* ---------------------------------- */
67
68 float **matrix( int nrl, int nrh, int ncl, int nch)
69 {
70 int i;
71 float **m;
72
73 m = (float **)malloc((unsigned) (nrh-nrl+1)*sizeof(float*));
74 if (!m)
75 {
76 message_alert("Error allocation farray. Please contact Serena Software. The program will now exit!","Utility");
77 fprintf(pcmlogfile,"error allocating float array\n");
78 exit(0);
79 }
80 m -= nrl;
81
82 for (i = nrl; i <= nrh; i++)
83 {
84 m[i] = (float *)malloc((unsigned) (nch-ncl+1)*sizeof(float));
85 if (!m[i])
86 {
87 message_alert("Error allocation farray. Please contact Serena Software. The program will now exit!","Utility");
88 fprintf(pcmlogfile,"error allocating float array\n");
89 exit(0);
90 }
91 m[i] -= ncl;
92 }
93 return m;
94 }
95 /* ---------------------------------- */
96
97 double **dmatrix( int nrl, int nrh, int ncl, int nch)
98 {
99 int i;
100 double **m;
101
102 m = (double **)malloc((unsigned) (nrh-nrl+1)*sizeof(double*));
103 if (!m)
104 {
105 message_alert("Error allocation darray. Please contact Serena Software. The program will now exit!","Utility");
106 fprintf(pcmlogfile,"error allocating double array\n");
107 exit(0);
108 }
109 m -= nrl;
110
111 for (i = nrl; i <= nrh; i++)
112 {
113 m[i] = (double *)malloc((unsigned) (nch-ncl+1)*sizeof(double));
114 if (!m[i])
115 {
116 message_alert("Error allocation darray. Please contact Serena Software. The program will now exit!","Utility");
117 fprintf(pcmlogfile,"error allocating double array\n");
118 exit(0);
119 }
120 m[i] -= ncl;
121 }
122 return m;
123 }
124 /* ---------------------------------- */
125
126 int **imatrix( int nrl, int nrh, int ncl, int nch)
127 {
128 int i, **m;
129
130 m = ( int **)malloc((unsigned) (nrh-nrl+1)*sizeof( int*));
131 if (!m)
132 {
133 message_alert("Error allocation iarray. Please contact Serena Software. The program will now exit!","Utility");
134 fprintf(pcmlogfile,"error allocating array\n");
135 exit(0);
136 }
137 m -= nrl;
138
139 for (i = nrl; i <= nrh; i++)
140 {
141 m[i] = ( int *)malloc((unsigned) (nch-ncl+1)*sizeof( int));
142 if (!m[i])
143 {
144 message_alert("Error allocation farray. Please contact Serena Software. The program will now exit!","Utility");
145 fprintf(pcmlogfile,"error allocating array\n");
146 exit(0);
147 }
148 m[i] -= ncl;
149 }
150 return m;
151 }
152 /* ---------------------------------- */
153 void free_vector(float *v, int nl, int nh)
154 {
155 free((char*) (v+nl));
156 }
157
158 void free_ivector( int *v, int nl, int nh)
159 {
160 free((char*) (v+nl));
161 }
162 void free_ilvector( long int *v, int nl, int nh)
163 {
164 free((char*) (v+nl));
165 }
166
167 void free_dvector(double *v, int nl, int nh)
168 {
169 free((char*) (v+nl));
170 }
171
172 void free_matrix(float **m, int nrl, int nrh, int ncl, int nch)
173 {
174 int i;
175 for (i=nrh; i >= nrl; i--)
176 {
177 free((char*) (m[i]+ncl));
178 }
179 free((char*) (m+nrl));
180 }
181
182 void free_imatrix( int **m, int nrl, int nrh, int ncl, int nch)
183 {
184 int i;
185 for (i=nrh; i >= nrl; i--)
186 {
187 free((char*) (m[i]+ncl));
188 }
189 free((char*) (m+nrl));
190 }
191
192 void free_dmatrix(double **m, int nrl, int nrh, int ncl, int nch)
193 {
194 int i;
195 for (i=nrh; i >= nrl; i--)
196 {
197 free((char*) (m[i]+ncl));
198 }
199 free((char*) (m+nrl));
200 }
201