Add Apache License version 2.0.
[pbc.git] / misc / darray.h
blobecbd04ad2d76dd9329fe2d3d6c8489767b964b2f
1 // darray = "dynamic array"
2 // A linked-list implementation using C arrays.
4 #ifndef __PBC_DARRAY_H__
5 #define __PBC_DARRAY_H__
7 #pragma GCC visibility push(hidden)
9 struct darray_s {
10 void **item;
11 int count;
12 int max;
15 typedef struct darray_s darray_t[1];
16 typedef struct darray_s *darray_ptr;
18 /*@manual darray
19 Initialize a dynamic array 'a'. Must be called before 'a' is used.
21 void darray_init(darray_t a);
22 darray_ptr darray_new(void);
24 void darray_free(darray_ptr a);
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, void *p);
36 /*@manual darray
37 Returns the pointer at index 'i' in the dynamic array 'a'.
39 static inline void *darray_at(darray_t a, int i) {
40 return a->item[i];
43 int darray_index_of(darray_ptr a, void *p);
44 void darray_remove(darray_ptr a, void *p);
45 void darray_remove_last(darray_ptr a);
46 void darray_remove_with_test(darray_ptr a, int (*test)(void *));
48 /*@manual darray
49 Removes the pointer at index 'i' in the dynamic array 'a'.
51 void darray_remove_index(darray_ptr a, int n);
52 void darray_copy(darray_ptr dst, darray_ptr src);
53 void darray_remove_all(darray_ptr d);
54 void darray_forall(darray_t a, void (*func)(void *));
55 void darray_forall2(darray_t a,
56 void (*func)(void *darray_item, void *scope_ptr),
57 void *scope_ptr);
58 void darray_forall3(darray_t a,
59 void (*func)(void *darray_item,
60 void *scope_ptr1,
61 void *scope_ptr2),
62 void *scope_ptr1,
63 void *scope_ptr2);
64 void darray_forall4(darray_t a,
65 void (*func)(void *darray_item,
66 void *scope_ptr1,
67 void *scope_ptr2,
68 void *scope_ptr3),
69 void *scope_ptr1,
70 void *scope_ptr2,
71 void *scope_ptr3);
73 void *darray_at_test(darray_ptr a, int (*test)(void *,void *), void *scope_ptr);
75 /*@manual darray
76 Returns the number of pointers held in 'a'.
78 static inline int darray_count(darray_ptr a) {
79 return a->count;
82 static inline int darray_is_empty(darray_ptr a) {
83 return !a->count;
86 static inline void *darray_last(darray_t a) {
87 return a->item[a->count - 1];
90 #pragma GCC visibility pop
92 #endif //__PBC_DARRAY_H__