EFL 1.7 svn doobies
[profile/ivi/eina.git] / src / include / eina_inlist.h
1 /* EINA - EFL data type library
2  * Copyright (C) 2002-2008 Carsten Haitzler, Vincent Torri
3  *
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.
8  *
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.
13  *
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/>.
17  */
18
19 #ifndef EINA_INLIST_H_
20 #define EINA_INLIST_H_
21
22 #include "eina_types.h"
23 #include "eina_iterator.h"
24 #include "eina_accessor.h"
25 #include <stddef.h>
26
27 /**
28  * @page eina_inlist_01_example_page Eina_Inlist basic usage
29  * @dontinclude eina_inlist_01.c
30  *
31  * To see the full source for this example, click here: @ref
32  * eina_inlist_01_c
33  *
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:
37  *
38  * @skip struct
39  * @until };
40  *
41  * The resulting node representing this struct can be exemplified by the
42  * following picture:
43  *
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
47  *
48  * Let's define a comparison function that will be used later during the
49  * sorting of the list:
50  *
51  * @skip int
52  * @until }
53  *
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
57  * EINA_INLIST_GET :
58  *
59  * @skip malloc
60  * @until append
61  *
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
64  * (head):
65  *
66  * @skip malloc
67  * @until append
68  *
69  * After appending 3 items, the list now should look similar to this:
70  *
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
74  *
75  * The macro @ref EINA_INLIST_FOREACH can be used to iterate over the list:
76  *
77  * @skip printf
78  * @until cur->a
79  *
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 :
83  *
84  * @skip eina_inlist_promote
85  * @until eina_inlist_demote
86  *
87  * Now let's use the @c sort_cb function declared above to sort our list:
88  *
89  * @skipline eina_inlist_sort
90  *
91  * Removing an element from the inlist is also similar to @ref Eina_List :
92  *
93  * @skip inlist_remove
94  * @until free
95  *
96  * Another way of walking through the inlist.
97  *
98  * @skip for
99  * @until }
100  *
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.
106  *
107  * Now to finish this example, lets delete this list:
108  *
109  * @skip while
110  * @until }
111  */
112
113 /**
114  * @page eina_inlist_02_example_page Eina_Inlist advanced usage - lists and inlists
115  * @dontinclude eina_inlist_02.c
116  *
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.
121  *
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:
125  *
126  * @skip struct
127  * @until };
128  *
129  * The resulting node representing this struct can be exemplified by the
130  * following picture:
131  *
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
135  *
136  * Now we need some pointers and auxiliar variables that will help us iterate on
137  * the lists:
138  *
139  * @skip struct
140  * @until l_next;
141  *
142  * Allocating 100 elements and putting them into an inlist, and the even
143  * elements also go to the normal list:
144  *
145  * @skip for
146  * @until }
147  *
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.
152  *
153  * The two lists, sharing some elements, can be represented by the following
154  * picture:
155  *
156  * @htmlonly
157  * <img src="eina_inlist-node_eg2-list-inlist.png" style="max-width: 100%;"/>
158  * @endhtmlonly
159  * @image rtf eina_inlist-node_eg2-list-inlist.png
160  * @image latex eina_inlist-node_eg2-list-inlist.eps width=\textwidth
161  *
162  * Accessing both lists is done normally, as if they didn't have any elements in
163  * common:
164  *
165  * @skip printf
166  * @until eina_list_count
167  *
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:
170  *
171  * @skip EINA_LIST_FOREACH_SAFE
172  * @until eina_list_count
173  *
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):
179  *
180  * @skip eina_list_free
181  * @until }
182  *
183  * Here is the full source code for this example: @ref eina_inlist_02_c
184  */
185
186 /**
187  * @page eina_inlist_03_example_page Eina_Inlist advanced usage - multi-inlists
188  * @dontinclude eina_inlist_03.c
189  *
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.
195  *
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
199  * Eina_Inlist too:
200  *
201  * @skip struct
202  * @until };
203  *
204  * The representation for this struct is:
205  *
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
209  *
210  * And we will define some convenience macros that are equivalent to @ref
211  * EINA_INLIST_GET and @ref EINA_INLIST_CONTAINER_GET :
212  *
213  * @skip define
214  * @until offsetof
215  *
216  * We need two pointers, one for each list, and a pointer that will be used as
217  * an iterator:
218  *
219  * @skipline Eina_Inlist
220  *
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:
226  *
227  * @skip for
228  * @until }
229  *
230  * And the resulting lists will be as follow:
231  *
232  * @htmlonly
233  * <img src="eina_inlist-node_eg3-two-inlists.png" style="max-width: 100%;"/>
234  * @endhtmlonly
235  * @image rtf eina_inlist-node_eg3-two-inlists.png
236  * @image latex eina_inlist-node_eg3-two-inlists.eps width=\textwidth
237  *
238  * For the first list, we can use the macro @ref EINA_INLIST_FOREACH to iterate
239  * over its elements:
240  *
241  * @skip FOREACH
242  * @until printf
243  *
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
247  * do it manually:
248  *
249  * @skip for
250  * @until }
251  *
252  * Let's just check that the two lists have the expected number of elements:
253  *
254  * @skip list count
255  * @until list_even count
256  *
257  * And removing the numbers divisible by 3 only from the second list:
258  *
259  * @skip itr
260  * @until list_even count
261  *
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
266  * with it:
267  *
268  * @skip while
269  * @until free
270  *
271  * To see the full source code for this example, click here: @ref
272  * eina_inlist_03_c
273  *
274  */
275
276 /**
277  * @page eina_inlist_01_c eina_inlist_01.c Eina_Inlist basic usage source
278  * @include eina_inlist_01.c
279  */
280
281 /**
282  * @page eina_inlist_02_c eina_inlist_02.c Eina_Inlist advanced usage - lists and inlists source
283  * @include eina_inlist_02.c
284  */
285
286 /**
287  * @page eina_inlist_03_c eina_inlist_03.c Eina_Inlist advanced usage - multi-inlists source
288  * @include eina_inlist_03.c
289  */
290
291 /**
292  * @addtogroup Eina_Inline_List_Group Inline List
293  *
294  * @brief These functions provide inline list management.
295  *
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.
300  *
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.
306  *
307  * Inline lists have their purposes, but if you don't know what those purposes are, go with
308  * regular lists instead.
309  *
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.
316  *
317  * A simple example demonstrating the basic usage of an inlist can be found
318  * here: @ref eina_inlist_01_example_page
319  *
320  * @section inlist_algo Algorithm
321  *
322  * The basic structure can be represented by the following picture:
323  *
324  * @image html eina_inlist-node.png
325  * @image rtf eina_inlist-node.png
326  * @image latex eina_inlist-node.eps
327  *
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.
335  *
336  * @section inlist_perf Performance
337  *
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
341  * time).
342  *
343  * @section inlist_advanced Advanced Usage
344  *
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.
352  *
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
357  *
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
362  * be found here:
363  * @ref eina_inlist_03_example_page
364  *
365  * List of examples:
366  * @li @ref eina_inlist_01_example_page
367  * @li @ref eina_inlist_02_example_page
368  * @li @ref eina_inlist_03_example_page
369  */
370
371 /**
372  * @addtogroup Eina_Data_Types_Group Data Types
373  *
374  * @{
375  */
376
377 /**
378  * @addtogroup Eina_Containers_Group Containers
379  *
380  * @{
381  */
382
383 /**
384  * @defgroup Eina_Inline_List_Group Inline List
385  *
386  * @{
387  */
388
389 /**
390  * @typedef Eina_Inlist
391  * Inlined list type.
392  */
393 typedef struct _Eina_Inlist Eina_Inlist;
394
395 /**
396  * @typedef Eina_Inlist_Sorted_State
397  * @since 1.1.0
398  * State of sorted Eina_Inlist
399  */
400 typedef struct _Eina_Inlist_Sorted_State Eina_Inlist_Sorted_State;
401
402 /**
403  * @struct _Eina_Inlist
404  * Inlined list type.
405  */
406 struct _Eina_Inlist
407 {
408    Eina_Inlist *next; /**< next node */
409    Eina_Inlist *prev; /**< previous node */
410    Eina_Inlist *last; /**< last node */
411 };
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)))
420
421
422 /**
423  * Add a new node to end of a list.
424  *
425  * @note this code is meant to be fast: appends are O(1) and do not
426  *       walk @a in_list.
427  *
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.
432  *
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.
435  *
436  * @return the new list head. Use it and not @a in_list anymore.
437  */
438 EAPI Eina_Inlist *eina_inlist_append(Eina_Inlist *in_list,
439                                      Eina_Inlist *in_item) EINA_ARG_NONNULL(2) EINA_WARN_UNUSED_RESULT;
440
441 /**
442  * Add a new node to beginning of list.
443  *
444  * @note this code is meant to be fast: appends are O(1) and do not
445  *       walk @a in_list.
446  *
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.
451  *
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.
454  *
455  * @return the new list head. Use it and not @a in_list anymore.
456  */
457 EAPI Eina_Inlist *eina_inlist_prepend(Eina_Inlist *in_list,
458                                       Eina_Inlist *in_item) EINA_ARG_NONNULL(2) EINA_WARN_UNUSED_RESULT;
459
460 /**
461  * Add a new node after the given relative item in list.
462  *
463  * @note this code is meant to be fast: appends are O(1) and do not
464  *       walk @a in_list.
465  *
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.
470  *
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().
475  *
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.
479  *
480  * @return the new list head. Use it and not @a list anymore.
481  */
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;
485
486 /**
487  * Add a new node before the given relative item in list.
488  *
489  * @note this code is meant to be fast: appends are O(1) and do not
490  *       walk @a in_list.
491  *
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.
496  *
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().
501  *
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.
505  *
506  * @return the new list head. Use it and not @a in_list anymore.
507  */
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;
511
512 /**
513  * Remove node from list.
514  *
515  * @note this code is meant to be fast: appends are O(1) and do not
516  *       walk @a list.
517  *
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
522  *       be returned.
523  *
524  * @param in_list existing list head, must not be @c NULL.
525  * @param in_item existing list node, must not be @c NULL.
526  *
527  * @return the new list head. Use it and not @a list anymore.
528  */
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;
531
532 /**
533  * Find given node in list, returns itself if found, NULL if not.
534  *
535  * @warning this is an expensive call and has O(n) cost, possibly
536  *    walking the whole list.
537  *
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.
540  *
541  * @return @a in_item if found, @c NULL if not.
542  */
543 EAPI Eina_Inlist   *eina_inlist_find(Eina_Inlist *in_list,
544                                      Eina_Inlist *in_item) EINA_ARG_NONNULL(2) EINA_WARN_UNUSED_RESULT;
545
546 /**
547  * Move existing node to beginning of list.
548  *
549  * @note this code is meant to be fast: appends are O(1) and do not
550  *       walk @a list.
551  *
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.
555  *
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.
558  *
559  * @return the new list head. Use it and not @a list anymore.
560  */
561 EAPI Eina_Inlist   *eina_inlist_promote(Eina_Inlist *list,
562                                         Eina_Inlist *item) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT;
563
564 /**
565  * Move existing node to end of list.
566  *
567  * @note this code is meant to be fast: appends are O(1) and do not
568  *       walk @a list.
569  *
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.
573  *
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.
576  *
577  * @return the new list head. Use it and not @a list anymore.
578  */
579 EAPI Eina_Inlist   *eina_inlist_demote(Eina_Inlist *list,
580                                        Eina_Inlist *item) EINA_ARG_NONNULL(1, 2) EINA_WARN_UNUSED_RESULT;
581
582 /**
583  * @brief Get the count of the number of items in a list.
584  *
585  * @param list The list whose count to return.
586  * @return The number of members in the list.
587  *
588  * This function returns how many members @p list contains. If the
589  * list is @c NULL, @c 0 is returned.
590  *
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!
594  */
595 EAPI unsigned int   eina_inlist_count(const Eina_Inlist *list) EINA_WARN_UNUSED_RESULT;
596
597
598 /**
599  * @brief Returns a new iterator associated to @a list.
600  *
601  * @param in_list The list.
602  * @return A new iterator.
603  *
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
608  * sane.
609  *
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
612  * is returned.
613  *
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!
617  */
618 EAPI Eina_Iterator *eina_inlist_iterator_new(const Eina_Inlist *in_list) EINA_MALLOC EINA_WARN_UNUSED_RESULT;
619
620 /**
621  * @brief Returns a new accessor associated to a list.
622  *
623  * @param in_list The list.
624  * @return A new accessor.
625  *
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.
631  */
632 EAPI Eina_Accessor *eina_inlist_accessor_new(const Eina_Inlist *in_list) EINA_MALLOC EINA_WARN_UNUSED_RESULT;
633
634 /**
635  * @brief Insert a new node into a sorted list.
636  *
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.
641  * @since 1.1.0
642  *
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().
648  *
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).
654  */
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;
656
657 /**
658  * @brief Create state with valid data in it.
659  *
660  * @return A valid Eina_Inlist_Sorted_State.
661  * @since 1.1.0
662  *
663  * See eina_inlist_sorted_state_insert() for more information.
664  */
665 EAPI Eina_Inlist_Sorted_State *eina_inlist_sorted_state_new(void);
666
667 /**
668  * @brief Force an Eina_Inlist_Sorted_State to match the content of a list.
669  *
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
673  * @since 1.1.0
674  *
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.
678  */
679 EAPI int eina_inlist_sorted_state_init(Eina_Inlist_Sorted_State *state, Eina_Inlist *list);
680
681 /**
682  * @brief Free an Eina_Inlist_Sorted_State.
683  *
684  * @param state The state to destroy
685  * @since 1.1.0
686  *
687  * See eina_inlist_sorted_state_insert() for more information.
688  */
689 EAPI void eina_inlist_sorted_state_free(Eina_Inlist_Sorted_State *state);
690
691 /**
692  * @brief Insert a new node into a sorted list.
693  *
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.
699  * @since 1.1.0
700  *
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().
710  *
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).
718  */
719 EAPI Eina_Inlist *eina_inlist_sorted_state_insert(Eina_Inlist *list,
720                                                   Eina_Inlist *item,
721                                                   Eina_Compare_Cb func,
722                                                   Eina_Inlist_Sorted_State *state);
723 /**
724  * @brief Sort a list according to the ordering func will return.
725  *
726  * @param head The list handle to sort.
727  * @param func A function pointer that can handle comparing the list data
728  * nodes.
729  * @return the new head of list.
730  *
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.
734  *
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.
737  *
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
740  * comparisons.
741  *
742  * Example:
743  * @code
744  * typedef struct _Sort_Ex Sort_Ex;
745  * struct _Sort_Ex
746  * {
747  *   INLIST;
748  *   const char *text;
749  * };
750  *
751  * int
752  * sort_cb(const Inlist *l1, const Inlist *l2)
753  * {
754  *    const Sort_Ex *x1;
755  *    const Sort_Ex *x2;
756  *
757  *    x1 = EINA_INLIST_CONTAINER_GET(l1, Sort_Ex);
758  *    x2 = EINA_INLIST_CONTAINER_GET(l2, Sort_Ex);
759  *
760  *    return(strcmp(x1->text, x2->text));
761  * }
762  * extern Eina_Inlist *list;
763  *
764  * list = eina_inlist_sort(list, sort_cb);
765  * @endcode
766  */
767 EAPI Eina_Inlist *eina_inlist_sort(Eina_Inlist *head, Eina_Compare_Cb func);
768
769 /* This two macros are helpers for the _FOREACH ones, don't use them */
770 /**
771  * @def _EINA_INLIST_OFFSET
772  * @param ref The reference to be used.
773  */
774 #define _EINA_INLIST_OFFSET(ref)         ((char *)&(ref)->__in_list - (char *)(ref))
775
776 #if !defined(__cplusplus)
777 /**
778  * @def _EINA_INLIST_CONTAINER
779  * @param ref The reference to be used.
780  * @param ptr The pointer to be used.
781  */
782 #define _EINA_INLIST_CONTAINER(ref, ptr) (void *)((char *)(ptr) - \
783                                                   _EINA_INLIST_OFFSET(ref))
784 #else
785 /*
786  * In C++ we can't assign a "type*" pointer to void* so we rely on GCC's typeof
787  * operator.
788  */
789 #define _EINA_INLIST_CONTAINER(ref, ptr) (typeof(ref))((char *)(ptr) - \
790                                                   _EINA_INLIST_OFFSET(ref))
791 #endif
792
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))
797 /**
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.
802  */
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; \
805         l; \
806         l = _EINA_INLIST_CONTAINER(l, list2), list2 = list2 ? list2->next : NULL)
807 /**
808  * @def EINA_INLIST_REVERSE_FOREACH
809  * @param list The list to be reversed.
810  * @param l The auxiliar variable to be used.
811  */
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))
815
816 /**
817  * @}
818  */
819
820 /**
821  * @}
822  */
823
824 /**
825  * @}
826  */
827
828 #endif /*EINA_INLIST_H_*/