ajwillia-ms pushed a commit to branch master.

http://git.enlightenment.org/tools/examples.git/commit/?id=f10e9e9413f22041d0f083466793bc167bf387bb

commit f10e9e9413f22041d0f083466793bc167bf387bb
Author: Andy Williams <[email protected]>
Date:   Tue Nov 14 17:28:42 2017 +0000

    Add a first pass Eina_Hash reference as well
---
 reference/c/eina/src/eina_hash.c | 105 +++++++++++++++++++++++++++++++++++++++
 reference/c/eina/src/meson.build |   6 +++
 2 files changed, 111 insertions(+)

diff --git a/reference/c/eina/src/eina_hash.c b/reference/c/eina/src/eina_hash.c
new file mode 100644
index 0000000..7d59183
--- /dev/null
+++ b/reference/c/eina/src/eina_hash.c
@@ -0,0 +1,105 @@
+#define EFL_EO_API_SUPPORT 1
+#define EFL_BETA_API_SUPPORT 1
+
+#include <stdio.h>
+#include <Eina.h>
+
+#include <Efl.h>
+#include <Elementary.h>
+
+static void
+_entry_free_cb(void *data)
+{
+   free(data);
+}
+
+static void
+_entry_print(const void *key, void *data)
+{
+   printf("  Name: %s\tNumber %s\n", (const char *)key, (char *)data);
+}
+
+static void
+_phonebook_print(Eina_Hash *book)
+{
+   unsigned int count;
+   Eina_Iterator *iter;
+   void *data;
+
+   count = eina_hash_population(book);
+   printf("Complete phone book (%d):\n", count);
+
+   iter = eina_hash_iterator_tuple_new(book);
+   while (eina_iterator_next(iter, &data))
+     {
+       Eina_Hash_Tuple *t = data;
+
+       _entry_print(t->key, t->data);
+     }
+   eina_iterator_free(iter);
+
+   printf("\n");
+}
+
+static Eina_Hash *
+_hash_create()
+{
+   const char *names[] =
+   {
+      "Wolfgang Amadeus Mozart", "Ludwig van Beethoven",
+      "Richard Georg Strauss", "Heitor Villa-Lobos"
+   };
+   const char *numbers[] =
+   {
+      "+01 23 456-78910", "+12 34 567-89101",
+      "+23 45 678-91012", "+34 56 789-10123"
+   };
+
+   Eina_Hash *hash;
+   unsigned int i;
+
+   // superfast is the type of hash, we could use others depending on key type
+   hash = eina_hash_string_superfast_new(_entry_free_cb);
+
+   // Add initial entries to our hash
+   for (i = 0; i < 4; i++)
+     eina_hash_add(hash, names[i], strdup(numbers[i]));
+
+   return hash;
+}
+
+EAPI_MAIN void
+efl_main(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
+{
+   Eina_Hash *phone_book;
+   const char *lookup_name = "Ludwig van Beethoven";
+   char *number, *old_num;
+
+   phone_book = _hash_create();
+   _phonebook_print(phone_book);
+
+   number = eina_hash_find(phone_book, lookup_name);
+   printf("Found entry:\n");
+   _entry_print(lookup_name, number);
+   printf("\n");
+
+   // Let's first add a new entry
+   eina_hash_set(phone_book, "Raul Seixas", strdup("+55 01 234-56789"));
+
+   // Change the phone number for an entry
+   old_num = eina_hash_set(phone_book, lookup_name, strdup("+12 34 
222-22222"));
+   if (old_num)
+     printf("Old number for %s was %s\n\n", lookup_name, old_num);
+
+   // Change the name (key) on an entry
+   eina_hash_move(phone_book, "Raul Seixas", "Alceu Valenca");
+
+   printf("After modifications\n");
+   _phonebook_print(phone_book);
+
+   eina_hash_free(phone_book);
+
+   efl_exit(0);
+}
+EFL_MAIN()
+
diff --git a/reference/c/eina/src/meson.build b/reference/c/eina/src/meson.build
index b26122a..7c5f683 100644
--- a/reference/c/eina/src/meson.build
+++ b/reference/c/eina/src/meson.build
@@ -21,3 +21,9 @@ executable('efl_reference_eina_iterator',
   install : true
 )
 
+executable('efl_reference_eina_hash',
+  files(['eina_hash.c']),
+  dependencies : deps,
+  include_directories : inc,
+  install : true
+)

-- 


Reply via email to