

To see the full source for this example, click here\+: \hyperlink{eina_inlist_01_c}{eina\+\_\+inlist\+\_\+01.\+c Eina\+\_\+\+Inlist basic usage source}

As explained before, inline lists mean its nodes pointers are part of same memory block/blob. This is done by using the macro \hyperlink{group__Eina__Inline__List__Group_gab30a76fcfa50307c797491705d60136a}{E\+I\+N\+A\+\_\+\+I\+N\+L\+I\+ST} inside the data structure that will be used\+:


\begin{DoxyCodeInclude}
\textcolor{keyword}{struct }my\_struct \{
     \hyperlink{group__Eina__Inline__List__Group_gab30a76fcfa50307c797491705d60136a}{EINA\_INLIST};
     \textcolor{keywordtype}{int} a, b;
\};

\end{DoxyCodeInclude}


The resulting node representing this struct can be exemplified by the following picture\+:

  
\begin{DoxyImageNoCaption}
  \mbox{\includegraphics[width=\textwidth,height=\textheight/2,keepaspectratio=true]{eina_inlist-node_eg1-my-struct}}
\end{DoxyImageNoCaption}


Let\textquotesingle{}s define a comparison function that will be used later during the sorting of the list\+:


\begin{DoxyCodeInclude}
\textcolor{keywordtype}{int}
sort\_cb(\textcolor{keyword}{const} \textcolor{keywordtype}{void} *d1, \textcolor{keyword}{const} \textcolor{keywordtype}{void} *d2)
\{
   \textcolor{keyword}{const} \hyperlink{struct__Eina__Inlist}{Eina\_Inlist} *l1, *l2;
   \textcolor{keyword}{const} \textcolor{keyword}{struct }my\_struct *x1, *x2;

   l1 = d1;
   l2 = d2;

   x1 = \hyperlink{group__Eina__Inline__List__Group_ga65080f9f858bdbeebc7ea46d17feb180}{EINA\_INLIST\_CONTAINER\_GET}(l1, \textcolor{keyword}{struct} my\_struct);
   x2 = \hyperlink{group__Eina__Inline__List__Group_ga65080f9f858bdbeebc7ea46d17feb180}{EINA\_INLIST\_CONTAINER\_GET}(l2, \textcolor{keyword}{struct} my\_struct);

   \textcolor{keywordflow}{return} x1->a - x2->a;
\}

\end{DoxyCodeInclude}


The \hyperlink{group__Eina__Inline__List__Group_gad93973c87f867f249482e936797096ad}{Eina\+\_\+\+Inlist} can be used exactly the same way as \hyperlink{group__Eina__List__Group_ga3295e1210efea1fb0fd6acbf3cbc1f29}{Eina\+\_\+\+List} when appending, prepending and removing items. But since we already have the node pointers inside the structure, they need to be retrieved with the macro \hyperlink{group__Eina__Inline__List__Group_ga5bc991428e75557c6195f6b9e38321fb}{E\+I\+N\+A\+\_\+\+I\+N\+L\+I\+S\+T\+\_\+\+G\+ET} \+:


\begin{DoxyCodeInclude}
   d = malloc(\textcolor{keyword}{sizeof}(*d));
   d->a = 1;
   d->b = 10;
   list = \hyperlink{group__Eina__Inline__List__Group_ga095c6abf8b0122e30271ce98c26c0524}{eina\_inlist\_append}(NULL, \hyperlink{group__Eina__Inline__List__Group_ga5bc991428e75557c6195f6b9e38321fb}{EINA\_INLIST\_GET}(d));

\end{DoxyCodeInclude}


Notice that \hyperlink{group__Eina__Inline__List__Group_ga095c6abf8b0122e30271ce98c26c0524}{eina\+\_\+inlist\+\_\+append} always receives the head of the list as first argument, and its return value should be used as the list pointer (head)\+:


\begin{DoxyCodeInclude}
   d = malloc(\textcolor{keyword}{sizeof}(*d));
   d->a = 2;
   d->b = 20;
   list = \hyperlink{group__Eina__Inline__List__Group_ga095c6abf8b0122e30271ce98c26c0524}{eina\_inlist\_append}(list, \hyperlink{group__Eina__Inline__List__Group_ga5bc991428e75557c6195f6b9e38321fb}{EINA\_INLIST\_GET}(d));

\end{DoxyCodeInclude}


After appending 3 items, the list now should look similar to this\+:

  
\begin{DoxyImageNoCaption}
  \mbox{\includegraphics[width=\textwidth]{eina_inlist-node_eg1-inlist}}
\end{DoxyImageNoCaption}


The macro \hyperlink{group__Eina__Inline__List__Group_ga65e496b9b16d527d23c1d735652b4e62}{E\+I\+N\+A\+\_\+\+I\+N\+L\+I\+S\+T\+\_\+\+F\+O\+R\+E\+A\+CH} can be used to iterate over the list\+:


\begin{DoxyCodeInclude}
   printf(\textcolor{stringliteral}{"list=%p\(\backslash\)n"}, list);
   \hyperlink{group__Eina__Inline__List__Group_ga65e496b9b16d527d23c1d735652b4e62}{EINA\_INLIST\_FOREACH}(list, cur)
     printf("\(\backslash\)ta=%d, b=%d\(\backslash\)n", cur->a, cur->b);

\end{DoxyCodeInclude}


\hyperlink{group__Eina__Inline__List__Group_gaaad2933c1da2d5f11a07d7c259df89fe}{eina\+\_\+inlist\+\_\+promote()}, \hyperlink{group__Eina__Inline__List__Group_gabea5613e6992fb5bc07271c999f439e0}{eina\+\_\+inlist\+\_\+demote()}, \hyperlink{group__Eina__Inline__List__Group_ga188a035d7159c3856d8b608187064534}{eina\+\_\+inlist\+\_\+append\+\_\+relative()} and similar functions all work in the same way as the \hyperlink{group__Eina__List__Group_ga3295e1210efea1fb0fd6acbf3cbc1f29}{Eina\+\_\+\+List} \+:


\begin{DoxyCodeInclude}
   list = \hyperlink{group__Eina__Inline__List__Group_gaaad2933c1da2d5f11a07d7c259df89fe}{eina\_inlist\_promote}(list, \hyperlink{group__Eina__Inline__List__Group_ga5bc991428e75557c6195f6b9e38321fb}{EINA\_INLIST\_GET}(d));

   d = malloc(\textcolor{keyword}{sizeof}(*d));
   d->a = 4;
   d->b = 40;
   list = \hyperlink{group__Eina__Inline__List__Group_ga188a035d7159c3856d8b608187064534}{eina\_inlist\_append\_relative}(list, 
      \hyperlink{group__Eina__Inline__List__Group_ga5bc991428e75557c6195f6b9e38321fb}{EINA\_INLIST\_GET}(d), list);

   list = \hyperlink{group__Eina__Inline__List__Group_gabea5613e6992fb5bc07271c999f439e0}{eina\_inlist\_demote}(list, \hyperlink{group__Eina__Inline__List__Group_ga5bc991428e75557c6195f6b9e38321fb}{EINA\_INLIST\_GET}(d));

\end{DoxyCodeInclude}


Now let\textquotesingle{}s use the {\ttfamily sort\+\_\+cb} function declared above to sort our list\+:


\begin{DoxyCodeInclude}
   list = \hyperlink{group__Eina__Inline__List__Group_gac1132bfed4e77a5b63b0fb81d46f20c9}{eina\_inlist\_sort}(list, sort\_cb);

\end{DoxyCodeInclude}


Removing an element from the inlist is also similar to \hyperlink{group__Eina__List__Group_ga3295e1210efea1fb0fd6acbf3cbc1f29}{Eina\+\_\+\+List} \+:


\begin{DoxyCodeInclude}
   list = \hyperlink{group__Eina__Inline__List__Group_gad84d4a8ad4c3392df4be8278ce10a274}{eina\_inlist\_remove}(list, \hyperlink{group__Eina__Inline__List__Group_ga5bc991428e75557c6195f6b9e38321fb}{EINA\_INLIST\_GET}(d));
   free(d);

\end{DoxyCodeInclude}


Another way of walking through the inlist.


\begin{DoxyCodeInclude}
   \textcolor{keywordflow}{for} (itr = list; itr != NULL; itr = itr->\hyperlink{struct__Eina__List_aa4ab38c660af0b7f905125e2fe206891}{next})
     \{
        cur = \hyperlink{group__Eina__Inline__List__Group_ga65080f9f858bdbeebc7ea46d17feb180}{EINA\_INLIST\_CONTAINER\_GET}(itr, \textcolor{keyword}{struct} my\_struct);
        printf(\textcolor{stringliteral}{"\(\backslash\)ta=%d, b=%d\(\backslash\)n"}, cur->a, cur->b);
     \}

\end{DoxyCodeInclude}


Notice that in the previous piece of code, since we only have the pointers to the inlist nodes, we have to use the \hyperlink{group__Eina__Inline__List__Group_ga65080f9f858bdbeebc7ea46d17feb180}{E\+I\+N\+A\+\_\+\+I\+N\+L\+I\+S\+T\+\_\+\+C\+O\+N\+T\+A\+I\+N\+E\+R\+\_\+\+G\+ET} macro that will return the pointer to the entire structure. Of course, in this case it is the same as the list pointer, since the \hyperlink{group__Eina__Inline__List__Group_gab30a76fcfa50307c797491705d60136a}{E\+I\+N\+A\+\_\+\+I\+N\+L\+I\+ST} macro was used in the beginning of the structure.

Now to finish this example, lets delete this list\+:


\begin{DoxyCodeInclude}
   \textcolor{keywordflow}{while} (list)
     \{
        \textcolor{keyword}{struct }my\_struct *aux = \hyperlink{group__Eina__Inline__List__Group_ga65080f9f858bdbeebc7ea46d17feb180}{EINA\_INLIST\_CONTAINER\_GET}(list,
                                                          \textcolor{keyword}{struct} my\_struct);
        list = \hyperlink{group__Eina__Inline__List__Group_gad84d4a8ad4c3392df4be8278ce10a274}{eina\_inlist\_remove}(list, list);
        free(aux);
     \}

\end{DoxyCodeInclude}
 