eina/array: added eina_array_find() api to eina_array.
authorsub.mohanty@samsung.com <sub.mohanty@samsung.com>
Mon, 26 Aug 2019 12:06:51 +0000 (08:06 -0400)
committerHosang Kim <hosang12.kim@samsung.com>
Wed, 28 Aug 2019 04:49:38 +0000 (13:49 +0900)
Summary: updated test suite for testing the api.

Reviewers: zmike, Hermet, cedric, segfaultxavi

Reviewed By: zmike, segfaultxavi

Subscribers: segfaultxavi, ProhtMeyhet, cedric, #reviewers, #committers

Tags: #efl

Differential Revision: https://phab.enlightenment.org/D9734

src/lib/eina/eina_array.h
src/lib/eina/eina_inline_array.x
src/tests/eina/eina_test_array.c

index 09d8290..c6e62ad 100644 (file)
@@ -412,6 +412,23 @@ static inline unsigned int eina_array_count_get(const Eina_Array *array) EINA_AR
 static inline unsigned int eina_array_count(const Eina_Array *array) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
 
 /**
+ * @brief Search for the given data in an array.
+ *
+ * @param[in] array The array.
+ * @param[in] data need to be found.
+ * @param[out] out_idx The position of the data in the array if found.
+ * @return EINA_TRUE if found otherwise returns EINA_FALSE.
+ *
+ * This function searches for the data pointer @p data inside @p array, returning @c EINA_TRUE if found.
+ * The exact position where the pointer is found can be retrieved through @p out_idx.
+ * Please note that only the pointer is compared, not the actual data pointed by it.
+ *
+ * @since 1.23
+ */
+static inline Eina_Bool  eina_array_find(const Eina_Array *array,
+                                         const void       *data,
+                                         unsigned int     *out_idx) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
+/**
  * @brief Gets a new iterator associated with an array.
  *
  * @param[in] array The array.
index 40a6e0d..f3c11f6 100644 (file)
@@ -93,6 +93,24 @@ eina_array_count(const Eina_Array *array)
 }
 
 static inline Eina_Bool
+eina_array_find(const Eina_Array *array, const void *data, unsigned int *out_idx)
+{
+   if (!array) return EINA_FALSE;
+
+   unsigned int i = 0;
+   for (; i < array->count; i++)
+     {
+        if (array->data[i] == data)
+          {
+             if (out_idx) *out_idx = i;
+             
+             return EINA_TRUE;
+          }
+     }
+   return EINA_FALSE;
+}
+
+static inline Eina_Bool
 eina_array_foreach(Eina_Array *array, Eina_Each_Cb cb, void *fdata)
 {
    void *data;
index c8c9764..fbe7966 100644 (file)
@@ -174,10 +174,39 @@ EFL_START_TEST(eina_array_remove_stuff)
 }
 EFL_END_TEST
 
+EFL_START_TEST(eina_array_find_test)
+{
+   Eina_Array sea;
+   unsigned int i;
+   unsigned int out = 0;
+
+   fail_if(eina_array_find(NULL, (void*)1, NULL) != EINA_FALSE);
+
+   eina_array_step_set(&sea, sizeof(sea), 5);
+
+   for (i =1 ; i < 10 ; i++)
+     eina_array_push(&sea, (void*)i);
+
+   fail_if(eina_array_find(&sea, (void*)15, NULL) != EINA_FALSE);
+
+   fail_if(eina_array_find(&sea, (void*)5, NULL) != EINA_TRUE);
+   fail_if(eina_array_find(&sea, (void*)6, &out) != EINA_TRUE);
+   fail_if(out != 5);
+
+   eina_array_data_set(&sea, 7, (void*)99);
+   fail_if(eina_array_find(&sea, (void*)99, &out) != EINA_TRUE);
+   fail_if(out != 7);
+
+   eina_array_flush(&sea);
+
+ }
+
+EFL_END_TEST
 void
 eina_test_array(TCase *tc)
 {
    tcase_add_test(tc, eina_array_simple);
    tcase_add_test(tc, eina_array_static);
    tcase_add_test(tc, eina_array_remove_stuff);
+   tcase_add_test(tc, eina_array_find_test);
 }