Slight tweaks.
[zddfun.git] / darray.h
blob5fa90374eeb2430986eb3d953324badf7d1ff64b
1 // A linked-list implementation using C arrays
2 // darray = "dynamic array"
4 #define __DARRAY_H__
6 // Thanks to this typedef, we can search-and-replace to produce dynamic arrays
7 // for other types: labour-intensive templates. I ought to write a script to
8 // do this.
9 typedef void *typ_t;
11 struct darray_s {
12 typ_t *item;
13 int count;
14 int max;
17 typedef struct darray_s darray_t[1];
18 typedef struct darray_s *darray_ptr;
20 /*@manual darray
21 Initialize a dynamic array ''a''. Must be called before ''a'' is used.
23 void darray_init(darray_t a);
24 darray_ptr darray_new();
26 /*@manual darray
27 Clears a dynamic array ''a''. Should be called after ''a'' is no longer needed.
29 void darray_clear(darray_t a);
31 /*@manual darray
32 Appends ''p'' to the dynamic array ''a''.
34 void darray_append(darray_t a, typ_t p);
36 /*@manual darray
37 Returns the pointer at index ''i'' in the dynamic array ''a''.
39 static inline typ_t darray_at(darray_t a, int i)
41 return a->item[i];
44 typ_t darray_at_test(darray_ptr a, int (*test)(typ_t));
46 int darray_index_of(darray_ptr a, typ_t p);
47 int darray_index_of_test(darray_ptr a, int (*test)(typ_t));
48 void darray_remove(darray_ptr a, typ_t p);
49 typ_t darray_remove_last(darray_ptr a);
50 void darray_remove_test(darray_ptr a, int (*test)(typ_t));
51 void darray_swap(darray_ptr a, int i, int j);
53 /*@manual darray
54 Removes the pointer at index ''i'' in the dynamic array ''a''.
56 void darray_remove_index(darray_ptr a, int n);
57 void darray_copy(darray_ptr dst, darray_ptr src);
58 void darray_remove_all(darray_ptr d);
59 void darray_forall(darray_t a, void (*func)(typ_t));
61 /*@manual darray
62 Returns the number of pointers held in ''a''.
64 static inline int darray_count(const darray_ptr a) { return a->count; }
66 static inline typ_t *darray_raw(const darray_ptr a) { return a->item; }
68 static inline int darray_set_count(darray_ptr a, int n) { return a->count = n; }
70 static inline int darray_is_empty(const darray_ptr a) { return !a->count; }
72 static inline typ_t darray_first(darray_t a) { return a->item[0]; }
74 static inline typ_t darray_last(darray_t a) { return a->item[a->count - 1]; }
76 void darray_qsort(darray_t a, int (*compar)(const void *, const void *));