7 ms_init(ms a
, ms_event_fn oninsert
, ms_event_fn onremove
)
9 a
->used
= a
->cap
= a
->last
= 0;
11 a
->oninsert
= oninsert
;
12 a
->onremove
= onremove
;
19 size_t ncap
= (a
->cap
<< 1) ? : 1;
21 nitems
= malloc(ncap
* sizeof(void *));
24 memcpy(nitems
, a
->items
, a
->used
* sizeof(void *));
31 ms_append(ms a
, void *item
)
33 if (a
->used
>= a
->cap
) grow(a
);
34 if (a
->used
>= a
->cap
) return 0;
36 a
->items
[a
->used
++] = item
;
37 if (a
->oninsert
) a
->oninsert(a
, item
, a
->used
- 1);
42 ms_delete(ms a
, size_t i
)
46 if (i
>= a
->used
) return 0;
48 a
->items
[i
] = a
->items
[--a
->used
];
50 /* it has already been removed now */
51 if (a
->onremove
) a
->onremove(a
, item
, i
);
58 while (ms_delete(a
, 0));
60 ms_init(a
, a
->oninsert
, a
->onremove
);
64 ms_remove(ms a
, void *item
)
68 for (i
= 0; i
< a
->used
; i
++) {
69 if (a
->items
[i
] == item
) return ms_delete(a
, i
);
75 ms_contains(ms a
, void *item
)
79 for (i
= 0; i
< a
->used
; i
++) {
80 if (a
->items
[i
] == item
) return 1;
90 if (!a
->used
) return NULL
;
92 a
->last
= a
->last
% a
->used
;
93 item
= a
->items
[a
->last
];
94 ms_delete(a
, a
->last
);