You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1652 lines
59 KiB

/* EINA - EFL data type library
* Copyright (C) 2002-2008 Carsten Haitzler, Vincent Torri, Jorge Luis Zapata Muga
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library;
* if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef EINA_LIST_H_
#define EINA_LIST_H_
#include <stdlib.h>
#include "eina_config.h"
// magic number checks for eina list nodes - this costs extra memory and a
// few cycles for some safety = aybe during debugging/devel only?
//#define EINA_LIST_MAGIC 1
#include "eina_types.h"
#include "eina_iterator.h"
#include "eina_accessor.h"
#ifdef EINA_LIST_MAGIC
# include "eina_magic.h"
#endif
/**
* @page eina_list_01_example_page Adding elements to Eina_List
* @dontinclude eina_list_01.c
*
* Creating an @ref Eina_List and adding elements to it is very easy and can be
* understood from an example:
* First thing is always to include Eina.h, for this example we also
* include stdio.h so we can use printf.
* @skip #include
* @until Eina.h
*
* Just some boilerplate code, declaring some variables and initializing eina.
* @until eina_init
* Here we add a sequence of elements to our list. By using append we add
* elements to the end of the list, so the list will look like this:@n
* @image rtf eina_list_example_01_a.png
* @image html eina_list_example_01_a.png
* @image latex eina_list_example_01_a.eps "" width=\textwidth
* @until roslin
* There are a couple of interesting things happening here, first is that we are
* passing a NULL pointer to the first @ref eina_list_append() call, when this
* is done a list is created. The other @b very important detail to notice is
* that the return value is attributed to the @a list variable, this needs to
* be done every time we use a a function that alters the contents of the list.
*
9 years ago
* Now that we have a list with some elements in it we can look at its contents.
* @until printf
*
9 years ago
* There are many ways of accessing elements in the list, including by its
* index:
* @until nth
* @note It should be noted that the index starts at 0.
*
* @ref eina_list_append() is not the only way to add elements to a a list. A
* common requirement is to add an element in a specific position this can be
* accomplished using @ref eina_list_append_relative() and
* @ref eina_list_append_relative_list():
* @until zarek
* First @a "cain" is added after the second element (remember that indexes are
* 0 based) and then we add @a "zarek" after @a "cain".
*
* @ref Eina_List also has prepend analogs to append functions we have used so
* far:
* @until lampkin
* With this additions our list now looks like this:@n
* @image rtf eina_list_example_01_b.png
* @image html eina_list_example_01_b.png
* @image latex eina_list_example_01_b.eps "" width=\textwidth
*
* Once done using the list it needs to be freed, and since we are done with
* eina that also need to be shutdown:
* @until }
*
* The full source code can be found on the examples folder
* on the @ref eina_list_01_c "eina_list_01.c" file.
*/
/**
* @page eina_list_01_c Adding elements to Eina_List example
*
* @include eina_list_01.c
* @example eina_list_01.c
*/
/**
* @page eina_list_02_example_page Sorting Eina_List elements
* @dontinclude eina_list_02.c
*
* If you don't know how to create lists see
* @ref eina_list_01_example_page.
*
* @skip #include
* @until boomer
* 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:
* @until return
*
* However if searching the list multiple times it probably is better to sort
* the list since the sorted_search functions are much faster:
* @until return
*
* 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 @ref eina_list_sorted_insert():
* @until 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:
* @until append
* @note @ref 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":
* @until split_list
*
* It is also possible to add entire lists of elements using
* @ref eina_list_sorted_merge():
* @until sorted_merge
*
* And as always release memory and shutdown eina before ending:
* @until }
*
* The full source code can be found on the examples folder
* on the @ref eina_list_02_c "eina_list_02.c" file.
*/
/**
* @page eina_list_02_c Sorting Eina_List elements example
*
* @include eina_list_02.c
* @example eina_list_02.c
*/
/**
* @page eina_list_03_example_page Reordering Eina_List elements
* @dontinclude eina_list_03.c
*
* If you don't know how to create lists see
* @ref eina_list_01_example_page.
*
* We start out with code that should be familiar by now:
* @skip #include
* @until gemenon
*
* You can move elements around in a list using @ref eina_list_move() or using
* @ref eina_list_promote_list() and @ref eina_list_demote_list() which move a
* list node to the head and end of the list respectively:
* @until demote
*
* Removing elements from a list can be done with ease:
* @until sagittarius
*
* To replace an element in the list it is not necessary to remove it and then
* re-add with the new value, it is possible to just change the value of a node:
* @until aquarius
*
* We will now take a peek to see if the list still has the right number of
* elements:
* @until printf
*
* Now that the list is in alphabetical order let's create a copy of it in
* reverse order and print every element to see if worked as expected:
* @until iterator_free
* @note Always remember to free your iterators when done using them.
*
* And as always release memory and shutdown eina before ending:
* @until }
*
* The full source code can be found on the examples folder
* on the @ref eina_list_03_c "eina_list_03.c" file.
*/
/**
* @page eina_list_03_c Reordering Eina_List elements example
*
* @include eina_list_03.c
* @example eina_list_03.c
*/
/**
* @page eina_list_04_example_page Eina_List and memory allocation
* @dontinclude eina_list_04.c
*
* If you don't know how to create lists see
* @ref eina_list_01_example_page. In this example we also use
* @ref Eina_Stringshare_Group, however it should be possible to understand the code
* regardless of previous knowledge about it.
*
* Here we have the usual list creation code with a twist, now we are using as
* data for the list memory that has to be freed later on.
* @skip #include
* @until Sharon
*
* This time we are going to iterate over our list in a different way:
* @until printf
*
* And now we are going to iterate over the list backwards:
* @until printf
*
* And now we need to free up the memory allocated during creation of the list:
* @until stringshare_del
* @note We don't need to use eina_list_free() since @ref EINA_LIST_FREE takes
* care of that.
*
* And shut everything down:
* @until }
*
* The full source code can be found on the examples folder
* on the @ref eina_list_04_c "eina_list_04.c" file.
*/
/**
* @page eina_list_04_c Eina_List and memory allocation example
*
* @include eina_list_04.c
* @example eina_list_04.c
*/
/**
* @addtogroup Eina_List_Group List
*
* @brief 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.
*
* @ref Eina_List nodes keep references to the previous node, the next node, its
* data and to an accounting structure.
*
* @image rtf eina_list.png
* @image html eina_list.png
* @image latex eina_list.eps width=5cm
*
* @ref Eina_List_Accounting is used to improve the performance of some
* functions. It is private and <b>should not</b> 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.
*
* Most functions have two versions that have the same effect but operate on
* different arguments, the @a plain functions operate over data(e.g..:
* @ref eina_list_append_relative, @ref eina_list_remove,
* @ref eina_list_data_find), the @a list versions of these functions operate
* on @ref Eina_List nodes.
*
* @warning You must @b always use the pointer to the first element of the list
* as the list!
* @warning You must @b never use a pointer to an element in the middle of the
* list as the list!
*
* Here are some examples of @ref Eina_List usage:
* @li @ref eina_list_01_example_page
* @li @ref eina_list_02_example_page
* @li @ref eina_list_03_example_page
* @li @ref eina_list_04_example_page
*/
/**
* @addtogroup Eina_Data_Types_Group Data Types
*
* @{
*/
/**
* @addtogroup Eina_Containers_Group Containers
*
* @{
*/
/**
* @defgroup Eina_List_Group List
*
* @{
*/
/**
* @typedef Eina_List
* Type for a generic double linked list.
*/
typedef struct _Eina_List Eina_List;
/**
* @typedef Eina_List_Accounting
* Cache used to store the last element of a list and the number of
* elements, for fast access.
*/
typedef struct _Eina_List_Accounting Eina_List_Accounting;
/**
* @struct _Eina_List
* Type for a generic double linked list.
*/
struct _Eina_List
{
void *data; /**< Pointer to list element payload */
Eina_List *next; /**< Next member in the list */
Eina_List *prev; /**< Previous member in the list */
Eina_List_Accounting *accounting; /**< Private list accounting info - don't touch */
#ifdef EINA_LIST_MAGIC
EINA_MAGIC
#endif
};
/**
* @struct _Eina_List_Accounting
* Cache used to store the last element of a list and the number of
* elements, for fast access. It is for private use and must not be
* touched.
*/
struct _Eina_List_Accounting
{
Eina_List *last; /**< Pointer to the last element of the list - don't touch */
unsigned int count; /**< Number of elements of the list - don't touch */
EINA_MAGIC
};
/**
* @brief Appends the given data to the given linked list.
*
* @param[in,out] list The given list.
* @param[in] data The data to append.
* @return A list pointer, or @c NULL on error.
*
* This function appends @p data to @p list. If @p list is @c 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.
*
* @code
* Eina_List *list = NULL;
* extern void *my_data;
*
* list = eina_list_append(list, my_data);
* @endcode
*
* @warning @p list must be a pointer to the first element of the list(or NULL).
*/
EAPI Eina_List *eina_list_append(Eina_List *list, const void *data) EINA_ARG_NONNULL(2) EINA_WARN_UNUSED_RESULT;
/**
* @brief Prepends the given data to the given linked list.
*
* @param[in,out] list The given list.
* @param[in] data The data to prepend.
* @return A list pointer, or @c NULL on error.
*
* This function prepends @p data to @p list. If @p list is @c 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:
* @code
* Eina_List *list = NULL;
* extern void *my_data;
*
* list = eina_list_prepend(list, my_data);
* @endcode
*
* @warning @p list must be a pointer to the first element of the list.
*/
EAPI Eina_List *eina_list_prepend(Eina_List *list, const void *data) EINA_ARG_NONNULL(2) EINA_WARN_UNUSED_RESULT;
/**
* @brief Inserts the given data into the given linked list after the specified data.
*
* @param[in,out] list The given linked list.
* @param[in] data The data to insert.
* @param[in] relative The data to insert after.
* @return A list pointer, or @c NULL on error.
*
* This function inserts @p data into @p list after @p relative. If
* @p relative is not in the list, @p data is appended to
* the list. If @p list is @c NULL, a new list is returned. If there
* are multiple instances of @p relative in the list, @p 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.
*
* @code
* Eina_List *list = NULL;
* extern void *my_data;
* extern void *relative_member;
*
* list = eina_list_append(list, relative_member);
* list = eina_list_append_relative(list, my_data, relative_member);
* @endcode
*
* @warning @p list must be a pointer to the first element of the list.
*/
EAPI Eina_List *eina_list_append_relative(Eina_List *list, const void *data, const void *relative) EINA_ARG_NONNULL(2) EINA_WARN_UNUSED_RESULT;
/**
* @brief Appends a list node to a linked list after the specified member.
*
* @param[in,out] list The given linked list.
* @param[in] data The data to insert.
* @param[in] relative The list node to insert after.
* @return A list pointer, or @c NULL on error.
*
* This function inserts @p data into @p list after the list node
* @p relative. If @p list or @p relative are @c NULL, @p data is just
* appended to @p list using eina_list_append(). If @p list is
* @c NULL, a new list is returned. If there are multiple instances
* of @p relative in the list, @p 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 @p list must be a pointer to the first element of the list.
*/
EAPI Eina_List *eina_list_append_relative_list(Eina_List *list, const void *data, Eina_List *relative) EINA_ARG_NONNULL(2) EINA_WARN_UNUSED_RESULT;
/**
* @brief Prepends a data pointer to a linked list before the specified member.
*
* @param[in,out] list The given linked list.
* @param[in] data The data to insert.
* @param[in] relative The member that data will be inserted before.
* @return A list pointer, or @c NULL on error.
*
* This function inserts @p data to @p list before @p relative. If
* @p relative is not in the list, @p data is prepended to the list
* with eina_list_prepend(). If @p list is @c NULL, a new list is
* returned. If there are multiple instances of @p relative in the
* list, @p 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.
*
* @code
* Eina_List *list = NULL;
* extern void *my_data;
* extern void *relative_member;
*
* list = eina_list_append(list, relative_member);
* list = eina_list_prepend_relative(list, my_data, relative_member);
* @endcode
*
* @warning @p list must be a pointer to the first element of the list.
*/
EAPI Eina_List *eina_list_prepend_relative(Eina_List *list, const void *data, const void *relative) EINA_ARG_NONNULL(2) EINA_WARN_UNUSED_RESULT;
/**
* @brief Prepends a list node to a linked list before the specified member.
*
* @param[in,out] list The given linked list.
* @param[in] data The data to insert.
* @param[in] relative The list node to insert before.
* @return A list pointer, or @c NULL on error.
*
* This function inserts @p data to @p list before the list node
* @p relative. If @p list or @p relative are @c NULL, @p data is just
* prepended to @p list using eina_list_prepend(). If @p list is
* @c NULL, a new list is returned. If there are multiple instances
* of @p relative in the list, @p 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 @p list must be a pointer to the first element of the list.
*/
EAPI Eina_List *eina_list_prepend_relative_list(Eina_List *list, const void *data, Eina_List *relative) EINA_ARG_NONNULL(2) EINA_WARN_UNUSED_RESULT;
/**
* @brief Inserts a new node into a sorted list.
*
* @param[in,out] list The given linked list, @b must be sorted.
* @param[in] func The function called for the sort.
* @param[in] data The data to be inserted in sorted order.
* @return A list pointer, or @c NULL on error.
*
* This function inserts values into a linked list assuming it was
* sorted and the result will be sorted. If @p list is @c 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.
*
* @note O(log2(n)) comparisons (calls to @p 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 @p list must be a pointer to the first element of the list.
*/
EAPI Eina_List *eina_list_sorted_insert(Eina_List *list, Eina_Compare_Cb func, const void *data) EINA_ARG_NONNULL(2, 3) EINA_WARN_UNUSED_RESULT;
/**
* @brief Removes the first instance of the specified data from the given list.
*
* @param[in,out] list The given list.
* @param[in] data The specified data.
* @return A list pointer, or @c NULL on error.
*
* This function removes the first instance of @p data from @p list. If
* @p data is not in the given list or is @c NULL, nothing is done and
* the specified @p list returned. If @p list is @c NULL, @c NULL is
* returned, otherwise a new list pointer that should be used in place
* of the one passed to this function is returned.
*
* @warning @p list must be a pointer to the first element of the list.
*/
EAPI Eina_List *eina_list_remove(Eina_List *list, const void *data) EINA_ARG_NONNULL(2) EINA_WARN_UNUSED_RESULT;
/**
* @brief Removes the specified list node.
*
* @param[in,out] list The given linked list.
* @param[in] remove_list The list node which is to be removed.
* @return A list pointer, or @c NULL on error.
*
* This function removes the list node @p remove_list from @p list and
* frees the list node structure @p remove_list. If @p list is
* @c NULL, this function returns @c NULL. If @p remove_list is
* @c NULL, it returns @p 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 rather than EINA_LIST_FOREACH_SAFE because we stop
* the loop after removing the current node.)
*
* @code
* 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;
* }
* }
* @endcode
*
* @warning @p list must be a pointer to the first element of the list.
*/
EAPI Eina_List *eina_list_remove_list(Eina_List *list, Eina_List *remove_list) EINA_ARG_NONNULL(2) EINA_WARN_UNUSED_RESULT;
/**
* @brief Moves the specified data to the head of the list.
*
* @param[in,out] list The given linked list.
* @param[in] move_list The list node to move.
* @return A new list handle to replace the old one, or @c NULL on error.
*
* This function moves @p move_list to the front of @p list. If list is
* @c NULL, @c NULL is returned. If @p move_list is @c NULL, @p list is
* returned. Otherwise, a new list pointer that should be used in place
* of the one passed to this function is returned.
*
* Example:
* @code
* 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;
* }
* }
* @endcode
*
* @warning @p list must be a pointer to the first element of the list.
*/
EAPI Eina_List *eina_list_promote_list(Eina_List *list, Eina_List *move_list) EINA_ARG_NONNULL(2) EINA_WARN_UNUSED_RESULT;
/**
* @brief Moves the specified data to the tail of the list.
*
* @param[in,out] list The given linked list.
* @param[in] move_list The list node to move.
* @return A new list handle to replace the old one, or @c NULL on error.
*
* This function move @p move_list to the end of @p list. If list is @c
* NULL, @c NULL is returned. If @p move_list is @c NULL, @p list is
* returned. Otherwise, a new list pointer that should be used in place
* of the one passed to this function is returned.
*
* Example:
* @code
* 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;
* }
* }
* @endcode
*
* @warning @p list must be a pointer to the first element of the list.
*/
EAPI Eina_List *eina_list_demote_list(Eina_List *list, Eina_List *move_list);
/**
* @brief Finds a member of a list and returns it.
*
* @param[in] list The linked list to search.
* @param[in] data The data member to find in the list.
* @return The data member pointer if found, or @c NULL otherwise.
*
* This function searches in @p list from beginning to end for the
* first member whose data pointer is @p data. If it is found, @p data
* will be returned, otherwise @c NULL will be returned.
*
* Example:
* @code
* 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);
* }
* @endcode
*
* @warning @p list must be a pointer to the first element of the list.
*/
EAPI void *eina_list_data_find(const Eina_List *list, const void *data) EINA_PURE EINA_ARG_NONNULL(2) EINA_WARN_UNUSED_RESULT;
/**
* @brief Finds a member of a list and returns it as a list node.
*
* @param[in] list The list to search for data.
* @param[in] data The data pointer to find in the list.
* @return A list node containing the data member on success, @c NULL
* otherwise.
*
* This function searches @p list from beginning to end for the
* first member whose data pointer is @p data. If it is found, the
* list node containing the specified member is returned, otherwise
* @c NULL is returned.