-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.h
469 lines (431 loc) · 25.5 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/**
* @file array.h
* @author Rouben Rostamian, [email protected]
* @author Ranjan Maitra, [email protected]
* @author Karin Dorman, [email protected]
* @date Thu Dec 6 08:29:14 CST 2012
*
* o This file defines the following macros:
*
* - MAKE_1ARRAY(a,n) make a 1D array of length "n"
* - CMAKE_1ARRAY(a,n) make a 1D array of length "n", using calloc
* - MAKE_2ARRAY(a,m,n) make a 2D array of dimensions "m x n"
* - CMAKE_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"
* - CMAKE_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"
* - CMAKE_4ARRAY(a,k,l,m,n) make a 4D array of dimensions "k x l x m x n"
* - MAKE_2JAGGED_ARRAY(a,m,v) make a 2D jagged array; v is array of len m
* - CMAKE_2JAGGED_ARRAY(a,m,v) make a 2D jagged array; v is array of len m
* - MAKE_3JAGGED_ARRAY(a,m,n,v) make a 3D jagged matrix; v is array of len n
* - CMAKE_3JAGGED_ARRAY(a,m,n,v) make a 3D jagged matrix; v is array of len n
* - MAKE_4JAGGED_ARRAY(a,k,m,n,v) make a 4D jagged matrix; v is array of len n
* - CMAKE_4JAGGED_ARRAY(a,k,m,n,v)make a 4D jagged matrix; v is array of len n
* - COPY_1ARRAY(a,b,n) copy a 1D array of length n
* - COPY_2ARRAY(a,b,n) copy a 2D array of length "any x n"
* - COPY_3ARRAY(a,b,n) make a 3D array of dimensions "any x any x n"
* - COPY_4ARRAY(a,b,n) make a 4D array of dimensions "any x any x any x n"
* - COPY_2JAGGED_ARRAY(a,b,v) make a 2D jagged array; v is array of len m
* - COPY_3JAGGED_ARRAY(a,b,v) make a 3D jagged matrix; v is array of len 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 The current versions lead to fragmented memory. TODO write other versions
* that distribute memory differently.
*
* 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 -------
*
*
* - 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
* - Revised Dec 2012
* . added CMAKE_* versions calling calloc() instead of malloc()
* . added *MAKE_*JAGGED_ARRAY versions for creating jagged arrays where
* last dimension is variable, depending on index of penultimate
* dimension
* . added *COPY*ARRAY macros for copying arays using efficient memcpy()
* calls
*/
#ifndef H_ARRAY_
#define H_ARRAY_
#include <stdio.h>
#include <stdlib.h>
/* pointer to desired MAKE_1ARRAY variant */
#define MAKE_1ARRAY MAKE_1ARRAY_DEFAULT
#define CMAKE_1ARRAY MAKE_1ARRAY_DEFAULT
/* ---------- 1D arrays ---------------------- */
#define MAKE_1ARRAY_DEFAULT(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 CMAKE_1ARRAY_DEFAULT(a,n) do { \
(a) = calloc((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 COPY_1ARRAY(a,b,n) do { \
(a) = memcpy((a), (b), (n) * sizeof *(a)) \
if ((a) == NULL) \
fprintf(stder, "*** in file %s, function %s(), line %d: " \
"memcpy failed!\n", __FILE__, __func__, __LINE__); \
} while (0)
#define FREE_1ARRAY(a) do { \
free(a); \
a = NULL; \
} while (0)
/* ---------- 2D arrays ---------------------- */
/* 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<(size_t)(m); \
ARRAY_H2RESERVED++) { \
MAKE_1ARRAY((a)[ARRAY_H2RESERVED],(n)); \
if ((a)[ARRAY_H2RESERVED]==NULL) { \
FREE_2ARRAY(a); \
break; \
} \
} \
} while (0)
#define CMAKE_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<(size_t)(m); \
ARRAY_H2RESERVED++) { \
CMAKE_1ARRAY((a)[ARRAY_H2RESERVED],(n)); \
if ((a)[ARRAY_H2RESERVED]==NULL) { \
FREE_2ARRAY(a); \
break; \
} \
} \
} while (0)
#define MAKE_2JAGGED_ARRAY(a,m,v) do { \
size_t ARRAY_H2RESERVED; \
MAKE_1ARRAY(a, (m)+1); \
if ((a) == NULL) \
break; \
(a)[m] = NULL; \
for (ARRAY_H2RESERVED=0; ARRAY_H2RESERVED<(size_t)(m); \
ARRAY_H2RESERVED++) { \
MAKE_1ARRAY((a)[ARRAY_H2RESERVED], (v)[ARRAY_H2RESERVED]); \
if ((a)[ARRAY_H2RESERVED] == NULL) { \
FREE_2ARRAY(a); \
break; \
} \
} \
} while (0)
#define CMAKE_2JAGGED_ARRAY(a,m,v) do { \
size_t ARRAY_H2RESERVED; \
MAKE_1ARRAY(a, (m)+1); \
if ((a) == NULL) \
break; \
(a)[m] = NULL; \
for (ARRAY_H2RESERVED=0; ARRAY_H2RESERVED<(size_t)(m); \
ARRAY_H2RESERVED++) { \
CMAKE_1ARRAY((a)[ARRAY_H2RESERVED], (v)[ARRAY_H2RESERVED]); \
if ((a)[ARRAY_H2RESERVED] == NULL) { \
FREE_2ARRAY(a); \
break; \
} \
} \
} while (0)
#define COPY_2ARRAY(a,b,n) do { \
size_t ARRAY_H2RESERVED; \
for (ARRAY_H2RESERVED=0; (a)[ARRAY_H2RESERVED]!=NULL; \
ARRAY_H2RESERVED++) { \
COPY_1ARRAY((a)[ARRAY_H2RESERVED], (b)[ARRAY_H2RESERVED], (n));\
if ((a)[ARRAY_H2RESERVED] == NULL) \
break; \
} \
} while (0)
#define COPY_2JAGGED_ARRAY(a,b,v) do { \
size_t ARRAY_H2RESERVED; \
for (ARRAY_H2RESERVED=0; (a)[ARRAY_H2RESERVED]!=NULL; \
ARRAY_H2RESERVED++) { \
COPY_1ARRAY((a)[ARRAY_H2RESERVED], (b)[ARRAY_H2RESERVED], \
(v)[ARRAY_H2RESERVED]); \
if ((a)[ARRAY_H2RESERVED] == NULL) \
break; \
} \
} while (0)
#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)
/* ---------- 3D arrays ---------------------- */
#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<(size_t)(p); \
ARRAY_H3RESERVED++) { \
MAKE_2ARRAY((a)[ARRAY_H3RESERVED],(q),(r)); \
if ((a)[ARRAY_H3RESERVED]==NULL) { \
FREE_3ARRAY(a); \
break; \
} \
} \
} while (0)
#define CMAKE_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<(size_t)(p); \
ARRAY_H3RESERVED++) { \
CMAKE_2ARRAY((a)[ARRAY_H3RESERVED],(q),(r)); \
if ((a)[ARRAY_H3RESERVED]==NULL) { \
FREE_3ARRAY(a); \
break; \
} \
} \
} while (0)
#define MAKE_3JAGGED_ARRAY(a,p,q,v) do { \
size_t ARRAY_H3RESERVED; \
MAKE_1ARRAY((a), (p)+1); \
if ((a) == NULL) \
break; \
(a)[p] = NULL; \
for (ARRAY_H3RESERVED=0; ARRAY_H3RESERVED<(size_t)(p); \
ARRAY_H3RESERVED++) { \
MAKE_2JAGGED_ARRAY((a)[ARRAY_H3RESERVED], (q), (v)); \
if ((a)[ARRAY_H3RESERVED] == NULL) { \
FREE_3ARRAY(a); \
break; \
} \
} \
} while (0)
#define CMAKE_3JAGGED_ARRAY(a,p,q,v) do { \
size_t ARRAY_H3RESERVED; \
MAKE_1ARRAY((a), (p)+1); \
if ((a) == NULL) \
break; \
(a)[p] = NULL; \
for (ARRAY_H3RESERVED=0; ARRAY_H3RESERVED<(size_t)(p); \
ARRAY_H3RESERVED++) { \
CMAKE_2JAGGED_ARRAY((a)[ARRAY_H3RESERVED], (q), (v)); \
if ((a)[ARRAY_H3RESERVED] == NULL) { \
FREE_3ARRAY(a); \
break; \
} \
} \
} while (0)
#define COPY_3ARRAY(a,b,r) do { \
size_t ARRAY_H3RESERVED; \
for (ARRAY_H3RESERVED=0; (a)[ARRAY_H3RESERVED]!=NULL; \
ARRAY_H3RESERVED++) { \
COPY_2ARRAY((a)[ARRAY_H3RESERVED], (b)[ARRAY_H3RESERVED], (r));\
if ((a)[ARRAY_H3RESERVED] == NULL) \
break; \
} \
} while (0)
#define COPY_3JAGGED_ARRAY(a,b,v) do { \
size_t ARRAY_H3RESERVED; \
for (ARRAY_H3RESERVED=0; (a)[ARRAY_H3RESERVED]!=NULL; \
ARRAY_H3RESERVED++) { \
COPY_2JAGGED_ARRAY((a)[ARRAY_H3RESERVED], \
(b)[ARRAY_H3RESERVED], (v)); \
if ((a)[ARRAY_H3RESERVED] == NULL) \
break; \
} \
} while (0)
#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)
/* ---------- 4D arrays ---------------------- */
#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<(size_t)(p); \
ARRAY_H4RESERVED++) { \
MAKE_3ARRAY((a)[ARRAY_H4RESERVED],(q),(r),(s)); \
if ((a)[ARRAY_H4RESERVED]==NULL) { \
FREE_4ARRAY(a); \
break; \
} \
} \
} while (0)
#define CMAKE_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<(size_t)(p); \
ARRAY_H4RESERVED++) { \
CMAKE_3ARRAY((a)[ARRAY_H4RESERVED],(q),(r),(s)); \
if ((a)[ARRAY_H4RESERVED]==NULL) { \
FREE_4ARRAY(a); \
break; \
} \
} \
} while (0)
#define MAKE_4JAGGED_ARRAY(a,p,q,r,v) do { \
size_t ARRAY_H4RESERVED; \
MAKE_1ARRAY(a,(p)+1); \
if (a==NULL) \
break; \
(a)[p] = NULL; \
for (ARRAY_H4RESERVED=0; ARRAY_H4RESERVED<(size_t)(p); \
ARRAY_H4RESERVED++) { \
MAKE_3JAGGED_ARRAY((a)[ARRAY_H4RESERVED],(q),(r),(v)); \
if ((a)[ARRAY_H4RESERVED]==NULL) { \
FREE_4ARRAY(a); \
break; \
} \
} \
} while (0)
#define CMAKE_4JAGGED_ARRAY(a,p,q,r,v) do { \
size_t ARRAY_H4RESERVED; \
MAKE_1ARRAY(a,(p)+1); \
if (a==NULL) \
break; \
(a)[p] = NULL; \
for (ARRAY_H4RESERVED=0; ARRAY_H4RESERVED<(size_t)(p); \
ARRAY_H4RESERVED++) { \
CMAKE_3JAGGED_ARRAY((a)[ARRAY_H4RESERVED],(q),(r),(v)); \
if ((a)[ARRAY_H4RESERVED]==NULL) { \
FREE_4ARRAY(a); \
break; \
} \
} \
} while (0)
#define COPY_4ARRAY(a,b,s) do { \
size_t ARRAY_H4RESERVED; \
for (ARRAY_H4RESERVED=0; (a)[ARRAY_H4RESERVED]!=NULL; \
ARRAY_H4RESERVED++) { \
COPY_3ARRAY((a)[ARRAY_H4RESERVED], (b)[ARRAY_H4RESERVED], (s));\
if ((a)[ARRAY_H4RESERVED] == NULL) \
break; \
} \
} while (0)
#define COPY_4JAGGED_ARRAY(a,b,v) do { \
size_t ARRAY_H4RESERVED; \
for (ARRAY_H4RESERVED=0; (a)[ARRAY_H4RESERVED]!=NULL; \
ARRAY_H4RESERVED++) { \
COPY_3JAGGED_ARRAY((a)[ARRAY_H4RESERVED], \
(b)[ARRAY_H4RESERVED], (v)); \
if ((a)[ARRAY_H4RESERVED] == NULL) \
break; \
} \
} while (0)
#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)
/* ---------- 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_ */