\hypertarget{eina_hash_07_8c-example}{}\section{eina\+\_\+hash\+\_\+07.\+c}
Same example as \mbox{\hyperlink{hash_01_example_page}{Eina\+\_\+\+Hash in action}} but using a \char`\"{}pointer\char`\"{} hash table instead of \char`\"{}string superfast\char`\"{}.


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

\textcolor{preprocessor}{#include <stdio.h>}
\textcolor{preprocessor}{#include <string.h>}
\textcolor{preprocessor}{#include <\mbox{\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{keyword}{const} \textcolor{keywordtype}{char} *\_nicknames[] = \{
       \textcolor{stringliteral}{"mozzart"},
       \textcolor{stringliteral}{"betho"},
       \textcolor{stringliteral}{"george"},
       \textcolor{stringliteral}{"hector"},
       NULL
\};

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

\textcolor{keyword}{static} \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}
\_phone\_book\_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}}, \textcolor{keyword}{const} \textcolor{keywordtype}{void} *key,
                       \textcolor{keywordtype}{void} *data, \textcolor{keywordtype}{void} *fdata \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})
\{
   Phone\_Entry **pe = (Phone\_Entry **)key;
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *nick = data;
   printf(\textcolor{stringliteral}{"%s: %s, nick=%s\(\backslash\)n"}, (*pe)->name, (*pe)->number, nick);

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

\textcolor{keywordtype}{int}
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}})
\{
   \mbox{\hyperlink{group__Eina__Hash__Group_ga9a89d7d3ee260d3a60572ebf651b07e0}{Eina\_Hash}} *phone\_book = NULL;
   \textcolor{keywordtype}{int} i;
   Phone\_Entry *entry\_vl = &\_start\_entries[3];
   Phone\_Entry *p = NULL;
   \textcolor{keywordtype}{char} *nick = NULL;
   \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}} r;
   \mbox{\hyperlink{struct__Eina__Iterator}{Eina\_Iterator}} *it;
   \textcolor{keywordtype}{void} *data;

   \mbox{\hyperlink{group__Eina__Main__Group_gafee4bfc0e509b6ffed6598edf6f4b3de}{eina\_init}}();

   phone\_book = \mbox{\hyperlink{group__Eina__Hash__Group_ga0cd41f505034160d99f3b907ad3d74ce}{eina\_hash\_pointer\_new}}(\_phone\_entry\_free\_cb);

   \textcolor{comment}{// Add initial entries to our hash}
   \textcolor{keywordflow}{for} (i = 0; \_start\_entries[i].name != NULL; i++)
     \{
        p = &\_start\_entries[i];
        \mbox{\hyperlink{group__Eina__Hash__Group_gafe3bb7c1f5616b4e158f60322e133260}{eina\_hash\_add}}(phone\_book, &p,
                      strdup(\_nicknames[i]));
     \}
   printf(\textcolor{stringliteral}{"Phonebook:\(\backslash\)n"});
   \mbox{\hyperlink{group__Eina__Hash__Group_ga2c23f8425d3afcc61827ab67b46d7190}{eina\_hash\_foreach}}(phone\_book, \_phone\_book\_foreach\_cb, NULL);
   printf(\textcolor{stringliteral}{"\(\backslash\)n"});

   \textcolor{comment}{// Look for a specific entry and get its nickname}
   nick = \mbox{\hyperlink{group__Eina__Hash__Group_ga45703d00fbabffc86e6836ffe98a4c92}{eina\_hash\_find}}(phone\_book, &entry\_vl);
   \textcolor{keywordflow}{if} (nick)
     \{
        printf(\textcolor{stringliteral}{"Printing entry.\(\backslash\)n"});
        printf(\textcolor{stringliteral}{"Name: %s\(\backslash\)n"}, entry\_vl->name);
        printf(\textcolor{stringliteral}{"Number: %s\(\backslash\)n"}, entry\_vl->number);
        printf(\textcolor{stringliteral}{"Nick: %s\(\backslash\)n\(\backslash\)n"}, nick);
     \}

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

   \textcolor{comment}{// Modify the pointer data of an entry and free the old one}
   p = &\_start\_entries[2];
   nick = \mbox{\hyperlink{group__Eina__Hash__Group_gaabf2f2af1d93f969591f2c5b7378c543}{eina\_hash\_modify}}(phone\_book, &p,
                           strdup(\textcolor{stringliteral}{"el jorge"}));
   free(nick);

   \textcolor{comment}{// Modify or add an entry to the hash with eina\_hash\_set}
   \textcolor{comment}{// Let's first add a new entry}
   Phone\_Entry *p1 = malloc(\textcolor{keyword}{sizeof}(*p1));
   p1->name = \textcolor{stringliteral}{"Raul Seixas"};
   p1->number = \textcolor{stringliteral}{"+55 01 234-56789"};
   nick = \mbox{\hyperlink{group__Eina__Hash__Group_ga53fd5f171289d63c5d8532bb28434cb3}{eina\_hash\_set}}(phone\_book, &p1,
                        strdup(\textcolor{stringliteral}{"raulzito"}));
   \textcolor{keywordflow}{if} (!nick)
     \{
        printf(\textcolor{stringliteral}{"No previous nick found for Raul Seixas. "});
        printf(\textcolor{stringliteral}{"Creating new entry.\(\backslash\)n"});
     \}
   \textcolor{keywordflow}{else}
     \{
        printf(\textcolor{stringliteral}{"Old nick for Raul Seixas was %s\(\backslash\)n"}, nick);
        free(nick);
     \}

   printf(\textcolor{stringliteral}{"\(\backslash\)n"});

   \textcolor{comment}{// Now change the nick}
   nick = \mbox{\hyperlink{group__Eina__Hash__Group_ga53fd5f171289d63c5d8532bb28434cb3}{eina\_hash\_set}}(phone\_book, &p1,
                        strdup(\textcolor{stringliteral}{"raulzao"}));
   \textcolor{keywordflow}{if} (nick)
     \{
        printf(\textcolor{stringliteral}{"Changing nick for Raul Seixas to raulzao. "});
        printf(\textcolor{stringliteral}{"Old nick was %s\(\backslash\)n"}, nick);
        free(nick);
     \}
   \textcolor{keywordflow}{else}
     \{
        printf(\textcolor{stringliteral}{"No previous nick found for Raul Seixas. "});
        printf(\textcolor{stringliteral}{"Creating new entry.\(\backslash\)n"});
     \}

   \textcolor{comment}{// There are many ways to iterate over our Phone book.}
   \textcolor{comment}{// First, iterate showing the names, phones and associated nicks.}
   printf(\textcolor{stringliteral}{"Phonebook:\(\backslash\)n"});
   \mbox{\hyperlink{group__Eina__Hash__Group_ga2c23f8425d3afcc61827ab67b46d7190}{eina\_hash\_foreach}}(phone\_book, \_phone\_book\_foreach\_cb, NULL);
   printf(\textcolor{stringliteral}{"\(\backslash\)n"});

   \textcolor{comment}{// Now iterate using an iterator}
   printf(\textcolor{stringliteral}{"Phonebook:\(\backslash\)n"});
   it = \mbox{\hyperlink{group__Eina__Hash__Group_gaf0e7e38ec02c4bcfca3c3c4dd13c6af9}{eina\_hash\_iterator\_tuple\_new}}(phone\_book);
   \textcolor{keywordflow}{while} (\mbox{\hyperlink{group__Eina__Iterator__Group_ga83346e93c32dbebc8fd66e4f278140c5}{eina\_iterator\_next}}(it, &data))
     \{
        \mbox{\hyperlink{struct__Eina__Hash__Tuple}{Eina\_Hash\_Tuple}} *t = data;
        Phone\_Entry **pe = (Phone\_Entry **)t->key;
        nick = t->data;
        printf(\textcolor{stringliteral}{"%s: %s, nick=%s\(\backslash\)n"}, (*pe)->name, (*pe)->number, nick);
     \}
   \mbox{\hyperlink{group__Eina__Iterator__Group_gaf9261991a4b05dc63e3401278a3281f8}{eina\_iterator\_free}}(it); \textcolor{comment}{// Always free the iterator after its use}
   printf(\textcolor{stringliteral}{"\(\backslash\)n"});

   \textcolor{comment}{// Just iterate over the keys (names)}
   printf(\textcolor{stringliteral}{"List of names/numbers in the phone book:\(\backslash\)n"});
   it = \mbox{\hyperlink{group__Eina__Hash__Group_ga1a090384b1c057daec8e19c6a1ea2c0d}{eina\_hash\_iterator\_key\_new}}(phone\_book);
   \textcolor{keywordflow}{while} (\mbox{\hyperlink{group__Eina__Iterator__Group_ga83346e93c32dbebc8fd66e4f278140c5}{eina\_iterator\_next}}(it, &data))
     \{
        Phone\_Entry **pe = (Phone\_Entry **)data;
        printf(\textcolor{stringliteral}{"%s: %s\(\backslash\)n"}, (*pe)->name, (*pe)->number);
     \}
   \mbox{\hyperlink{group__Eina__Iterator__Group_gaf9261991a4b05dc63e3401278a3281f8}{eina\_iterator\_free}}(it);
   printf(\textcolor{stringliteral}{"\(\backslash\)n"});

   \textcolor{comment}{// Just iterate over the data (nicks)}
   printf(\textcolor{stringliteral}{"List of nicks in the phone book:\(\backslash\)n"});
   it = \mbox{\hyperlink{group__Eina__Hash__Group_ga5191350d6044d93b6518a957f12f5ac3}{eina\_hash\_iterator\_data\_new}}(phone\_book);
   \textcolor{keywordflow}{while} (\mbox{\hyperlink{group__Eina__Iterator__Group_ga83346e93c32dbebc8fd66e4f278140c5}{eina\_iterator\_next}}(it, &data))
     \{
        nick = data;
        printf(\textcolor{stringliteral}{"%s\(\backslash\)n"}, nick);
     \}
   \mbox{\hyperlink{group__Eina__Iterator__Group_gaf9261991a4b05dc63e3401278a3281f8}{eina\_iterator\_free}}(it);
   printf(\textcolor{stringliteral}{"\(\backslash\)n"});

   \textcolor{comment}{// Check how many items are in the phone book}
   printf(\textcolor{stringliteral}{"There are %d items in the hash.\(\backslash\)n\(\backslash\)n"},
          \mbox{\hyperlink{group__Eina__Hash__Group_ga9289542f6724083eab306e5bb3ab10b0}{eina\_hash\_population}}(phone\_book));

   \textcolor{comment}{// Change the name (key) on an entry}
   Phone\_Entry *p2 = malloc(\textcolor{keyword}{sizeof}(*p2));
   p2->name = \textcolor{stringliteral}{"Alceu Valenca"};
   p2->number = \textcolor{stringliteral}{"000000000000"};
   \mbox{\hyperlink{group__Eina__Hash__Group_gaa390160f31c235d91947281244e77628}{eina\_hash\_move}}(phone\_book, p1, p2);
   printf(\textcolor{stringliteral}{"List of phones after change:\(\backslash\)n"});
   \mbox{\hyperlink{group__Eina__Hash__Group_ga2c23f8425d3afcc61827ab67b46d7190}{eina\_hash\_foreach}}(phone\_book, \_phone\_book\_foreach\_cb, NULL);
   printf(\textcolor{stringliteral}{"\(\backslash\)n"});

   \textcolor{comment}{// Empty the phone book, but don't destroy it}
   \mbox{\hyperlink{group__Eina__Hash__Group_gabe2ba51e5dbf6fd5f60628b8e8f00eaa}{eina\_hash\_free\_buckets}}(phone\_book);
   printf(\textcolor{stringliteral}{"There are %d items in the hash.\(\backslash\)n\(\backslash\)n"},
          \mbox{\hyperlink{group__Eina__Hash__Group_ga9289542f6724083eab306e5bb3ab10b0}{eina\_hash\_population}}(phone\_book));

   \textcolor{comment}{// Phone book could still be used, but we are freeing it since we are}
   \textcolor{comment}{// done for now}
   \mbox{\hyperlink{group__Eina__Hash__Group_gadb6acb720ce267cc1cd4f51893539f59}{eina\_hash\_free}}(phone\_book);

   free(p1);
   free(p2);

   \mbox{\hyperlink{group__Eina__Main__Group_ga2c8b4020427108a30c44707af22c99d3}{eina\_shutdown}}();
\}
\end{DoxyCodeInclude}
 