\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_ga5538b4a134987b8a01744fad1a845ba4}{eina\_stringshare\_del}}(e-\/>name);}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Stringshare__Group_ga5538b4a134987b8a01744fad1a845ba4}{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_ga228d9cdf8f84c88acf74c09d771eb3aa}{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_ga4e3ce40744291c1fb8574e26883c0bf2}{eina\_stringshare\_add}}(\_start\_entries[i].name);}
\DoxyCodeLine{        e-\/>number = \mbox{\hyperlink{group__Eina__Stringshare__Group_ga4e3ce40744291c1fb8574e26883c0bf2}{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_ga84a3b957ede675cb03f3c344695c1282}{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_ga9237dbade8e59ac827992c7a5146cb20}{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_ga76829f09c99852b1536efc813d99fc78}{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_ga8676c1d28a50581fc4c04d5c3092eacb}{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_ga28c1f0f114ca3e2910cce197c8fbbfd9}{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_ga5e72d0c5397936512407ce8cd66e98d6}{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_ga6e9dc02a249e1fcdb64e38e7dc47afb3}{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_ga76829f09c99852b1536efc813d99fc78}{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_ga81b8137665b8a4d2cb38ca50017d4c74}{eina\_init}}();}
\DoxyCodeLine{}
\DoxyCodeLine{   example\_hash\_stringshared();}
\DoxyCodeLine{   example\_hash\_big();}
\DoxyCodeLine{}
\DoxyCodeLine{   \mbox{\hyperlink{group__Eina__Main__Group_ga5bf35513199d5220341437c05696285d}{eina\_shutdown}}();}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 