\hypertarget{eina_hash_06_8c-example}{}\doxysection{eina\+\_\+hash\+\_\+06.\+c}
Same example as \mbox{\hyperlink{hash_01_example_page}{Eina\+\_\+\+Hash in action}} but using a \char`\"{}int64\char`\"{} hash table instead of \char`\"{}string superfast\char`\"{}.

Same example as \mbox{\hyperlink{hash_01_example_page}{Eina\+\_\+\+Hash in action}} but using a \char`\"{}int64\char`\"{} hash table instead of \char`\"{}string superfast\char`\"{}.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{comment}{//Compile with:}}
\DoxyCodeLine{\textcolor{comment}{//gcc -\/g eina\_hash\_06.c -\/o eina\_hash\_06 `pkg-\/config -\/-\/cflags -\/-\/libs eina`}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <stdio.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <string.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include <\mbox{\hyperlink{Eina_8h}{Eina.h}}>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{/*}}
\DoxyCodeLine{\textcolor{comment}{ * Eina Hash -\/ phonebook}}
\DoxyCodeLine{\textcolor{comment}{ *}}
\DoxyCodeLine{\textcolor{comment}{ * This example demonstrate the use of Eina Hash by implementing a phonebook}}
\DoxyCodeLine{\textcolor{comment}{ * that stores its contact data into the hash.}}
\DoxyCodeLine{\textcolor{comment}{ *}}
\DoxyCodeLine{\textcolor{comment}{ * It indexes the phone numbers by Contact Full Name, so it's a hash with}}
\DoxyCodeLine{\textcolor{comment}{ * string keys.}}
\DoxyCodeLine{\textcolor{comment}{ */}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{struct }\_Phone\_Entry \{}
\DoxyCodeLine{     int64\_t id; \textcolor{comment}{// Full name.}}
\DoxyCodeLine{     \textcolor{keyword}{const} \textcolor{keywordtype}{char} *number; \textcolor{comment}{// Phone number.}}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{typedef} \textcolor{keyword}{struct }\_Phone\_Entry Phone\_Entry;}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static} Phone\_Entry \_start\_entries[] = \{}
\DoxyCodeLine{       \{ 1, \textcolor{stringliteral}{"{}+01 23 456-\/78910"{}} \},}
\DoxyCodeLine{       \{ 2, \textcolor{stringliteral}{"{}+12 34 567-\/89101"{}} \},}
\DoxyCodeLine{       \{ 3, \textcolor{stringliteral}{"{}+23 45 678-\/91012"{}} \},}
\DoxyCodeLine{       \{ 4, \textcolor{stringliteral}{"{}+34 56 789-\/10123"{}} \},}
\DoxyCodeLine{       \{ -\/1, NULL \}}
\DoxyCodeLine{\}; \textcolor{comment}{// \_start\_entries}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static} \textcolor{keywordtype}{void}}
\DoxyCodeLine{\_phone\_entry\_free\_cb(\textcolor{keywordtype}{void} *data)}
\DoxyCodeLine{\{}
\DoxyCodeLine{   free(data);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static} \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_phone\_book\_foreach\_cb(\textcolor{keyword}{const} \mbox{\hyperlink{group__Eina__Hash__Group_ga9a89d7d3ee260d3a60572ebf651b07e0}{Eina\_Hash}} *phone\_book \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}}, \textcolor{keyword}{const} \textcolor{keywordtype}{void} *key,}
\DoxyCodeLine{                       \textcolor{keywordtype}{void} *data, \textcolor{keywordtype}{void} *fdata \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})}
\DoxyCodeLine{\{}
\DoxyCodeLine{   \textcolor{keyword}{const} int64\_t *\textcolor{keywordtype}{id} = key;}
\DoxyCodeLine{   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *number = data;}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"{}\%lld: \%s\(\backslash\)n"{}}, (\textcolor{keywordtype}{long} \textcolor{keywordtype}{long} \textcolor{keywordtype}{int})*\textcolor{keywordtype}{id}, number);}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// Return EINA\_FALSE to stop this callback from being called}}
\DoxyCodeLine{   \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}}
\DoxyCodeLine{main(\textcolor{keywordtype}{int} argc, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *argv[])}
\DoxyCodeLine{\{}
\DoxyCodeLine{   (void)argc;}
\DoxyCodeLine{   (void)argv;}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Hash__Group_ga9a89d7d3ee260d3a60572ebf651b07e0}{Eina\_Hash}} *phone\_book = NULL;}
\DoxyCodeLine{   \textcolor{keywordtype}{int} i;}
\DoxyCodeLine{   int64\_t entry\_id = 4;}
\DoxyCodeLine{   \textcolor{keywordtype}{char} *phone = NULL;}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}} r;}
\DoxyCodeLine{   \mbox{\hyperlink{struct__Eina__Iterator}{Eina\_Iterator}} *it;}
\DoxyCodeLine{   \textcolor{keywordtype}{void} *data;}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Main__Group_ga81b8137665b8a4d2cb38ca50017d4c74}{eina\_init}}();}
\DoxyCodeLine{}
\DoxyCodeLine{   phone\_book = \mbox{\hyperlink{group__Eina__Hash__Group_gae759c67394346cd9514a676b4ca8725f}{eina\_hash\_int64\_new}}(\_phone\_entry\_free\_cb);}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// Add initial entries to our hash}}
\DoxyCodeLine{   \textcolor{keywordflow}{for} (i = 0; \_start\_entries[i].id != -\/1; i++)}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        \mbox{\hyperlink{group__Eina__Hash__Group_ga5e72d0c5397936512407ce8cd66e98d6}{eina\_hash\_add}}(phone\_book, \&\_start\_entries[i].\textcolor{keywordtype}{id},}
\DoxyCodeLine{                      strdup(\_start\_entries[i].number));}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// Look for a specific entry and get its phone number}}
\DoxyCodeLine{   phone = \mbox{\hyperlink{group__Eina__Hash__Group_ga6e9dc02a249e1fcdb64e38e7dc47afb3}{eina\_hash\_find}}(phone\_book, \&entry\_id);}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (phone)}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"{}Printing entry.\(\backslash\)n"{}});}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"{}Id: \%lld\(\backslash\)n"{}}, (\textcolor{keywordtype}{long} \textcolor{keywordtype}{long} \textcolor{keywordtype}{int})entry\_id);}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"{}Number: \%s\(\backslash\)n\(\backslash\)n"{}}, phone);}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// Delete this entry}}
\DoxyCodeLine{   r = \mbox{\hyperlink{group__Eina__Hash__Group_ga315f85c5deae8e099e5a5a17d3557456}{eina\_hash\_del}}(phone\_book, \&entry\_id, NULL);}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"{}Hash entry successfully deleted? \%d\(\backslash\)n\(\backslash\)n"{}}, r);}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// Modify the pointer data of an entry and free the old one}}
\DoxyCodeLine{   int64\_t id3 = 3;}
\DoxyCodeLine{   phone = \mbox{\hyperlink{group__Eina__Hash__Group_ga74cea2c51afc5b5dc905bb204a562c51}{eina\_hash\_modify}}(phone\_book, \&id3,}
\DoxyCodeLine{                            strdup(\textcolor{stringliteral}{"{}+23 45 111-\/11111"{}}));}
\DoxyCodeLine{   free(phone);}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// Modify or add an entry to the hash with eina\_hash\_set}}
\DoxyCodeLine{   \textcolor{comment}{// Let's first add a new entry}}
\DoxyCodeLine{   int64\_t id5 = 5;}
\DoxyCodeLine{   phone = \mbox{\hyperlink{group__Eina__Hash__Group_gaec07a67481f4a4dc87f08d37a05a26f9}{eina\_hash\_set}}(phone\_book, \&id5,}
\DoxyCodeLine{                         strdup(\textcolor{stringliteral}{"{}+55 01 234-\/56789"{}}));}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (!phone)}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"{}No previous phone found for id5. "{}});}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"{}Creating new entry.\(\backslash\)n"{}});}
\DoxyCodeLine{     \}}
\DoxyCodeLine{   \textcolor{keywordflow}{else}}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"{}Old phone for id5 was \%s\(\backslash\)n"{}}, phone);}
\DoxyCodeLine{        free(phone);}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"{}\(\backslash\)n"{}});}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// Now change the phone number}}
\DoxyCodeLine{   phone = \mbox{\hyperlink{group__Eina__Hash__Group_gaec07a67481f4a4dc87f08d37a05a26f9}{eina\_hash\_set}}(phone\_book, \&id5,}
\DoxyCodeLine{                         strdup(\textcolor{stringliteral}{"{}+55 02 222-\/22222"{}}));}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (phone)}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"{}Changing phone for id5 to +55 02 222-\/22222. "{}});}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"{}Old phone was \%s\(\backslash\)n"{}}, phone);}
\DoxyCodeLine{        free(phone);}
\DoxyCodeLine{     \}}
\DoxyCodeLine{   \textcolor{keywordflow}{else}}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"{}No previous phone found for id5. "{}});}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"{}Creating new entry.\(\backslash\)n"{}});}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// There are many ways to iterate over our Phone book.}}
\DoxyCodeLine{   \textcolor{comment}{// First, iterate showing the names and associated numbers.}}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"{}List of phones:\(\backslash\)n"{}});}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Hash__Group_ga9237dbade8e59ac827992c7a5146cb20}{eina\_hash\_foreach}}(phone\_book, \_phone\_book\_foreach\_cb, NULL);}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"{}\(\backslash\)n"{}});}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// Now iterate using an iterator}}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"{}List of phones:\(\backslash\)n"{}});}
\DoxyCodeLine{   it = \mbox{\hyperlink{group__Eina__Hash__Group_ga7fcb2572c843c88ac8c81b61bd9845cc}{eina\_hash\_iterator\_tuple\_new}}(phone\_book);}
\DoxyCodeLine{   \textcolor{keywordflow}{while} (\mbox{\hyperlink{group__Eina__Iterator__Group_gad870f9b1d5fb8892cb9623ad0e7f75b2}{eina\_iterator\_next}}(it, \&data))}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        \mbox{\hyperlink{struct__Eina__Hash__Tuple}{Eina\_Hash\_Tuple}} *t = data;}
\DoxyCodeLine{        \textcolor{keyword}{const} int64\_t *\textcolor{keywordtype}{id} = t-\/>key;}
\DoxyCodeLine{        \textcolor{keyword}{const} \textcolor{keywordtype}{char} *number = t-\/>data;}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"{}\%lld: \%s\(\backslash\)n"{}}, (\textcolor{keywordtype}{long} \textcolor{keywordtype}{long} \textcolor{keywordtype}{int})*\textcolor{keywordtype}{id}, number);}
\DoxyCodeLine{     \}}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Iterator__Group_ga50da005e8d26c0a043d0d1c025bc9008}{eina\_iterator\_free}}(it); \textcolor{comment}{// Always free the iterator after its use}}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"{}\(\backslash\)n"{}});}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// Just iterate over the keys (names)}}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"{}List of ids in the phone book:\(\backslash\)n"{}});}
\DoxyCodeLine{   it = \mbox{\hyperlink{group__Eina__Hash__Group_gace4ade60f125b089adc14d63232fc88c}{eina\_hash\_iterator\_key\_new}}(phone\_book);}
\DoxyCodeLine{   \textcolor{keywordflow}{while} (\mbox{\hyperlink{group__Eina__Iterator__Group_gad870f9b1d5fb8892cb9623ad0e7f75b2}{eina\_iterator\_next}}(it, \&data))}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        \textcolor{keyword}{const} int64\_t *\textcolor{keywordtype}{id} = data;}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"{}\%lld\(\backslash\)n"{}}, (\textcolor{keywordtype}{long} \textcolor{keywordtype}{long} \textcolor{keywordtype}{int})*\textcolor{keywordtype}{id});}
\DoxyCodeLine{     \}}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Iterator__Group_ga50da005e8d26c0a043d0d1c025bc9008}{eina\_iterator\_free}}(it);}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"{}\(\backslash\)n"{}});}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// Just iterate over the data (numbers)}}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"{}List of numbers in the phone book:\(\backslash\)n"{}});}
\DoxyCodeLine{   it = \mbox{\hyperlink{group__Eina__Hash__Group_ga1fa6b5d170d65706416bfb1446d7b1b8}{eina\_hash\_iterator\_data\_new}}(phone\_book);}
\DoxyCodeLine{   \textcolor{keywordflow}{while} (\mbox{\hyperlink{group__Eina__Iterator__Group_gad870f9b1d5fb8892cb9623ad0e7f75b2}{eina\_iterator\_next}}(it, \&data))}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        \textcolor{keyword}{const} \textcolor{keywordtype}{char} *number = data;}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"{}\%s\(\backslash\)n"{}}, number);}
\DoxyCodeLine{     \}}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Iterator__Group_ga50da005e8d26c0a043d0d1c025bc9008}{eina\_iterator\_free}}(it);}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"{}\(\backslash\)n"{}});}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// Check how many items are in the phone book}}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"{}There are \%d items in the hash.\(\backslash\)n\(\backslash\)n"{}},}
\DoxyCodeLine{          \mbox{\hyperlink{group__Eina__Hash__Group_ga4bdeb2a1699703c62db697f229efe32d}{eina\_hash\_population}}(phone\_book));}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// Change the name (key) on an entry}}
\DoxyCodeLine{   int64\_t id6 = 6;}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Hash__Group_ga4b20a71099249899293d55161ef3bf35}{eina\_hash\_move}}(phone\_book, \&id5, \&id6);}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"{}List of phones after change:\(\backslash\)n"{}});}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Hash__Group_ga9237dbade8e59ac827992c7a5146cb20}{eina\_hash\_foreach}}(phone\_book, \_phone\_book\_foreach\_cb, NULL);}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"{}\(\backslash\)n"{}});}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// Empty the phone book, but don't destroy it}}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Hash__Group_ga42c4cb2fb8de9a2e2ef172cf5ea736f0}{eina\_hash\_free\_buckets}}(phone\_book);}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"{}There are \%d items in the hash.\(\backslash\)n\(\backslash\)n"{}},}
\DoxyCodeLine{          \mbox{\hyperlink{group__Eina__Hash__Group_ga4bdeb2a1699703c62db697f229efe32d}{eina\_hash\_population}}(phone\_book));}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// Phone book could still be used, but we are freeing it since we are}}
\DoxyCodeLine{   \textcolor{comment}{// done for now}}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Hash__Group_ga76829f09c99852b1536efc813d99fc78}{eina\_hash\_free}}(phone\_book);}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Main__Group_ga5bf35513199d5220341437c05696285d}{eina\_shutdown}}();}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 