\hypertarget{eina_hash_02_8c-example}{}\doxysection{eina\+\_\+hash\+\_\+02.\+c}

\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{comment}{//Compile with:}}
\DoxyCodeLine{\textcolor{comment}{//gcc -\/g eina\_hash\_02.c -\/o eina\_hash\_02 `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 -\/ Two more types of hash}}
\DoxyCodeLine{\textcolor{comment}{ *}}
\DoxyCodeLine{\textcolor{comment}{ * This example demonstrate two other types of hash in action -\/ using}}
\DoxyCodeLine{\textcolor{comment}{ * eina\_hash\_stringshared\_new and eina\_hash\_new.}}
\DoxyCodeLine{\textcolor{comment}{ *}}
\DoxyCodeLine{\textcolor{comment}{ * It indexes the phone numbers by Contact Full Name, so it's a hash with string}}
\DoxyCodeLine{\textcolor{comment}{ * keys, exactly the same as the other example.}}
\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{\};}
\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} \textcolor{keywordtype}{void}}
\DoxyCodeLine{\_phone\_book\_stringshared\_free\_cb(\textcolor{keywordtype}{void} *data)}
\DoxyCodeLine{\{}
\DoxyCodeLine{   Phone\_Entry *e = data;}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Stringshare__Group_ga495411b0bc85b9c0d59ec7255a025260}{eina\_stringshare\_del}}(e-\/>name);}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Stringshare__Group_ga495411b0bc85b9c0d59ec7255a025260}{eina\_stringshare\_del}}(e-\/>number);}
\DoxyCodeLine{   free(e);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static} \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_phone\_book\_stringshared\_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}},}
\DoxyCodeLine{                                    \textcolor{keyword}{const} \textcolor{keywordtype}{void} *key \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},}
\DoxyCodeLine{                                    \textcolor{keywordtype}{void} *data,}
\DoxyCodeLine{                                    \textcolor{keywordtype}{void} *fdata \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})}
\DoxyCodeLine{\{}
\DoxyCodeLine{   Phone\_Entry *e = data;}
\DoxyCodeLine{   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *name = e-\/>name; \textcolor{comment}{// e-\/>name == key}}
\DoxyCodeLine{   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *number = e-\/>number;}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"{}\%s: \%s\(\backslash\)n"{}}, name, number);}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static} \textcolor{keywordtype}{void}}
\DoxyCodeLine{example\_hash\_stringshared(\textcolor{keywordtype}{void})}
\DoxyCodeLine{\{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Hash__Group_ga9a89d7d3ee260d3a60572ebf651b07e0}{Eina\_Hash}} *phone\_book = NULL;}
\DoxyCodeLine{   \textcolor{keywordtype}{int} i;}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// Create the hash as before}}
\DoxyCodeLine{   phone\_book = \mbox{\hyperlink{group__Eina__Hash__Group_ga75266e6a40be3a377345b4d883b6e9a7}{eina\_hash\_stringshared\_new}}(\_phone\_book\_stringshared\_free\_cb);}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// Add initial entries to our hash, using direct\_add}}
\DoxyCodeLine{   \textcolor{keywordflow}{for} (i = 0; \_start\_entries[i].name != NULL; i++)}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        Phone\_Entry *e = malloc(\textcolor{keyword}{sizeof}(Phone\_Entry));}
\DoxyCodeLine{        e-\/>name = \mbox{\hyperlink{group__Eina__Stringshare__Group_ga5293082680141132ffcacc28866ef0ef}{eina\_stringshare\_add}}(\_start\_entries[i].name);}
\DoxyCodeLine{        e-\/>number = \mbox{\hyperlink{group__Eina__Stringshare__Group_ga5293082680141132ffcacc28866ef0ef}{eina\_stringshare\_add}}(\_start\_entries[i].number);}
\DoxyCodeLine{        \textcolor{comment}{// Since we are storing the key (name) in our struct, we can use}}
\DoxyCodeLine{        \textcolor{comment}{// eina\_hash\_direct\_add. It could be used in the previous example}}
\DoxyCodeLine{        \textcolor{comment}{// too, since each key is already stored in the \_start\_entries}}
\DoxyCodeLine{        \textcolor{comment}{// static array, but we started it with the default add function.}}
\DoxyCodeLine{        \mbox{\hyperlink{group__Eina__Hash__Group_ga50aa7d4303e5efbe8dc7f593b59b9f63}{eina\_hash\_direct\_add}}(phone\_book, e-\/>name, e);}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// Iterate over the elements}}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"{}List of phones:\(\backslash\)n"{}});}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Hash__Group_ga2c23f8425d3afcc61827ab67b46d7190}{eina\_hash\_foreach}}(phone\_book, \_phone\_book\_stringshared\_foreach\_cb, NULL);}
\DoxyCodeLine{   printf(\textcolor{stringliteral}{"{}\(\backslash\)n"{}});}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Hash__Group_gadb6acb720ce267cc1cd4f51893539f59}{eina\_hash\_free}}(phone\_book);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static} \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int}}
\DoxyCodeLine{\_phone\_book\_string\_key\_length(\textcolor{keyword}{const} \textcolor{keywordtype}{char} *key)}
\DoxyCodeLine{\{}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (!key)}
\DoxyCodeLine{      \textcolor{keywordflow}{return} 0;}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{return} (\textcolor{keywordtype}{int})strlen(key) + 1;}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static} \textcolor{keywordtype}{int}}
\DoxyCodeLine{\_phone\_book\_string\_key\_cmp(\textcolor{keyword}{const} \textcolor{keywordtype}{char} *key1, \textcolor{keywordtype}{int} key1\_length \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},}
\DoxyCodeLine{                     \textcolor{keyword}{const} \textcolor{keywordtype}{char} *key2, \textcolor{keywordtype}{int} key2\_length \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})}
\DoxyCodeLine{\{}
\DoxyCodeLine{   \textcolor{keywordflow}{return} strcmp(key1, key2);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static} \textcolor{keywordtype}{void}}
\DoxyCodeLine{example\_hash\_big(\textcolor{keywordtype}{void})}
\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} *phone;}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{comment}{// Create the same hash as used in eina\_hash\_01.c, but}}
\DoxyCodeLine{   \textcolor{comment}{// use 1024 (2 \string^ 10) buckets.}}
\DoxyCodeLine{   phone\_book = \mbox{\hyperlink{group__Eina__Hash__Group_ga32c9a8a82e2d20338618baf6af3d1614}{eina\_hash\_new}}(\mbox{\hyperlink{group__Eina__Hash__Group_gaaaa18f69c9854f63beae15c42f1256b8}{EINA\_KEY\_LENGTH}}(\_phone\_book\_string\_key\_length),}
\DoxyCodeLine{                              \mbox{\hyperlink{group__Eina__Hash__Group_ga1c48d74b1ed071d8c156a98f5f0a06f6}{EINA\_KEY\_CMP}}(\_phone\_book\_string\_key\_cmp),}
\DoxyCodeLine{                              \mbox{\hyperlink{group__Eina__Hash__Group_gaf53593bdd5cc3d238e5eea2ae86d42a0}{EINA\_KEY\_HASH}}(\mbox{\hyperlink{group__Eina__Hash__Group_ga3a8434dd0fe7446af605c75e05e8eff0}{eina\_hash\_superfast}}),}
\DoxyCodeLine{                              \_phone\_entry\_free\_cb,}
\DoxyCodeLine{                              10);}
\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}{// Look for a specific entry and get its phone number}}
\DoxyCodeLine{   phone = \mbox{\hyperlink{group__Eina__Hash__Group_ga45703d00fbabffc86e6836ffe98a4c92}{eina\_hash\_find}}(phone\_book, \textcolor{stringliteral}{"{}Heitor Villa-\/Lobos"{}});}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (phone)}
\DoxyCodeLine{     \{}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"{}Printing entry.\(\backslash\)n"{}});}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"{}Name: Heitor Villa-\/Lobos\(\backslash\)n"{}});}
\DoxyCodeLine{        printf(\textcolor{stringliteral}{"{}Number: \%s\(\backslash\)n\(\backslash\)n"{}}, phone);}
\DoxyCodeLine{     \}}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Hash__Group_gadb6acb720ce267cc1cd4f51893539f59}{eina\_hash\_free}}(phone\_book);}
\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__Main__Group_gafee4bfc0e509b6ffed6598edf6f4b3de}{eina\_init}}();}
\DoxyCodeLine{}
\DoxyCodeLine{   example\_hash\_stringshared();}
\DoxyCodeLine{   example\_hash\_big();}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Main__Group_ga2c8b4020427108a30c44707af22c99d3}{eina\_shutdown}}();}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 