sorted_array.[hc]: add function sorted_array_delete_by_index

pull/37/merge
YuQing 2021-11-17 20:37:18 +08:00
parent 976872192a
commit 4f29fd71eb
2 changed files with 25 additions and 0 deletions

View File

@ -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)
{

View File

@ -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: