array: add function for removing elements
This commit is contained in:
parent
f99b2f633b
commit
caf82b1a45
2 changed files with 18 additions and 0 deletions
15
array.c
15
array.c
|
@ -116,6 +116,21 @@ ARR_AppendElement(ARR_Instance array, void *element)
|
|||
memcpy(e, element, array->elem_size);
|
||||
}
|
||||
|
||||
void
|
||||
ARR_RemoveElement(ARR_Instance array, unsigned int index)
|
||||
{
|
||||
void *e, *l;
|
||||
|
||||
e = ARR_GetElement(array, index);
|
||||
l = ARR_GetElement(array, array->used - 1);
|
||||
|
||||
if (e < l)
|
||||
memmove(e, (char *)e + array->elem_size, (char *)l - (char *)e);
|
||||
array->used--;
|
||||
|
||||
realloc_array(array, array->used);
|
||||
}
|
||||
|
||||
void
|
||||
ARR_SetSize(ARR_Instance array, unsigned int size)
|
||||
{
|
||||
|
|
3
array.h
3
array.h
|
@ -47,6 +47,9 @@ extern void *ARR_GetElements(ARR_Instance array);
|
|||
/* Add a new element to the end of the array */
|
||||
extern void ARR_AppendElement(ARR_Instance array, void *element);
|
||||
|
||||
/* Remove element with given index */
|
||||
extern void ARR_RemoveElement(ARR_Instance array, unsigned int index);
|
||||
|
||||
/* Set the size of the array */
|
||||
extern void ARR_SetSize(ARR_Instance array, unsigned int size);
|
||||
|
||||
|
|
Loading…
Reference in a new issue