10 typedef struct _Evas_Hash_El Evas_Hash_El;
14 Evas_Object_List _list_data;
19 static inline int _evas_hash_gen(const char *key);
21 static int _evas_hash_alloc_error = 0;
24 _evas_hash_gen(const char *key)
26 unsigned int hash_num = 5381;
27 const unsigned char *ptr;
32 for (ptr = (unsigned char *)key; *ptr; ptr++)
33 hash_num = (hash_num * 33) ^ *ptr;
40 * @defgroup Evas_Hash_Data Hash Data Functions
42 * Functions that add, access or remove data from hashes.
44 * The following example shows how to add and then access data in a
47 * Evas_Hash *hash = NULL;
48 * extern void *my_data;
50 * hash = evas_hash_add(hash, "My Data", my_data);
51 * if (evas_hash_alloc_error())
53 * fprintf(stderr, "ERROR: Memory is low. Hash allocation failed.\n");
56 * if (evas_hash_find(hash, "My Data") == my_data)
58 * printf("My Data inserted and successfully found.\n");
62 * What follows is another example, showing how the @ref evas_hash_del
65 * extern Evas_Hash *hash;
68 * printf("Insert some data...\n");
69 * hash = evas_hash_add(hash, "My Data", my_data);
70 * printf("Removing by key...\n");
71 * hash = evas_hash_del(hash, "My Data", NULL);
72 * printf("Insert some more data as a NULL key...\n");
73 * hash = evas_hash_add(hash, NULL, my_data);
74 * printf("Removing by data as a NULL key...\n");
75 * hash = evas_hash_del(hash, NULL, my_data);
80 * Adds an entry to the given hash table.
82 * @p key is expected to be a unique string within the hash table.
83 * Otherwise, you cannot be sure which inserted data pointer will be
84 * accessed with @ref evas_hash_find , and removed with
85 * @ref evas_hash_del .
87 * Key strings are case sensitive.
89 * @ref evas_hash_alloc_error should be used to determine if an
90 * allocation error occurred during this function.
92 * @param hash The given hash table. Can be @c NULL, in which case a
93 * new hash table is allocated and returned.
94 * @param key A unique string. Can be @c NULL.
95 * @param data Data to associate with the string given by @p key.
96 * @return Either the given hash table, or if the given value for @p
97 * hash is @c NULL, then a new one. @c NULL will be returned
98 * if memory could not be allocated for a new table.
99 * @ingroup Evas_Hash_Data
102 evas_hash_add(Evas_Hash *hash, const char *key, const void *data)
107 if ((!key) || (!data))
110 _evas_hash_alloc_error = 0;
113 hash = calloc(1, sizeof(struct _Evas_Hash));
116 _evas_hash_alloc_error = 1;
121 if (!(el = malloc(sizeof(struct _Evas_Hash_El) + strlen(key) + 1)))
123 if (hash->population <= 0)
129 _evas_hash_alloc_error = 1;
133 el->key = ((char *)el) + sizeof(struct _Evas_Hash_El);
134 strcpy((char *)el->key, key);
135 el->data = (void *)data;
136 hash_num = _evas_hash_gen(key);
137 hash->buckets[hash_num] = evas_object_list_prepend(hash->buckets[hash_num],
144 * Adds an entry to the given hash table and does not duplicate the string key.
146 * @p key is expected to be a unique string within the hash table.
147 * Otherwise, you cannot be sure which inserted data pointer will be
148 * accessed with @ref evas_hash_find , and removed with
149 * @ref evas_hash_del . This call does not make a copy of the key so it must
150 * be a string constant or stored elsewhere (in the object being added) etc.
152 * Key strings are case sensitive.
154 * @ref evas_hash_alloc_error should be used to determine if an
155 * allocation error occurred during this function.
157 * @param hash The given hash table. Can be @c NULL, in which case a
158 * new hash table is allocated and returned.
159 * @param key A unique string. Can be @c NULL.
160 * @param data Data to associate with the string given by @p key.
161 * @return Either the given hash table, or if the given value for @p
162 * hash is @c NULL, then a new one. @c NULL will be returned
163 * if memory could not be allocated for a new table.
164 * @ingroup Evas_Hash_Data
167 evas_hash_direct_add(Evas_Hash *hash, const char *key, const void *data)
172 if ((!key) || (!data))
175 _evas_hash_alloc_error = 0;
178 hash = calloc(1, sizeof(struct _Evas_Hash));
181 _evas_hash_alloc_error = 1;
186 if (!(el = malloc(sizeof(struct _Evas_Hash_El))))
188 if (hash->population <= 0)
194 _evas_hash_alloc_error = 1;
199 el->data = (void *)data;
200 hash_num = _evas_hash_gen(key);
201 hash->buckets[hash_num] = evas_object_list_prepend(hash->buckets[hash_num],
208 * Removes the entry identified by @p key or @p data from the given
211 * If @p key is @c NULL, then @p data is used to find a match to
214 * @param hash The given hash table.
215 * @param key The key string. Can be @c NULL.
216 * @param data The data pointer to remove if @p key is @c NULL.
217 * Otherwise, not required and can be @c NULL.
218 * @return The modified hash table. If there are no entries left, the
219 * hash table will be freed and @c NULL will be returned.
220 * @ingroup Evas_Hash_Data
223 evas_hash_del(Evas_Hash *hash, const char *key, const void *data)
233 for (hash_num = 0; hash_num < 256; hash_num++)
235 for (l = hash->buckets[hash_num]; l; l = l->next)
237 el = (Evas_Hash_El *)l;
238 if (el->data == data)
240 hash->buckets[hash_num] = evas_object_list_remove(
241 hash->buckets[hash_num],
245 if (hash->population <= 0)
257 hash_num = _evas_hash_gen(key);
258 for (l = hash->buckets[hash_num]; l; l = l->next)
260 el = (Evas_Hash_El *)l;
261 if (!strcmp(el->key, key))
262 if ((!data) || (el->data == data))
264 hash->buckets[hash_num] = evas_object_list_remove(
265 hash->buckets[hash_num],
269 if (hash->population <= 0)
285 * Retrieves a specific entry in the given hash table.
286 * @param hash The given hash table.
287 * @param key The key string of the entry to find.
288 * @return The data pointer for the stored entry, or @c NULL if not
290 * @ingroup Evas_Hash_Data
293 evas_hash_find(const Evas_Hash *hash, const char *key)
299 _evas_hash_alloc_error = 0;
300 if ((!hash) || (!key))
303 hash_num = _evas_hash_gen(key);
304 for (l = hash->buckets[hash_num]; l; l = l->next)
306 el = (Evas_Hash_El *)l;
307 if (!strcmp(el->key, key))
309 if (l != hash->buckets[hash_num])
311 Evas_Object_List *bucket;
313 bucket = hash->buckets[hash_num];
314 bucket = evas_object_list_remove(bucket, el);
315 bucket = evas_object_list_prepend(bucket, el);
316 ((Evas_Hash *)hash)->buckets[hash_num] = bucket;
326 * Modifies the entry pointer at the specified key and returns the old entry
327 * @param hash The given hash table.
328 * @param key The key string of the entry to modify.
329 * @param data The data to replace the old entry, if it exists.
330 * @return The data pointer for the old stored entry, or @c NULL if not
331 * found. If an existing entry is not found, nothing is added to the
333 * @ingroup Evas_Hash_Data
336 evas_hash_modify(Evas_Hash *hash, const char *key, const void *data)
342 _evas_hash_alloc_error = 0;
346 hash_num = _evas_hash_gen(key);
347 for (l = hash->buckets[hash_num]; l; l = l->next)
349 el = (Evas_Hash_El *)l;
350 if ((key) && (!strcmp(el->key, key)))
354 if (l != hash->buckets[hash_num])
356 hash->buckets[hash_num] = evas_object_list_remove(
357 hash->buckets[hash_num],
359 hash->buckets[hash_num] = evas_object_list_prepend(
360 hash->buckets[hash_num],
365 el->data = (void *)data;
373 * @defgroup Evas_Hash_General_Group Hash General Functions
375 * Miscellaneous functions that operate on hash objects.
379 * Retrieves the number of buckets available in the given hash table.
380 * @param hash The given hash table.
381 * @return @c 256 if @p hash is not @c NULL. @c 0 otherwise.
382 * @ingroup Evas_Hash_General_Group
385 evas_hash_size(const Evas_Hash *hash)
394 * @todo Complete polishing documentation for evas_hash.c. The
395 * functions' docs may be grouped, but they need some simplification.
399 * Free an entire hash table
400 * @param hash The hash table to be freed
402 * This function frees up all the memory allocated to storing the specified
403 * hash tale pointed to by @p hash. Any entries in the table that the program
404 * has no more pointers for elsewhere may now be lost, so this should only be
405 * called if the program has lready freed any allocated data in the hash table
406 * or has the pointers for data in the table stored elswehere as well.
410 * extern Evas_Hash *hash;
412 * evas_hash_free(hash);
415 * @ingroup Evas_Hash_General_Group
418 evas_hash_free(Evas_Hash *hash)
425 size = evas_hash_size(hash);
426 for (i = 0; i < size; i++)
428 while (hash->buckets[i])
432 el = (Evas_Hash_El *)hash->buckets[i];
433 hash->buckets[i] = evas_object_list_remove(hash->buckets[i], el);
441 * Call a function on every member stored in the hash table
442 * @param hash The hash table whose members will be walked
443 * @param func The function to call on each parameter
444 * @param fdata The data pointer to pass to the function being called
446 * This function goes through every entry in the hash table @p hash and calls
447 * the function @p func on each member. The function should NOT modify the
448 * hash table contents if it returns 1. IF the hash table contents are
449 * modified by this function or the function wishes to stop processing it must
450 * return 0, otherwise return 1 to keep processing.
454 * extern Evas_Hash *hash;
456 * Evas_Bool hash_fn(Evas_Hash *hash, const char *key, void *data, void *fdata)
458 * printf("Func data: %s, Hash entry: %s / %p\n", fdata, key, data);
462 * int main(int argc, char **argv)
464 * char *hash_fn_data;
466 * hash_fn_data = strdup("Hello World");
467 * evas_hash_foreach(hash, hash_fn, hash_fn_data);
468 * free(hash_fn_data);
471 * @ingroup Evas_Hash_General_Group
474 evas_hash_foreach(const Evas_Hash *hash, Evas_Bool (*func)(
475 const Evas_Hash *hash,
478 void *fdata), const void *fdata)
485 size = evas_hash_size(hash);
486 for (i = 0; i < size; i++)
488 Evas_Object_List *l, *next_l;
490 for (l = hash->buckets[i]; l; )
495 el = (Evas_Hash_El *)l;
496 if (!func(hash, el->key, el->data, (void *)fdata))
505 * Return memory allocation failure flag after an function requiring allocation
506 * @return The state of the allocation flag
508 * This function returns the state of the memory allocation flag. This flag is
509 * set if memory allocations fail during evas_hash_add() calls. If they do, 1
510 * will be returned, otherwise 0 will be returned. The flag will remain in its
511 * current state until the next call that requires allocation is called, and
516 * Evas_Hash *hash = NULL;
517 * extern void *my_data;
519 * hash = evas_hash_add(hash, "My Data", my_data);
520 * if (evas_hash_alloc_error())
522 * fprintf(stderr, "ERROR: Memory is low. Hash allocation failed.\n");
525 * if (evas_hash_find(hash, "My Data") == my_data)
527 * printf("My Data inserted and successfully found.\n");
530 * @ingroup Evas_Hash_General_Group
533 evas_hash_alloc_error(void)
535 return _evas_hash_alloc_error;