eina/hash - Added examples that use different types of hash.
authorantognolli <antognolli@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Wed, 6 Jul 2011 17:59:58 +0000 (17:59 +0000)
committerantognolli <antognolli@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Wed, 6 Jul 2011 17:59:58 +0000 (17:59 +0000)
git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/eina@61092 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

src/examples/Makefile.am
src/examples/eina_hash_01.c
src/examples/eina_hash_03.c [new file with mode: 0644]
src/examples/eina_hash_04.c [new file with mode: 0644]
src/examples/eina_hash_05.c [new file with mode: 0644]
src/examples/eina_hash_06.c [new file with mode: 0644]
src/examples/eina_hash_07.c [new file with mode: 0644]

index 7585257..cd185bc 100644 (file)
@@ -18,6 +18,11 @@ SRCS = \
        eina_file_01.c \
        eina_hash_01.c \
        eina_hash_02.c \
+       eina_hash_03.c \
+       eina_hash_04.c \
+       eina_hash_05.c \
+       eina_hash_06.c \
+       eina_hash_07.c \
        eina_iterator_01.c \
        eina_list_01.c \
        eina_list_02.c \
@@ -48,6 +53,11 @@ pkglib_PROGRAMS += \
        eina_file_01 \
        eina_hash_01 \
        eina_hash_02 \
+       eina_hash_03 \
+       eina_hash_04 \
+       eina_hash_05 \
+       eina_hash_06 \
+       eina_hash_07 \
        eina_iterator_01 \
        eina_list_01 \
        eina_list_02 \
index 11e0076..549ca80 100644 (file)
@@ -112,10 +112,10 @@ main(int argc, const char *argv[])
    // Now change the phone number
    eina_error_set(0);
    phone = eina_hash_set(phone_book, "Raul Seixas",
-                        strdup("+55 02 234-56789"));
+                        strdup("+55 02 222-22222"));
    if (phone)
      {
-       printf("Changing phone for Raul Seixas to +55 02 234-56789. ");
+       printf("Changing phone for Raul Seixas to +55 02 222-22222. ");
        printf("Old phone was %s\n", phone);
        free(phone);
      }
diff --git a/src/examples/eina_hash_03.c b/src/examples/eina_hash_03.c
new file mode 100644 (file)
index 0000000..719d0d7
--- /dev/null
@@ -0,0 +1,195 @@
+#include <stdio.h>
+#include <string.h>
+#include <Eina.h>
+
+/*
+ * Eina Hash - phonebook
+ *
+ * This example demonstrate the use of Eina Hash by implementing a phonebook
+ * that stores its contact data into the hash.
+ *
+ * It indexes the phone numbers by Contact Full Name, so it's a hash with
+ * string keys.
+ */
+
+struct _Phone_Entry {
+     const char *name; // Full name.
+     const char *number; // Phone number.
+};
+
+typedef struct _Phone_Entry Phone_Entry;
+
+static Phone_Entry _start_entries[] = {
+       { "Wolfgang Amadeus Mozart", "+01 23 456-78910" },
+       { "Ludwig van Beethoven", "+12 34 567-89101" },
+       { "Richard Georg Strauss", "+23 45 678-91012" },
+       { "Heitor Villa-Lobos", "+34 56 789-10123" },
+       { NULL, NULL }
+}; // _start_entries
+
+static void
+_phone_entry_free_cb(void *data)
+{
+   free(data);
+}
+
+static Eina_Bool
+_phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key,
+                      void *data, void *fdata)
+{
+   const char *name = key;
+   const char *number = data;
+   printf("%s: %s\n", name, number);
+
+   // Return EINA_FALSE to stop this callback from being called
+   return EINA_TRUE;
+}
+
+int
+main(int argc, const char *argv[])
+{
+   Eina_Hash *phone_book = NULL;
+   int i;
+   const char *entry_name = "Heitor Villa-Lobos";
+   char *phone = NULL;
+   Eina_Bool r;
+   Eina_Iterator *it;
+   void *data;
+
+   eina_init();
+
+   phone_book = eina_hash_string_small_new(_phone_entry_free_cb);
+
+   // Add initial entries to our hash
+   for (i = 0; _start_entries[i].name != NULL; i++)
+     {
+       eina_hash_add(phone_book, _start_entries[i].name,
+                     strdup(_start_entries[i].number));
+     }
+
+   // Look for a specific entry and get its phone number
+   phone = eina_hash_find(phone_book, entry_name);
+   if (phone)
+     {
+       printf("Printing entry.\n");
+       printf("Name: %s\n", entry_name);
+       printf("Number: %s\n\n", phone);
+     }
+
+   // Delete this entry
+   r = eina_hash_del(phone_book, entry_name, NULL);
+   printf("Hash entry successfully deleted? %d\n\n", r);
+
+   // Modify the pointer data of an entry and free the old one
+   phone = eina_hash_modify(phone_book, "Richard Georg Strauss",
+                           strdup("+23 45 111-11111"));
+   free(phone);
+
+   // Modify or add an entry to the hash with eina_hash_set
+   // Let's first add a new entry
+   eina_error_set(0);
+   phone = eina_hash_set(phone_book, "Raul Seixas",
+                        strdup("+55 01 234-56789"));
+   if (!phone)
+     {
+       Eina_Error err = eina_error_get();
+       if (!err)
+         {
+            printf("No previous phone found for Raul Seixas. ");
+            printf("Creating new entry.\n");
+         }
+       else
+         printf("Error when setting phone for Raul Seixas\n");
+     }
+   else
+     {
+       printf("Old phone for Raul Seixas was %s\n", phone);
+       free(phone);
+     }
+
+   printf("\n");
+
+   // Now change the phone number
+   eina_error_set(0);
+   phone = eina_hash_set(phone_book, "Raul Seixas",
+                        strdup("+55 02 222-22222"));
+   if (phone)
+     {
+       printf("Changing phone for Raul Seixas to +55 02 222-22222. ");
+       printf("Old phone was %s\n", phone);
+       free(phone);
+     }
+   else
+     {
+       Eina_Error err = eina_error_get();
+       if (err)
+         printf("Error when changing phone for Raul Seixas\n");
+       else
+         {
+            printf("No previous phone found for Raul Seixas. ");
+            printf("Creating new entry.\n");
+         }
+     }
+
+   // There are many ways to iterate over our Phone book.
+   // First, iterate showing the names and associated numbers.
+   printf("List of phones:\n");
+   eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
+   printf("\n");
+
+   // Now iterate using an iterator
+   printf("List of phones:\n");
+   it = eina_hash_iterator_tuple_new(phone_book);
+   while (eina_iterator_next(it, &data))
+     {
+       Eina_Hash_Tuple *t = data;
+       const char *name = t->key;
+       const char *number = t->data;
+       printf("%s: %s\n", name, number);
+     }
+   eina_iterator_free(it); // Always free the iterator after its use
+   printf("\n");
+
+   // Just iterate over the keys (names)
+   printf("List of names in the phone book:\n");
+   it = eina_hash_iterator_key_new(phone_book);
+   while (eina_iterator_next(it, &data))
+     {
+       const char *name = data;
+       printf("%s\n", name);
+     }
+   eina_iterator_free(it);
+   printf("\n");
+
+   // Just iterate over the data (numbers)
+   printf("List of numbers in the phone book:\n");
+   it = eina_hash_iterator_data_new(phone_book);
+   while (eina_iterator_next(it, &data))
+     {
+       const char *number = data;
+       printf("%s\n", number);
+     }
+   eina_iterator_free(it);
+   printf("\n");
+
+   // Check how many items are in the phone book
+   printf("There are %d items in the hash.\n\n",
+         eina_hash_population(phone_book));
+
+   // Change the name (key) on an entry
+   eina_hash_move(phone_book, "Raul Seixas", "Alceu Valenca");
+   printf("List of phones after change:\n");
+   eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
+   printf("\n");
+
+   // Empty the phone book, but don't destroy it
+   eina_hash_free_buckets(phone_book);
+   printf("There are %d items in the hash.\n\n",
+         eina_hash_population(phone_book));
+
+   // Phone book could still be used, but we are freeing it since we are
+   // done for now
+   eina_hash_free(phone_book);
+
+   eina_shutdown();
+}
diff --git a/src/examples/eina_hash_04.c b/src/examples/eina_hash_04.c
new file mode 100644 (file)
index 0000000..e900475
--- /dev/null
@@ -0,0 +1,195 @@
+#include <stdio.h>
+#include <string.h>
+#include <Eina.h>
+
+/*
+ * Eina Hash - phonebook
+ *
+ * This example demonstrate the use of Eina Hash by implementing a phonebook
+ * that stores its contact data into the hash.
+ *
+ * It indexes the phone numbers by Contact Full Name, so it's a hash with
+ * string keys.
+ */
+
+struct _Phone_Entry {
+     const char *name; // Full name.
+     const char *number; // Phone number.
+};
+
+typedef struct _Phone_Entry Phone_Entry;
+
+static Phone_Entry _start_entries[] = {
+       { "Wolfgang Amadeus Mozart", "+01 23 456-78910" },
+       { "Ludwig van Beethoven", "+12 34 567-89101" },
+       { "Richard Georg Strauss", "+23 45 678-91012" },
+       { "Heitor Villa-Lobos", "+34 56 789-10123" },
+       { NULL, NULL }
+}; // _start_entries
+
+static void
+_phone_entry_free_cb(void *data)
+{
+   free(data);
+}
+
+static Eina_Bool
+_phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key,
+                      void *data, void *fdata)
+{
+   const char *name = key;
+   const char *number = data;
+   printf("%s: %s\n", name, number);
+
+   // Return EINA_FALSE to stop this callback from being called
+   return EINA_TRUE;
+}
+
+int
+main(int argc, const char *argv[])
+{
+   Eina_Hash *phone_book = NULL;
+   int i;
+   const char *entry_name = "Heitor Villa-Lobos";
+   char *phone = NULL;
+   Eina_Bool r;
+   Eina_Iterator *it;
+   void *data;
+
+   eina_init();
+
+   phone_book = eina_hash_string_djb2_new(_phone_entry_free_cb);
+
+   // Add initial entries to our hash
+   for (i = 0; _start_entries[i].name != NULL; i++)
+     {
+       eina_hash_add(phone_book, _start_entries[i].name,
+                     strdup(_start_entries[i].number));
+     }
+
+   // Look for a specific entry and get its phone number
+   phone = eina_hash_find(phone_book, entry_name);
+   if (phone)
+     {
+       printf("Printing entry.\n");
+       printf("Name: %s\n", entry_name);
+       printf("Number: %s\n\n", phone);
+     }
+
+   // Delete this entry
+   r = eina_hash_del(phone_book, entry_name, NULL);
+   printf("Hash entry successfully deleted? %d\n\n", r);
+
+   // Modify the pointer data of an entry and free the old one
+   phone = eina_hash_modify(phone_book, "Richard Georg Strauss",
+                           strdup("+23 45 111-11111"));
+   free(phone);
+
+   // Modify or add an entry to the hash with eina_hash_set
+   // Let's first add a new entry
+   eina_error_set(0);
+   phone = eina_hash_set(phone_book, "Raul Seixas",
+                        strdup("+55 01 234-56789"));
+   if (!phone)
+     {
+       Eina_Error err = eina_error_get();
+       if (!err)
+         {
+            printf("No previous phone found for Raul Seixas. ");
+            printf("Creating new entry.\n");
+         }
+       else
+         printf("Error when setting phone for Raul Seixas\n");
+     }
+   else
+     {
+       printf("Old phone for Raul Seixas was %s\n", phone);
+       free(phone);
+     }
+
+   printf("\n");
+
+   // Now change the phone number
+   eina_error_set(0);
+   phone = eina_hash_set(phone_book, "Raul Seixas",
+                        strdup("+55 02 222-22222"));
+   if (phone)
+     {
+       printf("Changing phone for Raul Seixas to +55 02 222-22222. ");
+       printf("Old phone was %s\n", phone);
+       free(phone);
+     }
+   else
+     {
+       Eina_Error err = eina_error_get();
+       if (err)
+         printf("Error when changing phone for Raul Seixas\n");
+       else
+         {
+            printf("No previous phone found for Raul Seixas. ");
+            printf("Creating new entry.\n");
+         }
+     }
+
+   // There are many ways to iterate over our Phone book.
+   // First, iterate showing the names and associated numbers.
+   printf("List of phones:\n");
+   eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
+   printf("\n");
+
+   // Now iterate using an iterator
+   printf("List of phones:\n");
+   it = eina_hash_iterator_tuple_new(phone_book);
+   while (eina_iterator_next(it, &data))
+     {
+       Eina_Hash_Tuple *t = data;
+       const char *name = t->key;
+       const char *number = t->data;
+       printf("%s: %s\n", name, number);
+     }
+   eina_iterator_free(it); // Always free the iterator after its use
+   printf("\n");
+
+   // Just iterate over the keys (names)
+   printf("List of names in the phone book:\n");
+   it = eina_hash_iterator_key_new(phone_book);
+   while (eina_iterator_next(it, &data))
+     {
+       const char *name = data;
+       printf("%s\n", name);
+     }
+   eina_iterator_free(it);
+   printf("\n");
+
+   // Just iterate over the data (numbers)
+   printf("List of numbers in the phone book:\n");
+   it = eina_hash_iterator_data_new(phone_book);
+   while (eina_iterator_next(it, &data))
+     {
+       const char *number = data;
+       printf("%s\n", number);
+     }
+   eina_iterator_free(it);
+   printf("\n");
+
+   // Check how many items are in the phone book
+   printf("There are %d items in the hash.\n\n",
+         eina_hash_population(phone_book));
+
+   // Change the name (key) on an entry
+   eina_hash_move(phone_book, "Raul Seixas", "Alceu Valenca");
+   printf("List of phones after change:\n");
+   eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
+   printf("\n");
+
+   // Empty the phone book, but don't destroy it
+   eina_hash_free_buckets(phone_book);
+   printf("There are %d items in the hash.\n\n",
+         eina_hash_population(phone_book));
+
+   // Phone book could still be used, but we are freeing it since we are
+   // done for now
+   eina_hash_free(phone_book);
+
+   eina_shutdown();
+}
diff --git a/src/examples/eina_hash_05.c b/src/examples/eina_hash_05.c
new file mode 100644 (file)
index 0000000..f8dbf78
--- /dev/null
@@ -0,0 +1,198 @@
+#include <stdio.h>
+#include <string.h>
+#include <Eina.h>
+
+/*
+ * Eina Hash - phonebook
+ *
+ * This example demonstrate the use of Eina Hash by implementing a phonebook
+ * that stores its contact data into the hash.
+ *
+ * It indexes the phone numbers by Contact Full Name, so it's a hash with
+ * string keys.
+ */
+
+struct _Phone_Entry {
+     int32_t id; // Full name.
+     const char *number; // Phone number.
+};
+
+typedef struct _Phone_Entry Phone_Entry;
+
+static Phone_Entry _start_entries[] = {
+       { 1, "+01 23 456-78910" },
+       { 2, "+12 34 567-89101" },
+       { 3, "+23 45 678-91012" },
+       { 4, "+34 56 789-10123" },
+       { -1, NULL }
+}; // _start_entries
+
+static void
+_phone_entry_free_cb(void *data)
+{
+   free(data);
+}
+
+static Eina_Bool
+_phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key,
+                      void *data, void *fdata)
+{
+   const int32_t *id = key;
+   const char *number = data;
+   printf("%d: %s\n", *id, number);
+
+   // Return EINA_FALSE to stop this callback from being called
+   return EINA_TRUE;
+}
+
+int
+main(int argc, const char *argv[])
+{
+   Eina_Hash *phone_book = NULL;
+   int i;
+   int32_t entry_id = 4;
+   char *phone = NULL;
+   Eina_Bool r;
+   Eina_Iterator *it;
+   void *data;
+
+   eina_init();
+
+   phone_book = eina_hash_int32_new(_phone_entry_free_cb);
+
+   // Add initial entries to our hash
+   for (i = 0; _start_entries[i].id != -1; i++)
+     {
+       eina_hash_add(phone_book, &_start_entries[i].id,
+                     strdup(_start_entries[i].number));
+     }
+
+   // Look for a specific entry and get its phone number
+   phone = eina_hash_find(phone_book, &entry_id);
+   if (phone)
+     {
+       printf("Printing entry.\n");
+       printf("Id: %d\n", entry_id);
+       printf("Number: %s\n\n", phone);
+     }
+
+   // Delete this entry
+   r = eina_hash_del(phone_book, &entry_id, NULL);
+   printf("Hash entry successfully deleted? %d\n\n", r);
+
+   // Modify the pointer data of an entry and free the old one
+   int32_t id3 = 3;
+   phone = eina_hash_modify(phone_book, &id3,
+                           strdup("+23 45 111-11111"));
+   free(phone);
+
+   // Modify or add an entry to the hash with eina_hash_set
+   // Let's first add a new entry
+   int32_t id5 = 5;
+   eina_error_set(0);
+   phone = eina_hash_set(phone_book, &id5,
+                        strdup("+55 01 234-56789"));
+   if (!phone)
+     {
+       Eina_Error err = eina_error_get();
+       if (!err)
+         {
+            printf("No previous phone found for id5. ");
+            printf("Creating new entry.\n");
+         }
+       else
+         printf("Error when setting phone for Raul Seixas\n");
+     }
+   else
+     {
+       printf("Old phone for id5 was %s\n", phone);
+       free(phone);
+     }
+
+   printf("\n");
+
+   // Now change the phone number
+   eina_error_set(0);
+   phone = eina_hash_set(phone_book, &id5,
+                        strdup("+55 02 222-22222"));
+   if (phone)
+     {
+       printf("Changing phone for id5 to +55 02 222-22222. ");
+       printf("Old phone was %s\n", phone);
+       free(phone);
+     }
+   else
+     {
+       Eina_Error err = eina_error_get();
+       if (err)
+         printf("Error when changing phone for id5\n");
+       else
+         {
+            printf("No previous phone found for id5. ");
+            printf("Creating new entry.\n");
+         }
+     }
+
+   // There are many ways to iterate over our Phone book.
+   // First, iterate showing the names and associated numbers.
+   printf("List of phones:\n");
+   eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
+   printf("\n");
+
+   // Now iterate using an iterator
+   printf("List of phones:\n");
+   it = eina_hash_iterator_tuple_new(phone_book);
+   while (eina_iterator_next(it, &data))
+     {
+       Eina_Hash_Tuple *t = data;
+       const int32_t *id = t->key;
+       const char *number = t->data;
+       printf("%d: %s\n", *id, number);
+     }
+   eina_iterator_free(it); // Always free the iterator after its use
+   printf("\n");
+
+   // Just iterate over the keys (names)
+   printf("List of ids in the phone book:\n");
+   it = eina_hash_iterator_key_new(phone_book);
+   while (eina_iterator_next(it, &data))
+     {
+       const int32_t *id = data;
+       printf("%d\n", *id);
+     }
+   eina_iterator_free(it);
+   printf("\n");
+
+   // Just iterate over the data (numbers)
+   printf("List of numbers in the phone book:\n");
+   it = eina_hash_iterator_data_new(phone_book);
+   while (eina_iterator_next(it, &data))
+     {
+       const char *number = data;
+       printf("%s\n", number);
+     }
+   eina_iterator_free(it);
+   printf("\n");
+
+   // Check how many items are in the phone book
+   printf("There are %d items in the hash.\n\n",
+         eina_hash_population(phone_book));
+
+   // Change the name (key) on an entry
+   int32_t id6 = 6;
+   eina_hash_move(phone_book, &id5, &id6);
+   printf("List of phones after change:\n");
+   eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
+   printf("\n");
+
+   // Empty the phone book, but don't destroy it
+   eina_hash_free_buckets(phone_book);
+   printf("There are %d items in the hash.\n\n",
+         eina_hash_population(phone_book));
+
+   // Phone book could still be used, but we are freeing it since we are
+   // done for now
+   eina_hash_free(phone_book);
+
+   eina_shutdown();
+}
diff --git a/src/examples/eina_hash_06.c b/src/examples/eina_hash_06.c
new file mode 100644 (file)
index 0000000..99a2bd7
--- /dev/null
@@ -0,0 +1,198 @@
+#include <stdio.h>
+#include <string.h>
+#include <Eina.h>
+
+/*
+ * Eina Hash - phonebook
+ *
+ * This example demonstrate the use of Eina Hash by implementing a phonebook
+ * that stores its contact data into the hash.
+ *
+ * It indexes the phone numbers by Contact Full Name, so it's a hash with
+ * string keys.
+ */
+
+struct _Phone_Entry {
+     int64_t id; // Full name.
+     const char *number; // Phone number.
+};
+
+typedef struct _Phone_Entry Phone_Entry;
+
+static Phone_Entry _start_entries[] = {
+       { 1, "+01 23 456-78910" },
+       { 2, "+12 34 567-89101" },
+       { 3, "+23 45 678-91012" },
+       { 4, "+34 56 789-10123" },
+       { -1, NULL }
+}; // _start_entries
+
+static void
+_phone_entry_free_cb(void *data)
+{
+   free(data);
+}
+
+static Eina_Bool
+_phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key,
+                      void *data, void *fdata)
+{
+   const int64_t *id = key;
+   const char *number = data;
+   printf("%ld: %s\n", *id, number);
+
+   // Return EINA_FALSE to stop this callback from being called
+   return EINA_TRUE;
+}
+
+int
+main(int argc, const char *argv[])
+{
+   Eina_Hash *phone_book = NULL;
+   int i;
+   int64_t entry_id = 4;
+   char *phone = NULL;
+   Eina_Bool r;
+   Eina_Iterator *it;
+   void *data;
+
+   eina_init();
+
+   phone_book = eina_hash_int64_new(_phone_entry_free_cb);
+
+   // Add initial entries to our hash
+   for (i = 0; _start_entries[i].id != -1; i++)
+     {
+       eina_hash_add(phone_book, &_start_entries[i].id,
+                     strdup(_start_entries[i].number));
+     }
+
+   // Look for a specific entry and get its phone number
+   phone = eina_hash_find(phone_book, &entry_id);
+   if (phone)
+     {
+       printf("Printing entry.\n");
+       printf("Id: %ld\n", entry_id);
+       printf("Number: %s\n\n", phone);
+     }
+
+   // Delete this entry
+   r = eina_hash_del(phone_book, &entry_id, NULL);
+   printf("Hash entry successfully deleted? %d\n\n", r);
+
+   // Modify the pointer data of an entry and free the old one
+   int64_t id3 = 3;
+   phone = eina_hash_modify(phone_book, &id3,
+                           strdup("+23 45 111-11111"));
+   free(phone);
+
+   // Modify or add an entry to the hash with eina_hash_set
+   // Let's first add a new entry
+   int64_t id5 = 5;
+   eina_error_set(0);
+   phone = eina_hash_set(phone_book, &id5,
+                        strdup("+55 01 234-56789"));
+   if (!phone)
+     {
+       Eina_Error err = eina_error_get();
+       if (!err)
+         {
+            printf("No previous phone found for id5. ");
+            printf("Creating new entry.\n");
+         }
+       else
+         printf("Error when setting phone for Raul Seixas\n");
+     }
+   else
+     {
+       printf("Old phone for id5 was %s\n", phone);
+       free(phone);
+     }
+
+   printf("\n");
+
+   // Now change the phone number
+   eina_error_set(0);
+   phone = eina_hash_set(phone_book, &id5,
+                        strdup("+55 02 222-22222"));
+   if (phone)
+     {
+       printf("Changing phone for id5 to +55 02 222-22222. ");
+       printf("Old phone was %s\n", phone);
+       free(phone);
+     }
+   else
+     {
+       Eina_Error err = eina_error_get();
+       if (err)
+         printf("Error when changing phone for id5\n");
+       else
+         {
+            printf("No previous phone found for id5. ");
+            printf("Creating new entry.\n");
+         }
+     }
+
+   // There are many ways to iterate over our Phone book.
+   // First, iterate showing the names and associated numbers.
+   printf("List of phones:\n");
+   eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
+   printf("\n");
+
+   // Now iterate using an iterator
+   printf("List of phones:\n");
+   it = eina_hash_iterator_tuple_new(phone_book);
+   while (eina_iterator_next(it, &data))
+     {
+       Eina_Hash_Tuple *t = data;
+       const int64_t *id = t->key;
+       const char *number = t->data;
+       printf("%ld: %s\n", *id, number);
+     }
+   eina_iterator_free(it); // Always free the iterator after its use
+   printf("\n");
+
+   // Just iterate over the keys (names)
+   printf("List of ids in the phone book:\n");
+   it = eina_hash_iterator_key_new(phone_book);
+   while (eina_iterator_next(it, &data))
+     {
+       const int64_t *id = data;
+       printf("%ld\n", *id);
+     }
+   eina_iterator_free(it);
+   printf("\n");
+
+   // Just iterate over the data (numbers)
+   printf("List of numbers in the phone book:\n");
+   it = eina_hash_iterator_data_new(phone_book);
+   while (eina_iterator_next(it, &data))
+     {
+       const char *number = data;
+       printf("%s\n", number);
+     }
+   eina_iterator_free(it);
+   printf("\n");
+
+   // Check how many items are in the phone book
+   printf("There are %d items in the hash.\n\n",
+         eina_hash_population(phone_book));
+
+   // Change the name (key) on an entry
+   int64_t id6 = 6;
+   eina_hash_move(phone_book, &id5, &id6);
+   printf("List of phones after change:\n");
+   eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
+   printf("\n");
+
+   // Empty the phone book, but don't destroy it
+   eina_hash_free_buckets(phone_book);
+   printf("There are %d items in the hash.\n\n",
+         eina_hash_population(phone_book));
+
+   // Phone book could still be used, but we are freeing it since we are
+   // done for now
+   eina_hash_free(phone_book);
+
+   eina_shutdown();
+}
diff --git a/src/examples/eina_hash_07.c b/src/examples/eina_hash_07.c
new file mode 100644 (file)
index 0000000..b3e486a
--- /dev/null
@@ -0,0 +1,219 @@
+#include <stdio.h>
+#include <string.h>
+#include <Eina.h>
+
+/*
+ * Eina Hash - phonebook
+ *
+ * This example demonstrate the use of Eina Hash by implementing a phonebook
+ * that stores its contact data into the hash.
+ *
+ * It indexes the phone numbers by Contact Full Name, so it's a hash with
+ * string keys.
+ */
+
+struct _Phone_Entry {
+     const char *name; // Full name.
+     const char *number; // Phone number.
+};
+
+typedef struct _Phone_Entry Phone_Entry;
+
+static Phone_Entry _start_entries[] = {
+       { "Wolfgang Amadeus Mozart", "+01 23 456-78910" },
+       { "Ludwig van Beethoven", "+12 34 567-89101" },
+       { "Richard Georg Strauss", "+23 45 678-91012" },
+       { "Heitor Villa-Lobos", "+34 56 789-10123" },
+       { NULL, NULL }
+}; // _start_entries
+
+static const char *_nicknames[] = {
+       "mozzart",
+       "betho",
+       "george",
+       "hector",
+       NULL
+};
+
+static void
+_phone_entry_free_cb(void *data)
+{
+   free(data);
+}
+
+static Eina_Bool
+_phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key,
+                      void *data, void *fdata)
+{
+   Phone_Entry **pe = (Phone_Entry **)key;
+   const char *nick = data;
+   printf("%s: %s, nick=%s\n", (*pe)->name, (*pe)->number, nick);
+
+   // Return EINA_FALSE to stop this callback from being called
+   return EINA_TRUE;
+}
+
+int
+main(int argc, const char *argv[])
+{
+   Eina_Hash *phone_book = NULL;
+   int i;
+   Phone_Entry *entry_vl = &_start_entries[3];
+   Phone_Entry *p = NULL;
+   char *nick = NULL;
+   Eina_Bool r;
+   Eina_Iterator *it;
+   void *data;
+
+   eina_init();
+
+   phone_book = eina_hash_pointer_new(_phone_entry_free_cb);
+
+   // Add initial entries to our hash
+   for (i = 0; _start_entries[i].name != NULL; i++)
+     {
+       p = &_start_entries[i];
+       eina_hash_add(phone_book, &p,
+                     strdup(_nicknames[i]));
+     }
+   printf("Phonebook:\n");
+   eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
+   printf("\n");
+
+   // Look for a specific entry and get its nickname
+   nick = eina_hash_find(phone_book, &entry_vl);
+   if (nick)
+     {
+       printf("Printing entry.\n");
+       printf("Name: %s\n", entry_vl->name);
+       printf("Number: %s\n", entry_vl->number);
+       printf("Nick: %s\n\n", nick);
+     }
+
+   // Delete this entry
+   r = eina_hash_del(phone_book, &entry_vl, NULL);
+   printf("Hash entry successfully deleted? %d\n\n", r);
+
+   // Modify the pointer data of an entry and free the old one
+   p = &_start_entries[2];
+   nick = eina_hash_modify(phone_book, &p,
+                          strdup("el jorge"));
+   free(nick);
+
+   // Modify or add an entry to the hash with eina_hash_set
+   // Let's first add a new entry
+   eina_error_set(0);
+   Phone_Entry *p1 = malloc(sizeof(*p1));
+   p1->name = "Raul Seixas";
+   p1->number = "+55 01 234-56789";
+   nick = eina_hash_set(phone_book, &p1,
+                       strdup("raulzito"));
+   if (!nick)
+     {
+       Eina_Error err = eina_error_get();
+       if (!err)
+         {
+            printf("No previous nick found for Raul Seixas. ");
+            printf("Creating new entry.\n");
+         }
+       else
+         printf("Error when setting nick for Raul Seixas\n");
+     }
+   else
+     {
+       printf("Old nick for Raul Seixas was %s\n", nick);
+       free(nick);
+     }
+
+   printf("\n");
+
+   // Now change the nick
+   eina_error_set(0);
+   nick = eina_hash_set(phone_book, &p1,
+                       strdup("raulzao"));
+   if (nick)
+     {
+       printf("Changing nick for Raul Seixas to raulzao. ");
+       printf("Old nick was %s\n", nick);
+       free(nick);
+     }
+   else
+     {
+       Eina_Error err = eina_error_get();
+       if (err)
+         printf("Error when changing nick for Raul Seixas\n");
+       else
+         {
+            printf("No previous nick found for Raul Seixas. ");
+            printf("Creating new entry.\n");
+         }
+     }
+
+   // There are many ways to iterate over our Phone book.
+   // First, iterate showing the names, phones and associated nicks.
+   printf("Phonebook:\n");
+   eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
+   printf("\n");
+
+   // Now iterate using an iterator
+   printf("Phonebook:\n");
+   it = eina_hash_iterator_tuple_new(phone_book);
+   while (eina_iterator_next(it, &data))
+     {
+       Eina_Hash_Tuple *t = data;
+       Phone_Entry **pe = (Phone_Entry **)t->key;
+       nick = t->data;
+       printf("%s: %s, nick=%s\n", (*pe)->name, (*pe)->number, nick);
+     }
+   eina_iterator_free(it); // Always free the iterator after its use
+   printf("\n");
+
+   // Just iterate over the keys (names)
+   printf("List of names/numbers in the phone book:\n");
+   it = eina_hash_iterator_key_new(phone_book);
+   while (eina_iterator_next(it, &data))
+     {
+       Phone_Entry **pe = (Phone_Entry **)data;
+       printf("%s: %s\n", (*pe)->name, (*pe)->number);
+     }
+   eina_iterator_free(it);
+   printf("\n");
+
+   // Just iterate over the data (nicks)
+   printf("List of nicks in the phone book:\n");
+   it = eina_hash_iterator_data_new(phone_book);
+   while (eina_iterator_next(it, &data))
+     {
+       nick = data;
+       printf("%s\n", nick);
+     }
+   eina_iterator_free(it);
+   printf("\n");
+
+   // Check how many items are in the phone book
+   printf("There are %d items in the hash.\n\n",
+         eina_hash_population(phone_book));
+
+   // Change the name (key) on an entry
+   Phone_Entry *p2 = malloc(sizeof(*p2));
+   p2->name = "Alceu Valenca";
+   p2->number = "000000000000";
+   eina_hash_move(phone_book, p1, p2);
+   printf("List of phones after change:\n");
+   eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL);
+   printf("\n");
+
+   // Empty the phone book, but don't destroy it
+   eina_hash_free_buckets(phone_book);
+   printf("There are %d items in the hash.\n\n",
+         eina_hash_population(phone_book));
+
+   // Phone book could still be used, but we are freeing it since we are
+   // done for now
+   eina_hash_free(phone_book);
+
+   free(p1);
+   free(p2);
+
+   eina_shutdown();
+}