[vlc-commits] vlc_arrays: add ARRAY_FIND()

Romain Vimont git at videolan.org
Thu Aug 9 14:04:56 CEST 2018


vlc | branch: master | Romain Vimont <rom1v at videolabs.io> | Wed Jul 18 15:51:56 2018 +0200| [1e61ff83820de335efe881597aaa4b3572bf9db9] | committer: Thomas Guillem

vlc_arrays: add ARRAY_FIND()

TAB_FIND() was implemented, but not ARRAY_FIND(). It may be useful.

Signed-off-by: Thomas Guillem <thomas at gllm.fr>

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=1e61ff83820de335efe881597aaa4b3572bf9db9
---

 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 b548ec2dc1..bc2ae2ae6d 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 9d40a00650..82277bbbc1 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();
 }



More information about the vlc-commits mailing list