summaryrefslogtreecommitdiff
path: root/reference/c/eina/src/eina_hash.c
blob: e7a1f6e913e1ddb601e582e769e5c126f2ebc72b (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#define EFL_EO_API_SUPPORT 1
#define EFL_BETA_API_SUPPORT 1

#include <stdio.h>

#include <Eina.h>
#include <Efl_Core.h>

/*
 * Eina Hash examples.
 *
 * These examples demonstrate how to work with eina_hash data and methods.
 *
 * We have two main hash objects here, firstly an int keyed hash with some
 * dummy content.
 * The second example is that of a simple telephone book. The names are the
 * string keys to the hash with phone numbers in the data field.
 */

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

static Eina_Hash *
_hash_create()
{
   Eina_Hash *hash;
   unsigned int i;

   // let's create a simple hash with integers as keys
   // No need to provide a free func since values are Short-Live Eina Strings
   // (eina_slstr) which will deallocate themselves.
   hash = eina_hash_int32_new(NULL);

   // Add initial entries to our hash
   for (i = 0; i < 10; i++)
     eina_hash_add(hash, &i, eina_slstr_printf("The number %d", i));

   return hash;
}

static void
_hash_demo()
{
   Eina_Hash *hash;
   Eina_Iterator *iter;
   unsigned int *key;
   const char *value;

   hash = _hash_create();

   // get an iterator of the keys so we can print a line per entry
   iter = eina_hash_iterator_key_new(hash);

   printf("Print contents of int hash\n");
   EINA_ITERATOR_FOREACH(iter, key)
   {
      // look up the value for the key so we can print both
      value = eina_hash_find(hash, key);
      printf("  Item found with id %d has value %s\n", *key, value);
   }
   eina_iterator_free(iter);
   printf("\n");

   eina_hash_free(hash);
}

// here we begin the phone book example

static void
_phonebook_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)
{
   Eina_Hash_Tuple *t;
   Eina_Iterator *iter;
   unsigned int count;

   count = eina_hash_population(book);
   printf("Complete phone book (%d):\n", count);

   // this iterator returns the key and value for each entry
   iter = eina_hash_iterator_tuple_new(book);
   EINA_ITERATOR_FOREACH(iter, t)
     _phonebook_entry_print(t->key, t->data);
   eina_iterator_free(iter);

   printf("\n");
}

static Eina_Hash *
_phonebook_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;
}

static void
_phonebook_demo()
{
   Eina_Hash *phone_book;
   const char *lookup_name = "Ludwig van Beethoven";
   char *number, *old_num;

   phone_book = _phonebook_create();
   _phonebook_print(phone_book);

   number = eina_hash_find(phone_book, lookup_name);
   printf("Found entry:\n");
   _phonebook_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);
     _entry_free_cb(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);
}

EAPI_MAIN void
efl_main(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
{
   _hash_demo();
   _phonebook_demo();

   efl_exit(0);
}
EFL_MAIN()