List
[Containers]
These functions provide double linked list management.
More...Data Structures | |
struct | _Eina_List |
Type for a generic double linked list. More... | |
struct | _Eina_List_Accounting |
Cache used to store the last element of a list and the number of elements, for fast access. More... | |
Defines | |
#define | EINA_LIST_FOREACH(list, l, data) |
Macro to iterate over a list. | |
#define | EINA_LIST_REVERSE_FOREACH(list, l, data) |
Macro to iterate over a list in the reverse order. | |
#define | EINA_LIST_FOREACH_SAFE(list, l, l_next, data) |
Macro to iterate over a list with support for node deletion. | |
#define | EINA_LIST_REVERSE_FOREACH_SAFE(list, l, l_prev, data) |
Macro to iterate over a list in the reverse order with support for deletion. | |
#define | EINA_LIST_FREE(list, data) |
Macro to remove each list node while having access to each node's data. | |
Typedefs | |
typedef struct _Eina_List | Eina_List |
Type for a generic double linked list. | |
typedef struct _Eina_List_Accounting | Eina_List_Accounting |
Cache used to store the last element of a list and the number of elements, for fast access. | |
Functions | |
Eina_List * | eina_list_append (Eina_List *list, const void *data) |
Append the given data to the given linked list. | |
Eina_List * | eina_list_prepend (Eina_List *list, const void *data) |
Prepends the given data to the given linked list. | |
Eina_List * | eina_list_append_relative (Eina_List *list, const void *data, const void *relative) |
Insert the given data into the given linked list after the specified data. | |
Eina_List * | eina_list_append_relative_list (Eina_List *list, const void *data, Eina_List *relative) |
Append a list node to a linked list after the specified member. | |
Eina_List * | eina_list_prepend_relative (Eina_List *list, const void *data, const void *relative) |
Prepend a data pointer to a linked list before the specified member. | |
Eina_List * | eina_list_prepend_relative_list (Eina_List *list, const void *data, Eina_List *relative) |
Prepend a list node to a linked list before the specified member. | |
Eina_List * | eina_list_sorted_insert (Eina_List *list, Eina_Compare_Cb func, const void *data) |
Insert a new node into a sorted list. | |
Eina_List * | eina_list_remove (Eina_List *list, const void *data) |
Remove the first instance of the specified data from the given list. | |
Eina_List * | eina_list_remove_list (Eina_List *list, Eina_List *remove_list) |
Remove the specified list node. | |
Eina_List * | eina_list_promote_list (Eina_List *list, Eina_List *move_list) |
Move the specified data to the head of the list. | |
Eina_List * | eina_list_demote_list (Eina_List *list, Eina_List *move_list) |
Move the specified data to the tail of the list. | |
void * | eina_list_data_find (const Eina_List *list, const void *data) |
Find a member of a list and return the member. | |
Eina_List * | eina_list_data_find_list (const Eina_List *list, const void *data) |
Find a member of a list and return the list node containing that member. | |
Eina_Bool | eina_list_move (Eina_List **to, Eina_List **from, void *data) |
Move a data pointer from one list to another. | |
Eina_Bool | eina_list_move_list (Eina_List **to, Eina_List **from, Eina_List *data) |
Move a list node from one list to another. | |
Eina_List * | eina_list_free (Eina_List *list) |
Free an entire list and all the nodes, ignoring the data contained. | |
void * | eina_list_nth (const Eina_List *list, unsigned int n) |
Get the nth member's data pointer in a list. | |
Eina_List * | eina_list_nth_list (const Eina_List *list, unsigned int n) |
Get the nth member's list node in a list. | |
Eina_List * | eina_list_reverse (Eina_List *list) |
Reverse all the elements in the list. | |
Eina_List * | eina_list_reverse_clone (const Eina_List *list) |
Clone (copy) all the elements in the list in reverse order. | |
Eina_List * | eina_list_clone (const Eina_List *list) |
Clone (copy) all the elements in the list in exactly same order. | |
Eina_List * | eina_list_sort (Eina_List *list, unsigned int limit, Eina_Compare_Cb func) |
Sort a list according to the ordering func will return. | |
Eina_List * | eina_list_merge (Eina_List *left, Eina_List *right) |
Merge two list. | |
Eina_List * | eina_list_sorted_merge (Eina_List *left, Eina_List *right, Eina_Compare_Cb func) |
Merge two sorted list according to the ordering func will return. | |
Eina_List * | eina_list_split_list (Eina_List *list, Eina_List *relative, Eina_List **right) |
Split a list into 2 lists. | |
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 is in the sorted list. | |
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. | |
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. | |
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. | |
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. | |
static Eina_List * | eina_list_last (const Eina_List *list) |
Get the last list node in the list. | |
static Eina_List * | eina_list_next (const Eina_List *list) |
Get the next list node after the specified list node. | |
static Eina_List * | eina_list_prev (const Eina_List *list) |
Get the previous list node before the specified list node. | |
static void * | eina_list_data_get (const Eina_List *list) |
Get the list node data member. | |
static void * | eina_list_data_set (Eina_List *list, const void *data) |
Set the list node data member. | |
static unsigned int | eina_list_count (const Eina_List *list) |
Get the count of the number of items in a list. | |
Eina_Iterator * | eina_list_iterator_new (const Eina_List *list) |
Returned a new iterator associated to a list. | |
Eina_Iterator * | eina_list_iterator_reversed_new (const Eina_List *list) |
Returned a new reversed iterator associated to a list. | |
Eina_Accessor * | eina_list_accessor_new (const Eina_List *list) |
Returned a new accessor associated to a list. |
Detailed Description
These functions provide double linked list management.Eina_List is a doubly linked list. It can store data of any type in the form of void pointers. It has convenience functions to do all the common operations which means it should rarely if ever be necessary to directly access the struct's fields. Nevertheless it can be useful to understand the inner workings of the data structure being used.
Eina_List nodes keep references to the previous node, the next node, its data and to an accounting structure.
Eina_List_Accounting is used to improve the performance of some functions. It is private and should not be modified. It contains a reference to the end of the list and the number of elements in the list.
- Note:
- Every function that modifies the contents of the list returns a pointer to the head of the list and it is essential that this be pointer be used in any future references to the list.
- Warning:
- You must always use the pointer to the first element of the list as the list!
You must never use a pointer to an element in the middle of the list as the list!
- Adding elements to Eina_List
- Sorting Eina_List elements
- Reordering Eina_List elements
- Eina_List and memory allocation
Define Documentation
#define EINA_LIST_FOREACH | ( | list, | |||
l, | |||||
data | ) |
Value:
for (l = list, \ data = eina_list_data_get(l); \ l; \ l = eina_list_next(l), \ data = eina_list_data_get(l))
- Parameters:
-
list The list to iterate over. l A list that is used as an iterator and points to the current node. data Current item's data.
list
from the first element to the last. data
is the data related to the current element. l
is an Eina_List used as the list iterator.
The following diagram ilustrates this macro iterating over a list of four elements("one", "two", "three" and "four"):
Full-size
It can be used to free list data, as in the following example:
Eina_List *list; Eina_List *l; char *data; // list is already filled, // its elements are just duplicated strings, // EINA_LIST_FOREACH will be used to free those strings EINA_LIST_FOREACH(list, l, data) free(data); eina_list_free(list);
- Note:
- This is not the optimal way to release memory allocated to a list, since it iterates over the list twice. For an optimized algorithm, use EINA_LIST_FREE().
- Warning:
list
must be a pointer to the first element of the list.Be careful when deleting list nodes. If you remove the current node and continue iterating, the code will fail because the macro will not be able to get the next node. Notice that it's OK to remove any node if you stop the loop after that. For destructive operations such as this, consider using EINA_LIST_FOREACH_SAFE().
Referenced by eina_list_append_relative(), eina_list_clone(), eina_list_data_find_list(), eina_list_prepend_relative(), eina_list_reverse_clone(), and eina_list_search_unsorted_list().
#define EINA_LIST_REVERSE_FOREACH | ( | list, | |||
l, | |||||
data | ) |
Value:
for (l = eina_list_last(list), \ data = eina_list_data_get(l); \ l; \ l = eina_list_prev(l), \ data = eina_list_data_get(l))
- Parameters:
-
list The list to iterate over. l A list that is used as an iterator and points to the current node. data Current item's data.
data
is the data related to the current element, while l
is an Eina_List that is used as the list iterator.
The following diagram ilustrates this macro iterating over a list of four elements("one", "two", "three" and "four"):
Full-size
It can be used to free list data, as in the following example:
Eina_List *list; Eina_List *l; char *data; // list is already filled, // its elements are just duplicated strings, // EINA_LIST_REVERSE_FOREACH will be used to free those strings EINA_LIST_REVERSE_FOREACH(list, l, data) free(data); eina_list_free(list);
- Note:
- This is not the optimal way to release memory allocated to a list, since it iterates over the list twice. For an optimized algorithm, use EINA_LIST_FREE().
- Warning:
list
must be a pointer to the first element of the list.Be careful when deleting list nodes. If you remove the current node and continue iterating, the code will fail because the macro will not be able to get the next node. Notice that it's OK to remove any node if you stop the loop after that. For destructive operations such as this, consider using EINA_LIST_REVERSE_FOREACH_SAFE().
#define EINA_LIST_FOREACH_SAFE | ( | list, | |||
l, | |||||
l_next, | |||||
data | ) |
Value:
for (l = list, \ l_next = eina_list_next(l), \ data = eina_list_data_get(l); \ l; \ l = l_next, \ l_next = eina_list_next(l), \ data = eina_list_data_get(l))
- Parameters:
-
list The list to iterate over. l A list that is used as an iterator and points to the current node. l_next A list that is used as an iterator and points to the next node. data Current item's data.
list
from the first element to the last. data
is the data related to the current element. l
is an Eina_List used as the list iterator.
Since this macro stores a pointer to the next list node in l_next
, deleting the current node and continuing looping is safe.
The following diagram ilustrates this macro iterating over a list of four elements("one", "two", "three" and "four"):
Full-size
This macro can be used to free list nodes, as in the following example:
Eina_List *list; Eina_List *l; Eina_List *l_next; char *data; // list is already filled, // its elements are just duplicated strings, // EINA_LIST_FOREACH_SAFE will be used to free elements that match "key". EINA_LIST_FOREACH_SAFE(list, l, l_next, data) if (strcmp(data, "key") == 0) { free(data); list = eina_list_remove_list(list, l); }
- Warning:
list
must be a pointer to the first element of the list.
#define EINA_LIST_REVERSE_FOREACH_SAFE | ( | list, | |||
l, | |||||
l_prev, | |||||
data | ) |
Value:
for (l = eina_list_last(list), \ l_prev = eina_list_prev(l), \ data = eina_list_data_get(l); \ l; \ l = l_prev, \ l_prev = eina_list_prev(l), \ data = eina_list_data_get(l))
- Parameters:
-
list The list to iterate over. l A list that is used as an iterator and points to the current node. l_prev A list that is used as an iterator and points to the previous node. data Current item's data.
data
is the data related to the current element, while l
is an Eina_List that is used as the list iterator.
Since this macro stores a pointer to the previous list node in l_prev
, deleting the current node and continuing looping is safe.
The following diagram ilustrates this macro iterating over a list of four elements("one", "two", "three" and "four"):
Full-size
This macro can be used to free list nodes, as in the following example:
Eina_List *list; Eina_List *l; Eina_List *l_prev; char *data; // list is already filled, // its elements are just duplicated strings, // EINA_LIST_REVERSE_FOREACH_SAFE will be used to free elements that match "key". EINA_LIST_REVERSE_FOREACH_SAFE(list, l, l_prev, data) if (strcmp(data, "key") == 0) { free(data); list = eina_list_remove_list(list, l); }
- Warning:
list
must be a pointer to the first element of the list.
#define EINA_LIST_FREE | ( | list, | |||
data | ) |
Value:
for (data = eina_list_data_get(list); \ list; \ list = eina_list_remove_list(list, list), \ data = eina_list_data_get(list))
- Parameters:
-
list The list that will be cleared. data Current node's data.
data
.
The following diagram ilustrates this macro iterating over a list of four elements("one", "two", "three" and "four"):
Full-size
If you do not need to release node data, it is easier to call eina_list_free().
Eina_List *list; char *data; // list is already filled, // its elements are just duplicated strings, EINA_LIST_FREE(list, data) free(data);
- Warning:
list
must be a pointer to the first element of the list.
- See also:
- eina_list_free()
Referenced by eina_benchmark_free().
Function Documentation
Append the given data to the given linked list.
- Parameters:
-
list The given list. data The data to append.
- Returns:
- A list pointer.
data
to list
. If list
is NULL
, a new list is returned. On success, a new list pointer that should be used in place of the one given to this function is returned. Otherwise, the old pointer is returned.The following example code demonstrates how to ensure that the given data has been successfully appended.
Eina_List *list = NULL; extern void *my_data; list = eina_list_append(list, my_data); if (eina_error_get()) { fprintf(stderr, "ERROR: Memory is low. List allocation failed.\n"); exit(-1); }
- Warning:
list
must be a pointer to the first element of the list(or NULL).
References _Eina_List::accounting, _Eina_List::data, eina_error_set(), _Eina_List_Accounting::last, _Eina_List::next, and _Eina_List::prev.
Referenced by eina_benchmark_run(), eina_list_append_relative(), eina_list_append_relative_list(), eina_list_clone(), eina_list_move(), eina_list_move_list(), eina_list_sorted_insert(), and eina_rectangle_pool_new().
Prepends the given data to the given linked list.
- Parameters:
-
list The given list. data The data to prepend.
- Returns:
- A list pointer.
data
to list
. If list
is NULL
, a new list is returned. On success, a new list pointer that should be used in place of the one given to this function is returned. Otherwise, the old pointer is returned.The following example code demonstrates how to ensure that the given data has been successfully prepended.
Example:
Eina_List *list = NULL; extern void *my_data; list = eina_list_prepend(list, my_data); if (eina_error_get()) { fprintf(stderr, "ERROR: Memory is low. List allocation failed.\n"); exit(-1); }
- Warning:
list
must be a pointer to the first element of the list.
References _Eina_List::data, eina_error_set(), _Eina_List::next, and _Eina_List::prev.
Referenced by eina_list_prepend_relative(), eina_list_prepend_relative_list(), and eina_list_reverse_clone().
Insert the given data into the given linked list after the specified data.
- Parameters:
-
list The given linked list. data The data to insert. relative The data to insert after.
- Returns:
- A list pointer.
data
to list
after relative
. If relative
is not in the list, data
is appended to the end of the list. If list
is NULL
, a new list is returned. If there are multiple instances of relative
in the list, data
is inserted after the first instance.On success, a new list pointer that should be used in place of the one given to this function is returned. Otherwise, the old pointer is returned.The following example code demonstrates how to ensure that the given data has been successfully inserted.
Eina_List *list = NULL; extern void *my_data; extern void *relative_member; list = eina_list_append(list, relative_member); if (eina_error_get()) { fprintf(stderr, "ERROR: Memory is low. List allocation failed.\n"); exit(-1); } list = eina_list_append_relative(list, my_data, relative_member); if (eina_error_get()) { fprintf(stderr, "ERROR: Memory is low. List allocation failed.\n"); exit(-1); }
- Warning:
list
must be a pointer to the first element of the list.
References eina_list_append(), eina_list_append_relative_list(), and EINA_LIST_FOREACH.
Eina_List* eina_list_append_relative_list | ( | Eina_List * | list, | |
const void * | data, | |||
Eina_List * | relative | |||
) |
Append a list node to a linked list after the specified member.
- Parameters:
-
list The given linked list. data The data to insert. relative The list node to insert after.
- Returns:
- A list pointer.
data
to list
after the list node relative
. If list
or relative
are NULL
, data
is just appended to list
using eina_list_append(). If list
is NULL
, a new list is returned. If there are multiple instances of relative
in the list, data
is inserted after the first instance. On success, a new list pointer that should be used in place of the one given to this function is returned. Otherwise, the old pointer is returned.
- Warning:
list
must be a pointer to the first element of the list.
References _Eina_List::accounting, _Eina_List::data, eina_error_set(), eina_list_append(), _Eina_List_Accounting::last, _Eina_List::next, and _Eina_List::prev.
Referenced by eina_list_append_relative(), and eina_list_sorted_insert().
Eina_List* eina_list_prepend_relative | ( | Eina_List * | list, | |
const void * | data, | |||
const void * | relative | |||
) |
Prepend a data pointer to a linked list before the specified member.
- Parameters:
-
list The given linked list. data The data to insert. relative The data to insert before.
- Returns:
- A list pointer.
data
to list
before relative
. If relative
is not in the list, data
is prepended to the list with eina_list_prepend(). If list
is NULL
, a new list is returned. If there are multiple instances of relative
in the list, data
is inserted before the first instance. On success, a new list pointer that should be used in place of the one given to this function is returned. Otherwise, the old pointer is returned.The following code example demonstrates how to ensure that the given data has been successfully inserted.
Eina_List *list = NULL; extern void *my_data; extern void *relative_member; list = eina_list_append(list, relative_member); if (eina_error_get_error()) { fprintf(stderr, "ERROR: Memory is low. List allocation failed.\n"); exit(-1); } list = eina_list_prepend_relative(list, my_data, relative_member); if (eina_error_get()) { fprintf(stderr, "ERROR: Memory is low. List allocation failed.\n"); exit(-1); }
- Warning:
list
must be a pointer to the first element of the list.
References EINA_LIST_FOREACH, eina_list_prepend(), and eina_list_prepend_relative_list().
Eina_List* eina_list_prepend_relative_list | ( | Eina_List * | list, | |
const void * | data, | |||
Eina_List * | relative | |||
) |
Prepend a list node to a linked list before the specified member.
- Parameters:
-
list The given linked list. data The data to insert. relative The list node to insert before.
- Returns:
- A list pointer.
data
to list
before the list node relative
. If list
or relative
are NULL
, data
is just prepended to list
using eina_list_prepend(). If list
is NULL
, a new list is returned. If there are multiple instances of relative
in the list, data
is inserted before the first instance. On success, a new list pointer that should be used in place of the one given to this function is returned. Otherwise, the old pointer is returned.
- Warning:
list
must be a pointer to the first element of the list.
References _Eina_List::data, eina_error_set(), eina_list_prepend(), _Eina_List::next, and _Eina_List::prev.
Referenced by eina_list_prepend_relative(), and eina_list_sorted_insert().
Eina_List* eina_list_sorted_insert | ( | Eina_List * | list, | |
Eina_Compare_Cb | func, | |||
const void * | data | |||
) |
Insert a new node into a sorted list.
- Parameters:
-
list The given linked list, must be sorted. func The function called for the sort. data The data to insert sorted.
- Returns:
- A list pointer.
list
is NULLL
, a new list is returned. On success, a new list pointer that should be used in place of the one given to this function is returned. Otherwise, the old pointer is returned. See eina_error_get().
- Note:
- O(log2(n)) comparisons (calls to
func
) average/worst case performance as it uses eina_list_search_sorted_near_list() and thus is bounded to that. As said in eina_list_search_sorted_near_list(), lists do not have O(1) access time, so walking to the correct node can be costly, consider worst case to be almost O(n) pointer dereference (list walk).
- Warning:
list
must be a pointer to the first element of the list.
References eina_list_append(), eina_list_append_relative_list(), eina_list_prepend_relative_list(), and eina_list_search_sorted_near_list().
Remove the first instance of the specified data from the given list.
- Parameters:
-
list The given list. data The specified data.
- Returns:
- A list pointer.
data
from list
. If the specified data is not in the given list (tihis include the case where data
is NULL
), nothing is done. If list
is NULL
, NULL
is returned, otherwise a new list pointer that should be used in place of the one passed to this function.
- Warning:
list
must be a pointer to the first element of the list.
References eina_list_data_find_list(), and eina_list_remove_list().
Remove the specified list node.
- Parameters:
-
list The given linked list. remove_list The list node which is to be removed.
- Returns:
- A list pointer.
remove_list
from list
and frees the list node structure remove_list
. If list
is NULL
, this function returns NULL
. If remove_list
is NULL
, it returns list
, otherwise, a new list pointer that should be used in place of the one passed to this function.The following code gives an example (notice we use EINA_LIST_FOREACH instead of EINA_LIST_FOREACH_SAFE because we stop the loop after removing the current node).
extern Eina_List *list; Eina_List *l; extern void *my_data; void *data EINA_LIST_FOREACH(list, l, data) { if (data == my_data) { list = eina_list_remove_list(list, l); break; } }
- Warning:
list
must be a pointer to the first element of the list.
References _Eina_List::accounting, _Eina_List_Accounting::last, _Eina_List::next, and _Eina_List::prev.
Referenced by eina_list_move(), eina_list_move_list(), and eina_list_remove().
Move the specified data to the head of the list.
- Parameters:
-
list The list handle to move the data. move_list The list node to move.
- Returns:
- A new list handle to replace the old one
move_list
to the front of list
. If list is NULL
, NULL
is returned. If move_list
is NULL
, list
is returned. Otherwise, a new list pointer that should be used in place of the one passed to this function.Example:
extern Eina_List *list; Eina_List *l; extern void *my_data; void *data; EINA_LIST_FOREACH(list, l, data) { if (data == my_data) { list = eina_list_promote_list(list, l); break; } }
- Warning:
list
must be a pointer to the first element of the list.
References _Eina_List::accounting, _Eina_List_Accounting::last, _Eina_List::next, and _Eina_List::prev.
Move the specified data to the tail of the list.
- Parameters:
-
list The list handle to move the data. move_list The list node to move.
- Returns:
- A new list handle to replace the old one
move_list
to the back of list
. If list is NULL
, NULL
is returned. If move_list
is NULL
, list
is returned. Otherwise, a new list pointer that should be used in place of the one passed to this function.Example:
extern Eina_List *list; Eina_List *l; extern void *my_data; void *data; EINA_LIST_FOREACH(list, l, data) { if (data == my_data) { list = eina_list_demote_list(list, l); break; } }
- Warning:
list
must be a pointer to the first element of the list.
References _Eina_List::accounting, _Eina_List_Accounting::last, _Eina_List::next, and _Eina_List::prev.
void* eina_list_data_find | ( | const Eina_List * | list, | |
const void * | data | |||
) |
Find a member of a list and return the member.
- Parameters:
-
list The list to search for a data. data The data pointer to find in the list.
- Returns:
- The found member data pointer if found,
NULL
otherwise.
list
from beginning to end for the first member whose data pointer is data
. If it is found, data
will be returned, otherwise NULL
will be returned.Example:
extern Eina_List *list; extern void *my_data; if (eina_list_data_find(list, my_data) == my_data) { printf("Found member %p\n", my_data); }
- Warning:
list
must be a pointer to the first element of the list.
References eina_list_data_find_list().
Find a member of a list and return the list node containing that member.
- Parameters:
-
list The list to search for data. data The data pointer to find in the list.
- Returns:
- The found members list node on success,
NULL
otherwise.
list
from beginning to end for the first member whose data pointer is data
. If it is found, the list node containing the specified member is returned, otherwise NULL
is returned.
- Warning:
list
must be a pointer to the first element of the list.
References EINA_LIST_FOREACH.
Referenced by eina_list_data_find(), eina_list_move(), and eina_list_remove().
Move a data pointer from one list to another.
- Parameters:
-
to The list to move the data to from The list to move from data The data to move
- Returns:
- EINA_TRUE on success, else EINA_FALSE
- Warning:
list
must be a pointer to the first element of the list.
References EINA_FALSE, eina_list_append(), eina_list_data_find_list(), eina_list_remove_list(), EINA_SAFETY_ON_NULL_RETURN_VAL, and EINA_TRUE.
Move a list node from one list to another.
- Parameters:
-
to The list to move the data to from The list to move from data The list node containing the data to move
- Returns:
- EINA_TRUE on success, else EINA_FALSE
- Warning:
list
must be a pointer to the first element of the list.
References _Eina_List::data, EINA_FALSE, eina_list_append(), eina_list_remove_list(), EINA_SAFETY_ON_NULL_RETURN_VAL, and EINA_TRUE.
Free an entire list and all the nodes, ignoring the data contained.
- Parameters:
-
list The list to free
- Returns:
- A
NULL
pointer
list
. It does not free the data of the nodes. To free them, use EINA_LIST_FREE.
References _Eina_List::next.
void* eina_list_nth | ( | const Eina_List * | list, | |
unsigned int | n | |||
) |
Get the nth member's data pointer in a list.
- Parameters:
-
list The list to get the specified member number from. n The number of the element (0 being the first).
- Returns:
- The data pointer stored in the specified element.
n
, in the list
. The first element in the array is element number 0. If the element number n
does not exist, NULL
is returned. Otherwise, the data of the found element is returned.
- Note:
- Worst case is O(n).
- Warning:
list
must be a pointer to the first element of the list.
References _Eina_List::data, and eina_list_nth_list().
Get the nth member's list node in a list.
- Parameters:
-
list The list to get the specfied member number from. n The number of the element (0 being the first).
- Returns:
- The list node stored in the numbered element.
n
, in list
. The first element in the array is element number 0. If the element number n
does not exist or list
is NULL
or n
is greater than the count of elements in list
minus 1, NULL
is returned. Otherwise the list node stored in the numbered element is returned.
- Note:
- Worst case is O(n).
- Warning:
list
must be a pointer to the first element of the list.
References _Eina_List::accounting, _Eina_List_Accounting::count, _Eina_List_Accounting::last, _Eina_List::next, and _Eina_List::prev.
Referenced by eina_list_nth(), and eina_list_sort().
Reverse all the elements in the list.
- Parameters:
-
list The list to reverse.
- Returns:
- The list head after it has been reversed.
list
, so the last member is now first, and so on. If list
is NULL
, this functon returns NULL
.
- Note:
- in-place: this will change the given list, so you should now point to the new list head that is returned by this function.
- Warning:
list
must be a pointer to the first element of the list.
References _Eina_List::accounting, _Eina_List::data, _Eina_List_Accounting::last, _Eina_List::next, and _Eina_List::prev.
Clone (copy) all the elements in the list in reverse order.
- Parameters:
-
list The list to reverse.
- Returns:
- The new list that has been reversed.
list
, so the last member is now first, and so on. If list
is NULL
, this functon returns NULL
. This returns a copy of the given list.
- Note:
- copy: this will copy the list and you should then eina_list_free() when it is not required anymore.
- Warning:
list
must be a pointer to the first element of the list.
- See also:
- eina_list_reverse()
References EINA_LIST_FOREACH, and eina_list_prepend().
Clone (copy) all the elements in the list in exactly same order.
- Parameters:
-
list The list to clone.
- Returns:
- The new list that has been cloned.
list
. If list
is NULL
, this functon returns NULL
. This returns a copy of the given list.
- Note:
- copy: this will copy the list and you should then eina_list_free() when it is not required anymore.
- Warning:
list
must be a pointer to the first element of the list.
- See also:
- eina_list_reverse_clone()
References eina_list_append(), and EINA_LIST_FOREACH.
Eina_List* eina_list_sort | ( | Eina_List * | list, | |
unsigned int | limit, | |||
Eina_Compare_Cb | func | |||
) |
Sort a list according to the ordering func will return.
- Parameters:
-
list The list handle to sort. limit The maximum number of list elements to sort. func A function pointer that can handle comparing the list data nodes.
- Returns:
- the new head of list.
list
. size
if the number of the first element to sort. If limit
is 0 or greater than the number of elements in list
, all the elements are sorted. func
is used to compare two elements of list
. If list
or func
are NULL
, this function returns NULL
.
- Note:
- in-place: this will change the given list, so you should now point to the new list head that is returned by this function.
worst case is O(n * log2(n)) comparisons (calls to func()), O(n) comparisons average case. That means that for 1,000,000 list elements, sort will usually do 1,000,000 comparisons, but may do up to 20,000,000.
int sort_cb(const void *d1, const void *d2) { const char *txt = d1; const char *txt2 = d2; if(!txt) return(1); if(!txt2) return(-1); return(strcmp(txt, txt2)); } extern Eina_List *list; list = eina_list_sort(list, eina_list_count(list), sort_cb);
- Warning:
list
must be a pointer to the first element of the list.
References _Eina_List::accounting, _Eina_List_Accounting::count, _Eina_List::data, eina_list_nth_list(), EINA_SAFETY_ON_NULL_RETURN_VAL, _Eina_List_Accounting::last, _Eina_List::next, and _Eina_List::prev.
Referenced by eina_rectangle_pool_request().
Merge two list.
- Parameters:
-
left Head list to merge. right Tail list to merge.
- Returns:
- A new merged list.
Both left and right do not exist anymore after the merge.
- Note:
- merge cost is O(n), being n the size of the smallest list. This is due the need to fix accounting of that segment, making count and last access O(1).
- Warning:
list
must be a pointer to the first element of the list.
References _Eina_List::accounting, _Eina_List_Accounting::count, _Eina_List_Accounting::last, _Eina_List::next, and _Eina_List::prev.
Eina_List* eina_list_sorted_merge | ( | Eina_List * | left, | |
Eina_List * | right, | |||
Eina_Compare_Cb | func | |||
) |
Merge two sorted list according to the ordering func will return.
- Parameters:
-
left First list to merge. right Second list to merge. func A function pointer that can handle comparing the list data nodes.
- Returns:
- A new sorted list.
left
and right
, and choose the smallest one to be head of the returned list. It will continue this process for all entry of both list.
Both left and right do not exist anymore after the merge. If func
is NULL
, it will return NULL
.
Example:
int sort_cb(void *d1, void *d2) { const char *txt = NULL; const char *txt2 = NULL; if(!d1) return(1); if(!d2) return(-1); return(strcmp((const char*)d1, (const char*)d2)); } extern Eina_List *sorted1; extern Eina_List *sorted2; list = eina_list_sorted_merge(sorted1, sorted2, sort_cb);
- Warning:
list
must be a pointer to the first element of the list.
References _Eina_List::accounting, _Eina_List_Accounting::count, _Eina_List::data, EINA_SAFETY_ON_NULL_RETURN_VAL, _Eina_List_Accounting::last, _Eina_List::next, and _Eina_List::prev.
Split a list into 2 lists.
- Parameters:
-
list List to split. relative The list will be split after relative
.right The head of the new right list.
- Returns:
- The new left list
list
into two lists ( left and right ) after the node relative
. Relative
will become the last node of the left list. If list
or right
are NULL
list is returns. If relative
is NULL right is set to list
and NULL
is returns. If relative
is the last node of list
list is returns and right
is set to NULL
.list does not exist anymore after the split.
- Warning:
list
must be a pointer to the first element of the list.
References _Eina_List::accounting, _Eina_List_Accounting::count, eina_list_last(), eina_list_next(), _Eina_List_Accounting::last, _Eina_List::next, and _Eina_List::prev.
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 is in the sorted list.
- Parameters:
-
list The list to search for data, must be sorted. func A function pointer that can handle comparing the list data nodes. data reference value to search. result_cmp if provided returns the result of func(node->data, data) node being the last (returned) node. If node was found (exact match), then it is 0. If returned node is smaller than requested data, it is less than 0 and if it's bigger it's greater than 0. It is the last value returned by func().
- Returns:
- the nearest node,
NULL
if not found.
data
as it's data in list
, if such a node exists it will be returned and result_cmp
will be 0
. If the data of no node in list
is equal to data
, the node with the nearest value to that will be returned and result_cmp
will be the return value of func
with data
and the returned node's data as arguments.This function is useful for inserting an element in the list only in case it isn't already present in the list, the naive way of doing this would be:
void *ptr = eina_list_data_find(list, "my data"); if (!ptr) eina_list_sorted_insert(list, "my data");
However this has the downside of walking through the list twice, once to check if the data is already present and another to insert the element in the corret position. This can be done more eficiently:
int cmp_result; l = eina_list_search_sorted_near_list(list, cmp_func, "my data", &cmp_result); if (cmp_result > 0) list = eina_list_prepend_relative_list(list, "my data", l); else if (cmp_result < 0) list = eina_list_append_relative_list(list, "my data", l);
If cmp_result is 0 the element is already in the list and we need not insert it, if cmp_result is greater than zero "my @a data" needs to come after l(the nearest node present), if less than zero before.
- Note:
- O(log2(n)) average/worst case performance, for 1,000,000 elements it will do a maximum of 20 comparisons. This is much faster than the 1,000,000 comparisons made naively walking the list from head to tail, so depending on the number of searches and insertions, it may be worth to eina_list_sort() the list and do the searches later. As lists do not have O(1) access time, walking to the correct node can be costly, consider worst case to be almost O(n) pointer dereference (list walk).
- Warning:
list
must be a pointer to the first element of the list.
References _Eina_List::accounting, _Eina_List_Accounting::count, _Eina_List::data, _Eina_List_Accounting::last, and _Eina_List::next.
Referenced by eina_list_search_sorted_list(), and eina_list_sorted_insert().
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.
- Parameters:
-
list The list to search for data, must be sorted. func A function pointer that can handle comparing the list data nodes. data reference value to search.
- Returns:
- the node if func(node->data, data) == 0,
NULL
if not found.
Example: imagine user gives a string, you check if it's in the list before duplicating its contents.
- Note:
- O(log2(n)) average/worst case performance, for 1,000,000 elements it will do a maximum of 20 comparisons. This is much faster than the 1,000,000 comparisons made by eina_list_search_unsorted_list(), so depending on the number of searches and insertions, it may be worth to eina_list_sort() the list and do the searches later. As said in eina_list_search_sorted_near_list(), lists do not have O(1) access time, so walking to the correct node can be costly, consider worst case to be almost O(n) pointer dereference (list walk).
- Warning:
list
must be a pointer to the first element of the list.
References eina_list_search_sorted_near_list().
Referenced by eina_list_search_sorted().
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.
- Parameters:
-
list The list to search for data, must be sorted. func A function pointer that can handle comparing the list data nodes. data reference value to search.
- Returns:
- the node value (
node->data
) if func(node->data, data) == 0, NULL if not found.
Example: imagine user gives a string, you check if it's in the list before duplicating its contents.
- Note:
- O(log2(n)) average/worst case performance, for 1,000,000 elements it will do a maximum of 20 comparisons. This is much faster than the 1,000,000 comparisons made by eina_list_search_unsorted(), so depending on the number of searches and insertions, it may be worth to eina_list_sort() the list and do the searches later. As said in eina_list_search_sorted_near_list(), lists do not have O(1) access time, so walking to the correct node can be costly, consider worst case to be almost O(n) pointer dereference (list walk).
- Warning:
list
must be a pointer to the first element of the list.
- See also:
- eina_list_search_sorted_list()
References eina_list_data_get(), and eina_list_search_sorted_list().
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.
- Parameters:
-
list The list to search for data, may be unsorted. func A function pointer that can handle comparing the list data nodes. data reference value to search.
- Returns:
- the node if func(node->data, data) == 0,
NULL
if not found.
Example: imagine user gives a string, you check if it's in the list before duplicating its contents.
- Note:
- this is expensive and may walk the whole list, it's order-N, that is for 1,000,000 elements list it may walk and compare 1,000,000 nodes.
- Warning:
list
must be a pointer to the first element of the list.
References EINA_LIST_FOREACH.
Referenced by eina_list_search_unsorted().
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.
- Parameters:
-
list The list to search for data, may be unsorted. func A function pointer that can handle comparing the list data nodes. data reference value to search.
- Returns:
- the node value (
node->data
) if func(node->data, data) == 0,NULL
if not found.
Example: imagine user gives a string, you check if it's in the list before duplicating its contents.
- Note:
- this is expensive and may walk the whole list, it's order-N, that is for 1,000,000 elements list it may walk and compare 1,000,000 nodes.
- Warning:
list
must be a pointer to the first element of the list.
References eina_list_data_get(), and eina_list_search_unsorted_list().
Get the last list node in the list.
- Parameters:
-
list The list to get the last list node from.
- Returns:
- The last list node in the list.
list
. If list
is NULL
or empty, NULL
is returned.This is a order-1 operation (it takes the same short time regardless of the length of the list).
- Warning:
list
must be a pointer to the first element of the list.
Referenced by eina_list_iterator_reversed_new(), and eina_list_split_list().
Get the next list node after the specified list node.
- Parameters:
-
list The list node to get the next list node from
- Returns:
- The next list node on success,
NULL
otherwise.
list
. It is equivalent to list->next. If list
is NULL
or if no next list node exists, it returns NULL
.
- Warning:
list
must be a pointer to the first element of the list.
Referenced by eina_list_split_list().
Get the previous list node before the specified list node.
- Parameters:
-
list The list node to get the previous list node from.
- Returns:
- The previous list node o success,
NULL
otherwise. if no previous list node exists
list
. It is equivalent to list->prev. If list
is NULL
or if no previous list node exists, it returns NULL
.
- Warning:
list
must be a pointer to the first element of the list.
static void* eina_list_data_get | ( | const Eina_List * | list | ) | [inline, static] |
Get the list node data member.
- Parameters:
-
list The list node to get the data member of.
- Returns:
- The data member from the list node.
list
. It is equivalent to list->data. If list
is NULL
, this function returns NULL
.
- Warning:
list
must be a pointer to the first element of the list.
Referenced by eina_list_search_sorted(), and eina_list_search_unsorted().
static void* eina_list_data_set | ( | Eina_List * | list, | |
const void * | data | |||
) | [inline, static] |
Set the list node data member.
- Parameters:
-
list The list node to get the data member of. data The data member to the list node.
- Returns:
- The previous data value.
data
of the specified list node list
. It returns the previous data of the node. If list
is NULL
, this function returns NULL
.
- Warning:
list
must be a pointer to the first element of the list.
static unsigned int eina_list_count | ( | const Eina_List * | list | ) | [inline, static] |
Get the count of the number of items in a list.
- Parameters:
-
list The list whose count to return.
- Returns:
- The number of members in the list.
list
contains. If the list is NULL
, 0
is returned.NB: This is an order-1 operation and takes the same time regardless of the length of the list.
- Warning:
list
must be a pointer to the first element of the list.
Eina_Iterator* eina_list_iterator_new | ( | const Eina_List * | list | ) |
Returned a new iterator associated to a list.
- Parameters:
-
list The list.
- Returns:
- A new iterator.
list
. If list
is NULL
or the count member of list
is less or equal than 0, this function still returns a valid iterator that will always return false on eina_iterator_next(), thus keeping API sane.If the memory can not be allocated, NULL is returned and EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is returned.
- Warning:
list
must be a pointer to the first element of the list.if the list structure changes then the iterator becomes invalid! That is, if you add or remove nodes this iterator behavior is undefined and your program may crash!
References EINA_ERROR_OUT_OF_MEMORY, eina_error_set(), EINA_MAGIC_SET, FUNC_ITERATOR_FREE, FUNC_ITERATOR_GET_CONTAINER, and FUNC_ITERATOR_NEXT.
Eina_Iterator* eina_list_iterator_reversed_new | ( | const Eina_List * | list | ) |
Returned a new reversed iterator associated to a list.
- Parameters:
-
list The list.
- Returns:
- A new iterator.
list
. If list
is NULL
or the count member of list
is less or equal than 0, this function still returns a valid iterator that will always return false on eina_iterator_next(), thus keeping API sane.Unlike eina_list_iterator_new(), this will walk the list backwards.
If the memory can not be allocated, NULL is returned and EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is returned.
- Warning:
list
must be a pointer to the first element of the list.if the list structure changes then the iterator becomes invalid! That is, if you add or remove nodes this iterator behavior is undefined and your program may crash!
References EINA_ERROR_OUT_OF_MEMORY, eina_error_set(), eina_list_last(), EINA_MAGIC_SET, FUNC_ITERATOR_FREE, FUNC_ITERATOR_GET_CONTAINER, and FUNC_ITERATOR_NEXT.
Eina_Accessor* eina_list_accessor_new | ( | const Eina_List * | list | ) |
Returned a new accessor associated to a list.
- Parameters:
-
list The list.
- Returns:
- A new accessor.
list
. If list
is NULL
or the count member of list
is less or equal than 0, this function returns NULL
. If the memory can not be allocated, NULL
is returned and EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid accessor is returned.
- Warning:
list
must be a pointer to the first element of the list.
References EINA_ERROR_OUT_OF_MEMORY, eina_error_set(), EINA_MAGIC_SET, FUNC_ACCESSOR_FREE, FUNC_ACCESSOR_GET_AT, and FUNC_ACCESSOR_GET_CONTAINER.