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