2 //gcc -g eina_hash_02.c -o eina_hash_02 `pkg-config --cflags --libs eina`
9 * Eina Hash - Two more types of hash
11 * This example demonstrate two other types of hash in action - using
12 * eina_hash_stringshared_new and eina_hash_new.
14 * It indexes the phone numbers by Contact Full Name, so it's a hash with string
15 * keys, exactly the same as the other example.
19 const char *name; // Full name.
20 const char *number; // Phone number.
23 typedef struct _Phone_Entry Phone_Entry;
25 static Phone_Entry _start_entries[] = {
26 { "Wolfgang Amadeus Mozart", "+01 23 456-78910" },
27 { "Ludwig van Beethoven", "+12 34 567-89101" },
28 { "Richard Georg Strauss", "+23 45 678-91012" },
29 { "Heitor Villa-Lobos", "+34 56 789-10123" },
34 _phone_entry_free_cb(void *data)
40 _phone_book_stringshared_free_cb(void *data)
42 Phone_Entry *e = data;
43 eina_stringshare_del(e->name);
44 eina_stringshare_del(e->number);
49 _phone_book_stringshared_foreach_cb(const Eina_Hash *phone_book,
50 const void *key, void *data, void *fdata)
52 Phone_Entry *e = data;
53 const char *name = e->name; // e->name == key
54 const char *number = e->number;
55 printf("%s: %s\n", name, number);
61 example_hash_stringshared(void)
63 Eina_Hash *phone_book = NULL;
66 // Create the hash as before
67 phone_book = eina_hash_stringshared_new(_phone_book_stringshared_free_cb);
69 // Add initial entries to our hash, using direct_add
70 for (i = 0; _start_entries[i].name != NULL; i++)
72 Phone_Entry *e = malloc(sizeof(Phone_Entry));
73 e->name = eina_stringshare_add(_start_entries[i].name);
74 e->number = eina_stringshare_add(_start_entries[i].number);
75 // Since we are storing the key (name) in our struct, we can use
76 // eina_hash_direct_add. It could be used in the previous example
77 // too, since each key is already stored in the _start_entries
78 // static array, but we started it with the default add function.
79 eina_hash_direct_add(phone_book, e->name, e);
82 // Iterate over the elements
83 printf("List of phones:\n");
84 eina_hash_foreach(phone_book, _phone_book_stringshared_foreach_cb, NULL);
87 eina_hash_free(phone_book);
91 _phone_book_string_key_length(const char *key)
96 return (int)strlen(key) + 1;
100 _phone_book_string_key_cmp(const char *key1, int key1_length,
101 const char *key2, int key2_length)
103 return strcmp(key1, key2);
107 example_hash_big(void)
109 Eina_Hash *phone_book = NULL;
113 // Create the same hash as used in eina_hash_01.c, but
114 // use 1024 (2 ^ 10) buckets.
115 phone_book = eina_hash_new(EINA_KEY_LENGTH(_phone_book_string_key_length),
116 EINA_KEY_CMP(_phone_book_string_key_cmp),
117 EINA_KEY_HASH(eina_hash_superfast),
118 _phone_entry_free_cb,
120 for (i = 0; _start_entries[i].name != NULL; i++)
122 eina_hash_add(phone_book, _start_entries[i].name,
123 strdup(_start_entries[i].number));
126 // Look for a specific entry and get its phone number
127 phone = eina_hash_find(phone_book, "Heitor Villa-Lobos");
130 printf("Printing entry.\n");
131 printf("Name: Heitor Villa-Lobos\n");
132 printf("Number: %s\n\n", phone);
135 eina_hash_free(phone_book);
139 main(int argc, const char *argv[])
143 example_hash_stringshared();