[vlc-devel] [PATCH 2/2] vlc_arrays: add ARRAY_FIND()

Romain Vimont rom1v at videolabs.io
Wed Jul 18 15:51:56 CEST 2018


TAB_FIND() was implemented, but not ARRAY_FIND(). It may be useful.
---
 include/vlc_arrays.h |  3 +++
 src/test/arrays.c    | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/include/vlc_arrays.h b/include/vlc_arrays.h
index b548ec2dc1a..bc2ae2ae6d7 100644
--- a/include/vlc_arrays.h
+++ b/include/vlc_arrays.h
@@ -224,6 +224,9 @@ static inline void *realloc_or_free( void *p, size_t sz )
     }                                                                       \
 }
 
+#define ARRAY_FIND(array, p, idx)                                           \
+  TAB_FIND((array).i_size, (array).p_elems, p, idx)
+
 #define ARRAY_REMOVE(array,pos)                                             \
   do {                                                                      \
     if( (array).i_size - (pos) - 1 )                                        \
diff --git a/src/test/arrays.c b/src/test/arrays.c
index 9d40a00650d..82277bbbc12 100644
--- a/src/test/arrays.c
+++ b/src/test/arrays.c
@@ -77,6 +77,46 @@ static void test_array_foreach(void)
     ARRAY_RESET(array);
 }
 
+static void test_array_find(void)
+{
+    DECL_ARRAY(int) array;
+    ARRAY_INIT(array);
+
+    ARRAY_APPEND(array, 17);
+    ARRAY_APPEND(array, 52);
+    ARRAY_APPEND(array, 26);
+    ARRAY_APPEND(array, 13);
+    ARRAY_APPEND(array, 40);
+    ARRAY_APPEND(array, 20);
+    ARRAY_APPEND(array, 10);
+    ARRAY_APPEND(array, 5);
+
+    int index;
+
+    ARRAY_FIND(array, 17, index);
+    assert(index == 0);
+
+    ARRAY_FIND(array, 52, index);
+    assert(index == 1);
+
+    ARRAY_FIND(array, 26, index);
+    assert(index == 2);
+
+    ARRAY_FIND(array, 13, index);
+    assert(index == 3);
+
+    ARRAY_FIND(array, 10, index);
+    assert(index == 6);
+
+    ARRAY_FIND(array, 5, index);
+    assert(index == 7);
+
+    ARRAY_FIND(array, 14, index);
+    assert(index == -1);
+
+    ARRAY_RESET(array);
+}
+
 static void test_array_bsearch(void)
 {
     struct item {
@@ -122,5 +162,6 @@ int main(void)
 {
     test_array_insert_remove();
     test_array_foreach();
+    test_array_find();
     test_array_bsearch();
 }
-- 
2.18.0



More information about the vlc-devel mailing list