-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.h
415 lines (330 loc) · 19.2 KB
/
array.h
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* array.h
o This file defines the following macros:
MAKE_1ARRAY(a,n) make a 1D array of length "n"
MAKE_2ARRAY(a,m,n) make a 2D array of dimensions "m x n"
MAKE_3ARRAY(a,l,m,n) make a 3D array of dimensions "l x m x n"
MAKE_4ARRAY(a,k,l,m,n) make a 4D array of dimensions "k x l x m x n"
FREE_1ARRAY(a) free memory allocated by MAKE_1ARRAY()
FREE_2ARRAY(a) free memory allocated by MAKE_2ARRAY()
FREE_3ARRAY(a) free memory allocated by MAKE_3ARRAY()
FREE_4ARRAY(a) free memory allocated by MAKE_4ARRAY()
o Additionally, it defines the following convenience macros as synonyms:
MAKE_VECTOR(a,n) same as MAKE_1ARRAY(a,n)
FREE_VECTOR(a) same as FREE_1ARRAY(a)
MAKE_MATRIX(a,m,n) same as MAKE_2ARRAY(a,m,n)
FREE_MATRIX(a) same as FREE_2ARRAY(a)
o Additionally, it declares and uses the identifiers:
ARRAY_H2RESERVED
ARRAY_H3RESERVED
ARRAY_H4RESERVED
within local blocks within the macros. THESE IDENTIFIERS
ARE RESERVED. The user should not use identifiers with this
names within the scope of these macros.
o vector/matrix/array elements can be of _any_ type.
o If malloc() fails during the execution of any of the MAKE_* macros:
. An "out of memory" message is printed to stderr.
. The macro's first argument is set to NULL.
o After a call to any of the FREE_*() macros, the macro's argument
is set to NULL.
o The FREE_*() macros can be applied to previously FREE_*()-ed
arrays with no ill effect.
o Note that macro arguments are evaluated more than once.
o Customization
When malloc() returns NULL, MAKE_1ARRAY prints a message on stderr
and the program continues. The user can alter this behavior by
redefining the MAKE_1ARRAY macro. For instance, to call exit()
whenever malloc() fails, the user can do:
#undef MAKE_1ARRAY
#define MAKE_1ARRAY(a,n) do { \
(a) = malloc((n) * sizeof *(a)); \
if ((a)==NULL) { \
fprintf(stderr, "*** in file %s, function %s(), line %d: " \
"out of memory!\n", __FILE__, __func__, __LINE__); \
exit(EXIT_FAILURE); \
} \
} while (0)
Since only MAKE_1ARRAY calls malloc() explicitly, this change affects
the behavior of not only MAKE_1ARRAY but all other MAKE_* macros as well.
---- SAMPLE USAGE -------------
#include "array.h"
int main(void)
{
float ***a; // can use any other type instead of "float"
size_t p=3, q=4, r=5; // will make a 3x4x5 3-D array of float
MAKE_3ARRAY(a, p, q, r);
if (a==NULL)
return EXIT_FAILURE;
a[2][0][1] = 3.14;
printf("%g \n", a[2][0][1]);
FREE_3ARRAY(a);
return EXIT_SUCCESS;
}
---- END OF SAMPLE USAGE -------
Author: Rouben Rostamian, <rostamian@umbc.edu>
June 2000
Revised Jul 2000
Revised Sep 2002
Revised Jun 2003 - macros don't call exit anymore
changed macro names to all-caps
Revised Aug 2003 - changed reserved names to all caps
Revised Dec 2003 - changed array index types to size_t (were int)
- added an "out of memory" message, printed to stderr
- most FREE* macros now come before MAKE* macros,
for possible improved efficiency in preprocessing
*/
#ifndef H_ARRAY_
#define H_ARRAY_
#include <stdio.h>
#include <stdlib.h>
/* ---------- 1D arrays ---------------------- */
#define MAKE_1ARRAY(a,n) do { \
(a) = malloc((n) * sizeof *(a)); \
if ((a)==NULL) \
fprintf(stderr, "*** in file %s, function %s(), line %d: " \
"out of memory!\n", __FILE__, __func__, __LINE__); \
} while (0)
#define FREE_1ARRAY(a) do { \
free(a); \
a = NULL; \
} while (0)
/* ---------- 2D arrays ---------------------- */
#define FREE_2ARRAY(a) do { \
size_t ARRAY_H2RESERVED; \
if (a==NULL) \
break; \
for (ARRAY_H2RESERVED=0; (a)[ARRAY_H2RESERVED]!=NULL; ARRAY_H2RESERVED++)\
FREE_1ARRAY((a)[ARRAY_H2RESERVED]); \
FREE_1ARRAY(a); \
} while (0)
/* note: parenthesize first arg because it may be given as `*a' */
#define MAKE_2ARRAY(a,m,n) do { \
size_t ARRAY_H2RESERVED; \
MAKE_1ARRAY(a,(m)+1); \
if (a==NULL) \
break; \
(a)[m] = NULL; \
for (ARRAY_H2RESERVED=0; ARRAY_H2RESERVED<(m); ARRAY_H2RESERVED++) { \
MAKE_1ARRAY((a)[ARRAY_H2RESERVED],(n)); \
if ((a)[ARRAY_H2RESERVED]==NULL) { \
FREE_2ARRAY(a); \
break; \
} \
} \
} while (0)
/* ---------- 3D arrays ---------------------- */
#define FREE_3ARRAY(a) do { \
size_t ARRAY_H3RESERVED; \
if (a==NULL) \
break; \
for (ARRAY_H3RESERVED=0; (a)[ARRAY_H3RESERVED]!=NULL; ARRAY_H3RESERVED++)\
FREE_2ARRAY((a)[ARRAY_H3RESERVED]); \
FREE_1ARRAY(a); \
} while (0)
#define MAKE_3ARRAY(a,p,q,r) do { \
size_t ARRAY_H3RESERVED; \
MAKE_1ARRAY(a,(p)+1); \
if (a==NULL) \
break; \
(a)[p] = NULL; \
for (ARRAY_H3RESERVED=0; ARRAY_H3RESERVED<(p); ARRAY_H3RESERVED++) { \
MAKE_2ARRAY((a)[ARRAY_H3RESERVED],(q),(r)); \
if ((a)[ARRAY_H3RESERVED]==NULL) { \
FREE_3ARRAY(a); \
break; \
} \
} \
} while (0)
/* ---------- 3D arrays ---------------------- */
#define FREE_4ARRAY(a) do { \
size_t ARRAY_H4RESERVED; \
if (a==NULL) \
break; \
for (ARRAY_H4RESERVED=0; (a)[ARRAY_H4RESERVED]!=NULL; ARRAY_H4RESERVED++)\
FREE_3ARRAY((a)[ARRAY_H4RESERVED]); \
FREE_1ARRAY(a); \
} while (0)
#define MAKE_4ARRAY(a,p,q,r,s) do { \
size_t ARRAY_H4RESERVED; \
MAKE_1ARRAY(a,(p)+1); \
if (a==NULL) \
break; \
(a)[p] = NULL; \
for (ARRAY_H4RESERVED=0; ARRAY_H4RESERVED<(p); ARRAY_H4RESERVED++) { \
MAKE_3ARRAY((a)[ARRAY_H4RESERVED],(q),(r),(s)); \
if ((a)[ARRAY_H4RESERVED]==NULL) { \
FREE_4ARRAY(a); \
break; \
} \
} \
} while (0)
/* ---------- synonyms ---------------------- */
#define MAKE_VECTOR(a,n) MAKE_1ARRAY(a,n)
#define MAKE_MATRIX(a,m,n) MAKE_2ARRAY(a,m,n)
#define FREE_VECTOR(a) FREE_1ARRAY(a)
#define FREE_MATRIX(a) FREE_2ARRAY(a)
#endif /* H_ARRAY_ */
/* array.h
o This file defines the following macros:
MAKE_1ARRAY(a,n) make a 1D array of length "n"
MAKE_2ARRAY(a,m,n) make a 2D array of dimensions "m x n"
MAKE_3ARRAY(a,l,m,n) make a 3D array of dimensions "l x m x n"
MAKE_4ARRAY(a,k,l,m,n) make a 4D array of dimensions "k x l x m x n"
FREE_1ARRAY(a) free memory allocated by MAKE_1ARRAY()
FREE_2ARRAY(a) free memory allocated by MAKE_2ARRAY()
FREE_3ARRAY(a) free memory allocated by MAKE_3ARRAY()
FREE_4ARRAY(a) free memory allocated by MAKE_4ARRAY()
o Additionally, it defines the following convenience macros as synonyms:
MAKE_VECTOR(a,n) same as MAKE_1ARRAY(a,n)
FREE_VECTOR(a) same as FREE_1ARRAY(a)
MAKE_MATRIX(a,m,n) same as MAKE_2ARRAY(a,m,n)
FREE_MATRIX(a) same as FREE_2ARRAY(a)
o Additionally, it declares and uses the identifiers:
ARRAY_H2RESERVED
ARRAY_H3RESERVED
ARRAY_H4RESERVED
within local blocks within the macros. THESE IDENTIFIERS
ARE RESERVED. The user should not use identifiers with this
names within the scope of these macros.
o vector/matrix/array elements can be of _any_ type.
o If malloc() fails during the execution of any of the MAKE_* macros:
. An "out of memory" message is printed to stderr.
. The macro's first argument is set to NULL.
o After a call to any of the FREE_*() macros, the macro's argument
is set to NULL.
o The FREE_*() macros can be applied to previously FREE_*()-ed
arrays with no ill effect.
o Note that macro arguments are evaluated more than once.
o Customization
When malloc() returns NULL, MAKE_1ARRAY prints a message on stderr
and the program continues. The user can alter this behavior by
redefining the MAKE_1ARRAY macro. For instance, to call exit()
whenever malloc() fails, the user can do:
#undef MAKE_1ARRAY
#define MAKE_1ARRAY(a,n) do { \
(a) = malloc((n) * sizeof *(a)); \
if ((a)==NULL) { \
fprintf(stderr, "*** in file %s, function %s(), line %d: " \
"out of memory!\n", __FILE__, __func__, __LINE__); \
exit(EXIT_FAILURE); \
} \
} while (0)
Since only MAKE_1ARRAY calls malloc() explicitly, this change affects
the behavior of not only MAKE_1ARRAY but all other MAKE_* macros as well.
---- SAMPLE USAGE -------------
#include "array.h"
int main(void)
{
float ***a; // can use any other type instead of "float"
size_t p=3, q=4, r=5; // will make a 3x4x5 3-D array of float
MAKE_3ARRAY(a, p, q, r);
if (a==NULL)
return EXIT_FAILURE;
a[2][0][1] = 3.14;
printf("%g \n", a[2][0][1]);
FREE_3ARRAY(a);
return EXIT_SUCCESS;
}
---- END OF SAMPLE USAGE -------
Author: Rouben Rostamian, <rostamian@umbc.edu>
June 2000
Revised Jul 2000
Revised Sep 2002
Revised Jun 2003 - macros don't call exit anymore
changed macro names to all-caps
Revised Aug 2003 - changed reserved names to all caps
Revised Dec 2003 - changed array index types to size_t (were int)
- added an "out of memory" message, printed to stderr
- most FREE* macros now come before MAKE* macros,
for possible improved efficiency in preprocessing
*/
#ifndef H_ARRAY_
#define H_ARRAY_
#include <stdio.h>
#include <stdlib.h>
/* ---------- 1D arrays ---------------------- */
#define MAKE_1ARRAY(a,n) do { \
(a) = malloc((n) * sizeof *(a)); \
if ((a)==NULL) \
fprintf(stderr, "*** in file %s, function %s(), line %d: " \
"out of memory!\n", __FILE__, __func__, __LINE__); \
} while (0)
#define FREE_1ARRAY(a) do { \
free(a); \
a = NULL; \
} while (0)
/* ---------- 2D arrays ---------------------- */
#define FREE_2ARRAY(a) do { \
size_t ARRAY_H2RESERVED; \
if (a==NULL) \
break; \
for (ARRAY_H2RESERVED=0; (a)[ARRAY_H2RESERVED]!=NULL; ARRAY_H2RESERVED++)\
FREE_1ARRAY((a)[ARRAY_H2RESERVED]); \
FREE_1ARRAY(a); \
} while (0)
/* note: parenthesize first arg because it may be given as `*a' */
#define MAKE_2ARRAY(a,m,n) do { \
size_t ARRAY_H2RESERVED; \
MAKE_1ARRAY(a,(m)+1); \
if (a==NULL) \
break; \
(a)[m] = NULL; \
for (ARRAY_H2RESERVED=0; ARRAY_H2RESERVED<(m); ARRAY_H2RESERVED++) { \
MAKE_1ARRAY((a)[ARRAY_H2RESERVED],(n)); \
if ((a)[ARRAY_H2RESERVED]==NULL) { \
FREE_2ARRAY(a); \
break; \
} \
} \
} while (0)
/* ---------- 3D arrays ---------------------- */
#define FREE_3ARRAY(a) do { \
size_t ARRAY_H3RESERVED; \
if (a==NULL) \
break; \
for (ARRAY_H3RESERVED=0; (a)[ARRAY_H3RESERVED]!=NULL; ARRAY_H3RESERVED++)\
FREE_2ARRAY((a)[ARRAY_H3RESERVED]); \
FREE_1ARRAY(a); \
} while (0)
#define MAKE_3ARRAY(a,p,q,r) do { \
size_t ARRAY_H3RESERVED; \
MAKE_1ARRAY(a,(p)+1); \
if (a==NULL) \
break; \
(a)[p] = NULL; \
for (ARRAY_H3RESERVED=0; ARRAY_H3RESERVED<(p); ARRAY_H3RESERVED++) { \
MAKE_2ARRAY((a)[ARRAY_H3RESERVED],(q),(r)); \
if ((a)[ARRAY_H3RESERVED]==NULL) { \
FREE_3ARRAY(a); \
break; \
} \
} \
} while (0)
/* ---------- 3D arrays ---------------------- */
#define FREE_4ARRAY(a) do { \
size_t ARRAY_H4RESERVED; \
if (a==NULL) \
break; \
for (ARRAY_H4RESERVED=0; (a)[ARRAY_H4RESERVED]!=NULL; ARRAY_H4RESERVED++)\
FREE_3ARRAY((a)[ARRAY_H4RESERVED]); \
FREE_1ARRAY(a); \
} while (0)
#define MAKE_4ARRAY(a,p,q,r,s) do { \
size_t ARRAY_H4RESERVED; \
MAKE_1ARRAY(a,(p)+1); \
if (a==NULL) \
break; \
(a)[p] = NULL; \
for (ARRAY_H4RESERVED=0; ARRAY_H4RESERVED<(p); ARRAY_H4RESERVED++) { \
MAKE_3ARRAY((a)[ARRAY_H4RESERVED],(q),(r),(s)); \
if ((a)[ARRAY_H4RESERVED]==NULL) { \
FREE_4ARRAY(a); \
break; \
} \
} \
} while (0)
/* ---------- synonyms ---------------------- */
#define MAKE_VECTOR(a,n) MAKE_1ARRAY(a,n)
#define MAKE_MATRIX(a,m,n) MAKE_2ARRAY(a,m,n)
#define FREE_VECTOR(a) FREE_1ARRAY(a)
#define FREE_MATRIX(a) FREE_2ARRAY(a)
#endif /* H_ARRAY_ */