\hypertarget{eina_hash_08_8c-example}{}\doxysection{eina\+\_\+hash\+\_\+08.\+c}
This example shows the the usage of \mbox{\hyperlink{group__Eina__Hash__Group_ga5e72d0c5397936512407ce8cd66e98d6}{eina\+\_\+hash\+\_\+add()}}, \mbox{\hyperlink{group__Eina__Hash__Group_gad95ec62ef78ec789103b4cf15912cdd5}{eina\+\_\+hash\+\_\+add\+\_\+by\+\_\+hash()}}, \mbox{\hyperlink{group__Eina__Hash__Group_ga550b14c4322a67c9b0ce14bd66b53dc5}{eina\+\_\+hash\+\_\+direct\+\_\+add\+\_\+by\+\_\+hash()}}, \mbox{\hyperlink{group__Eina__Hash__Group_ga315f85c5deae8e099e5a5a17d3557456}{eina\+\_\+hash\+\_\+del()}}, \mbox{\hyperlink{group__Eina__Hash__Group_gae39ec64f5bc2b8bcb823b629555cc332}{eina\+\_\+hash\+\_\+del\+\_\+by\+\_\+key\+\_\+hash()}}, \mbox{\hyperlink{group__Eina__Hash__Group_gadcb1a5cd7604dd23c8c9c1d3c4b773b5}{eina\+\_\+hash\+\_\+del\+\_\+by\+\_\+key()}}, \mbox{\hyperlink{group__Eina__Hash__Group_gaaf0be23d41adb51ec20478b45a697ce2}{eina\+\_\+hash\+\_\+del\+\_\+by\+\_\+data()}}, \mbox{\hyperlink{group__Eina__Hash__Group_gaa99a33c2e83108ed3a08d613c88ef59d}{eina\+\_\+hash\+\_\+find\+\_\+by\+\_\+hash()}} and \mbox{\hyperlink{group__Eina__Hash__Group_ga9bea6b9f7ebd5c02afc53521e4118423}{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_ga81b8137665b8a4d2cb38ca50017d4c74}{eina\_init}}();}
\DoxyCodeLine{}
\DoxyCodeLine{   phone\_book = \mbox{\hyperlink{group__Eina__Hash__Group_ga9fdef2647ff77d42d2b3271c1f71030e}{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_ga5e72d0c5397936512407ce8cd66e98d6}{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_ga315f85c5deae8e099e5a5a17d3557456}{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_ga28c1f0f114ca3e2910cce197c8fbbfd9}{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_gae39ec64f5bc2b8bcb823b629555cc332}{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_gadcb1a5cd7604dd23c8c9c1d3c4b773b5}{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_ga28c1f0f114ca3e2910cce197c8fbbfd9}{eina\_hash\_superfast}}(entry\_name, entry\_size);}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Hash__Group_gad95ec62ef78ec789103b4cf15912cdd5}{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_gaaf0be23d41adb51ec20478b45a697ce2}{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_ga28c1f0f114ca3e2910cce197c8fbbfd9}{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_ga550b14c4322a67c9b0ce14bd66b53dc5}{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_gaa99a33c2e83108ed3a08d613c88ef59d}{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_ga9bea6b9f7ebd5c02afc53521e4118423}{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_ga76829f09c99852b1536efc813d99fc78}{eina\_hash\_free}}(phone\_book);}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Main__Group_ga5bf35513199d5220341437c05696285d}{eina\_shutdown}}();}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 