diff options
author | sub.mohanty@samsung.com <sub.mohanty@samsung.com> | 2019-08-26 08:06:51 -0400 |
---|---|---|
committer | Mike Blumenkrantz <zmike@samsung.com> | 2019-08-26 08:31:08 -0400 |
commit | 5bd95c9850df0fa914dde04761065515b9d3f7e4 (patch) | |
tree | 1fffb3ff4e1cf24361f16db00333d068c86b85bb | |
parent | c6b52d4f09841e24f03150c457705ffe57b320db (diff) | |
download | efl-5bd95c9850df0fa914dde04761065515b9d3f7e4.tar.gz |
eina/array: added eina_array_find() api to eina_array.
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
-rw-r--r-- | src/lib/eina/eina_array.h | 17 | ||||
-rw-r--r-- | src/lib/eina/eina_inline_array.x | 18 | ||||
-rw-r--r-- | src/tests/eina/eina_test_array.c | 29 |
3 files changed, 64 insertions, 0 deletions
diff --git a/src/lib/eina/eina_array.h b/src/lib/eina/eina_array.h index 09d8290352..c6e62ad142 100644 --- a/src/lib/eina/eina_array.h +++ b/src/lib/eina/eina_array.h @@ -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. diff --git a/src/lib/eina/eina_inline_array.x b/src/lib/eina/eina_inline_array.x index 40a6e0dcc7..f3c11f67a4 100644 --- a/src/lib/eina/eina_inline_array.x +++ b/src/lib/eina/eina_inline_array.x @@ -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; diff --git a/src/tests/eina/eina_test_array.c b/src/tests/eina/eina_test_array.c index c8c9764a5f..fbe7966af0 100644 --- a/src/tests/eina/eina_test_array.c +++ b/src/tests/eina/eina_test_array.c @@ -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); } |