source: vis_dev/glu-2.3/src/array/array.h @ 13

Last change on this file since 13 was 13, checked in by cecile, 13 years ago

library glu 2.3

File size: 3.6 KB
Line 
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
18typedef 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
26EXTERN array_t *array_do_alloc ARGS((int, int));
27EXTERN array_t *array_dup ARGS((array_t *));
28EXTERN array_t *array_join ARGS((array_t *, array_t *));
29EXTERN void array_free ARGS((array_t *));
30EXTERN int array_append ARGS((array_t *, array_t *));
31EXTERN void array_sort ARGS((array_t *, int (*)(const void *, const void *)));
32EXTERN void array_uniq ARGS((array_t *, int (*)(char **, char **), void (*)(char *)));
33EXTERN int array_abort ARGS((array_t *, int));
34EXTERN int array_resize ARGS((array_t *, int));
35EXTERN char *array_do_data ARGS((array_t *));
36
37extern int unsigned array_global_index;
38extern int array_global_insert;
39
40#define array_alloc(type, number)               \
41    array_do_alloc(sizeof(type), number)
42
43#define array_insert(type, a, i, datum)         \
44    (  -(a)->index != sizeof(type) ? array_abort((a),4) : 0,\
45        (a)->index = (i),\
46        (a)->index < 0 ? array_abort((a),0) : 0,\
47        (a)->index >= (a)->n_size ?\
48        array_global_insert = array_resize(a, (a)->index + 1) : 0,\
49        array_global_insert != ARRAY_OUT_OF_MEM ?\
50        *((type *) ((a)->space + (a)->index * (a)->obj_size)) = datum : datum,\
51        array_global_insert != ARRAY_OUT_OF_MEM ?\
52        ((a)->index >= (a)->num ? (a)->num = (a)->index + 1 : 0) : 0,\
53        array_global_insert != ARRAY_OUT_OF_MEM ?\
54        ((a)->index = -(int)sizeof(type)) : ARRAY_OUT_OF_MEM )
55
56#define array_insert_last(type, array, datum)   \
57    array_insert(type, array, (array)->num, datum)
58
59/* RB, added this without understanding locking */
60#define array_remove_last(a) \
61    (  -(a)->index != (a)->obj_size ? array_abort((a),4) : 0,\
62       (a)->index = -(a)->index,\
63       (a)->num ? (a)->num-- : array_abort((a),5),\
64       (a)->index = -(a)->index )
65
66#define array_fetch(type, a, i)                 \
67    (array_global_index = (i),                          \
68      (array_global_index >= (unsigned) ((a)->num)) ? array_abort((a),1) : 0,\
69      *((type *) ((a)->space + array_global_index * (a)->obj_size)))
70
71#define array_fetch_p(type, a, i)                       \
72    (array_global_index = (i),                             \
73      (array_global_index >= (unsigned) ((a)->num)) ? array_abort((a),1) : 0,\
74      ((type *) ((a)->space + array_global_index * (a)->obj_size)))
75
76#define array_fetch_last(type, array)           \
77    array_fetch(type, array, ((array)->num)-1)
78
79#define array_n(array)                          \
80    (array)->num
81
82#define array_data(type, array)                 \
83    (type *) array_do_data(array)
84
85#define arrayForEachItem(                                      \
86  type,  /* type of object stored in array */                  \
87  array, /* array to iterate */                                \
88  i,     /* int, local variable for iterator */                \
89  data   /* object of type */                                  \
90)                                                              \
91  for((i) = 0;                                                 \
92      (((i) < array_n((array)))                                \
93       && (((data) = array_fetch(type, (array), (i))), 1));    \
94      (i)++)
95
96#endif
97
98
99
100
101
102
Note: See TracBrowser for help on using the repository browser.