

This example describes the usage of multiple inlists storing the same data. It means that some data may appear in more than one inlist at the same time. We will demonstrate this by creating an inlist with 100 numbers, and adding the odd numbers to the second inlist, then remove the numbers divisible by 3 from the second list.

To accomplish this, it is necessary to have two inlist pointers in the struct that is going to be stored. We are using the default inlist member \mbox{\hyperlink{group__Eina__Inline__List__Group_gab30a76fcfa50307c797491705d60136a}{EINA\+\_\+\+INLIST}}, and adding another member {\ttfamily even} that is of type \mbox{\hyperlink{group__Eina__Inline__List__Group_gad93973c87f867f249482e936797096ad}{Eina\+\_\+\+Inlist}} too\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{keyword}{struct\ }my\_struct\ \{}
\DoxyCodeLine{\ \ \ \ \ \mbox{\hyperlink{group__Eina__Inline__List__Group_gab30a76fcfa50307c797491705d60136a}{EINA\_INLIST}};}
\DoxyCodeLine{\ \ \ \ \ \mbox{\hyperlink{struct__Eina__Inlist}{Eina\_Inlist}}\ even;}
\DoxyCodeLine{\ \ \ \ \ \textcolor{keywordtype}{int}\ a,\ b;}
\DoxyCodeLine{\};}

\end{DoxyCodeInclude}
 The representation for this struct is\+:

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


And we will define some convenience macros that are equivalent to \mbox{\hyperlink{group__Eina__Inline__List__Group_ga5bc991428e75557c6195f6b9e38321fb}{EINA\+\_\+\+INLIST\+\_\+\+GET}} and \mbox{\hyperlink{group__Eina__Inline__List__Group_ga8c4e6011e7caa9ec5c22b003c4950338}{EINA\+\_\+\+INLIST\+\_\+\+CONTAINER\+\_\+\+GET}} \+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#define\ EVEN\_INLIST\_GET(Inlist)\ (\&\ ((Inlist)-\/>even))}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#define\ EVEN\_INLIST\_CONTAINER\_GET(ptr,\ type)\ \(\backslash\)}}
\DoxyCodeLine{\textcolor{preprocessor}{\ \ \ ((type\ *)((char\ *)ptr\ -\/\ offsetof(type,\ even)))}}

\end{DoxyCodeInclude}
 We need two pointers, one for each list, and a pointer that will be used as an iterator\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__Eina__Inlist}{Eina\_Inlist}}\ *list\ =\ NULL,\ *list\_even\ =\ NULL,\ *itr;}

\end{DoxyCodeInclude}
 Now we allocate and add to the first list every number from 0 to 99. These nodes data also have the \mbox{\hyperlink{group__Eina__Inline__List__Group_gad93973c87f867f249482e936797096ad}{Eina\+\_\+\+Inlist}} node info for the second list ({\ttfamily even}). We will use them to add just the even numbers to the second list, the {\ttfamily list\+\_\+even}. Also notice that we are using our macro {\ttfamily EVEN\+\_\+\+INLIST\+\_\+\+GET} to get the pointer to the even list node info\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ 100;\ i++)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ d\ =\ malloc(\textcolor{keyword}{sizeof}(*d));}
\DoxyCodeLine{\ \ \ \ \ \ \ \ d-\/>a\ =\ i;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ d-\/>b\ =\ i\ *\ 10;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ list\ =\ \mbox{\hyperlink{group__Eina__Inline__List__Group_gaef650e653f43178ecfee2c661bea7a85}{eina\_inlist\_append}}(list,\ \mbox{\hyperlink{group__Eina__Inline__List__Group_ga5bc991428e75557c6195f6b9e38321fb}{EINA\_INLIST\_GET}}(d));}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((i\ \%\ 2)\ ==\ 0)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ list\_even\ =\ \mbox{\hyperlink{group__Eina__Inline__List__Group_ga2cbaa546cd6224dde756d43a603ef463}{eina\_inlist\_prepend}}(list\_even,\ EVEN\_INLIST\_GET(d));}
\DoxyCodeLine{\ \ \ \ \ \}}

\end{DoxyCodeInclude}
 And the resulting lists will be as follow\+:

  
\begin{DoxyImageNoCaption}
  \mbox{\includegraphics[width=\textwidth]{eina_inlist-node_eg3-two-inlists}}
\end{DoxyImageNoCaption}


For the first list, we can use the macro \mbox{\hyperlink{group__Eina__Inline__List__Group_ga65e496b9b16d527d23c1d735652b4e62}{EINA\+\_\+\+INLIST\+\_\+\+FOREACH}} to iterate over its elements\+:


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

\end{DoxyCodeInclude}
 But for the second list, we have to do it manually. Of course we could create a similar macro to \mbox{\hyperlink{group__Eina__Inline__List__Group_ga65e496b9b16d527d23c1d735652b4e62}{EINA\+\_\+\+INLIST\+\_\+\+FOREACH}}, but since this macro is more complex than the other two and we are using it only once, it\textquotesingle{}s better to just do it manually\+:


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

\end{DoxyCodeInclude}
 Let\textquotesingle{}s just check that the two lists have the expected number of elements\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}list\ count=\%d\(\backslash\)n"{}},\ \mbox{\hyperlink{group__Eina__Inline__List__Group_ga3039bdea008dcb10d52f648d154690f8}{eina\_inlist\_count}}(list));}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}list\_even\ count=\%d\(\backslash\)n\(\backslash\)n"{}},\ \mbox{\hyperlink{group__Eina__Inline__List__Group_ga3039bdea008dcb10d52f648d154690f8}{eina\_inlist\_count}}(list\_even));}

\end{DoxyCodeInclude}
 And removing the numbers divisible by 3 only from the second list\+:


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ itr\ =\ list\_even;}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{while}\ (itr)}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \mbox{\hyperlink{struct__Eina__Inlist}{Eina\_Inlist}}\ *next\ =\ itr-\/>\mbox{\hyperlink{struct__Eina__Inlist_ae95deba8e271f94f8ad8aecf7fdb5dde}{next}};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ cur\ =\ EVEN\_INLIST\_CONTAINER\_GET(itr,\ \textcolor{keyword}{struct}\ my\_struct);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((cur-\/>a\ \%\ 3)\ ==\ 0)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ list\_even\ =\ \mbox{\hyperlink{group__Eina__Inline__List__Group_gaf885b08d5b5a750a901f3780d3c65338}{eina\_inlist\_remove}}(list\_even,\ itr);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ itr\ =\ next;}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}list\ count=\%d\(\backslash\)n"{}},\ \mbox{\hyperlink{group__Eina__Inline__List__Group_ga3039bdea008dcb10d52f648d154690f8}{eina\_inlist\_count}}(list));}
\DoxyCodeLine{\ \ \ printf(\textcolor{stringliteral}{"{}list\_even\ count=\%d\(\backslash\)n\(\backslash\)n"{}},\ \mbox{\hyperlink{group__Eina__Inline__List__Group_ga3039bdea008dcb10d52f648d154690f8}{eina\_inlist\_count}}(list\_even));}

\end{DoxyCodeInclude}
 Now that we don\textquotesingle{}t need the two lists anymore, we can just free all the items. Since all of the allocated data was put into the first list, and both lists are made of pointers to inside the data structures, we can free only the first list (that contains all the elements) and the second list will be gone with it\+:


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

\end{DoxyCodeInclude}
 To see the full source code for this example, click here\+: \mbox{\hyperlink{eina_inlist_03_c}{eina\+\_\+inlist\+\_\+03.\+c Eina\+\_\+\+Inlist advanced usage -\/ multi-\/inlists source}} 