]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
array: add function for removing elements
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 1 Mar 2023 15:02:16 +0000 (16:02 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 2 Mar 2023 09:41:28 +0000 (10:41 +0100)
array.c
array.h

diff --git a/array.c b/array.c
index d70cff9c7ce35572eac4ed73c60cb4edf03ce396..b31ba561c7a074a37dd29b3dc49176dfd2a121fc 100644 (file)
--- a/array.c
+++ b/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)
 {
diff --git a/array.h b/array.h
index c812e84839417313116322548c54192ab27d54b5..f4fbddb465ea822f30519ccc2c40958890f38cf3 100644 (file)
--- a/array.h
+++ b/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);