\hypertarget{eina_hash_08_8c-example}{}\doxysection{eina\+\_\+hash\+\_\+08.\+c}
This example shows the the usage of \mbox{\hyperlink{group__Eina__Hash__Group_gafe3bb7c1f5616b4e158f60322e133260}{eina\+\_\+hash\+\_\+add()}}, \mbox{\hyperlink{group__Eina__Hash__Group_ga4637f61969ecc13adaca17def6ed1a91}{eina\+\_\+hash\+\_\+add\+\_\+by\+\_\+hash()}}, \mbox{\hyperlink{group__Eina__Hash__Group_ga1a88c8632539860e1df25d1812f4146f}{eina\+\_\+hash\+\_\+direct\+\_\+add\+\_\+by\+\_\+hash()}}, \mbox{\hyperlink{group__Eina__Hash__Group_gad8a9ac3c61ead249a6927a12e9de871a}{eina\+\_\+hash\+\_\+del()}}, \mbox{\hyperlink{group__Eina__Hash__Group_ga7454f861781493345ff389a2605a9237}{eina\+\_\+hash\+\_\+del\+\_\+by\+\_\+key\+\_\+hash()}}, \mbox{\hyperlink{group__Eina__Hash__Group_gacdcde189b35d525773d699793daa9017}{eina\+\_\+hash\+\_\+del\+\_\+by\+\_\+key()}}, \mbox{\hyperlink{group__Eina__Hash__Group_ga66ca7b9af4f40eeefd8aa401e07147c1}{eina\+\_\+hash\+\_\+del\+\_\+by\+\_\+data()}}, \mbox{\hyperlink{group__Eina__Hash__Group_ga42dee6ba7f023156397ec4ece52e3cf5}{eina\+\_\+hash\+\_\+find\+\_\+by\+\_\+hash()}} and \mbox{\hyperlink{group__Eina__Hash__Group_ga821f033abb9edea2929df2ecb4880a3a}{eina\+\_\+hash\+\_\+modify\+\_\+by\+\_\+hash()}}.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{comment}{//Compile with:}}
\DoxyCodeLine{\textcolor{comment}{//gcc -\/g eina\_hash\_08.c -\/o eina\_hash\_08 `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{     \textcolor{keyword}{const} \textcolor{keywordtype}{char} *name; \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{       \{ \textcolor{stringliteral}{"Wolfgang Amadeus Mozart"}, \textcolor{stringliteral}{"+01 23 456-\/78910"} \},}
\DoxyCodeLine{       \{ \textcolor{stringliteral}{"Ludwig van Beethoven"}, \textcolor{stringliteral}{"+12 34 567-\/89101"} \},}
\DoxyCodeLine{       \{ \textcolor{stringliteral}{"Richard Georg Strauss"}, \textcolor{stringliteral}{"+23 45 678-\/91012"} \},}
\DoxyCodeLine{       \{ \textcolor{stringliteral}{"Heitor Villa-\/Lobos"}, \textcolor{stringliteral}{"+34 56 789-\/10123"} \},}
\DoxyCodeLine{       \{ NULL, 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{keywordtype}{int}}
\DoxyCodeLine{main(\textcolor{keywordtype}{int} argc \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}}, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *argv[] \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})}
\DoxyCodeLine{\{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Hash__Group_ga9a89d7d3ee260d3a60572ebf651b07e0}{Eina\_Hash}} *phone\_book = NULL;}
\DoxyCodeLine{   \textcolor{keywordtype}{int} i;}
\DoxyCodeLine{   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *entry\_name = \textcolor{stringliteral}{"Heitor Villa-\/Lobos"};}
\DoxyCodeLine{   \textcolor{keywordtype}{int} entry\_size;}
\DoxyCodeLine{   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *saved\_entry\_name = \textcolor{stringliteral}{"Alceu Valenca"};}
\DoxyCodeLine{   \textcolor{keywordtype}{int} saved\_entry\_size = \textcolor{keyword}{sizeof}(\textcolor{stringliteral}{"Alceu Valenca"});}
\DoxyCodeLine{   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *phone = NULL;}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}} r;}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Main__Group_gafee4bfc0e509b6ffed6598edf6f4b3de}{eina\_init}}();}
\DoxyCodeLine{}
\DoxyCodeLine{   phone\_book = \mbox{\hyperlink{group__Eina__Hash__Group_gad7203e20b1496445cb819c7862171c87}{eina\_hash\_string\_superfast\_new}}(\_phone\_entry\_free\_cb);}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// Add initial entries to our hash}}
\DoxyCodeLine{   \textcolor{keywordflow}{for} (i = 0; \_start\_entries[i].name != NULL; i++)}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        \mbox{\hyperlink{group__Eina__Hash__Group_gafe3bb7c1f5616b4e158f60322e133260}{eina\_hash\_add}}(phone\_book, \_start\_entries[i].name,}
\DoxyCodeLine{                      strdup(\_start\_entries[i].number));}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// Delete entries}}
\DoxyCodeLine{   r = \mbox{\hyperlink{group__Eina__Hash__Group_gad8a9ac3c61ead249a6927a12e9de871a}{eina\_hash\_del}}(phone\_book, entry\_name, NULL);}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"Hash entry successfully deleted? \%d\(\backslash\)n\(\backslash\)n"}, r);}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordtype}{int} hash = \mbox{\hyperlink{group__Eina__Hash__Group_ga3a8434dd0fe7446af605c75e05e8eff0}{eina\_hash\_superfast}}(\textcolor{stringliteral}{"Ludwig van Beethoven"},}
\DoxyCodeLine{                                  \textcolor{keyword}{sizeof}(\textcolor{stringliteral}{"Ludwig van Beethoven"}));}
\DoxyCodeLine{}
\DoxyCodeLine{   r = \mbox{\hyperlink{group__Eina__Hash__Group_ga7454f861781493345ff389a2605a9237}{eina\_hash\_del\_by\_key\_hash}}(phone\_book, \textcolor{stringliteral}{"Ludwig van Beethoven"},}
\DoxyCodeLine{                                 \textcolor{keyword}{sizeof}(\textcolor{stringliteral}{"Ludwig van Beethoven"}), hash);}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"Hash entry successfully deleted? \%d\(\backslash\)n\(\backslash\)n"}, r);}
\DoxyCodeLine{}
\DoxyCodeLine{   r = \mbox{\hyperlink{group__Eina__Hash__Group_gacdcde189b35d525773d699793daa9017}{eina\_hash\_del\_by\_key}}(phone\_book, \textcolor{stringliteral}{"Richard Georg Strauss"});}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"Hash entry successfully deleted? \%d\(\backslash\)n\(\backslash\)n"}, r);}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// add entry by hash}}
\DoxyCodeLine{   entry\_name = \textcolor{stringliteral}{"Raul\_Seixas"};}
\DoxyCodeLine{   entry\_size = \textcolor{keyword}{sizeof}(\textcolor{stringliteral}{"Raul Seixas"});}
\DoxyCodeLine{   phone = strdup(\textcolor{stringliteral}{"+33 33 333-\/33333"});}
\DoxyCodeLine{   hash = \mbox{\hyperlink{group__Eina__Hash__Group_ga3a8434dd0fe7446af605c75e05e8eff0}{eina\_hash\_superfast}}(entry\_name, entry\_size);}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Hash__Group_ga4637f61969ecc13adaca17def6ed1a91}{eina\_hash\_add\_by\_hash}}(phone\_book, entry\_name, entry\_size, hash, phone);}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// don't need to free 'phone' after the next del:}}
\DoxyCodeLine{   r = \mbox{\hyperlink{group__Eina__Hash__Group_ga66ca7b9af4f40eeefd8aa401e07147c1}{eina\_hash\_del\_by\_data}}(phone\_book, phone);}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"Hash entry successfully deleted? \%d\(\backslash\)n\(\backslash\)n"}, r);}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// add entry by hash directly -\/ no copy of the key will be done}}
\DoxyCodeLine{   hash = \mbox{\hyperlink{group__Eina__Hash__Group_ga3a8434dd0fe7446af605c75e05e8eff0}{eina\_hash\_superfast}}(saved\_entry\_name, saved\_entry\_size);}
\DoxyCodeLine{   phone = strdup(\textcolor{stringliteral}{"+44 44 444-\/44444"});}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Hash__Group_ga1a88c8632539860e1df25d1812f4146f}{eina\_hash\_direct\_add\_by\_hash}}(phone\_book, saved\_entry\_name,}
\DoxyCodeLine{                                saved\_entry\_size, hash, phone);}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// find the added entry by its hash:}}
\DoxyCodeLine{   phone = \mbox{\hyperlink{group__Eina__Hash__Group_ga42dee6ba7f023156397ec4ece52e3cf5}{eina\_hash\_find\_by\_hash}}(phone\_book, saved\_entry\_name,}
\DoxyCodeLine{                                  saved\_entry\_size, hash);}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (phone)}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        \textcolor{keywordtype}{char} *newphone = strdup(\textcolor{stringliteral}{"+55 55 555-\/55555"});}
\DoxyCodeLine{        phone = \mbox{\hyperlink{group__Eina__Hash__Group_ga821f033abb9edea2929df2ecb4880a3a}{eina\_hash\_modify\_by\_hash}}(phone\_book, saved\_entry\_name,}
\DoxyCodeLine{                                         saved\_entry\_size, hash, newphone);}
\DoxyCodeLine{        \textcolor{keywordflow}{if} (phone)}
\DoxyCodeLine{          printf(\textcolor{stringliteral}{"changing phone to \%s, old one was \%s\(\backslash\)n"}, newphone, phone);}
\DoxyCodeLine{        \textcolor{keywordflow}{else}}
\DoxyCodeLine{          printf(\textcolor{stringliteral}{"couldn't modify entry identified by \%d\(\backslash\)n"}, hash);}
\DoxyCodeLine{     \}}
\DoxyCodeLine{   \textcolor{keywordflow}{else}}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"couldn't find entry identified by \%d\(\backslash\)n"}, hash);}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Hash__Group_gadb6acb720ce267cc1cd4f51893539f59}{eina\_hash\_free}}(phone\_book);}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Main__Group_ga2c8b4020427108a30c44707af22c99d3}{eina\_shutdown}}();}
\DoxyCodeLine{\}}
\end{DoxyCodeInclude}
 