13 void darray_init(darray_ptr a
)
17 a
->item
= malloc(sizeof(typ_t
) * a
->max
);
20 darray_ptr
darray_new()
22 darray_ptr res
= malloc(sizeof(darray_t
));
27 void darray_remove_all(darray_ptr a
)
32 a
->item
= malloc(sizeof(typ_t
) * a
->max
);
35 typ_t
darray_remove_last(darray_ptr a
)
39 return(a
->item
[a
->count
]);
42 void darray_realloc(darray_ptr a
, int size
)
45 a
->item
= realloc(a
->item
, sizeof(typ_t
) * a
->max
);
48 void darray_append(darray_ptr a
, typ_t p
)
50 if (a
->count
== a
->max
) {
51 if (!a
->max
) a
->max
= max_init
;
53 a
->item
= realloc(a
->item
, sizeof(typ_t
) * a
->max
);
55 a
->item
[a
->count
] = p
;
59 int darray_index_of(darray_ptr a
, typ_t p
)
62 for (i
=0; i
<a
->count
; i
++) {
63 if (a
->item
[i
] == p
) return i
;
68 void darray_clear(darray_t a
)
75 void darray_remove_index(darray_ptr a
, int n
)
77 assert(a
->count
>= n
-1);
79 memmove(a
->item
+ n
, a
->item
+ n
+ 1, sizeof(typ_t
) * (a
->count
- n
));
82 void darray_remove(darray_ptr a
, typ_t p
)
85 for (i
=0; i
<a
->count
; i
++) {
86 if (a
->item
[i
] == p
) {
87 darray_remove_index(a
, i
);
94 int darray_index_of_test(darray_ptr a
, int (*test
)(typ_t
))
97 for (i
= 0; i
< a
->count
; i
++) if (test(a
->item
[i
])) return i
;
101 typ_t
darray_at_test(darray_ptr a
, int (*test
)(typ_t
))
104 for (i
= 0; i
< a
->count
; i
++) {
105 typ_t p
= a
->item
[i
];
106 if (test(p
)) return p
;
111 void darray_remove_test(darray_ptr a
, int (*test
)(typ_t
))
114 while ((i
= darray_index_of_test(a
, test
) >= 0)) {
115 darray_remove_index(a
, i
);
119 void darray_swap(darray_ptr a
, int i
, int j
)
121 typ_t tmp
= a
->item
[j
];
122 a
->item
[j
] = a
->item
[i
];
126 void darray_copy(darray_ptr dst
, darray_ptr src
)
128 darray_realloc(dst
, src
->count
);
129 memcpy(dst
->item
, src
->item
, src
->count
* sizeof(typ_t
));
130 dst
->count
= src
->count
;
133 void darray_forall(darray_t a
, void (*func
)(typ_t
))
135 typ_t
*item
= a
->item
;
136 const typ_t
*end
= item
+ a
->count
;
137 while (item
!= end
) {
143 void darray_qsort(darray_t a
, int (*compar
)(const void *, const void *)) {
144 qsort(a
->item
, a
->count
, sizeof(void *), compar
);