1 /* EINA - EFL data type library
2 * Copyright (C) 2002-2008 Carsten Haitzler, Vincent Torri
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
19 #ifndef EINA_INLIST_H_
20 #define EINA_INLIST_H_
22 #include "eina_types.h"
23 #include "eina_iterator.h"
24 #include "eina_accessor.h"
28 * @page eina_inlist_01_example_page Eina_Inlist basic usage
29 * @dontinclude eina_inlist_01.c
31 * To see the full source for this example, click here: @ref
34 * As explained before, inline lists mean its nodes pointers are part of same
35 * memory block/blob. This is done by using the macro @ref EINA_INLIST inside the
36 * data structure that will be used:
41 * The resulting node representing this struct can be exemplified by the
44 * @image html eina_inlist-node_eg1-my-struct.png
45 * @image rtf eina_inlist-node_eg1-my-struct.png
46 * @image latex eina_inlist-node_eg1-my-struct.eps
48 * Let's define a comparison function that will be used later during the
49 * sorting of the list:
54 * The @ref Eina_Inlist can be used exactly the same way as @ref Eina_List when
55 * appending, prepending and removing items. But since we already have the node
56 * pointers inside the structure, they need to be retrieved with the macro @ref
62 * Notice that @ref eina_inlist_append always receives the head of the list as
63 * first argument, and its return value should be used as the list pointer
69 * After appending 3 items, the list now should look similar to this:
71 * @image html eina_inlist-node_eg1-inlist.png
72 * @image rtf eina_inlist-node_eg1-inlist.png
73 * @image latex eina_inlist-node_eg1-inlist.eps width=\textwidth
75 * The macro @ref EINA_INLIST_FOREACH can be used to iterate over the list:
80 * @ref eina_inlist_promote(), @ref eina_inlist_demote(), @ref
81 * eina_inlist_append_relative() and similar functions all work in the same way
82 * as the @ref Eina_List :
84 * @skip eina_inlist_promote
85 * @until eina_inlist_demote
87 * Now let's use the @c sort_cb function declared above to sort our list:
89 * @skipline eina_inlist_sort
91 * Removing an element from the inlist is also similar to @ref Eina_List :
96 * Another way of walking through the inlist.
101 * Notice that in the previous piece of code, since we only have the pointers to
102 * the inlist nodes, we have to use the @ref EINA_INLIST_CONTAINER_GET macro
103 * that will return the pointer to the entire structure. Of course, in this case
104 * it is the same as the list pointer, since the @ref EINA_INLIST macro was used
105 * in the beginning of the structure.
107 * Now to finish this example, lets delete this list:
114 * @page eina_inlist_02_example_page Eina_Inlist advanced usage - lists and inlists
115 * @dontinclude eina_inlist_02.c
117 * This example describes the usage of @ref Eina_Inlist mixed with @ref
118 * Eina_List . We create and add elements to an inlist, and the even members
119 * are also added to a normal list. Later we remove the elements divisible by 3
120 * from this normal list.
122 * The struct that is going to be used is the same used in @ref
123 * eina_inlist_01_example_page , since we still need the @ref EINA_INLIST macro to
124 * declare the inlist node info:
129 * The resulting node representing this struct can be exemplified by the
132 * @image html eina_inlist-node_eg2-my-struct.png
133 * @image rtf eina_inlist-node_eg2-my-struct.png
134 * @image latex eina_inlist-node_eg2-my-struct.eps
136 * Now we need some pointers and auxiliar variables that will help us iterate on
142 * Allocating 100 elements and putting them into an inlist, and the even
143 * elements also go to the normal list:
148 * After this point, what we have are two distinct lists that share some
149 * elements. The first list (inlist) is defined by the pointers inside the
150 * elements data structure, while the second list (normal list) has its own node
151 * data structure that is kept outside of the elements.
153 * The two lists, sharing some elements, can be represented by the following
157 * <img src="eina_inlist-node_eg2-list-inlist.png" style="max-width: 100%;"/>
159 * @image rtf eina_inlist-node_eg2-list-inlist.png
160 * @image latex eina_inlist-node_eg2-list-inlist.eps width=\textwidth
162 * Accessing both lists is done normally, as if they didn't have any elements in
166 * @until eina_list_count
168 * We can remove elements from the normal list, but we just don't free them
169 * because they are still stored in the inlist:
171 * @skip EINA_LIST_FOREACH_SAFE
172 * @until eina_list_count
174 * To finish this example, we want to free both lists, we can't just free all
175 * elements on the second list (normal list) because they are still being used
176 * in the inlist. So we first discard the normal list without freeing its
177 * elements, then we free all elements in the inlist (that contains all elements
178 * allocated until now):
180 * @skip eina_list_free
183 * Here is the full source code for this example: @ref eina_inlist_02_c
187 * @page eina_inlist_03_example_page Eina_Inlist advanced usage - multi-inlists
188 * @dontinclude eina_inlist_03.c
190 * This example describes the usage of multiple inlists storing the same data.
191 * It means that some data may appear in more than one inlist at the same time.
192 * We will demonstrate this by creating an inlist with 100 numbers, and adding
193 * the odd numbers to the second inlist, then remove the numbers divisible by 3
194 * from the second list.
196 * To accomplish this, it is necessary to have two inlist pointers in the struct
197 * that is going to be stored. We are using the default inlist member @ref
198 * EINA_INLIST, and adding another member @c even that is of type @ref
204 * The representation for this struct is:
206 * @image html eina_inlist-node_eg3-my-struct.png
207 * @image rtf eina_inlist-node_eg3-my-struct.png
208 * @image latex eina_inlist-node_eg3-my-struct.eps
210 * And we will define some convenience macros that are equivalent to @ref
211 * EINA_INLIST_GET and @ref EINA_INLIST_CONTAINER_GET :
216 * We need two pointers, one for each list, and a pointer that will be used as
219 * @skipline Eina_Inlist
221 * Now we allocate and add to the first list every number from 0 to 99. These
222 * nodes data also have the @ref Eina_Inlist node info for the second list (@c
223 * even). We will use them to add just the even numbers to the second list, the
224 * @c list_even. Also notice that we are using our macro @c EVEN_INLIST_GET to
225 * get the pointer to the even list node info:
230 * And the resulting lists will be as follow:
233 * <img src="eina_inlist-node_eg3-two-inlists.png" style="max-width: 100%;"/>
235 * @image rtf eina_inlist-node_eg3-two-inlists.png
236 * @image latex eina_inlist-node_eg3-two-inlists.eps width=\textwidth
238 * For the first list, we can use the macro @ref EINA_INLIST_FOREACH to iterate
244 * But for the second list, we have to do it manually. Of course we could create
245 * a similar macro to @ref EINA_INLIST_FOREACH, but since this macro is more
246 * complex than the other two and we are using it only once, it's better to just
252 * Let's just check that the two lists have the expected number of elements:
255 * @until list_even count
257 * And removing the numbers divisible by 3 only from the second list:
260 * @until list_even count
262 * Now that we don't need the two lists anymore, we can just free all the items.
263 * Since all of the allocated data was put into the first list, and both lists
264 * are made of pointers to inside the data structures, we can free only the
265 * first list (that contains all the elements) and the second list will be gone
271 * To see the full source code for this example, click here: @ref
277 * @page eina_inlist_01_c eina_inlist_01.c Eina_Inlist basic usage source
278 * @include eina_inlist_01.c
282 * @page eina_inlist_02_c eina_inlist_02.c Eina_Inlist advanced usage - lists and inlists source
283 * @include eina_inlist_02.c
287 * @page eina_inlist_03_c eina_inlist_03.c Eina_Inlist advanced usage - multi-inlists source
288 * @include eina_inlist_03.c
292 * @addtogroup Eina_Inline_List_Group Inline List
294 * @brief These functions provide inline list management.
296 * Inline lists mean its nodes pointers are part of same memory as
297 * data. This has the benefit of fragmenting memory less and avoiding
298 * @c node->data indirection, but has the drawback of higher cost for some
299 * common operations like count and sort.
301 * It is possible to have inlist nodes to be part of regular lists, created with
302 * @ref eina_list_append() or @ref eina_list_prepend(). It's also possible to
303 * have a structure with two inlist pointers, thus be part of two different
304 * inlists at the same time, but the current convenience macros provided won't
305 * work for both of them. Consult @ref inlist_advanced for more info.
307 * Inline lists have their purposes, but if you don't know what those purposes are, go with
308 * regular lists instead.
310 * Tip: When using inlists in more than one place (that is, passing them around
311 * functions or keeping a pointer to them in a structure) it's more correct
312 * to keep a pointer to the first container, and not a pointer to the first
313 * inlist item (mostly they are the same, but that's not always correct).
314 * This lets the compiler to do type checking and let the programmer know
315 * exactly what type this list is.
317 * A simple example demonstrating the basic usage of an inlist can be found
318 * here: @ref eina_inlist_01_example_page
320 * @section inlist_algo Algorithm
322 * The basic structure can be represented by the following picture:
324 * @image html eina_inlist-node.png
325 * @image rtf eina_inlist-node.png
326 * @image latex eina_inlist-node.eps
328 * One data structure will also have the node information, with three pointers:
329 * @a prev, @a next and @a last. The @a last pointer is just valid for the first
330 * element (the list head), otherwise each insertion in the list would have to
331 * be done updating every node with the correct pointer. This means that it's
332 * always very important to keep a pointer to the first element of the list,
333 * since it is the only one that has the correct information to allow a proper
334 * O(1) append to the list.
336 * @section inlist_perf Performance
338 * Due to the nature of the inlist, there's no accounting information, and no
339 * easy access to the last element from each list node. This means that @ref
340 * eina_inlist_count() is order-N, while @ref eina_list_count() is order-1 (constant
343 * @section inlist_advanced Advanced Usage
345 * The basic usage considers a struct that will have the user data, and also
346 * have an inlist node information (prev, next and last pointers) created with
347 * @ref EINA_INLIST during the struct declaration. This allows one to use the
348 * convenience macros @ref EINA_INLIST_GET(), @ref EINA_INLIST_CONTAINER_GET(),
349 * @ref EINA_INLIST_FOREACH() and so. This happens because the @ref EINA_INLIST
350 * macro declares a struct member with the name @a __inlist, and all the other
351 * macros assume that this struct member has this name.
353 * It may be the case that someone needs to have some inlist nodes added to a
354 * @ref Eina_List too. If this happens, the inlist nodes can be added to the
355 * @ref Eina_List without any problems. This example demonstrates this case:
356 * @ref eina_inlist_02_example_page
358 * It's also possible to have some data that is part of two different inlists.
359 * If this is the case, then it won't be possible to use the convenience macros
360 * to both of the lists. It will be necessary to create a new set of macros that
361 * will allow access to the second list node info. An example for this usage can
363 * @ref eina_inlist_03_example_page
366 * @li @ref eina_inlist_01_example_page
367 * @li @ref eina_inlist_02_example_page
368 * @li @ref eina_inlist_03_example_page
372 * @addtogroup Eina_Data_Types_Group Data Types
378 * @addtogroup Eina_Containers_Group Containers
384 * @defgroup Eina_Inline_List_Group Inline List
390 * @typedef Eina_Inlist
393 typedef struct _Eina_Inlist Eina_Inlist;
396 * @typedef Eina_Inlist_Sorted_State
398 * State of sorted Eina_Inlist
400 typedef struct _Eina_Inlist_Sorted_State Eina_Inlist_Sorted_State;
403 * @struct _Eina_Inlist
408 Eina_Inlist *next; /**< next node */
409 Eina_Inlist *prev; /**< previous node */
410 Eina_Inlist *last; /**< last node */
412 /** Used for declaring an inlist member in a struct */
413 #define EINA_INLIST Eina_Inlist __in_list
414 /** Utility macro to get the inlist object of a struct */
415 #define EINA_INLIST_GET(Inlist) (& ((Inlist)->__in_list))
416 /** Utility macro to get the container object of an inlist */
417 #define EINA_INLIST_CONTAINER_GET(ptr, \
418 type) ((type *)((char *)ptr - \
419 offsetof(type, __in_list)))
423 * Add a new node to end of a list.
425 * @note this code is meant to be fast: appends are O(1) and do not
428 * @note @a in_item is considered to be in no list. If it was in another
429 * list before, eina_inlist_remove() it before adding. No
430 * check of @a new_l prev and next pointers is done, so it's safe
431 * to have them uninitialized.
433 * @param in_list existing list head or @c NULL to create a new list.
434 * @param in_item new list node, must not be @c NULL.
436 * @return the new list head. Use it and not @a in_list anymore.
438 EAPI Eina_Inlist *eina_inlist_append(Eina_Inlist *in_list,
439 Eina_Inlist *in_item) EINA_ARG_NONNULL(2) EINA_WARN_UNUSED_RESULT;
442 * Add a new node to beginning of list.
444 * @note this code is meant to be fast: appends are O(1) and do not
447 * @note @a new_l is considered to be in no list. If it was in another
448 * list before, eina_inlist_remove() it before adding. No
449 * check of @a new_l prev and next pointers is done, so it's safe
450 * to have them uninitialized.
452 * @param in_list existing list head or @c NULL to create a new list.
453 * @param in_item new list node, must not be @c NULL.
455 * @return the new list head. Use it and not @a in_list anymore.
457 EAPI Eina_Inlist *eina_inlist_prepend(Eina_Inlist *in_list,
458 Eina_Inlist *in_item) EINA_ARG_NONNULL(2) EINA_WARN_UNUSED_RESULT;
461 * Add a new node after the given relative item in list.
463 * @note this code is meant to be fast: appends are O(1) and do not
466 * @note @a in_item_l is considered to be in no list. If it was in another
467 * list before, eina_inlist_remove() it before adding. No
468 * check of @a in_item prev and next pointers is done, so it's safe
469 * to have them uninitialized.
471 * @note @a in_relative is considered to be inside @a in_list, no checks are
472 * done to confirm that and giving nodes from different lists
473 * will lead to problems. Giving NULL @a in_relative is the same as
474 * eina_list_append().
476 * @param in_list existing list head or @c NULL to create a new list.
477 * @param in_item new list node, must not be @c NULL.
478 * @param in_relative reference node, @a in_item will be added after it.
480 * @return the new list head. Use it and not @a list anymore.
482 EAPI Eina_Inlist *eina_inlist_append_relative(Eina_Inlist *in_list,
483 Eina_Inlist *in_item,
484 Eina_Inlist *in_relative) EINA_ARG_NONNULL(2) EINA_WARN_UNUSED_RESULT;
487 * Add a new node before the given relative item in list.
489 * @note this code is meant to be fast: appends are O(1) and do not
492 * @note @a in_item is considered to be in no list. If it was in another
493 * list before, eina_inlist_remove() it before adding. No
494 * check of @a in_item prev and next pointers is done, so it's safe
495 * to have them uninitialized.
497 * @note @a in_relative is considered to be inside @a in_list, no checks are
498 * done to confirm that and giving nodes from different lists
499 * will lead to problems. Giving NULL @a in_relative is the same as
500 * eina_list_prepend().
502 * @param in_list existing list head or @c NULL to create a new list.
503 * @param in_item new list node, must not be @c NULL.
504 * @param in_relative reference node, @a in_item will be added before it.
506 * @return the new list head. Use it and not @a in_list anymore.
508 EAPI Eina_Inlist *eina_inlist_prepend_relative(Eina_Inlist *in_list,
509 Eina_Inlist *in_item,
510 Eina_Inlist *in_relative) EINA_ARG_NONNULL(2) EINA_WARN_UNUSED_RESULT;
513 * Remove node from list.
515 * @note this code is meant to be fast: appends are O(1) and do not
518 * @note @a in_item is considered to be inside @a in_list, no checks are
519 * done to confirm that and giving nodes from different lists
520 * will lead to problems, especially if @a in_item is the head since
521 * it will be different from @a list and the wrong new head will
524 * @param in_list existing list head, must not be @c NULL.
525 * @param in_item existing list node, must not be @c NULL.
527 * @return the new list head. Use it and not @a list anymore.
529 EAPI Eina_Inlist *eina_inlist_remove(Eina_Inlist *in_list,
530 Eina_Inlist *in_item) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT;
533 * Find given node in list, returns itself if found, NULL if not.
535 * @warning this is an expensive call and has O(n) cost, possibly
536 * walking the whole list.
538 * @param in_list existing list to search @a in_item in, must not be @c NULL.
539 * @param in_item what to search for, must not be @c NULL.
541 * @return @a in_item if found, @c NULL if not.
543 EAPI Eina_Inlist *eina_inlist_find(Eina_Inlist *in_list,
544 Eina_Inlist *in_item) EINA_ARG_NONNULL(2) EINA_WARN_UNUSED_RESULT;
547 * Move existing node to beginning of list.
549 * @note this code is meant to be fast: appends are O(1) and do not
552 * @note @a item is considered to be inside @a list. No checks are
553 * done to confirm this, and giving nodes from different lists
554 * will lead to problems.
556 * @param list existing list head or @c NULL to create a new list.
557 * @param item list node to move to beginning (head), must not be @c NULL.
559 * @return the new list head. Use it and not @a list anymore.
561 EAPI Eina_Inlist *eina_inlist_promote(Eina_Inlist *list,
562 Eina_Inlist *item) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT;
565 * Move existing node to end of list.
567 * @note this code is meant to be fast: appends are O(1) and do not
570 * @note @a item is considered to be inside @a list. No checks are
571 * done to confirm this, and giving nodes from different lists
572 * will lead to problems.
574 * @param list existing list head or @c NULL to create a new list.
575 * @param item list node to move to end (tail), must not be @c NULL.
577 * @return the new list head. Use it and not @a list anymore.
579 EAPI Eina_Inlist *eina_inlist_demote(Eina_Inlist *list,
580 Eina_Inlist *item) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT;
583 * @brief Get the count of the number of items in a list.
585 * @param list The list whose count to return.
586 * @return The number of members in the list.
588 * This function returns how many members @p list contains. If the
589 * list is @c NULL, @c 0 is returned.
591 * @warning This is an order-N operation and so the time will depend
592 * on the number of elements on the list, so, it might become
593 * slow for big lists!
595 EAPI unsigned int eina_inlist_count(const Eina_Inlist *list) EINA_WARN_UNUSED_RESULT;
599 * @brief Returns a new iterator associated to @a list.
601 * @param in_list The list.
602 * @return A new iterator.
604 * This function returns a newly allocated iterator associated to @p
605 * in_list. If @p in_list is @c NULL or the count member of @p in_list is less
606 * or equal than 0, this function still returns a valid iterator that
607 * will always return false on eina_iterator_next(), thus keeping API
610 * If the memory can not be allocated, @c NULL is returned
611 * and #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator
614 * @warning if the list structure changes then the iterator becomes
615 * invalid, and if you add or remove nodes iterator
616 * behavior is undefined, and your program may crash!
618 EAPI Eina_Iterator *eina_inlist_iterator_new(const Eina_Inlist *in_list) EINA_MALLOC EINA_WARN_UNUSED_RESULT;
621 * @brief Returns a new accessor associated to a list.
623 * @param in_list The list.
624 * @return A new accessor.
626 * This function returns a newly allocated accessor associated to
627 * @p in_list. If @p in_list is @c NULL or the count member of @p in_list is
628 * less or equal than @c 0, this function returns @c NULL. If the memory can
629 * not be allocated, @c NULL is returned and #EINA_ERROR_OUT_OF_MEMORY is
630 * set. Otherwise, a valid accessor is returned.
632 EAPI Eina_Accessor *eina_inlist_accessor_new(const Eina_Inlist *in_list) EINA_MALLOC EINA_WARN_UNUSED_RESULT;
635 * @brief Insert a new node into a sorted list.
637 * @param list The given linked list, @b must be sorted.
638 * @param item list node to insert, must not be @c NULL.
639 * @param func The function called for the sort.
640 * @return A list pointer.
643 * This function inserts item into a linked list assuming it was
644 * sorted and the result will be sorted. If @p list is @c NULLL, item
645 * is returned. On success, a new list pointer that should be
646 * used in place of the one given to this function is
647 * returned. Otherwise, the old pointer is returned. See eina_error_get().
649 * @note O(log2(n)) comparisons (calls to @p func) average/worst case
650 * performance. As said in eina_list_search_sorted_near_list(),
651 * lists do not have O(1) access time, so walking to the correct node
652 * can be costly, consider worst case to be almost O(n) pointer
653 * dereference (list walk).
655 EAPI Eina_Inlist *eina_inlist_sorted_insert(Eina_Inlist *list, Eina_Inlist *item, Eina_Compare_Cb func) EINA_ARG_NONNULL(2, 3) EINA_WARN_UNUSED_RESULT;
658 * @brief Create state with valid data in it.
660 * @return A valid Eina_Inlist_Sorted_State.
663 * See eina_inlist_sorted_state_insert() for more information.
665 EAPI Eina_Inlist_Sorted_State *eina_inlist_sorted_state_new(void);
668 * @brief Force an Eina_Inlist_Sorted_State to match the content of a list.
670 * @param state The state to update
671 * @param list The list to match
672 * @return The number of item in the actually in the list
675 * See eina_inlist_sorted_state_insert() for more information. This function is
676 * usefull if you didn't use eina_inlist_sorted_state_insert() at some point, but
677 * still think you have a sorted list. It will only correctly work on a sorted list.
679 EAPI int eina_inlist_sorted_state_init(Eina_Inlist_Sorted_State *state, Eina_Inlist *list);
682 * @brief Free an Eina_Inlist_Sorted_State.
684 * @param state The state to destroy
687 * See eina_inlist_sorted_state_insert() for more information.
689 EAPI void eina_inlist_sorted_state_free(Eina_Inlist_Sorted_State *state);
692 * @brief Insert a new node into a sorted list.
694 * @param list The given linked list, @b must be sorted.
695 * @param item list node to insert, must not be @c NULL.
696 * @param func The function called for the sort.
697 * @param state The current array for initial dichotomic search
698 * @return A list pointer.
701 * This function inserts item into a linked list assuming @p state match
702 * the exact content order of the list. It use @p state to do a fast
703 * first step dichotomic search before starting to walk the inlist itself.
704 * This make this code much faster than eina_inlist_sorted_insert() as it
705 * doesn't need to walk the list at all. The result is of course a sorted
706 * list with an updated state.. If @p list is @c NULLL, item
707 * is returned. On success, a new list pointer that should be
708 * used in place of the one given to this function is
709 * returned. Otherwise, the old pointer is returned. See eina_error_get().
711 * @note O(log2(n)) comparisons (calls to @p func) average/worst case
712 * performance. As said in eina_list_search_sorted_near_list(),
713 * lists do not have O(1) access time, so walking to the correct node
714 * can be costly, but this version try to minimize that by making it a
715 * O(log2(n)) for number small number. After n == 256, it start to add a
716 * linear cost again. Consider worst case to be almost O(n) pointer
717 * dereference (list walk).
719 EAPI Eina_Inlist *eina_inlist_sorted_state_insert(Eina_Inlist *list,
721 Eina_Compare_Cb func,
722 Eina_Inlist_Sorted_State *state);
724 * @brief Sort a list according to the ordering func will return.
726 * @param head The list handle to sort.
727 * @param func A function pointer that can handle comparing the list data
729 * @return the new head of list.
731 * This function sorts all the elements of @p head. @p func is used to
732 * compare two elements of @p head. If @p head or @p func are @c NULL,
733 * this function returns @c NULL.
735 * @note @b in-place: this will change the given list, so you should
736 * now point to the new list head that is returned by this function.
738 * @note Worst case is O(n * log2(n)) comparisons (calls to func()).
739 * That means that for 1,000,000 list elements, sort will do 20,000,000
744 * typedef struct _Sort_Ex Sort_Ex;
752 * sort_cb(const Inlist *l1, const Inlist *l2)
757 * x1 = EINA_INLIST_CONTAINER_GET(l1, Sort_Ex);
758 * x2 = EINA_INLIST_CONTAINER_GET(l2, Sort_Ex);
760 * return(strcmp(x1->text, x2->text));
762 * extern Eina_Inlist *list;
764 * list = eina_inlist_sort(list, sort_cb);
767 EAPI Eina_Inlist *eina_inlist_sort(Eina_Inlist *head, Eina_Compare_Cb func);
769 /* This two macros are helpers for the _FOREACH ones, don't use them */
771 * @def _EINA_INLIST_OFFSET
772 * @param ref The reference to be used.
774 #define _EINA_INLIST_OFFSET(ref) ((char *)&(ref)->__in_list - (char *)(ref))
776 #if !defined(__cplusplus)
778 * @def _EINA_INLIST_CONTAINER
779 * @param ref The reference to be used.
780 * @param ptr The pointer to be used.
782 #define _EINA_INLIST_CONTAINER(ref, ptr) (void *)((char *)(ptr) - \
783 _EINA_INLIST_OFFSET(ref))
786 * In C++ we can't assign a "type*" pointer to void* so we rely on GCC's typeof
789 #define _EINA_INLIST_CONTAINER(ref, ptr) (typeof(ref))((char *)(ptr) - \
790 _EINA_INLIST_OFFSET(ref))
793 /** Macro to iterate over an inlist */
794 #define EINA_INLIST_FOREACH(list, l) \
795 for (l = NULL, l = (list ? _EINA_INLIST_CONTAINER(l, list) : NULL); l; \
796 l = (EINA_INLIST_GET(l)->next ? _EINA_INLIST_CONTAINER(l, EINA_INLIST_GET(l)->next) : NULL))
798 * @def EINA_INLIST_FOREACH_SAFE
799 * @param list The first list to be used.
800 * @param list2 The second list to be used.
801 * @param l The auxiliar variable to be used.
803 #define EINA_INLIST_FOREACH_SAFE(list, list2, l) \
804 for (l = (list ? _EINA_INLIST_CONTAINER(l, list) : NULL), list2 = l ? ((EINA_INLIST_GET(l) ? EINA_INLIST_GET(l)->next : NULL)) : NULL; \
806 l = _EINA_INLIST_CONTAINER(l, list2), list2 = list2 ? list2->next : NULL)
808 * @def EINA_INLIST_REVERSE_FOREACH
809 * @param list The list to be reversed.
810 * @param l The auxiliar variable to be used.
812 #define EINA_INLIST_REVERSE_FOREACH(list, l) \
813 for (l = NULL, l = (list ? _EINA_INLIST_CONTAINER(l, list->last) : NULL); \
814 l; l = (EINA_INLIST_GET(l)->prev ? _EINA_INLIST_CONTAINER(l, EINA_INLIST_GET(l)->prev) : NULL))
828 #endif /*EINA_INLIST_H_*/