On Tue, Jul 07, 2020 at 05:22:45PM -0500, Eric Blake wrote:
An upcoming patch wants to remove an arbitrary element from a vector.
Also, add testsuite coverage for other functions added since the original unit test was written. It's a bit awkward that the compare for VECT_search and VECT_sort differ in type, but such is life (we indeed have search code where typing the key differently is useful).
I think you should just push this kind of patch, but I have one comment below:
Signed-off-by: Eric Blake <eblake@redhat.com> --- common/utils/vector.h | 10 ++++++++++ common/utils/test-vector.c | 26 +++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-)
diff --git a/common/utils/vector.h b/common/utils/vector.h index c14644a7..880fd308 100644 --- a/common/utils/vector.h +++ b/common/utils/vector.h @@ -125,6 +125,16 @@ f (v->ptr[i]); \ } \ \ + /* Remove i'th element. i=0 => beginning i=size-1 => end */ \ + static inline int \ + name##_remove (name *v, size_t i) \ + { \ + if (i >= v->size) return -1; \ + memmove (&v->ptr[i], &v->ptr[i+1], (v->size-i) * sizeof (type)); \ + v->size--; \ + return 0; \ + } \
Do we need to have this function return an error indication? I would think that the check should be replaced with an assert. If it happens it's an internal error. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html