From affe1503182be3eb440302aace31b001536c829b Mon Sep 17 00:00:00 2001 From: rephorm Date: Thu, 28 Dec 2006 03:30:11 +0000 Subject: [PATCH] add ecore_list_find() to find a data node via a compare callback SVN revision: 27578 --- legacy/ecore/src/lib/ecore/Ecore_Data.h | 2 ++ legacy/ecore/src/lib/ecore/ecore_list.c | 30 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/legacy/ecore/src/lib/ecore/Ecore_Data.h b/legacy/ecore/src/lib/ecore/Ecore_Data.h index 20717267b8..a6b4096985 100644 --- a/legacy/ecore/src/lib/ecore/Ecore_Data.h +++ b/legacy/ecore/src/lib/ecore/Ecore_Data.h @@ -113,6 +113,8 @@ extern "C" { /* Traversing the list and returning data */ EAPI void *ecore_list_next(Ecore_List * list); + EAPI void *ecore_list_find(Ecore_List *list, Ecore_Compare_Cb function, + void *user_data); /* Check to see if there is any data in the list */ EAPI int ecore_list_is_empty(Ecore_List * list); diff --git a/legacy/ecore/src/lib/ecore/ecore_list.c b/legacy/ecore/src/lib/ecore/ecore_list.c index d4e84e9d90..0fbf755fc4 100644 --- a/legacy/ecore/src/lib/ecore/ecore_list.c +++ b/legacy/ecore/src/lib/ecore/ecore_list.c @@ -25,6 +25,7 @@ static void *_ecore_list_goto_index(Ecore_List *list, int index); /* Iterative function */ static int _ecore_list_for_each(Ecore_List *list, Ecore_For_Each function, void *user_data); +static void *_ecore_list_find(Ecore_List *list, Ecore_Compare_Cb function, void *user_data); /* Private double linked list functions */ static void *_ecore_dlist_previous(Ecore_DList * list); @@ -1020,6 +1021,35 @@ _ecore_list_for_each(Ecore_List *list, Ecore_For_Each function, void *user_data) return TRUE; } +/** + * Find data in @p list using the compare function @p func + * @param list The list. + * @param function The function to test each node of @p list with + * @param user_data Data to match against (used by @p function) + * @return the first matching data node, or NULL if none match + */ +EAPI void * +ecore_list_find(Ecore_List *list, Ecore_Compare_Cb function, void *user_data) +{ + CHECK_PARAM_POINTER_RETURN("list", list, NULL); + + return _ecore_list_find(list, function, user_data); +} + +/* The real meat of finding a node via a compare cb */ +static void * +_ecore_list_find(Ecore_List *list, Ecore_Compare_Cb function, void *user_data) +{ + void *value; + if (!list || !function) return NULL; + + _ecore_list_goto_first(list); + while ((value = _ecore_list_next(list)) != NULL) + if (!function(value, user_data)) return value; + + return NULL; +} + /* Initialize a node to starting values */ EAPI int ecore_list_node_init(Ecore_List_Node *node)