aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eina/src/examples/eina_hash_02.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/eina/src/examples/eina_hash_02.c')
-rw-r--r--libraries/eina/src/examples/eina_hash_02.c147
1 files changed, 0 insertions, 147 deletions
diff --git a/libraries/eina/src/examples/eina_hash_02.c b/libraries/eina/src/examples/eina_hash_02.c
deleted file mode 100644
index 7baec93..0000000
--- a/libraries/eina/src/examples/eina_hash_02.c
+++ /dev/null
@@ -1,147 +0,0 @@
1//Compile with:
2//gcc -g eina_hash_02.c -o eina_hash_02 `pkg-config --cflags --libs eina`
3
4#include <stdio.h>
5#include <string.h>
6#include <Eina.h>
7
8/*
9 * Eina Hash - Two more types of hash
10 *
11 * This example demonstrate two other types of hash in action - using
12 * eina_hash_stringshared_new and eina_hash_new.
13 *
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.
16 */
17
18struct _Phone_Entry {
19 const char *name; // Full name.
20 const char *number; // Phone number.
21};
22
23typedef struct _Phone_Entry Phone_Entry;
24
25static 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" },
30 { NULL, NULL }
31};
32
33static void
34_phone_entry_free_cb(void *data)
35{
36 free(data);
37}
38
39static void
40_phone_book_stringshared_free_cb(void *data)
41{
42 Phone_Entry *e = data;
43 eina_stringshare_del(e->name);
44 eina_stringshare_del(e->number);
45 free(e);
46}
47
48static Eina_Bool
49_phone_book_stringshared_foreach_cb(const Eina_Hash *phone_book,
50 const void *key, void *data, void *fdata)
51{
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);
56
57 return EINA_TRUE;
58}
59
60static void
61example_hash_stringshared(void)
62{
63 Eina_Hash *phone_book = NULL;
64 int i;
65
66 // Create the hash as before
67 phone_book = eina_hash_stringshared_new(_phone_book_stringshared_free_cb);
68
69 // Add initial entries to our hash, using direct_add
70 for (i = 0; _start_entries[i].name != NULL; i++)
71 {
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);
80 }
81
82 // Iterate over the elements
83 printf("List of phones:\n");
84 eina_hash_foreach(phone_book, _phone_book_stringshared_foreach_cb, NULL);
85 printf("\n");
86
87 eina_hash_free(phone_book);
88}
89
90static unsigned int
91_phone_book_string_key_length(const char *key)
92{
93 if (!key)
94 return 0;
95
96 return (int)strlen(key) + 1;
97}
98
99static int
100_phone_book_string_key_cmp(const char *key1, int key1_length,
101 const char *key2, int key2_length)
102{
103 return strcmp(key1, key2);
104}
105
106static void
107example_hash_big(void)
108{
109 Eina_Hash *phone_book = NULL;
110 int i;
111 const char *phone;
112
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,
119 10);
120 for (i = 0; _start_entries[i].name != NULL; i++)
121 {
122 eina_hash_add(phone_book, _start_entries[i].name,
123 strdup(_start_entries[i].number));
124 }
125
126 // Look for a specific entry and get its phone number
127 phone = eina_hash_find(phone_book, "Heitor Villa-Lobos");
128 if (phone)
129 {
130 printf("Printing entry.\n");
131 printf("Name: Heitor Villa-Lobos\n");
132 printf("Number: %s\n\n", phone);
133 }
134
135 eina_hash_free(phone_book);
136}
137
138int
139main(int argc, const char *argv[])
140{
141 eina_init();
142
143 example_hash_stringshared();
144 example_hash_big();
145
146 eina_shutdown();
147}