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

\begin{DoxyCodeInclude}
\textcolor{comment}{//Compile with:}
\textcolor{comment}{//gcc -g eina\_hash\_02.c -o eina\_hash\_02 `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 - Two more types of hash}
\textcolor{comment}{ *}
\textcolor{comment}{ * This example demonstrate two other types of hash in action - using}
\textcolor{comment}{ * eina\_hash\_stringshared\_new and eina\_hash\_new.}
\textcolor{comment}{ *}
\textcolor{comment}{ * It indexes the phone numbers by Contact Full Name, so it's a hash with string}
\textcolor{comment}{ * keys, exactly the same as the other example.}
\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{keyword}{static} \textcolor{keywordtype}{void}
\_phone\_entry\_free\_cb(\textcolor{keywordtype}{void} *data)
\{
   free(data);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_phone\_book\_stringshared\_free\_cb(\textcolor{keywordtype}{void} *data)
\{
   Phone\_Entry *e = data;
   \hyperlink{group__Eina__Stringshare__Group_ga4453d2aff2b325c7b45048f30df9b1f5}{eina\_stringshare\_del}(e->name);
   \hyperlink{group__Eina__Stringshare__Group_ga4453d2aff2b325c7b45048f30df9b1f5}{eina\_stringshare\_del}(e->number);
   free(e);
\}

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

   \textcolor{keywordflow}{return} \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE};
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
example\_hash\_stringshared(\textcolor{keywordtype}{void})
\{
   \hyperlink{group__Eina__Hash__Group_ga9a89d7d3ee260d3a60572ebf651b07e0}{Eina\_Hash} *phone\_book = NULL;
   \textcolor{keywordtype}{int} i;

   \textcolor{comment}{// Create the hash as before}
   phone\_book = \hyperlink{group__Eina__Hash__Group_ga75266e6a40be3a377345b4d883b6e9a7}{eina\_hash\_stringshared\_new}(\_phone\_book\_stringshared\_free\_cb);

   \textcolor{comment}{// Add initial entries to our hash, using direct\_add}
   \textcolor{keywordflow}{for} (i = 0; \_start\_entries[i].name != NULL; i++)
     \{
        Phone\_Entry *e = malloc(\textcolor{keyword}{sizeof}(Phone\_Entry));
        e->name = \hyperlink{group__Eina__Stringshare__Group_gac7a3f073cafb7af63b2c7cc2eabd1c1f}{eina\_stringshare\_add}(\_start\_entries[i].name);
        e->number = \hyperlink{group__Eina__Stringshare__Group_gac7a3f073cafb7af63b2c7cc2eabd1c1f}{eina\_stringshare\_add}(\_start\_entries[i].number);
        \textcolor{comment}{// Since we are storing the key (name) in our struct, we can use}
        \textcolor{comment}{// eina\_hash\_direct\_add. It could be used in the previous example}
        \textcolor{comment}{// too, since each key is already stored in the \_start\_entries}
        \textcolor{comment}{// static array, but we started it with the default add function.}
        \hyperlink{group__Eina__Hash__Group_ga50aa7d4303e5efbe8dc7f593b59b9f63}{eina\_hash\_direct\_add}(phone\_book, e->name, e);
     \}

   \textcolor{comment}{// Iterate over the elements}
   printf(\textcolor{stringliteral}{"List of phones:\(\backslash\)n"});
   \hyperlink{group__Eina__Hash__Group_ga2c23f8425d3afcc61827ab67b46d7190}{eina\_hash\_foreach}(phone\_book, \_phone\_book\_stringshared\_foreach\_cb, NULL);
   printf(\textcolor{stringliteral}{"\(\backslash\)n"});

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

\textcolor{keyword}{static} \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int}
\_phone\_book\_string\_key\_length(\textcolor{keyword}{const} \textcolor{keywordtype}{char} *key)
\{
   \textcolor{keywordflow}{if} (!key)
      \textcolor{keywordflow}{return} 0;

   \textcolor{keywordflow}{return} (\textcolor{keywordtype}{int})strlen(key) + 1;
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{int}
\_phone\_book\_string\_key\_cmp(\textcolor{keyword}{const} \textcolor{keywordtype}{char} *key1, \textcolor{keywordtype}{int} key1\_length \hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED},
                     \textcolor{keyword}{const} \textcolor{keywordtype}{char} *key2, \textcolor{keywordtype}{int} key2\_length EINA\_UNUSED)
\{
   \textcolor{keywordflow}{return} strcmp(key1, key2);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
example\_hash\_big(\textcolor{keywordtype}{void})
\{
   \hyperlink{group__Eina__Hash__Group_ga9a89d7d3ee260d3a60572ebf651b07e0}{Eina\_Hash} *phone\_book = NULL;
   \textcolor{keywordtype}{int} i;
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *phone;

   \textcolor{comment}{// Create the same hash as used in eina\_hash\_01.c, but}
   \textcolor{comment}{// use 1024 (2 ^ 10) buckets.}
   phone\_book = \hyperlink{group__Eina__Hash__Group_ga32c9a8a82e2d20338618baf6af3d1614}{eina\_hash\_new}(\hyperlink{group__Eina__Hash__Group_gaaaa18f69c9854f63beae15c42f1256b8}{EINA\_KEY\_LENGTH}(\_phone\_book\_string\_key\_length),
                              \hyperlink{group__Eina__Hash__Group_ga1c48d74b1ed071d8c156a98f5f0a06f6}{EINA\_KEY\_CMP}(\_phone\_book\_string\_key\_cmp),
                              \hyperlink{group__Eina__Hash__Group_gaf53593bdd5cc3d238e5eea2ae86d42a0}{EINA\_KEY\_HASH}(\hyperlink{group__Eina__Hash__Group_ga3a8434dd0fe7446af605c75e05e8eff0}{eina\_hash\_superfast}),
                              \_phone\_entry\_free\_cb,
                              10);
   \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}{// Look for a specific entry and get its phone number}
   phone = \hyperlink{group__Eina__Hash__Group_ga45703d00fbabffc86e6836ffe98a4c92}{eina\_hash\_find}(phone\_book, \textcolor{stringliteral}{"Heitor Villa-Lobos"});
   \textcolor{keywordflow}{if} (phone)
     \{
        printf(\textcolor{stringliteral}{"Printing entry.\(\backslash\)n"});
        printf(\textcolor{stringliteral}{"Name: Heitor Villa-Lobos\(\backslash\)n"});
        printf(\textcolor{stringliteral}{"Number: %s\(\backslash\)n\(\backslash\)n"}, phone);
     \}

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

\textcolor{keywordtype}{int}
main(\textcolor{keywordtype}{int} argc, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *argv[])
\{
   (void)argc;
   (void)argv;
   \hyperlink{group__Eina__Main__Group_gabf448ada5bc81a07342b66c6f818ee5e}{eina\_init}();

   example\_hash\_stringshared();
   example\_hash\_big();

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