Added list shrinking.

This commit is contained in:
Scott Anderson 2017-05-10 13:11:49 +12:00
parent 4e27b1e726
commit 8429e871f8

View file

@ -88,6 +88,21 @@ void list_delete(list_t *list, size_t index) {
memmove(&array[index], &array[index + 1], size * (list->length - index)); memmove(&array[index], &array[index + 1], size * (list->length - index));
--list->length; --list->length;
/* We shrink very sparse lists, but only down to a certain size.
* The choice of >= 8 is somewhat arbitrary, but leaves a minimum
* size of 4 elements.
*/
if (list->length <= list->capacity / 4 && list->capacity >= 8) {
size_t new_cap = list->capacity / 2;
void *data = realloc(list->data, list->memb_size * new_cap);
if (!data) {
return;
}
list->data = data;
list->capacity = new_cap;
}
} }
void list_swap(list_t *list, size_t i1, size_t i2) { void list_swap(list_t *list, size_t i1, size_t i2) {