source: vis_dev/glu-2.1/src/array/array.h @ 8

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

src glu

File size: 3.7 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
37//duplique le tableau old en ignorant la case d'indice i
38EXTERN array_t *array_partial_dup ARGS((array_t *, int));
39
40extern int unsigned array_global_index;
41extern int array_global_insert;
42
43#define array_alloc(type, number)               \
44    array_do_alloc(sizeof(type), number)
45
46#define array_insert(type, a, i, datum)         \
47    (  -(a)->index != sizeof(type) ? array_abort((a),4) : 0,\
48        (a)->index = (i),\
49        (a)->index < 0 ? array_abort((a),0) : 0,\
50        (a)->index >= (a)->n_size ?\
51        array_global_insert = array_resize(a, (a)->index + 1) : 0,\
52        array_global_insert != ARRAY_OUT_OF_MEM ?\
53        *((type *) ((a)->space + (a)->index * (a)->obj_size)) = datum : datum,\
54        array_global_insert != ARRAY_OUT_OF_MEM ?\
55        ((a)->index >= (a)->num ? (a)->num = (a)->index + 1 : 0) : 0,\
56        array_global_insert != ARRAY_OUT_OF_MEM ?\
57        ((a)->index = -(int)sizeof(type)) : ARRAY_OUT_OF_MEM )
58
59#define array_insert_last(type, array, datum)   \
60    array_insert(type, array, (array)->num, datum)
61
62/* RB, added this without understanding locking */
63#define array_remove_last(a) \
64    (  -(a)->index != (a)->obj_size ? array_abort((a),4) : 0,\
65       (a)->index = -(a)->index,\
66       (a)->num ? (a)->num-- : array_abort((a),5),\
67       (a)->index = -(a)->index )
68
69#define array_fetch(type, a, i)                 \
70    (array_global_index = (i),                          \
71      (array_global_index >= (unsigned) ((a)->num)) ? array_abort((a),1) : 0,\
72      *((type *) ((a)->space + array_global_index * (a)->obj_size)))
73
74#define array_fetch_p(type, a, i)                       \
75    (array_global_index = (i),                             \
76      (array_global_index >= (unsigned) ((a)->num)) ? array_abort((a),1) : 0,\
77      ((type *) ((a)->space + array_global_index * (a)->obj_size)))
78
79#define array_fetch_last(type, array)           \
80    array_fetch(type, array, ((array)->num)-1)
81
82#define array_n(array)                          \
83    (array)->num
84
85#define array_data(type, array)                 \
86    (type *) array_do_data(array)
87
88#define arrayForEachItem(                                      \
89  type,  /* type of object stored in array */                  \
90  array, /* array to iterate */                                \
91  i,     /* int, local variable for iterator */                \
92  data   /* object of type */                                  \
93)                                                              \
94  for((i) = 0;                                                 \
95      (((i) < array_n((array)))                                \
96       && (((data) = array_fetch(type, (array), (i))), 1));    \
97      (i)++)
98
99#endif
100
101
102
103
104
105
Note: See TracBrowser for help on using the repository browser.