diff --git a/src/sorted_array.c b/src/sorted_array.c index 7d32d45..b3ea85f 100644 --- a/src/sorted_array.c +++ b/src/sorted_array.c @@ -108,6 +108,21 @@ int sorted_array_insert(SortedArrayContext *ctx, return 0; } +void sorted_array_delete_by_index(SortedArrayContext *ctx, + void *base, int *count, const int index) +{ + int move_count; + char *start; + + start = (char *)base + ctx->element_size * index; + move_count = *count - (index + 1); + if (move_count > 0) { + memmove(start, start + ctx->element_size, + ctx->element_size * move_count); + } + (*count)--; +} + int sorted_array_delete(SortedArrayContext *ctx, void *base, int *count, const void *elt) { diff --git a/src/sorted_array.h b/src/sorted_array.h index beb6e4f..c9f2e74 100644 --- a/src/sorted_array.h +++ b/src/sorted_array.h @@ -65,6 +65,16 @@ extern "C" { int sorted_array_delete(SortedArrayContext *ctx, void *base, int *count, const void *elt); + /** delete an element by index + * parameters: + * ctx: the context for sorted array + * base: the pointer of the sorted array (the first array element) + * count: the count of the sorted array (for input and output) + * index: the element index to delete + * return: 0 for success, != 0 for error + */ + void sorted_array_delete_by_index(SortedArrayContext *ctx, + void *base, int *count, const int index); /** find element from the sorted array * parameters: