forked from enlightenment/efl
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
This commit is contained in:
parent
c6b52d4f09
commit
5bd95c9850
|
@ -411,6 +411,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.
|
||||
*
|
||||
|
|
|
@ -92,6 +92,24 @@ eina_array_count(const Eina_Array *array)
|
|||
return array->count;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue