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


\begin{DoxyCodeInclude}
\textcolor{comment}{//Compile with:}
\textcolor{comment}{//gcc -g eina\_hash\_08.c -o eina\_hash\_08 `pkg-config --cflags --libs eina`}

\textcolor{preprocessor}{#include <stdio.h>}
\textcolor{preprocessor}{#include <string.h>}
\textcolor{preprocessor}{#include <\hyperlink{Eina_8h}{Eina.h}>}

\textcolor{comment}{/*}
\textcolor{comment}{ * Eina Hash - phonebook}
\textcolor{comment}{ *}
\textcolor{comment}{ * This example demonstrate the use of Eina Hash by implementing a phonebook}
\textcolor{comment}{ * that stores its contact data into the hash.}
\textcolor{comment}{ *}
\textcolor{comment}{ * It indexes the phone numbers by Contact Full Name, so it's a hash with}
\textcolor{comment}{ * string keys.}
\textcolor{comment}{ */}

\textcolor{keyword}{struct }\_Phone\_Entry \{
     \textcolor{keyword}{const} \textcolor{keywordtype}{char} *name; \textcolor{comment}{// Full name.}
     \textcolor{keyword}{const} \textcolor{keywordtype}{char} *number; \textcolor{comment}{// Phone number.}
\};

\textcolor{keyword}{typedef} \textcolor{keyword}{struct }\_Phone\_Entry Phone\_Entry;

\textcolor{keyword}{static} Phone\_Entry \_start\_entries[] = \{
       \{ \textcolor{stringliteral}{"Wolfgang Amadeus Mozart"}, \textcolor{stringliteral}{"+01 23 456-78910"} \},
       \{ \textcolor{stringliteral}{"Ludwig van Beethoven"}, \textcolor{stringliteral}{"+12 34 567-89101"} \},
       \{ \textcolor{stringliteral}{"Richard Georg Strauss"}, \textcolor{stringliteral}{"+23 45 678-91012"} \},
       \{ \textcolor{stringliteral}{"Heitor Villa-Lobos"}, \textcolor{stringliteral}{"+34 56 789-10123"} \},
       \{ NULL, NULL \}
\}; \textcolor{comment}{// \_start\_entries}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_phone\_entry\_free\_cb(\textcolor{keywordtype}{void} *data)
\{
   free(data);
\}

\textcolor{keyword}{static} \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}
\_phone\_book\_foreach\_cb(\textcolor{keyword}{const} \hyperlink{group__Eina__Hash__Group_ga9a89d7d3ee260d3a60572ebf651b07e0}{Eina\_Hash} *phone\_book \hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}, \textcolor{keyword}{const} \textcolor{keywordtype}{void} *key,
                       \textcolor{keywordtype}{void} *data, \textcolor{keywordtype}{void} *fdata \hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED})
\{
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *name = key;
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *number = data;
   printf(\textcolor{stringliteral}{"%s: %s\(\backslash\)n"}, name, number);

   \textcolor{comment}{// Return EINA\_FALSE to stop this callback from being called}
   \textcolor{keywordflow}{return} \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE};
\}

\textcolor{keywordtype}{int}
main(\textcolor{keywordtype}{int} argc \hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *argv[] \hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED})
\{
   \hyperlink{group__Eina__Hash__Group_ga9a89d7d3ee260d3a60572ebf651b07e0}{Eina\_Hash} *phone\_book = NULL;
   \textcolor{keywordtype}{int} i;
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *entry\_name = \textcolor{stringliteral}{"Heitor Villa-Lobos"};
   \textcolor{keywordtype}{int} entry\_size;
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *saved\_entry\_name = \textcolor{stringliteral}{"Alceu Valenca"};
   \textcolor{keywordtype}{int} saved\_entry\_size = \textcolor{keyword}{sizeof}(\textcolor{stringliteral}{"Alceu Valenca"});
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *phone = NULL;
   \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool} r;

   \hyperlink{group__Eina__Main__Group_gabf448ada5bc81a07342b66c6f818ee5e}{eina\_init}();

   phone\_book = \hyperlink{group__Eina__Hash__Group_gad7203e20b1496445cb819c7862171c87}{eina\_hash\_string\_superfast\_new}(\_phone\_entry\_free\_cb);

   \textcolor{comment}{// Add initial entries to our hash}
   \textcolor{keywordflow}{for} (i = 0; \_start\_entries[i].name != NULL; i++)
     \{
        \hyperlink{group__Eina__Hash__Group_gafe3bb7c1f5616b4e158f60322e133260}{eina\_hash\_add}(phone\_book, \_start\_entries[i].name,
                      strdup(\_start\_entries[i].number));
     \}

   \textcolor{comment}{// Delete entries}
   r = \hyperlink{group__Eina__Hash__Group_gad8a9ac3c61ead249a6927a12e9de871a}{eina\_hash\_del}(phone\_book, entry\_name, NULL);
   printf(\textcolor{stringliteral}{"Hash entry successfully deleted? %d\(\backslash\)n\(\backslash\)n"}, r);

   \textcolor{keywordtype}{int} hash = \hyperlink{group__Eina__Hash__Group_ga3a8434dd0fe7446af605c75e05e8eff0}{eina\_hash\_superfast}(\textcolor{stringliteral}{"Ludwig van Beethoven"},
                                  \textcolor{keyword}{sizeof}(\textcolor{stringliteral}{"Ludwig van Beethoven"}));

   r = \hyperlink{group__Eina__Hash__Group_ga7454f861781493345ff389a2605a9237}{eina\_hash\_del\_by\_key\_hash}(phone\_book, \textcolor{stringliteral}{"Ludwig van Beethoven"},
                                 \textcolor{keyword}{sizeof}(\textcolor{stringliteral}{"Ludwig van Beethoven"}), hash);
   printf(\textcolor{stringliteral}{"Hash entry successfully deleted? %d\(\backslash\)n\(\backslash\)n"}, r);

   r = \hyperlink{group__Eina__Hash__Group_gacdcde189b35d525773d699793daa9017}{eina\_hash\_del\_by\_key}(phone\_book, \textcolor{stringliteral}{"Richard Georg Strauss"});
   printf(\textcolor{stringliteral}{"Hash entry successfully deleted? %d\(\backslash\)n\(\backslash\)n"}, r);

   \textcolor{comment}{// add entry by hash}
   entry\_name = \textcolor{stringliteral}{"Raul\_Seixas"};
   entry\_size = \textcolor{keyword}{sizeof}(\textcolor{stringliteral}{"Raul Seixas"});
   phone = strdup(\textcolor{stringliteral}{"+33 33 333-33333"});
   hash = \hyperlink{group__Eina__Hash__Group_ga3a8434dd0fe7446af605c75e05e8eff0}{eina\_hash\_superfast}(entry\_name, entry\_size);
   \hyperlink{group__Eina__Hash__Group_ga4637f61969ecc13adaca17def6ed1a91}{eina\_hash\_add\_by\_hash}(phone\_book, entry\_name, entry\_size, hash, phone);

   \textcolor{comment}{// don't need to free 'phone' after the next del:}
   r = \hyperlink{group__Eina__Hash__Group_ga66ca7b9af4f40eeefd8aa401e07147c1}{eina\_hash\_del\_by\_data}(phone\_book, phone);
   printf(\textcolor{stringliteral}{"Hash entry successfully deleted? %d\(\backslash\)n\(\backslash\)n"}, r);

   \textcolor{comment}{// add entry by hash directly - no copy of the key will be done}
   hash = \hyperlink{group__Eina__Hash__Group_ga3a8434dd0fe7446af605c75e05e8eff0}{eina\_hash\_superfast}(saved\_entry\_name, saved\_entry\_size);
   phone = strdup(\textcolor{stringliteral}{"+44 44 444-44444"});
   \hyperlink{group__Eina__Hash__Group_ga1a88c8632539860e1df25d1812f4146f}{eina\_hash\_direct\_add\_by\_hash}(phone\_book, saved\_entry\_name,
                                saved\_entry\_size, hash, phone);

   \textcolor{comment}{// find the added entry by its hash:}
   phone = \hyperlink{group__Eina__Hash__Group_ga42dee6ba7f023156397ec4ece52e3cf5}{eina\_hash\_find\_by\_hash}(phone\_book, saved\_entry\_name,
                                  saved\_entry\_size, hash);
   \textcolor{keywordflow}{if} (phone)
     \{
        \textcolor{keywordtype}{char} *newphone = strdup(\textcolor{stringliteral}{"+55 55 555-55555"});
        phone = \hyperlink{group__Eina__Hash__Group_ga821f033abb9edea2929df2ecb4880a3a}{eina\_hash\_modify\_by\_hash}(phone\_book, saved\_entry\_name,
                                         saved\_entry\_size, hash, newphone);
        \textcolor{keywordflow}{if} (phone)
          printf(\textcolor{stringliteral}{"changing phone to %s, old one was %s\(\backslash\)n"}, newphone, phone);
        \textcolor{keywordflow}{else}
          printf(\textcolor{stringliteral}{"couldn't modify entry identified by %d\(\backslash\)n"}, hash);
     \}
   \textcolor{keywordflow}{else}
     \{
        printf(\textcolor{stringliteral}{"couldn't find entry identified by %d\(\backslash\)n"}, hash);
     \}

   \hyperlink{group__Eina__Hash__Group_gadb6acb720ce267cc1cd4f51893539f59}{eina\_hash\_free}(phone\_book);

   \hyperlink{group__Eina__Main__Group_ga6741fcaba116e31e96ce14dcfa03f568}{eina\_shutdown}();
\}
\end{DoxyCodeInclude}
 