1 | /* |
---|
2 | * $Id: array.h,v 1.6 2002/12/08 03:37:19 fabio Exp $ |
---|
3 | * |
---|
4 | */ |
---|
5 | #ifndef ARRAY_H |
---|
6 | #define ARRAY_H |
---|
7 | |
---|
8 | /* Return value when memory allocation fails */ |
---|
9 | #define ARRAY_OUT_OF_MEM -10000 |
---|
10 | |
---|
11 | /* |
---|
12 | * In between accesses, the "index" field of the array_t structure |
---|
13 | * holds the negative of the size of the objects stored in the array. |
---|
14 | * This allows the functions and macros to perform a rudimentary form |
---|
15 | * of type checking. |
---|
16 | */ |
---|
17 | |
---|
18 | typedef struct array_t { |
---|
19 | char *space; |
---|
20 | int num; /* number of array elements. */ |
---|
21 | int n_size; /* size of 'data' array (in objects) */ |
---|
22 | int obj_size; /* size of each array object. */ |
---|
23 | int index; /* combined index and locking flag. */ |
---|
24 | } array_t; |
---|
25 | |
---|
26 | EXTERN array_t *array_do_alloc ARGS((int, int)); |
---|
27 | EXTERN array_t *array_dup ARGS((array_t *)); |
---|
28 | EXTERN array_t *array_join ARGS((array_t *, array_t *)); |
---|
29 | EXTERN void array_free ARGS((array_t *)); |
---|
30 | EXTERN int array_append ARGS((array_t *, array_t *)); |
---|
31 | EXTERN void array_sort ARGS((array_t *, int (*)(const void *, const void *))); |
---|
32 | EXTERN void array_uniq ARGS((array_t *, int (*)(char **, char **), void (*)(char *))); |
---|
33 | EXTERN int array_abort ARGS((array_t *, int)); |
---|
34 | EXTERN int array_resize ARGS((array_t *, int)); |
---|
35 | EXTERN char *array_do_data ARGS((array_t *)); |
---|
36 | EXTERN array_t * array_partial_dup(array_t *old, int i); |
---|
37 | |
---|
38 | extern int unsigned array_global_index; |
---|
39 | extern int array_global_insert; |
---|
40 | |
---|
41 | #define array_alloc(type, number) \ |
---|
42 | array_do_alloc(sizeof(type), number) |
---|
43 | |
---|
44 | #define array_insert(type, a, i, datum) \ |
---|
45 | ( -(a)->index != sizeof(type) ? array_abort((a),4) : 0,\ |
---|
46 | (a)->index = (i),\ |
---|
47 | (a)->index < 0 ? array_abort((a),0) : 0,\ |
---|
48 | (a)->index >= (a)->n_size ?\ |
---|
49 | array_global_insert = array_resize(a, (a)->index + 1) : 0,\ |
---|
50 | array_global_insert != ARRAY_OUT_OF_MEM ?\ |
---|
51 | *((type *) ((a)->space + (a)->index * (a)->obj_size)) = datum : datum,\ |
---|
52 | array_global_insert != ARRAY_OUT_OF_MEM ?\ |
---|
53 | ((a)->index >= (a)->num ? (a)->num = (a)->index + 1 : 0) : 0,\ |
---|
54 | array_global_insert != ARRAY_OUT_OF_MEM ?\ |
---|
55 | ((a)->index = -(int)sizeof(type)) : ARRAY_OUT_OF_MEM ) |
---|
56 | |
---|
57 | #define array_insert_last(type, array, datum) \ |
---|
58 | array_insert(type, array, (array)->num, datum) |
---|
59 | |
---|
60 | /* RB, added this without understanding locking */ |
---|
61 | #define array_remove_last(a) \ |
---|
62 | ( -(a)->index != (a)->obj_size ? array_abort((a),4) : 0,\ |
---|
63 | (a)->index = -(a)->index,\ |
---|
64 | (a)->num ? (a)->num-- : array_abort((a),5),\ |
---|
65 | (a)->index = -(a)->index ) |
---|
66 | |
---|
67 | #define array_fetch(type, a, i) \ |
---|
68 | (array_global_index = (i), \ |
---|
69 | (array_global_index >= (unsigned) ((a)->num)) ? array_abort((a),1) : 0,\ |
---|
70 | *((type *) ((a)->space + array_global_index * (a)->obj_size))) |
---|
71 | |
---|
72 | #define array_fetch_p(type, a, i) \ |
---|
73 | (array_global_index = (i), \ |
---|
74 | (array_global_index >= (unsigned) ((a)->num)) ? array_abort((a),1) : 0,\ |
---|
75 | ((type *) ((a)->space + array_global_index * (a)->obj_size))) |
---|
76 | |
---|
77 | #define array_fetch_last(type, array) \ |
---|
78 | array_fetch(type, array, ((array)->num)-1) |
---|
79 | |
---|
80 | #define array_n(array) \ |
---|
81 | (array)->num |
---|
82 | |
---|
83 | #define array_data(type, array) \ |
---|
84 | (type *) array_do_data(array) |
---|
85 | |
---|
86 | #define arrayForEachItem( \ |
---|
87 | type, /* type of object stored in array */ \ |
---|
88 | array, /* array to iterate */ \ |
---|
89 | i, /* int, local variable for iterator */ \ |
---|
90 | data /* object of type */ \ |
---|
91 | ) \ |
---|
92 | for((i) = 0; \ |
---|
93 | (((i) < array_n((array))) \ |
---|
94 | && (((data) = array_fetch(type, (array), (i))), 1)); \ |
---|
95 | (i)++) |
---|
96 | |
---|
97 | #endif |
---|
98 | |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | |
---|
103 | |
---|