aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eina/src/examples/eina_hash_02.c
blob: d43d7a3de5342d5786b1c4a3f015d343c7ddedfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdio.h>
#include <string.h>
#include <Eina.h>

/*
 * Eina Hash - Two more types of hash
 *
 * This example demonstrate two other types of hash in action - using
 * eina_hash_stringshared_new and eina_hash_new.
 *
 * It indexes the phone numbers by Contact Full Name, so it's a hash with string
 * keys, exactly the same as the other example.
 */

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 }
};

static void
_phone_entry_free_cb(void *data)
{
   free(data);
}

static void
_phone_book_stringshared_free_cb(void *data)
{
   Phone_Entry *e = data;
   eina_stringshare_del(e->name);
   eina_stringshare_del(e->number);
   free(e);
}

static Eina_Bool
_phone_book_stringshared_foreach_cb(const Eina_Hash *phone_book,
				    const void *key, void *data, void *fdata)
{
   Phone_Entry *e = data;
   const char *name = e->name; // e->name == key
   const char *number = e->number;
   printf("%s: %s\n", name, number);

   return EINA_TRUE;
}

static void
example_hash_stringshared(void)
{
   Eina_Hash *phone_book = NULL;
   int i;

   // Create the hash as before
   phone_book = eina_hash_stringshared_new(_phone_book_stringshared_free_cb);

   // Add initial entries to our hash, using direct_add
   for (i = 0; _start_entries[i].name != NULL; i++)
     {
	Phone_Entry *e = malloc(sizeof(Phone_Entry));
	e->name = eina_stringshare_add(_start_entries[i].name);
	e->number = eina_stringshare_add(_start_entries[i].number);
	// Since we are storing the key (name) in our struct, we can use
	// eina_hash_direct_add. It could be used in the previous example
	// too, since each key is already stored in the _start_entries
	// static array, but we started it with the default add function.
	eina_hash_direct_add(phone_book, e->name, e);
     }

   // Iterate over the elements
   printf("List of phones:\n");
   eina_hash_foreach(phone_book, _phone_book_stringshared_foreach_cb, NULL);
   printf("\n");

   eina_hash_free(phone_book);
}

static unsigned int
_phone_book_string_key_length(const char *key)
{
   if (!key)
      return 0;

   return (int)strlen(key) + 1;
}

static int
_phone_book_string_key_cmp(const char *key1, int key1_length,
                     const char *key2, int key2_length)
{
   return strcmp(key1, key2);
}

static void
example_hash_big(void)
{
   Eina_Hash *phone_book = NULL;
   int i;
   const char *phone;

   // Create the same hash as used in eina_hash_01.c, but
   // use 1024 (2 ^ 10) buckets.
   phone_book = eina_hash_new(EINA_KEY_LENGTH(_phone_book_string_key_length),
			      EINA_KEY_CMP(_phone_book_string_key_cmp),
			      EINA_KEY_HASH(eina_hash_superfast),
			      _phone_entry_free_cb,
			      10);
   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, "Heitor Villa-Lobos");
   if (phone)
     {
        printf("Printing entry.\n");
        printf("Name: Heitor Villa-Lobos\n");
        printf("Number: %s\n\n", phone);
     }

   eina_hash_free(phone_book);
}

int
main(int argc, const char *argv[])
{
   eina_init();

   example_hash_stringshared();
   example_hash_big();

   eina_shutdown();
}