X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=libcitadel%2Flib%2Farray.c;h=a8adb292d84ba950fbfabd59a3146f97637622ee;hb=8de23d32481fc85d2af106eabe6f6c3962d0dd28;hp=c8f433f5d5c3bdf2a070a060a56936854d8468ae;hpb=7202fdfff37c9518ff93bb65fd8d85956316ee71;p=citadel.git diff --git a/libcitadel/lib/array.c b/libcitadel/lib/array.c index c8f433f5d..a8adb292d 100644 --- a/libcitadel/lib/array.c +++ b/libcitadel/lib/array.c @@ -65,7 +65,7 @@ void array_append(Array *arr, void *new_element) { ++arr->num_elements; if (arr->num_elements > arr->num_alloc) { - arr->num_alloc = arr->num_alloc * 2; + arr->num_alloc = arr->num_alloc * 2; // whenever we exceed the buffer size, we double it. arr->the_elements = realloc(arr->the_elements, (arr->element_size * arr->num_alloc)); } @@ -96,3 +96,26 @@ int array_len(Array *arr) { void array_sort(Array *arr, int (*compar)(const void *, const void *)) { qsort(arr->the_elements, arr->num_elements, arr->element_size, compar); } + + +/* + * Delete an element from an array + */ +void array_delete_element_at(Array *arr, int index) { + + if (index >= arr->num_elements) { // If the supplied index is out of bounds, return quietly. + return; + } + + --arr->num_elements; + + if (index == arr->num_elements) { // If we deleted the element at the end, there's no more to be done. + return; + } + + memcpy( + (arr->the_elements + (index * arr->element_size)), + arr->the_elements + ((index+1) * arr->element_size), + (arr->num_elements - index) * arr->element_size + ); +}