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)
15 typedef struct darray_s darray_t
[1];
16 typedef struct darray_s
*darray_ptr
;
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
);
27 Clears a dynamic array 'a'. Should be called after 'a' is no longer needed.
29 void darray_clear(darray_t a
);
32 Appends 'p' to the dynamic array 'a'.
34 void darray_append(darray_t a
, void *p
);
37 Returns the pointer at index 'i' in the dynamic array 'a'.
39 static inline void *darray_at(darray_t a
, int 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 *));
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
),
58 void darray_forall3(darray_t a
,
59 void (*func
)(void *darray_item
,
64 void darray_forall4(darray_t a
,
65 void (*func
)(void *darray_item
,
73 void *darray_at_test(darray_ptr a
, int (*test
)(void *,void *), void *scope_ptr
);
76 Returns the number of pointers held in 'a'.
78 static inline int darray_count(darray_ptr a
) {
82 static inline int darray_is_empty(darray_ptr a
) {
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__