sorted_array.[hc]: add function sorted_array_delete_by_index
parent
976872192a
commit
4f29fd71eb
|
|
@ -108,6 +108,21 @@ int sorted_array_insert(SortedArrayContext *ctx,
|
||||||
return 0;
|
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,
|
int sorted_array_delete(SortedArrayContext *ctx,
|
||||||
void *base, int *count, const void *elt)
|
void *base, int *count, const void *elt)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,16 @@ extern "C" {
|
||||||
int sorted_array_delete(SortedArrayContext *ctx,
|
int sorted_array_delete(SortedArrayContext *ctx,
|
||||||
void *base, int *count, const void *elt);
|
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
|
/** find element from the sorted array
|
||||||
* parameters:
|
* parameters:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue