If you don't know how to create lists see Adding elements to Eina_List.
#include <stdio.h>
#include <string.h>
int
main(int argc, char **argv)
{
(void)argc;
(void)argv;
void *data;
int cmp_result;
This is the code we have already seen to create a list. Now if we need to search the list we can do it like this:
However if searching the list multiple times it probably is better to sort the list since the sorted_search functions are much faster:
Once the list is sorted it's not a good idea to use append/prepend functions since that would add the element in the wrong place, instead elements should be added with eina_list_sorted_insert():
A noteworthy use case is adding an element to a list only if it doesn't exist already, this can accomplished by searching for the element that is closest to what is being added, and if that doesn't match add:
if (cmp_result > 0)
else if (cmp_result < 0)
- Note
- eina_list_search_sorted_near_list() will tell you not only the nearest node to what was searched for but how it compares to your term, this way it is easy to know if you have to add before or after that node.
It is sometimes useful to get a portion of the list as another list, here we take every element that comes after "boomer" and split it into "other_list":
It is also possible to add entire lists of elements using eina_list_sorted_merge():
And as always release memory and shutdown eina before ending:
The full source code can be found on the examples folder on the eina_list_02.c file.
EAPI Eina_List * eina_list_split_list(Eina_List *list, Eina_List *relative, Eina_List **right)
Splits a list into 2 lists.
Definition: eina_list.c:1316
EAPI Eina_List * eina_list_sort(Eina_List *list, unsigned int limit, Eina_Compare_Cb func)
Sorts a list according to the ordering func will return.
Definition: eina_list.c:1112
EAPI Eina_List * eina_list_search_sorted_near_list(const Eina_List *list, Eina_Compare_Cb func, const void *data, int *result_cmp)
Returns node nearest to data from the sorted list.
Definition: eina_list.c:1439
EAPI Eina_List * eina_list_append_relative_list(Eina_List *list, const void *data, Eina_List *relative)
Appends a list node to a linked list after the specified member.
Definition: eina_list.c:670
EAPI Eina_List * eina_list_append(Eina_List *list, const void *data)
Appends the given data to the given linked list.
Definition: eina_list.c:584
EAPI void * eina_list_search_sorted(const Eina_List *list, Eina_Compare_Cb func, const void *data)
Returns node data if it is in the sorted list.
Definition: eina_list.c:1541
void * data
Pointer to list element payload.
Definition: eina_list.h:319
int eina_init(void)
Initializes the Eina library.
Definition: eina_main.c:279
EAPI Eina_List * eina_list_sorted_insert(Eina_List *list, Eina_Compare_Cb func, const void *data)
Inserts a new node into a sorted list.
Definition: eina_list.c:757
int(* Eina_Compare_Cb)(const void *data1, const void *data2)
Definition: eina_types.h:548
EAPI Eina_List * eina_list_prepend_relative_list(Eina_List *list, const void *data, Eina_List *relative)
Prepends a list node to a linked list before the specified member.
Definition: eina_list.c:724
EAPI void * eina_list_search_unsorted(const Eina_List *list, Eina_Compare_Cb func, const void *data)
Returns node data if it is in the unsorted list.
Definition: eina_list.c:1565
int eina_shutdown(void)
Shuts down the Eina library.
Definition: eina_main.c:350
EAPI Eina_List * eina_list_free(Eina_List *list)
Frees an entire list and all the nodes, ignoring the data contained.
Definition: eina_list.c:823
EAPI Eina_List * eina_list_search_unsorted_list(const Eina_List *list, Eina_Compare_Cb func, const void *data)
Returns node if data is in the unsorted list.
Definition: eina_list.c:1549
Definition: eina_list.h:317
EAPI Eina_List * eina_list_search_sorted_list(const Eina_List *list, Eina_Compare_Cb func, const void *data)
Returns node if data is in the sorted list.
Definition: eina_list.c:1522
EAPI Eina_List * eina_list_sorted_merge(Eina_List *left, Eina_List *right, Eina_Compare_Cb func)
Merges two sorted list according to the ordering func will return.
Definition: eina_list.c:1362