diff options
Diffstat (limited to 'libraries/eina/src/examples/eina_hash_02.c')
-rw-r--r-- | libraries/eina/src/examples/eina_hash_02.c | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/libraries/eina/src/examples/eina_hash_02.c b/libraries/eina/src/examples/eina_hash_02.c new file mode 100644 index 0000000..d43d7a3 --- /dev/null +++ b/libraries/eina/src/examples/eina_hash_02.c | |||
@@ -0,0 +1,144 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <string.h> | ||
3 | #include <Eina.h> | ||
4 | |||
5 | /* | ||
6 | * Eina Hash - Two more types of hash | ||
7 | * | ||
8 | * This example demonstrate two other types of hash in action - using | ||
9 | * eina_hash_stringshared_new and eina_hash_new. | ||
10 | * | ||
11 | * It indexes the phone numbers by Contact Full Name, so it's a hash with string | ||
12 | * keys, exactly the same as the other example. | ||
13 | */ | ||
14 | |||
15 | struct _Phone_Entry { | ||
16 | const char *name; // Full name. | ||
17 | const char *number; // Phone number. | ||
18 | }; | ||
19 | |||
20 | typedef struct _Phone_Entry Phone_Entry; | ||
21 | |||
22 | static Phone_Entry _start_entries[] = { | ||
23 | { "Wolfgang Amadeus Mozart", "+01 23 456-78910" }, | ||
24 | { "Ludwig van Beethoven", "+12 34 567-89101" }, | ||
25 | { "Richard Georg Strauss", "+23 45 678-91012" }, | ||
26 | { "Heitor Villa-Lobos", "+34 56 789-10123" }, | ||
27 | { NULL, NULL } | ||
28 | }; | ||
29 | |||
30 | static void | ||
31 | _phone_entry_free_cb(void *data) | ||
32 | { | ||
33 | free(data); | ||
34 | } | ||
35 | |||
36 | static void | ||
37 | _phone_book_stringshared_free_cb(void *data) | ||
38 | { | ||
39 | Phone_Entry *e = data; | ||
40 | eina_stringshare_del(e->name); | ||
41 | eina_stringshare_del(e->number); | ||
42 | free(e); | ||
43 | } | ||
44 | |||
45 | static Eina_Bool | ||
46 | _phone_book_stringshared_foreach_cb(const Eina_Hash *phone_book, | ||
47 | const void *key, void *data, void *fdata) | ||
48 | { | ||
49 | Phone_Entry *e = data; | ||
50 | const char *name = e->name; // e->name == key | ||
51 | const char *number = e->number; | ||
52 | printf("%s: %s\n", name, number); | ||
53 | |||
54 | return EINA_TRUE; | ||
55 | } | ||
56 | |||
57 | static void | ||
58 | example_hash_stringshared(void) | ||
59 | { | ||
60 | Eina_Hash *phone_book = NULL; | ||
61 | int i; | ||
62 | |||
63 | // Create the hash as before | ||
64 | phone_book = eina_hash_stringshared_new(_phone_book_stringshared_free_cb); | ||
65 | |||
66 | // Add initial entries to our hash, using direct_add | ||
67 | for (i = 0; _start_entries[i].name != NULL; i++) | ||
68 | { | ||
69 | Phone_Entry *e = malloc(sizeof(Phone_Entry)); | ||
70 | e->name = eina_stringshare_add(_start_entries[i].name); | ||
71 | e->number = eina_stringshare_add(_start_entries[i].number); | ||
72 | // Since we are storing the key (name) in our struct, we can use | ||
73 | // eina_hash_direct_add. It could be used in the previous example | ||
74 | // too, since each key is already stored in the _start_entries | ||
75 | // static array, but we started it with the default add function. | ||
76 | eina_hash_direct_add(phone_book, e->name, e); | ||
77 | } | ||
78 | |||
79 | // Iterate over the elements | ||
80 | printf("List of phones:\n"); | ||
81 | eina_hash_foreach(phone_book, _phone_book_stringshared_foreach_cb, NULL); | ||
82 | printf("\n"); | ||
83 | |||
84 | eina_hash_free(phone_book); | ||
85 | } | ||
86 | |||
87 | static unsigned int | ||
88 | _phone_book_string_key_length(const char *key) | ||
89 | { | ||
90 | if (!key) | ||
91 | return 0; | ||
92 | |||
93 | return (int)strlen(key) + 1; | ||
94 | } | ||
95 | |||
96 | static int | ||
97 | _phone_book_string_key_cmp(const char *key1, int key1_length, | ||
98 | const char *key2, int key2_length) | ||
99 | { | ||
100 | return strcmp(key1, key2); | ||
101 | } | ||
102 | |||
103 | static void | ||
104 | example_hash_big(void) | ||
105 | { | ||
106 | Eina_Hash *phone_book = NULL; | ||
107 | int i; | ||
108 | const char *phone; | ||
109 | |||
110 | // Create the same hash as used in eina_hash_01.c, but | ||
111 | // use 1024 (2 ^ 10) buckets. | ||
112 | phone_book = eina_hash_new(EINA_KEY_LENGTH(_phone_book_string_key_length), | ||
113 | EINA_KEY_CMP(_phone_book_string_key_cmp), | ||
114 | EINA_KEY_HASH(eina_hash_superfast), | ||
115 | _phone_entry_free_cb, | ||
116 | 10); | ||
117 | for (i = 0; _start_entries[i].name != NULL; i++) | ||
118 | { | ||
119 | eina_hash_add(phone_book, _start_entries[i].name, | ||
120 | strdup(_start_entries[i].number)); | ||
121 | } | ||
122 | |||
123 | // Look for a specific entry and get its phone number | ||
124 | phone = eina_hash_find(phone_book, "Heitor Villa-Lobos"); | ||
125 | if (phone) | ||
126 | { | ||
127 | printf("Printing entry.\n"); | ||
128 | printf("Name: Heitor Villa-Lobos\n"); | ||
129 | printf("Number: %s\n\n", phone); | ||
130 | } | ||
131 | |||
132 | eina_hash_free(phone_book); | ||
133 | } | ||
134 | |||
135 | int | ||
136 | main(int argc, const char *argv[]) | ||
137 | { | ||
138 | eina_init(); | ||
139 | |||
140 | example_hash_stringshared(); | ||
141 | example_hash_big(); | ||
142 | |||
143 | eina_shutdown(); | ||
144 | } | ||