1 /* EINA - EFL data type library
2 * Copyright (C) 2002-2008 Carsten Haitzler, Gustavo Sverzut Barbieri, Tilman Sauerbeck,
3 * Vincent Torri, Cedric Bail, Jorge Luis Zapata Muga,
4 * Corey Donohoe, Arnaud de Turckheim, Alexandre Becoulet
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library;
18 * if not, see <http://www.gnu.org/licenses/>.
20 * This file incorporates work covered by the following copyright and
23 * Copyright (C) 2004 ncn
24 * Copyright (C) 2006 Sebastian Dransfeld
25 * Copyright (C) 2007 Christopher Michael
27 * Permission is hereby granted, free of charge, to any person obtaining a copy
28 * of this software and associated documentation files (the "Software"), to
29 * deal in the Software without restriction, including without limitation the
30 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
31 * sell copies of the Software, and to permit persons to whom the Software is
32 * furnished to do so, subject to the following conditions:
34 * The above copyright notice and this permission notice shall be included in
35 * all copies of the Software and its Copyright notices. In addition publicly
36 * documented acknowledgment must be given that this software has been used if no
37 * source code of this software is made available publicly. This includes
38 * acknowledgments in either Copyright notices, Manuals, Publicity and Marketing
39 * documents or any documentation provided with any product containing this
40 * software. This License does not apply to any software that links to the
41 * libraries provided by this software (statically or dynamically), but only to
42 * the software provided.
44 * Please see the OLD-COPYING.PLAIN for a plain-english explanation of this notice
47 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
48 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
49 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
50 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
51 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
52 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
57 * @page tutorial_list_page List Tutorial
75 #include "eina_config.h"
76 #include "eina_private.h"
77 #include "eina_error.h"
78 #include "eina_mempool.h"
80 /* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */
81 #include "eina_safety_checks.h"
82 #include "eina_list.h"
85 /*============================================================================*
87 *============================================================================*/
93 #define EINA_MAGIC_CHECK_LIST(d, ...) \
95 if (!EINA_MAGIC_CHECK(d, EINA_MAGIC_LIST)) \
97 EINA_MAGIC_FAIL(d, EINA_MAGIC_LIST); \
102 #define EINA_MAGIC_CHECK_LIST_ITERATOR(d, ...) \
104 if (!EINA_MAGIC_CHECK(d, EINA_MAGIC_LIST_ITERATOR)) \
106 EINA_MAGIC_FAIL(d, EINA_MAGIC_LIST_ITERATOR); \
107 return __VA_ARGS__; \
111 #define EINA_MAGIC_CHECK_LIST_ACCESSOR(d, ...) \
113 if (!EINA_MAGIC_CHECK(d, EINA_MAGIC_LIST_ACCESSOR)) \
115 EINA_MAGIC_FAIL(d, EINA_MAGIC_LIST_ACCESSOR); \
116 return __VA_ARGS__; \
120 #define EINA_MAGIC_CHECK_LIST_ACCOUNTING(d) \
122 if (!EINA_MAGIC_CHECK(d, EINA_MAGIC_LIST_ACCOUNTING)) \
124 EINA_MAGIC_FAIL(d, EINA_MAGIC_LIST_ACCOUNTING); \
129 #define EINA_LIST_SORT_STACK_SIZE 32
131 typedef struct _Eina_Iterator_List Eina_Iterator_List;
132 typedef struct _Eina_Accessor_List Eina_Accessor_List;
134 struct _Eina_Iterator_List
136 Eina_Iterator iterator;
138 const Eina_List *head;
139 const Eina_List *current;
144 struct _Eina_Accessor_List
146 Eina_Accessor accessor;
148 const Eina_List *head;
149 const Eina_List *current;
156 static Eina_Mempool *_eina_list_mp = NULL;
157 static Eina_Mempool *_eina_list_accounting_mp = NULL;
158 static int _eina_list_log_dom = -1;
160 #define ERR(...) EINA_LOG_DOM_ERR(_eina_list_log_dom, __VA_ARGS__)
161 #define DBG(...) EINA_LOG_DOM_DBG(_eina_list_log_dom, __VA_ARGS__)
163 static inline Eina_List_Accounting*
164 _eina_list_mempool_accounting_new(__UNUSED__ Eina_List *list)
166 Eina_List_Accounting *tmp;
168 tmp = eina_mempool_malloc(_eina_list_accounting_mp, sizeof (Eina_List_Accounting));
169 if (!tmp) return NULL;
171 EINA_MAGIC_SET(tmp, EINA_MAGIC_LIST_ACCOUNTING);
176 _eina_list_mempool_accounting_free(Eina_List_Accounting *accounting)
178 EINA_MAGIC_CHECK_LIST_ACCOUNTING(accounting);
180 EINA_MAGIC_SET(accounting, EINA_MAGIC_NONE);
181 eina_mempool_free(_eina_list_accounting_mp, accounting);
184 static inline Eina_List*
185 _eina_list_mempool_list_new(__UNUSED__ Eina_List *list)
189 tmp = eina_mempool_malloc(_eina_list_mp, sizeof (Eina_List));
190 if (!tmp) return NULL;
192 EINA_MAGIC_SET(tmp, EINA_MAGIC_LIST);
197 _eina_list_mempool_list_free(Eina_List *list)
199 EINA_MAGIC_CHECK_LIST(list);
201 list->accounting->count--;
202 if (list->accounting->count == 0)
203 _eina_list_mempool_accounting_free(list->accounting);
205 EINA_MAGIC_SET(list, EINA_MAGIC_NONE);
206 eina_mempool_free(_eina_list_mp, list);
210 _eina_list_setup_accounting(Eina_List *list)
212 EINA_MAGIC_CHECK_LIST(list, NULL);
214 list->accounting = _eina_list_mempool_accounting_new(list);
215 if (!list->accounting) goto on_error;
217 list->accounting->last = list;
218 list->accounting->count = 1;
223 _eina_list_mempool_list_free(list);
228 _eina_list_update_accounting(Eina_List *list, Eina_List *new_list)
230 EINA_MAGIC_CHECK_LIST(list);
231 EINA_MAGIC_CHECK_LIST(new_list);
233 list->accounting->count++;
234 new_list->accounting = list->accounting;
238 static Eina_Mempool2 _eina_list_mempool =
244 static Eina_Mempool2 _eina_list_accounting_mempool =
246 sizeof(Eina_List_Accounting),
253 eina_list_iterator_next(Eina_Iterator_List *it, void **data)
255 EINA_MAGIC_CHECK_LIST_ITERATOR(it, EINA_FALSE);
257 if (it->current == NULL) return EINA_FALSE;
258 *data = eina_list_data_get(it->current);
260 it->current = eina_list_next(it->current);
266 eina_list_iterator_prev(Eina_Iterator_List *it, void **data)
268 EINA_MAGIC_CHECK_LIST_ITERATOR(it, EINA_FALSE);
270 if (it->current == NULL) return EINA_FALSE;
271 *data = eina_list_data_get(it->current);
273 it->current = eina_list_prev(it->current);
279 eina_list_iterator_get_container(Eina_Iterator_List *it)
281 EINA_MAGIC_CHECK_LIST_ITERATOR(it, NULL);
283 return (Eina_List *) it->head;
287 eina_list_iterator_free(Eina_Iterator_List *it)
289 EINA_MAGIC_CHECK_LIST_ITERATOR(it);
295 eina_list_accessor_get_at(Eina_Accessor_List *it, unsigned int index, void **data)
297 const Eina_List *over;
301 EINA_MAGIC_CHECK_LIST_ACCESSOR(it, EINA_FALSE);
303 if (index >= eina_list_count(it->head)) return EINA_FALSE;
305 if (it->index == index)
309 else if (index > it->index)
311 /* After current position. */
312 middle = ((eina_list_count(it->head) - it->index) >> 1) + it->index;
316 /* Go backward from the end. */
317 for (i = eina_list_count(it->head) - 1, over = eina_list_last(it->head);
318 i > index && over != NULL;
319 --i, over = eina_list_prev(over))
324 /* Go forward from current. */
325 for (i = it->index, over = it->current;
326 i < index && over != NULL;
327 ++i, over = eina_list_next(over))
333 /* Before current position. */
334 middle = it->index >> 1;
338 /* Go backward from current. */
339 for (i = it->index, over = it->current;
340 i > index && over != NULL;
341 --i, over = eina_list_prev(over))
346 /* Go forward from start. */
347 for (i = 0, over = it->head;
348 i < index && over != NULL;
349 ++i, over = eina_list_next(over))
354 if (over == NULL) return EINA_FALSE;
359 *data = eina_list_data_get(it->current);
364 eina_list_accessor_get_container(Eina_Accessor_List *it)
366 EINA_MAGIC_CHECK_LIST_ACCESSOR(it, NULL);
368 return (Eina_List *) it->head;
372 eina_list_accessor_free(Eina_Accessor_List *it)
374 EINA_MAGIC_CHECK_LIST_ACCESSOR(it);
380 eina_list_sort_rebuild_prev(Eina_List *list)
382 Eina_List *prev = NULL;
384 EINA_MAGIC_CHECK_LIST(list, NULL);
386 for (; list; list = list->next)
396 eina_list_sort_merge(Eina_List *a, Eina_List *b, Eina_Compare_Cb func)
398 Eina_List *first, *last;
400 if (func(a->data, b->data) < 0)
401 a = (last = first = a)->next;
403 b = (last = first = b)->next;
406 if (func(a->data, b->data) < 0)
407 a = (last = last->next = a)->next;
409 b = (last = last->next = b)->next;
411 last->next = a ? a : b;
420 /*============================================================================*
422 *============================================================================*/
424 /*============================================================================*
426 *============================================================================*/
429 * @addtogroup Eina_List_Group List
431 * @brief These functions provide double linked list management.
433 * For more information, you can look at the @ref tutorial_list_page.
440 * @brief Initialize the list module.
442 * @return #EINA_TRUE on success, #EINA_FALSE on failure.
444 * This function sets up the list module of Eina. It is called by
447 * This function creates mempool to speed up list node and accounting
448 * management, using EINA_MEMPOOL environment variable if it is set to
449 * choose the memory pool type to use.
456 const char *choice, *tmp;
458 _eina_list_log_dom = eina_log_domain_register("eina_list", EINA_LOG_COLOR_DEFAULT);
459 if (_eina_list_log_dom < 0)
461 EINA_LOG_ERR("Could not register log domain: eina_list");
465 #ifdef EINA_DEFAULT_MEMPOOL
466 choice = "pass_through";
468 choice = "chained_mempool";
470 tmp = getenv("EINA_MEMPOOL");
474 _eina_list_mp = eina_mempool_add
475 (choice, "list", NULL, sizeof(Eina_List), 320);
478 ERR("ERROR: Mempool for list cannot be allocated in list init.");
481 _eina_list_accounting_mp = eina_mempool_add
482 (choice, "list_accounting", NULL, sizeof(Eina_List_Accounting), 80);
483 if (!_eina_list_accounting_mp)
485 ERR("ERROR: Mempool for list accounting cannot be allocated in list init.");
486 eina_mempool_del(_eina_list_mp);
490 eina_magic_string_set(EINA_MAGIC_ITERATOR, "Eina Iterator");
491 eina_magic_string_set(EINA_MAGIC_ACCESSOR, "Eina Accessor");
492 eina_magic_string_set(EINA_MAGIC_LIST, "Eina List");
493 eina_magic_string_set(EINA_MAGIC_LIST_ITERATOR, "Eina List Iterator");
494 eina_magic_string_set(EINA_MAGIC_LIST_ACCESSOR, "Eina List Accessor");
495 eina_magic_string_set(EINA_MAGIC_LIST_ACCOUNTING, "Eina List Accounting");
500 eina_log_domain_unregister(_eina_list_log_dom);
501 _eina_list_log_dom = -1;
507 * @brief Shut down the list module.
509 * @return #EINA_TRUE on success, #EINA_FALSE on failure.
511 * This function shuts down the list module set up by
512 * eina_list_init(). It is called by eina_shutdown().
514 * @see eina_shutdown()
517 eina_list_shutdown(void)
519 eina_mempool_del(_eina_list_accounting_mp);
520 eina_mempool_del(_eina_list_mp);
522 eina_log_domain_unregister(_eina_list_log_dom);
523 _eina_list_log_dom = -1;
528 * @brief Append the given data to the given linked list.
530 * @param list The given list.
531 * @param data The data to append.
532 * @return A list pointer.
534 * This function appends @p data to @p list. If @p list is @c NULL, a
535 * new list is returned. On success, a new list pointer that should be
536 * used in place of the one given to this function is
537 * returned. Otherwise, the old pointer is returned.
539 * The following example code demonstrates how to ensure that the
540 * given data has been successfully appended.
543 * Eina_List *list = NULL;
544 * extern void *my_data;
546 * list = eina_list_append(list, my_data);
547 * if (eina_error_get())
549 * fprintf(stderr, "ERROR: Memory is low. List allocation failed.\n");
555 eina_list_append(Eina_List *list, const void *data)
557 Eina_List *l, *new_l;
560 new_l = _eina_list_mempool_list_new(list);
561 if (!new_l) return list;
563 new_l->data = (void *)data;
567 return _eina_list_setup_accounting(new_l);
570 EINA_MAGIC_CHECK_LIST(list, NULL);
572 l = list->accounting->last;
573 list->accounting->last = new_l;
578 _eina_list_update_accounting(list, new_l);
583 * @brief Prepends the given data to the given linked list.
585 * @param list The given list.
586 * @param data The data to prepend.
587 * @return A list pointer.
589 * This function prepends @p data to @p list. If @p list is @c NULL, a
590 * new list is returned. On success, a new list pointer that should be
591 * used in place of the one given to this function is
592 * returned. Otherwise, the old pointer is returned.
594 * The following example code demonstrates how to ensure that the
595 * given data has been successfully prepended.
599 * Eina_List *list = NULL;
600 * extern void *my_data;
602 * list = eina_list_prepend(list, my_data);
603 * if (eina_error_get())
605 * fprintf(stderr, "ERROR: Memory is low. List allocation failed.\n");
611 eina_list_prepend(Eina_List *list, const void *data)
616 new_l = _eina_list_mempool_list_new(list);
617 if (!new_l) return list;
621 new_l->data = (void *)data;
623 if (!list) return _eina_list_setup_accounting(new_l);
625 EINA_MAGIC_CHECK_LIST(list, NULL);
629 _eina_list_update_accounting(list, new_l);
635 * @brief Insert the given data into the given linked list after the specified data.
637 * @param list The given linked list.
638 * @param data The data to insert.
639 * @param relative The data to insert after.
640 * @return A list pointer.
642 * This function inserts @p data to @p list after @p relative. If
643 * @p relative is not in the list, @p data is appended to the end of
644 * the list. If @p list is @c NULL, a new list is returned. If there
645 * are multiple instances of @p relative in the list, @p data is
646 * inserted after the first instance.On success, a new list pointer
647 * that should be used in place of the one given to this function is
648 * returned. Otherwise, the old pointer is returned.
650 * The following example code demonstrates how to ensure that the
651 * given data has been successfully inserted.
654 * Eina_List *list = NULL;
655 * extern void *my_data;
656 * extern void *relative_member;
658 * list = eina_list_append(list, relative_member);
659 * if (eina_error_get())
661 * fprintf(stderr, "ERROR: Memory is low. List allocation failed.\n");
664 * list = eina_list_append_relative(list, my_data, relative_member);
665 * if (eina_error_get())
667 * fprintf(stderr, "ERROR: Memory is low. List allocation failed.\n");
673 eina_list_append_relative(Eina_List *list, const void *data, const void *relative)
678 if (list) EINA_MAGIC_CHECK_LIST(list, NULL);
680 EINA_LIST_FOREACH(list, l, list_data)
682 if (list_data == relative)
683 return eina_list_append_relative_list(list, data, l);
686 return eina_list_append(list, data);
690 * @brief Append a list node to a linked list after the specified member
692 * @param list The given linked list.
693 * @param data The data to insert.
694 * @param relative The list node to insert after.
695 * @return A list pointer.
697 * This function inserts @p data to @p list after the list node
698 * @p relative. If @p list or @p relative are @c NULL, @p data is just
699 * appended to @p list using eina_list_append(). If @p list is
700 * @c NULL, a new list is returned. If there are multiple instances
701 * of @p relative in the list, @p data is inserted after the first
702 * instance. On success, a new list pointer that should be used in
703 * place of the one given to this function is returned. Otherwise, the
704 * old pointer is returned.
707 eina_list_append_relative_list(Eina_List *list, const void *data, Eina_List *relative)
711 if ((!list) || (!relative)) return eina_list_append(list, data);
713 new_l = _eina_list_mempool_list_new(list);
714 if (!new_l) return list;
716 EINA_MAGIC_CHECK_LIST(relative, NULL);
717 new_l->next = relative->next;
718 new_l->data = (void *)data;
721 relative->next->prev = new_l;
723 relative->next = new_l;
724 new_l->prev = relative;
726 _eina_list_update_accounting(list, new_l);
729 new_l->accounting->last = new_l;
735 * @brief Prepend a data pointer to a linked list before the specified member
737 * @param list The given linked list.
738 * @param data The data to insert.
739 * @param relative The data to insert before.
740 * @return A list pointer.
742 * This function inserts @p data to @p list before @p relative. If
743 * @p relative is not in the list, @p data is prepended to the list
744 * with eina_list_prepend(). If @p list is @c NULL, a new list is
745 * returned. If there are multiple instances of @p relative in the
746 * list, @p data is inserted before the first instance. On success, a
747 * new list pointer that should be used in place of the one given to
748 * this function is returned. Otherwise, the old pointer is returned.
750 * The following code example demonstrates how to ensure that the
751 * given data has been successfully inserted.
754 * Eina_List *list = NULL;
755 * extern void *my_data;
756 * extern void *relative_member;
758 * list = eina_list_append(list, relative_member);
759 * if (eina_error_get_error())
761 * fprintf(stderr, "ERROR: Memory is low. List allocation failed.\n");
764 * list = eina_list_prepend_relative(list, my_data, relative_member);
765 * if (eina_error_get())
767 * fprintf(stderr, "ERROR: Memory is low. List allocation failed.\n");
773 eina_list_prepend_relative(Eina_List *list, const void *data, const void *relative)
778 if (list) EINA_MAGIC_CHECK_LIST(list, NULL);
780 EINA_LIST_FOREACH(list, l, list_data)
782 if (list_data == relative)
783 return eina_list_prepend_relative_list(list, data, l);
785 return eina_list_prepend(list, data);
789 * @brief Prepend a list node to a linked list before the specified member
791 * @param list The given linked list.
792 * @param data The data to insert.
793 * @param relative The list node to insert before.
794 * @return A list pointer.
796 * This function inserts @p data to @p list before the list node
797 * @p relative. If @p list or @p relative are @c NULL, @p data is just
798 * prepended to @p list using eina_list_prepend(). If @p list is
799 * @c NULL, a new list is returned. If there are multiple instances
800 * of @p relative in the list, @p data is inserted before the first
801 * instance. On success, a new list pointer that should be used in
802 * place of the one given to this function is returned. Otherwise, the
803 * old pointer is returned.
806 eina_list_prepend_relative_list(Eina_List *list, const void *data, Eina_List *relative)
810 if ((!list) || (!relative)) return eina_list_prepend(list, data);
812 new_l = _eina_list_mempool_list_new(list);
813 if (!new_l) return list;
815 EINA_MAGIC_CHECK_LIST(relative, NULL);
817 new_l->prev = relative->prev;
818 new_l->next = relative;
819 new_l->data = (void *)data;
821 if (relative->prev) relative->prev->next = new_l;
822 relative->prev = new_l;
824 _eina_list_update_accounting(list, new_l);
833 * @brief Insert a new node into a sorted list.
835 * @param list The given linked list, @b must be sorted.
836 * @param data The data to insert sorted.
837 * @return A list pointer.
839 * This function inserts values into a linked list assuming it was
840 * sorted and the result will be sorted. If @p list is @c NULLL, a new
841 * list is returned. On success, a new list pointer that should be
842 * used in place of the one given to this function is
843 * returned. Otherwise, the old pointer is returned. See eina_error_get().
845 * @note O(log2(n)) comparisons (calls to func) average/worst case
846 * performance as it uses eina_list_search_sorted_near_list() and thus
847 * is bounded to that. As said in eina_list_search_sorted_near_list(),
848 * lists do not have O(1) access time, so walking to the correct node
849 * can be costly, consider worst case to be almost O(n) pointer
850 * dereference (list walk).
853 eina_list_sorted_insert(Eina_List *list, Eina_Compare_Cb func, const void *data)
858 if (!list) return eina_list_append(NULL, data);
860 lnear = eina_list_search_sorted_near_list(list, func, data, &cmp);
862 return eina_list_append_relative_list(list, data, lnear);
864 return eina_list_prepend_relative_list(list, data, lnear);
868 * @brief Remove the first instance of the specified data from the given list.
870 * @param list The given list.
871 * @param data The specified data.
872 * @return A list pointer.
874 * This function removes the first instance of @p data from
875 * @p list. If the specified data is not in the given list (tihis
876 * include the case where @p data is @c NULL), nothing is done. If
877 * @p list is @c NULL, @c NULL is returned, otherwise a new list
878 * pointer that should be used in place of the one passed to this
882 eina_list_remove(Eina_List *list, const void *data)
886 if (list) EINA_MAGIC_CHECK_LIST(list, NULL);
888 l = eina_list_data_find_list(list, data);
889 return eina_list_remove_list(list, l);
893 * @brief Remove the specified data.
895 * @param list The given linked list.
896 * @param remove_list The list node which is to be removed.
897 * @return A list pointer.
899 * This function removes the list node @p remove_list from @p list and
900 * frees the list node structure @p remove_list. If @p list is
901 * @c NULL, this function returns @c NULL. If @p remove_list is
902 * @c NULL, it returns @p list, otherwise, a new list pointer that
903 * should be used in place of the one passed to this function.
905 * The following code gives an example (notice we use EINA_LIST_FOREACH
906 * instead of EINA_LIST_FOREACH_SAFE because we stop the loop after
907 * removing the current node).
910 * extern Eina_List *list;
912 * extern void *my_data;
915 * EINA_LIST_FOREACH(list, l, data)
917 * if (data == my_data)
919 * list = eina_list_remove_list(list, l);
926 eina_list_remove_list(Eina_List *list, Eina_List *remove_list)
930 if (!list) return NULL;
931 if (!remove_list) return list;
933 EINA_MAGIC_CHECK_LIST(remove_list, NULL);
935 if (remove_list->next) remove_list->next->prev = remove_list->prev;
936 if (remove_list->prev)
938 remove_list->prev->next = remove_list->next;
942 return_l = remove_list->next;
943 if (remove_list == remove_list->accounting->last)
945 EINA_MAGIC_CHECK_LIST(list, NULL);
946 list->accounting->last = remove_list->prev;
948 _eina_list_mempool_list_free(remove_list);
953 * @brief Free an entire list and all the nodes, ignoring the data contained.
955 * @param list The list to free
956 * @return A NULL pointer
958 * This function frees all the nodes of @p list. It does not free the
959 * data of the nodes. To free them, use #EINA_LIST_FREE.
962 eina_list_free(Eina_List *list)
964 Eina_List *l, *free_l;
966 if (!list) return NULL;
968 EINA_MAGIC_CHECK_LIST(list, NULL);
975 _eina_list_mempool_list_free(free_l);
982 * @brief Move the specified data to the head of the list.
984 * @param list The list handle to move the data.
985 * @param move_list The list node to move.
986 * @return A new list handle to replace the old one
988 * This function move @p move_list to the front of @p list. If list is
989 * @c NULL, @c NULL is returned. If @p move_list is @c NULL,
990 * @p list is returned. Otherwise, a new list pointer that should be
991 * used in place of the one passed to this function.
995 * extern Eina_List *list;
997 * extern void *my_data;
1000 * EINA_LIST_FOREACH(list, l, data)
1002 * if (data == my_data)
1004 * list = eina_list_promote_list(list, l);
1011 eina_list_promote_list(Eina_List *list, Eina_List *move_list)
1013 if (!list) return NULL;
1014 if (!move_list) return list;
1015 /* Promoting head to be head. */
1016 if (move_list == list) return list;
1017 if (move_list->next == list) return move_list;
1019 EINA_MAGIC_CHECK_LIST(list, NULL);
1020 EINA_MAGIC_CHECK_LIST(move_list, NULL);
1022 /* Remove the promoted item from the list. */
1023 if (!move_list->prev)
1024 move_list->next->prev = NULL;
1027 move_list->prev->next = move_list->next;
1028 if (move_list == list->accounting->last)
1029 list->accounting->last = move_list->prev;
1031 move_list->next->prev = move_list->prev;
1034 /* Add the promoted item in the list. */
1035 move_list->next = list;
1036 move_list->prev = list->prev;
1037 list->prev = move_list;
1038 if (move_list->prev)
1039 move_list->prev->next = move_list;
1045 * @brief Move the specified data to the tail of the list.
1047 * @param list The list handle to move the data.
1048 * @param move_list The list node to move.
1049 * @return A new list handle to replace the old one
1051 * This function move @p move_list to the back of @p list. If list is
1052 * @c NULL, @c NULL is returned. If @p move_list is @c NULL,
1053 * @p list is returned. Otherwise, a new list pointer that should be
1054 * used in place of the one passed to this function.
1058 * extern Eina_List *list;
1060 * extern void *my_data;
1063 * EINA_LIST_FOREACH(list, l, data)
1065 * if (data == my_data)
1067 * list = eina_list_demote_list(list, l);
1074 eina_list_demote_list(Eina_List *list, Eina_List *move_list)
1076 if (!list) return NULL;
1077 if (!move_list) return list;
1078 /* Demoting tail to be tail. */
1079 if (move_list == list->accounting->last) return list;
1081 EINA_MAGIC_CHECK_LIST(list, NULL);
1082 EINA_MAGIC_CHECK_LIST(move_list, NULL);
1084 /* Update pointer list if necessary. */
1085 if (list == move_list)
1086 list = move_list->next;
1087 /* Remove the demoted item from the list. */
1088 if (move_list->prev)
1089 move_list->prev->next = move_list->next;
1090 move_list->next->prev = move_list->prev;
1091 /* Add the demoted item in the list. */
1092 move_list->prev = list->accounting->last;
1093 move_list->prev->next = move_list;
1094 move_list->next = NULL;
1095 list->accounting->last = move_list;
1101 * @brief Find a member of a list and return the member.
1103 * @param list The list to search for a data.
1104 * @param data The data pointer to find in the list.
1105 * @return The found member data pointer if foun, @c NULL otherwise.
1107 * This function searches in @p list from beginning to end for the
1108 * first member whose data pointer is @p data. If it is found, @p data
1109 * will be returned, otherwise NULL will be returned.
1113 * extern Eina_List *list;
1114 * extern void *my_data;
1116 * if (eina_list_data_find(list, my_data) == my_data)
1118 * printf("Found member %p\n", my_data);
1123 eina_list_data_find(const Eina_List *list, const void *data)
1125 if (eina_list_data_find_list(list, data)) return (void*) data;
1130 * @brief Find a member of a list and return the list node containing that member.
1132 * @param list The list to search for data.
1133 * @param data The data pointer to find in the list.
1134 * @return The found members list node on success, @c NULL otherwise.
1136 * This function searches in @p list from beginning to end for the
1137 * first member whose data pointer is @p data. If it is found, the
1138 * list node containing the specified member is returned, otherwise
1139 * @c NULL is returned.
1142 eina_list_data_find_list(const Eina_List *list, const void *data)
1147 if (list) EINA_MAGIC_CHECK_LIST(list, NULL);
1149 EINA_LIST_FOREACH(list, l, list_data)
1151 if (list_data == data) return (Eina_List *)l;
1158 * @brief Get the nth member's data pointer in a list.
1160 * @param list The list to get the specified member number from.
1161 * @param n The number of the element (0 being the first).
1162 * @return The data pointer stored in the specified element.
1164 * This function returns the data pointer of element number @p n, in
1165 * the @p list. The first element in the array is element number 0. If
1166 * the element number @p n does not exist, @c NULL is
1167 * returned. Otherwise, the data of the found element is returned.
1170 eina_list_nth(const Eina_List *list, unsigned int n)
1174 l = eina_list_nth_list(list, n);
1175 return l ? l->data : NULL;
1179 * @brief Get the nth member's list node in a list.
1181 * @param list The list to get the specfied member number from.
1182 * @param n The number of the element (0 being the first).
1183 * @return The list node stored in the numbered element.
1185 * This function returns the list node of element number @p n, in
1186 * @ list. The first element in the array is element number 0. If the
1187 * element number @p n does not exist or @p list is @c NULL or @p n is
1188 * greater than the count of elements in @p list minus 1, @c NULL is
1189 * returned. Otherwise the list node stored in the numbered element is
1193 eina_list_nth_list(const Eina_List *list, unsigned int n)
1198 if (list) EINA_MAGIC_CHECK_LIST(list, NULL);
1200 /* check for non-existing nodes */
1201 if ((!list) || (n > (list->accounting->count - 1)))
1204 /* if the node is in the 2nd half of the list, search from the end
1205 * else, search from the beginning.
1207 if (n > (list->accounting->count / 2))
1209 for (i = list->accounting->count - 1,
1210 l = list->accounting->last;
1214 if (i == n) return (Eina_List *)l;
1219 for (i = 0, l = list; l; l = l->next, i++)
1221 if (i == n) return (Eina_List *)l;
1228 * @brief Get the last list node in the list.
1230 * @param list The list to get the last list node from.
1231 * @return The last list node in the list.
1233 * This function returns the last list node in the list. If @p list is
1234 * @c NULL or empty, @c NULL is returned.
1236 * This is a order-1 operation (it takes the same short time
1237 * regardless of the length of the list).
1239 static inline Eina_List *eina_list_last(const Eina_List *list);
1242 * @brief Get the next list node after the specified list node.
1244 * @param list The list node to get the next list node from
1245 * @return The next list node on success, @c NULL otherwise.
1247 * This function returns the next list node after the current one in
1248 * @p list. It is equivalent to list->next. If @p list is @c NULL or
1249 * if no next list node exists, it returns @c NULL.
1251 static inline Eina_List *eina_list_next(const Eina_List *list);
1254 * @brief Get the previous list node before the specified list node.
1256 * @param list The list node to get the previous list node from.
1257 * @return The previous list node o success, @c NULL otherwise.
1258 * if no previous list node exists
1260 * This function returns the previous list node before the current one
1261 * in @p list. It is equivalent to list->prev. If @p list is @c NULL or
1262 * if no previous list node exists, it returns @c NULL.
1264 static inline Eina_List *eina_list_prev(const Eina_List *list);
1267 * @brief Get the list node data member.
1269 * @param list The list node to get the data member of.
1270 * @return The data member from the list node.
1272 * This function returns the data member of the specified list node @p
1273 * list. It is equivalent to list->data. If @p list is @c NULL, this
1274 * function returns @c NULL.
1276 static inline void *eina_list_data_get(const Eina_List *list);
1279 * @brief Get the count of the number of items in a list.
1281 * @param list The list whose count to return.
1282 * @return The number of members in the list.
1284 * This function returns how many members @p list contains. If the
1285 * list is @c NULL, 0 is returned.
1287 * NB: This is an order-1 operation and takes the same tiem regardless
1288 * of the length of the list.
1290 static inline unsigned int eina_list_count(const Eina_List *list);
1293 * @brief Reverse all the elements in the list.
1295 * @param list The list to reverse.
1296 * @return The list head after it has been reversed.
1298 * This function reverses the order of all elements in @p list, so the
1299 * last member is now first, and so on. If @p list is @c NULL, this
1300 * functon returns @c NULL.
1302 * @note @b in-place: this will change the given list, so you should
1303 * now point to the new list head that is returned by this function.
1305 * @see eina_list_reverse_clone()
1306 * @see eina_list_iterator_reversed_new()
1309 eina_list_reverse(Eina_List *list)
1313 if (!list) return NULL;
1315 EINA_MAGIC_CHECK_LIST(list, NULL);
1318 l2 = list->accounting->last;
1324 l1->data = l2->data;
1327 if (l1 == l2) break;
1335 * @brief Clone (copy) all the elements in the list in reverse order.
1337 * @param list The list to reverse.
1338 * @return The new list that has been reversed.
1340 * This function reverses the order of all elements in @p list, so the
1341 * last member is now first, and so on. If @p list is @c NULL, this
1342 * functon returns @c NULL. This returns a copy of the given list.
1344 * @note @b copy: this will copy the list and you should then
1345 * eina_list_free() when it is not required anymore.
1347 * @see eina_list_reverse()
1348 * @see eina_list_clone()
1351 eina_list_reverse_clone(const Eina_List *list)
1357 if (!list) return NULL;
1359 EINA_MAGIC_CHECK_LIST(list, NULL);
1362 EINA_LIST_FOREACH(list, l, data)
1363 clone = eina_list_prepend(clone, data);
1369 * @brief Clone (copy) all the elements in the list in exact order.
1371 * @param list The list to clone.
1372 * @return The new list that has been cloned.
1374 * This function clone in order of all elements in @p list. If @p list
1375 * is @c NULL, this functon returns @c NULL. This returns a copy of
1378 * @note @b copy: this will copy the list and you should then
1379 * eina_list_free() when it is not required anymore.
1381 * @see eina_list_reverse_clone()
1384 eina_list_clone(const Eina_List *list)
1390 if (!list) return NULL;
1392 EINA_MAGIC_CHECK_LIST(list, NULL);
1395 EINA_LIST_FOREACH(list, l, data)
1396 clone = eina_list_append(clone, data);
1402 * @brief Sort a list according to the ordering func will return.
1404 * @param list The list handle to sort.
1405 * @param size The length of the list to sort.
1406 * @param func A function pointer that can handle comparing the list data
1408 * @return the new head of list.
1410 * This function sorts @p list. @p size if the number of the first
1411 * element to sort. If @p size is 0 or greater than the number of
1412 * elements in @p list, all the elemnts are sorted. @p func is used to
1413 * compare two elements of @p list. If @p list or @p func are @c NULL,
1414 * this function returns @c NULL.
1416 * @note @b in-place: this will change the given list, so you should
1417 * now point to the new list head that is returned by this function.
1419 * @note worst case is O(n * log2(n)) comparisons (calls to func()),
1420 * O(n) comparisons average case. That means that for 1,000,000 list
1421 * elements, sort will usually do 1,000,000 comparisons, but may do up
1427 * sort_cb(const void *d1, const void *d2)
1429 * const char *txt = NULL;
1430 * const char *txt2 = NULL;
1432 * if(!d1) return(1);
1433 * if(!d2) return(-1);
1435 * return(strcmp((const char*)d1, (const char*)d2));
1437 * extern Eina_List *list;
1439 * list = eina_list_sort(list, eina_list_count(list), sort_cb);
1443 eina_list_sort(Eina_List *list, unsigned int size, Eina_Compare_Cb func)
1447 Eina_List *tail = list;
1448 Eina_List *unsort = NULL;
1449 Eina_List *stack[EINA_LIST_SORT_STACK_SIZE];
1451 EINA_SAFETY_ON_NULL_RETURN_VAL(func, list);
1452 if (!list) return NULL;
1453 EINA_MAGIC_CHECK_LIST(list, NULL);
1455 /* if the caller specified an invalid size, sort the whole list */
1457 (size > list->accounting->count))
1458 size = list->accounting->count;
1460 if (size != list->accounting->count)
1462 unsort = eina_list_nth_list(list, size);
1464 unsort->prev->next = NULL;
1469 unsigned int idx, tmp;
1471 Eina_List *a = tail;
1472 Eina_List *b = tail->next;
1482 if (func(a->data, b->data) < 0)
1483 ((stack[i++] = a)->next = b)->next = 0;
1485 ((stack[i++] = b)->next = a)->next = 0;
1488 for (idx = n ^ tmp; idx &= idx - 1; i--)
1489 stack[i-2] = eina_list_sort_merge(stack[i-2], stack[i-1], func);
1493 stack[i-1] = eina_list_sort_merge(stack[i-1], stack[i], func);
1496 tail = eina_list_sort_rebuild_prev(list);
1500 tail->next = unsort;
1501 unsort->prev = tail;
1504 list->accounting->last = tail;
1510 * @brief Merge two list.
1512 * @param left Head list to merge.
1513 * @param right Tail list to merge.
1514 * @return A new merged list.
1516 * This function put right at the end of left and return the head.
1518 * Both left and right does not exist anymore after the merge.
1520 * @note merge cost is O(n), being @b n the size of the smallest
1521 * list. This is due the need to fix accounting of that segment,
1522 * making count and last access O(1).
1525 eina_list_merge(Eina_List *left, Eina_List *right)
1527 unsigned int n_left, n_right;
1529 if (!left) return right;
1530 if (!right) return left;
1532 left->accounting->last->next = right;
1533 right->prev = left->accounting->last;
1535 n_left = left->accounting->count;
1536 n_right = right->accounting->count;
1538 if (n_left >= n_right)
1540 Eina_List *itr = right;
1541 left->accounting->last = right->accounting->last;
1542 left->accounting->count += n_right;
1544 _eina_list_mempool_accounting_free(right->accounting);
1548 itr->accounting = left->accounting;
1555 Eina_List *itr = left->accounting->last;
1556 right->accounting->count += n_left;
1558 _eina_list_mempool_accounting_free(left->accounting);
1562 itr->accounting = right->accounting;
1572 * @brief Merge two sorted list according to the ordering func will return.
1574 * @param left First list to merge.
1575 * @param right Second list to merge.
1576 * @param func A function pointer that can handle comparing the list data
1578 * @return A new sorted list.
1580 * This function compare the head of @p left and @p right, and choose the
1581 * smallest one to be head of the returned list. It will continue this process
1582 * for all entry of both list.
1584 * Both left and right does not exist anymore after the merge.
1585 * If @p func is NULL, it will return NULL.
1590 * sort_cb(void *d1, void *d2)
1592 * const char *txt = NULL;
1593 * const char *txt2 = NULL;
1595 * if(!d1) return(1);
1596 * if(!d2) return(-1);
1598 * return(strcmp((const char*)d1, (const char*)d2));
1600 * extern Eina_List *sorted1;
1601 * extern Eina_List *sorted2;
1603 * list = eina_list_sorted_merge(sorted1, sorted2, sort_cb);
1607 eina_list_sorted_merge(Eina_List *left, Eina_List *right, Eina_Compare_Cb func)
1612 EINA_SAFETY_ON_NULL_RETURN_VAL(func, NULL);
1614 if (!left) return right;
1615 if (!right) return left;
1617 if (func(left->data, right->data) < 0)
1622 ret->accounting->count += right->accounting->count;
1624 _eina_list_mempool_accounting_free(right->accounting);
1630 right = right->next;
1631 ret->accounting->count += left->accounting->count;
1633 _eina_list_mempool_accounting_free(left->accounting);
1636 while (left && right)
1638 if (func(left->data, right->data) < 0)
1640 current->next = left;
1641 left->prev = current;
1646 current->next = right;
1647 right->prev = current;
1648 right = right->next;
1651 current = current->next;
1652 current->accounting = ret->accounting;
1657 current->next = left;
1658 left->prev = current;
1659 current->accounting = ret->accounting;
1664 current->next = right;
1665 right->prev = current;
1666 current->accounting = ret->accounting;
1669 while (current->next)
1671 current = current->next;
1672 current->accounting = ret->accounting;
1675 ret->accounting->last = current;
1681 * @brief Returns node nearest to data is in the sorted list.
1683 * @param list The list to search for data, @b must be sorted.
1684 * @param func A function pointer that can handle comparing the list data nodes.
1685 * @param data reference value to search.
1686 * @param result_cmp if provided returns the result of
1687 * func(node->data, data) node being the last (returned) node. If node
1688 * was found (exact match), then it is 0. If returned node is smaller
1689 * than requested data, it is less than 0 and if it's bigger it's
1690 * greater than 0. It is the last value returned by func().
1691 * @return the nearest node, NULL if not found.
1693 * This can be used to check if some value is inside the list and get
1694 * the nearest container node in this case. It should be used when list is
1695 * known to be sorted as it will do binary search for results.
1697 * Example: imagine user gives a string, you check if it's in the list
1698 * before duplicating its contents, otherwise you want to insert it
1699 * sorted. In this case you get the result of this function and either
1700 * append or prepend the value.
1702 * @note O(log2(n)) average/worst case performance, for 1,000,000
1703 * elements it will do a maximum of 20 comparisons. This is much
1704 * faster than the 1,000,000 comparisons made naively walking the list
1705 * from head to tail, so depending on the number of searches and
1706 * insertions, it may be worth to eina_list_sort() the list and do he
1707 * searches later. As lists do not have O(1) access time, walking to
1708 * the correct node can be costly, consider worst case to be almost
1709 * O(n) pointer dereference (list walk).
1711 * @see eina_list_search_sorted_list()
1712 * @see eina_list_sort()
1713 * @see eina_list_sorted_merge()
1716 eina_list_search_sorted_near_list(const Eina_List *list, Eina_Compare_Cb func, const void *data, int *result_cmp)
1718 const Eina_List *ct;
1719 unsigned int inf, sup, cur;
1724 if (result_cmp) *result_cmp = 0;
1728 if (list->accounting->count == 1)
1730 if (result_cmp) *result_cmp = func(list->data, data);
1731 return (Eina_List *)list;
1734 /* list walk is expensive, do quick check: tail */
1735 ct = list->accounting->last;
1736 cmp = func(ct->data, data);
1740 /* list walk is expensive, do quick check: head */
1742 cmp = func(ct->data, data);
1746 /* inclusive bounds */
1748 sup = list->accounting->count - 2;
1752 /* no loop, just compare if comparison value is important to caller */
1755 if (result_cmp) cmp = func(ct->data, data);
1761 unsigned int tmp = cur;
1762 cur = inf + ((sup - inf) >> 1);
1763 if (tmp < cur) for (; tmp != cur; tmp++, ct = ct->next);
1764 else if (tmp > cur) for (; tmp != cur; tmp--, ct = ct->prev);
1766 cmp = func(ct->data, data);
1781 if (result_cmp) *result_cmp = cmp;
1782 return (Eina_List *)ct;
1786 * @brief Returns node if data is in the sorted list.
1788 * @param list The list to search for data, @b must be sorted.
1789 * @param func A function pointer that can handle comparing the list data nodes.
1790 * @param data reference value to search.
1791 * @return the node if func(node->data, data) == 0, NULL if not found.
1793 * This can be used to check if some value is inside the list and get
1794 * the container node in this case. It should be used when list is
1795 * known to be sorted as it will do binary search for results.
1797 * Example: imagine user gives a string, you check if it's in the list
1798 * before duplicating its contents.
1800 * @note O(log2(n)) average/worst case performance, for 1,000,000
1801 * elements it will do a maximum of 20 comparisons. This is much
1802 * faster than the 1,000,000 comparisons made by
1803 * eina_list_search_unsorted_list(), so depending on the number of
1804 * searches and insertions, it may be worth to eina_list_sort() the
1805 * list and do he searches later. As said in
1806 * eina_list_search_sorted_near_list(), lists do not have O(1) access
1807 * time, so walking to the correct node can be costly, consider worst
1808 * case to be almost O(n) pointer dereference (list walk).
1810 * @see eina_list_search_sorted()
1811 * @see eina_list_sort()
1812 * @see eina_list_sorted_merge()
1813 * @see eina_list_search_unsorted_list()
1814 * @see eina_list_search_sorted_near_list()
1817 eina_list_search_sorted_list(const Eina_List *list, Eina_Compare_Cb func, const void *data)
1822 lnear = eina_list_search_sorted_near_list(list, func, data, &cmp);
1823 if (!lnear) return NULL;
1831 * @brief Returns node data if it is in the sorted list.
1833 * @param list The list to search for data, @b must be sorted.
1834 * @param func A function pointer that can handle comparing the list data nodes.
1835 * @param data reference value to search.
1836 * @return the node value (@c node->data) if func(node->data, data) == 0,
1837 * NULL if not found.
1839 * This can be used to check if some value is inside the list and get
1840 * the existing instance in this case. It should be used when list is
1841 * known to be sorted as it will do binary search for results.
1843 * Example: imagine user gives a string, you check if it's in the list
1844 * before duplicating its contents.
1846 * @note O(log2(n)) average/worst case performance, for 1,000,000
1847 * elements it will do a maximum of 20 comparisons. This is much
1848 * faster than the 1,000,000 comparisons made by
1849 * eina_list_search_unsorted(), so depending on the number of
1850 * searches and insertions, it may be worth to eina_list_sort() the
1851 * list and do he searches later. As said in
1852 * eina_list_search_sorted_near_list(), lists do not have O(1) access
1853 * time, so walking to the correct node can be costly, consider worst
1854 * case to be almost O(n) pointer dereference (list walk).
1856 * @see eina_list_search_sorted_list()
1857 * @see eina_list_sort()
1858 * @see eina_list_sorted_merge()
1859 * @see eina_list_search_unsorted_list()
1862 eina_list_search_sorted(const Eina_List *list, Eina_Compare_Cb func, const void *data)
1864 return eina_list_data_get(eina_list_search_sorted_list(list, func, data));
1868 * @brief Returns node if data is in the unsorted list.
1870 * @param list The list to search for data, may be unsorted.
1871 * @param func A function pointer that can handle comparing the list data nodes.
1872 * @param data reference value to search.
1873 * @return the node if func(node->data, data) == 0, NULL if not found.
1875 * This can be used to check if some value is inside the list and get
1876 * the container node in this case.
1878 * Example: imagine user gives a string, you check if it's in the list
1879 * before duplicating its contents.
1881 * @note this is expensive and may walk the whole list, it's order-N,
1882 * that is for 1,000,000 elements list it may walk and compare
1885 * @see eina_list_search_sorted_list()
1886 * @see eina_list_search_unsorted()
1889 eina_list_search_unsorted_list(const Eina_List *list, Eina_Compare_Cb func, const void *data)
1894 EINA_LIST_FOREACH(list, l, d)
1897 return (Eina_List*) l;
1903 * @brief Returns node data if it is in the unsorted list.
1905 * @param list The list to search for data, may be unsorted.
1906 * @param func A function pointer that can handle comparing the list data nodes.
1907 * @param data reference value to search.
1908 * @return the node value (@c node->data) if func(node->data, data) == 0,
1909 * NULL if not found.
1911 * This can be used to check if some value is inside the list and get
1912 * the existing instance in this case.
1914 * Example: imagine user gives a string, you check if it's in the list
1915 * before duplicating its contents.
1917 * @note this is expensive and may walk the whole list, it's order-N,
1918 * that is for 1,000,000 elements list it may walk and compare
1921 * @see eina_list_search_sorted()
1922 * @see eina_list_search_unsorted_list()
1925 eina_list_search_unsorted(const Eina_List *list, Eina_Compare_Cb func, const void *data)
1927 return eina_list_data_get(eina_list_search_unsorted_list(list, func, data));
1932 * @brief Returned a new iterator asociated to a list.
1934 * @param list The list.
1935 * @return A new iterator.
1937 * This function returns a newly allocated iterator associated to @p
1938 * list. If @p list is @c NULL or the count member of @p list is less
1939 * or equal than 0, this function still returns a valid iterator that
1940 * will always return false on eina_iterator_next(), thus keeping API
1943 * If the memory can not be allocated, NULL is returned and
1944 * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
1947 * @warning if the list structure changes then the iterator becomes
1948 * invalid! That is, if you add or remove nodes this iterator
1949 * behavior is undefined and your program may crash!
1951 EAPI Eina_Iterator *
1952 eina_list_iterator_new(const Eina_List *list)
1954 Eina_Iterator_List *it;
1957 it = calloc(1, sizeof (Eina_Iterator_List));
1959 eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
1963 EINA_MAGIC_SET(it, EINA_MAGIC_LIST_ITERATOR);
1964 EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);
1969 it->iterator.next = FUNC_ITERATOR_NEXT(eina_list_iterator_next);
1970 it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(eina_list_iterator_get_container);
1971 it->iterator.free = FUNC_ITERATOR_FREE(eina_list_iterator_free);
1973 return &it->iterator;
1977 * @brief Returned a new reversed iterator asociated to a list.
1979 * @param list The list.
1980 * @return A new iterator.
1982 * This function returns a newly allocated iterator associated to @p
1983 * list. If @p list is @c NULL or the count member of @p list is less
1984 * or equal than 0, this function still returns a valid iterator that
1985 * will always return false on eina_iterator_next(), thus keeping API
1988 * Unlike eina_list_iterator_new(), this will walk the list backwards.
1990 * If the memory can not be allocated, NULL is returned and
1991 * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
1994 * @warning if the list structure changes then the iterator becomes
1995 * invalid! That is, if you add or remove nodes this iterator
1996 * behavior is undefined and your program may crash!
1998 EAPI Eina_Iterator *
1999 eina_list_iterator_reversed_new(const Eina_List *list)
2001 Eina_Iterator_List *it;
2004 it = calloc(1, sizeof (Eina_Iterator_List));
2006 eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
2010 EINA_MAGIC_SET(it, EINA_MAGIC_LIST_ITERATOR);
2011 EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);
2013 it->head = eina_list_last(list);
2014 it->current = it->head;
2016 it->iterator.next = FUNC_ITERATOR_NEXT(eina_list_iterator_prev);
2017 it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(eina_list_iterator_get_container);
2018 it->iterator.free = FUNC_ITERATOR_FREE(eina_list_iterator_free);
2020 return &it->iterator;
2024 * @brief Returned a new accessor asociated to a list.
2026 * @param list The list.
2027 * @return A new accessor.
2029 * This function returns a newly allocated accessor associated to
2030 * @p list. If @p list is @c NULL or the count member of @p list is
2031 * less or equal than 0, this function returns NULL. If the memory can
2032 * not be allocated, NULL is returned and #EINA_ERROR_OUT_OF_MEMORY is
2033 * set. Otherwise, a valid accessor is returned.
2035 EAPI Eina_Accessor *
2036 eina_list_accessor_new(const Eina_List *list)
2038 Eina_Accessor_List *it;
2041 it = calloc(1, sizeof (Eina_Accessor_List));
2043 eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
2047 EINA_MAGIC_SET(it, EINA_MAGIC_LIST_ACCESSOR);
2048 EINA_MAGIC_SET(&it->accessor, EINA_MAGIC_ACCESSOR);
2054 it->accessor.get_at = FUNC_ACCESSOR_GET_AT(eina_list_accessor_get_at);
2055 it->accessor.get_container = FUNC_ACCESSOR_GET_CONTAINER(eina_list_accessor_get_container);
2056 it->accessor.free = FUNC_ACCESSOR_FREE(eina_list_accessor_free);
2058 return &it->accessor;