1 /* EINA - EFL data type library
2 * Copyright (C) 2002-2008 Carsten Haitzler, Gustavo Sverzut Barbieri,
3 * Vincent Torri, Jorge Luis Zapata Muga, Cedric Bail
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library;
17 * if not, see <http://www.gnu.org/licenses/>.
23 #include "eina_types.h"
24 #include "eina_iterator.h"
27 * @page hash_01_example_page Eina_Hash in action
28 * @dontinclude eina_hash_01.c
30 * We are going to store some tuples into our table, that will map each @a name
31 * to a @a number. The cost to access a given number from the name should be
32 * very small, even with many entries in our table. This is the initial data:
34 * @until // _start_entries
36 * Before starting to play with the hash, let's write a callback that will be
37 * used to free the elements from it. Since we are just storing strduped
38 * strings, we just need to free them:
43 * We also need a callback to iterate over the elements of the list later, so
44 * we are defining it now:
49 * Now let's create our @ref Eina_Hash using @ref
50 * eina_hash_string_superfast_new :
55 * Now we add the keys and data to the hash using @ref eina_hash_add . This
56 * means that the key is copied inside the table, together with the pointer to
57 * the data (phone numbers).
62 * Some basic manipulations with the hash, like finding a value given a key,
63 * deleting an entry, modifying an entry are exemplified in the following lines.
64 * Notice that the @ref eina_hash_modify function returns the old value stored
65 * in that entry, and it needs to be freed, while the @ref eina_hash_del
66 * function already calls our free callback:
71 * The @ref eina_hash_set function can be used to set a key-value entry to the
72 * table if it doesn't exist, or to modify an existent entry. It returns the old
73 * entry if it was already set, and NULL otherwise. But since it will
74 * return NULL on error too, we need to check if an error has occurred:
77 * @until printf("\n");
79 * There are different ways of iterate over the entries of a hash. Here we show
80 * two of them: using @ref eina_hash_foreach and @ref Eina_Iterator .
82 * @skip List of phones
83 * @until eina_iterator_free(it);
85 * It's also possible to change the key for a specific entry, without having to
86 * remove the entry from the table and adding it again:
88 * @skipline eina_hash_move
90 * We can remove all the elements from the table without free the table itself:
92 * @skip Empty the phone book
93 * @until eina_hash_population
95 * Or free the the entire table with its content:
97 * @skipline eina_hash_free
100 * The full code for this example can be seen here: @ref eina_hash_01_c
104 * @page eina_hash_01_c Hash table in action
106 * @include eina_hash_01.c
107 * @example eina_hash_01.c
111 * @page hash_02_example_page Different types of tables
113 * This example shows two more types of hash tables that can be created using
114 * @ref Eina_Hash . For more types, consult the reference documentation of @ref
116 * @include eina_hash_02.c
117 * @example eina_hash_02.c
121 * @addtogroup Eina_Hash_Group Hash Table
123 * @brief Hash table management. Useful for mapping keys to values.
125 * The hash table is useful for when one wants to implement a table that maps
126 * keys (usually strings) to data, and have relatively fast access time. The
127 * performance is proportional to the load factor of the table (number of
128 * elements / number of buckets). See @ref hashtable_algo for implementation
131 * Different implementations exists depending on what kind of key will be used
132 * to access the data: strings, integers, pointers, stringshared or your own.
134 * Eina hash tables can copy the keys when using eina_hash_add() or not when
135 * using eina_hash_direct_add().
137 * @section hashtable_algo Algorithm
139 * The Eina_Hash is implemented using an array of N "buckets", where each
140 * bucket is a pointer to a structure that is the head of a <a
141 * href="http://en.wikipedia.org/wiki/Red-black_tree">red-black tree</a>. The
142 * array can then be indexed by the [hash_of_element mod N]. The
143 * hash_of_element is calculated using the hashing function, passed as
144 * parameter to the @ref eina_hash_new function. N is the number of buckets
145 * (array positions), and is calculated based on the buckets_power_size
146 * (argument of @ref eina_hash_new too). The following picture ilustrates the
149 * @image html 01_hash-table.jpg
151 * Adding an element to the hash table is made of:
152 * @li calculating the hash for that key (using the specified hash function);
153 * @li calculate the array position [hash mod N];
154 * @li add the element to the rbtree on that position.
156 * The two first steps have constant time, proportional to the hash function
157 * being used. Adding the key to the rbtree will be proportional on the number
158 * of keys on that bucket.
160 * The average cost of lookup depends on the number of keys per
161 * bucket (load factor) of the table, if the distribution of keys is
162 * sufficiently uniform.
164 * @section hashtable_perf Performance
166 * As said before, the performance depends on the load factor. So trying to keep
167 * it as small as possible will improve the hash table performance. But
168 * increasing the buckets_power_size will also increase the memory consumption.
169 * The default hash table creation functions already have a good number of
170 * buckets, enough for most cases. Particularly for strings, if just a few keys
171 * (less than 30) will be added to the hash table, @ref
172 * eina_hash_string_small_new should be used. If just stringshared keys are
173 * being added, use @ref eina_hash_stringshared_new. If a lot of keys will be
174 * added to the hash table (e.g. more than 1000), then it's better to increase
175 * the buckets_power_size. See @ref eina_hash_new for more details.
177 * When adding a new key to a hash table, use @ref eina_hash_add or @ref
178 * eina_hash_direct_add (the latter if this key is already stored elsewhere). If
179 * the key may be already inside the hash table, instead of checking with
180 * @ref eina_hash_find and then doing @ref eina_hash_add, one can use just @ref
181 * eina_hash_set (this will change the data pointed by this key if it was
182 * already present in the table).
184 * @section hashtable_tutorial Tutorial
186 * These examples show many Eina_Hash functions in action:
187 * @li @ref hash_01_example_page
188 * @li @ref hash_02_example_page
194 * @addtogroup Eina_Data_Types_Group Data Types
200 * @addtogroup Eina_Containers_Group Containers
206 * @defgroup Eina_Hash_Group Hash Table
213 * Type for a generic hash table.
215 typedef struct _Eina_Hash Eina_Hash;
217 typedef struct _Eina_Hash_Tuple Eina_Hash_Tuple;
219 struct _Eina_Hash_Tuple
221 const void *key; /**< The key */
222 void *data; /**< The data associated to the key */
223 unsigned int key_length; /**< The length of the key */
226 typedef unsigned int (*Eina_Key_Length)(const void *key);
227 #define EINA_KEY_LENGTH(Function) ((Eina_Key_Length)Function)
228 typedef int (*Eina_Key_Cmp)(const void *key1, int key1_length, const void *key2, int key2_length);
229 #define EINA_KEY_CMP(Function) ((Eina_Key_Cmp)Function)
230 typedef int (*Eina_Key_Hash)(const void *key, int key_length);
231 #define EINA_KEY_HASH(Function) ((Eina_Key_Hash)Function)
232 typedef Eina_Bool (*Eina_Hash_Foreach)(const Eina_Hash *hash, const void *key, void *data, void *fdata);
236 * @brief Create a new hash table.
238 * @param key_length_cb The function called when getting the size of the key.
239 * @param key_cmp_cb The function called when comparing the keys.
240 * @param key_hash_cb The function called when getting the values.
241 * @param data_free_cb The function called on each value when the hash
242 * table is freed. @c NULL can be passed as callback.
243 * @param buckets_power_size The size of the buckets.
244 * @return The new hash table.
246 * This function creates a new hash table using user-defined callbacks
247 * to manage the hash table. On failure, @c NULL is returned and
248 * #EINA_ERROR_OUT_OF_MEMORY is set. If @p key_cmp_cb or @p key_hash_cb
249 * are @c NULL, @c NULL is returned. If @p buckets_power_size is
250 * smaller or equal than 2, or if it is greater or equal than 17,
251 * @c NULL is returned.
253 * Pre-defined functions are available to create a hash table. See
254 * eina_hash_string_djb2_new(), eina_hash_string_superfast_new(),
255 * eina_hash_string_small_new(), eina_hash_int32_new(),
256 * eina_hash_int64_new(), eina_hash_pointer_new() and
257 * eina_hash_stringshared_new().
259 EAPI Eina_Hash *eina_hash_new(Eina_Key_Length key_length_cb,
260 Eina_Key_Cmp key_cmp_cb,
261 Eina_Key_Hash key_hash_cb,
262 Eina_Free_Cb data_free_cb,
263 int buckets_power_size) EINA_MALLOC EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(2, 3);
266 * @brief Create a new hash table using the djb2 algorithm.
268 * @param data_free_cb The function called on each value when the hash table
269 * is freed. @c NULL can be passed as callback.
270 * @return The new hash table.
272 * This function creates a new hash table using the djb2 algorithm for
273 * table management and strcmp() to compare the keys. Values can then
274 * be looked up with pointers other than the original key pointer that
275 * was used to add values. On failure, this function returns @c NULL.
277 EAPI Eina_Hash *eina_hash_string_djb2_new(Eina_Free_Cb data_free_cb);
280 * @brief Create a new hash table for use with strings.
282 * @param data_free_cb The function called on each value when the hash table
283 * is freed. @c NULL can be passed as callback.
284 * @return The new hash table.
286 * This function creates a new hash table using the superfast algorithm
287 * for table management and strcmp() to compare the keys. Values can
288 * then be looked up with pointers other than the original key pointer
289 * that was used to add values. On failure, this function returns
292 EAPI Eina_Hash *eina_hash_string_superfast_new(Eina_Free_Cb data_free_cb);
295 * @brief Create a new hash table for use with strings with small bucket size.
297 * @param data_free_cb The function called on each value when the hash table
298 * is freed. @c NULL can be passed as callback.
299 * @return The new hash table.
301 * This function creates a new hash table using the superfast algorithm
302 * for table management and strcmp() to compare the keys, but with a
303 * smaller bucket size (compared to eina_hash_string_superfast_new())
304 * which will minimize the memory used by the returned hash
305 * table. Values can then be looked up with pointers other than the
306 * original key pointer that was used to add values. On failure, this
307 * function returns @c NULL.
309 EAPI Eina_Hash *eina_hash_string_small_new(Eina_Free_Cb data_free_cb);
312 * @brief Create a new hash table for use with 32bit integers.
314 * @param data_free_cb The function called on each value when the hash table
315 * is freed. @c NULL can be passed as callback.
316 * @return The new hash table.
318 * This function creates a new hash table where keys are 32bit integers.
319 * When adding or looking up in the hash table, pointers to 32bit integers
320 * must be passed. They can be addresses on the stack if you let the
321 * eina_hash copy the key. Values can then
322 * be looked up with pointers other than the original key pointer that was
323 * used to add values. This method is not suitable to match string keys as
324 * it would only match the first character.
325 * On failure, this function returns @c NULL.
327 EAPI Eina_Hash *eina_hash_int32_new(Eina_Free_Cb data_free_cb);
330 * @brief Create a new hash table for use with 64bit integers.
332 * @param data_free_cb The function called on each value when the hash table
333 * is freed. @c NULL can be passed as callback.
334 * @return The new hash table.
336 * This function creates a new hash table where keys are 64bit integers.
337 * When adding or looking up in the hash table, pointers to 64bit integers
338 * must be passed. They can be addresses on the stack. Values can then
339 * be looked up with pointers other than the original key pointer that was
340 * used to add values. This method is not suitable to match string keys as
341 * it would only match the first character.
342 * On failure, this function returns @c NULL.
344 EAPI Eina_Hash *eina_hash_int64_new(Eina_Free_Cb data_free_cb);
347 * @brief Create a new hash table for use with pointers.
349 * @param data_free_cb The function called on each value when the hash table
350 * is freed. @c NULL can be passed as callback.
351 * @return The new hash table.
353 * This function creates a new hash table using the int64/int32 algorithm for
354 * table management and dereferenced pointers to compare the
355 * keys. Values can then be looked up with pointers other than the
356 * original key pointer that was used to add values. This method may
357 * appear to be able to match string keys, actually it only matches
358 * the first character. On failure, this function returns @c NULL.
360 EAPI Eina_Hash *eina_hash_pointer_new(Eina_Free_Cb data_free_cb);
363 * @brief Create a new hash table optimized for stringshared values.
365 * @param data_free_cb The function called on each value when the hash table
366 * is freed. @c NULL can be passed as callback.
367 * @return The new hash table.
369 * This function creates a new hash table optimized for stringshared
370 * values. Values CAN NOT be looked up with pointers not
371 * equal to the original key pointer that was used to add a value. On failure,
372 * this function returns @c NULL.
374 * Excerpt of code that will NOT work with this type of hash:
377 * extern Eina_Hash *hash;
378 * extern const char *value;
379 * const char *a = eina_stringshare_add("key");
381 * eina_hash_add(hash, a, value);
382 * eina_hash_find(hash, "key")
385 EAPI Eina_Hash *eina_hash_stringshared_new(Eina_Free_Cb data_free_cb);
388 * @brief Add an entry to the given hash table.
390 * @param hash The given hash table.
391 * @param key A unique key.
392 * @param data Data to associate with the string given by @p key.
393 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
395 * This function adds @p key to @p hash. @p hash, @p key and @p data
396 * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
397 * expected to be unique within the hash table. Key uniqueness varies
398 * depending on the type of @p hash: a stringshared @ref Eina_Hash
399 * need only have unique pointers for keys, but the strings in the
400 * pointers may be identical. All other hash types require the strings
401 * themselves to be unique. Failure to use sufficient uniqueness will
402 * result in unexpected results when inserting data pointers accessed
403 * with eina_hash_find(), and removed with eina_hash_del(). Key
404 * strings are case sensitive. If an error occurs, eina_error_get()
405 * should be used to determine if an allocation error occurred during
406 * this function. This function returns #EINA_FALSE if an error
407 * occurred, #EINA_TRUE otherwise.
409 EAPI Eina_Bool eina_hash_add(Eina_Hash *hash,
411 const void *data) EINA_ARG_NONNULL(1, 2, 3);
414 * @brief Add an entry to the given hash table without duplicating the string
417 * @param hash The given hash table. Can be @c NULL.
418 * @param key A unique key. Can be @c NULL.
419 * @param data Data to associate with the string given by @p key.
420 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
422 * This function adds @p key to @p hash. @p hash, @p key and @p data
423 * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
424 * expected to be unique within the hash table. Key uniqueness varies
425 * depending on the type of @p hash: a stringshared @ref Eina_Hash
426 * need only have unique pointers for keys, but the strings in the
427 * pointers may be identical. All other hash types require the strings
428 * themselves to be unique. Failure to use sufficient uniqueness will
429 * result in unexpected results when inserting data pointers accessed
430 * with eina_hash_find(), and removed with eina_hash_del(). This
431 * function does not make a copy of @p key, so it must be a string
432 * constant or stored elsewhere ( in the object being added). Key
433 * strings are case sensitive. If an error occurs, eina_error_get()
434 * should be used to determine if an allocation error occurred during
435 * this function. This function returns #EINA_FALSE if an error
436 * occurred, #EINA_TRUE otherwise.
438 EAPI Eina_Bool eina_hash_direct_add(Eina_Hash *hash,
440 const void *data) EINA_ARG_NONNULL(1, 2, 3);
443 * @brief Remove the entry identified by a key or a data from the given
446 * @param hash The given hash table.
447 * @param key The key.
448 * @param data The data pointer to remove if the key is @c NULL.
449 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
451 * This function removes the entry identified by @p key or @p data
452 * from @p hash. If a free function was given to the
453 * callback on creation, it will be called for the data being
454 * deleted. If @p hash is @c NULL, the functions returns immediately
455 * #EINA_FALSE. If @p key is @c NULL, then @p data is used to find the a
456 * match to remove, otherwise @p key is used and @p data is not
457 * required and can be @c NULL. This function returns #EINA_FALSE if
458 * an error occurred, #EINA_TRUE otherwise.
460 * @note if you know you already have the key, use
461 * eina_hash_del_by_key() or eina_hash_del_by_key_hash(). If you
462 * know you don't have the key, use eina_hash_del_by_data()
465 EAPI Eina_Bool eina_hash_del(Eina_Hash *hash,
467 const void *data) EINA_ARG_NONNULL(1);
470 * @brief Retrieve a specific entry in the given hash table.
472 * @param hash The given hash table.
473 * @param key The key of the entry to find.
474 * @return The data pointer for the stored entry on success, @c NULL
477 * This function retrieves the entry associated to @p key in
478 * @p hash. If @p hash is @c NULL, this function returns immediately
479 * @c NULL. This function returns the data pointer on success, @c NULL
482 EAPI void *eina_hash_find(const Eina_Hash *hash,
483 const void *key) EINA_ARG_NONNULL(1, 2);
486 * @brief Modify the entry pointer at the specified key and return the old
488 * @param hash The given hash table.
489 * @param key The key of the entry to modify.
490 * @param data The data to replace the old entry.
491 * @return The data pointer for the old stored entry on success, or
494 * This function modifies the data of @p key with @p data in @p
495 * hash. If no entry is found, nothing is added to @p hash. On success
496 * this function returns the old entry, otherwise it returns @c NULL.
498 EAPI void *eina_hash_modify(Eina_Hash *hash,
500 const void *data) EINA_ARG_NONNULL(1, 2, 3);
503 * @brief Modify the entry pointer at the specified key and return the
504 * old entry or add the entry if not found.
506 * @param hash The given hash table.
507 * @param key The key of the entry to modify.
508 * @param data The data to replace the old entry
509 * @return The data pointer for the old stored entry, or @c NULL
512 * This function modifies the data of @p key with @p data in @p
513 * hash. If no entry is found, @p data is added to @p hash with the
514 * key @p key. On success this function returns the old entry,
515 * otherwise it returns @c NULL. To check for errors, use
518 EAPI void *eina_hash_set(Eina_Hash *hash,
520 const void *data) EINA_ARG_NONNULL(1, 2);
523 * @brief Change the key associated with a data without triggering the
526 * @param hash The given hash table.
527 * @param old_key The current key associated with the data
528 * @param new_key The new key to associate data with
529 * @return EINA_FALSE in any case but success, EINA_TRUE on success.
531 * This function allows for the move of data from one key to another,
532 * but does not call the Eina_Free_Cb associated with the hash table
533 * when destroying the old key.
535 EAPI Eina_Bool eina_hash_move(Eina_Hash *hash,
537 const void *new_key) EINA_ARG_NONNULL(1, 2, 3);
540 * Free the given hash table resources.
542 * @param hash The hash table to be freed.
544 * This function frees up all the memory allocated to storing @p hash,
545 * and call the free callback if it has been passed to the hash table
546 * at creation time. If no free callback has been passed, any entries
547 * in the table that the program has no more pointers for elsewhere
548 * may now be lost, so this should only be called if the program has
549 * already freed any allocated data in the hash table or has the
550 * pointers for data in the table stored elsewhere as well. If @p hash
551 * is @c NULL, the function returns immediately.
555 * extern Eina_Hash *hash;
557 * eina_hash_free(hash);
561 EAPI void eina_hash_free(Eina_Hash *hash) EINA_ARG_NONNULL(1);
564 * Free the given hash table buckets resources.
566 * @param hash The hash table whose buckets have to be freed.
568 * This function frees up all the memory allocated to storing the
569 * buckets of @p hash, and calls the free callback on all hash table
570 * buckets if it has been passed to the hash table at creation time,
571 * then frees the buckets. If no free callback has been passed, no
572 * buckets value will be freed. If @p hash is @c NULL, the function
573 * returns immediately.
575 EAPI void eina_hash_free_buckets(Eina_Hash *hash) EINA_ARG_NONNULL(1);
578 * @brief Returns the number of entries in the given hash table.
580 * @param hash The given hash table.
581 * @return The number of entries in the hash table.
583 * This function returns the number of entries in @p hash, or 0 on
584 * error. If @p hash is @c NULL, 0 is returned.
586 EAPI int eina_hash_population(const Eina_Hash *hash) EINA_ARG_NONNULL(1);
589 * @brief Add an entry to the given hash table.
591 * @param hash The given hash table.
592 * @param key A unique key.
593 * @param key_length The length of the key.
594 * @param key_hash The hash that will always match key.
595 * @param data The data to associate with the string given by the key.
596 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
598 * This function adds @p key to @p hash. @p hash, @p key and @p data
599 * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
600 * expected to be a unique string within the hash table. Otherwise,
601 * one cannot be sure which inserted data pointer will be accessed
602 * with @ref eina_hash_find, and removed with @ref eina_hash_del. Do
603 * not forget to count '\\0' for string when setting the value of
604 * @p key_length. @p key_hash is expected to always match
605 * @p key. Otherwise, one cannot be sure to find it again with @ref
606 * eina_hash_find_by_hash. Key strings are case sensitive. If an error
607 * occurs, eina_error_get() should be used to determine if an
608 * allocation error occurred during this function. This function
609 * returns #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
611 EAPI Eina_Bool eina_hash_add_by_hash(Eina_Hash *hash,
615 const void *data) EINA_ARG_NONNULL(1, 2, 5);
618 * @brief Add an entry to the given hash table and do not duplicate the string
621 * @param hash The given hash table. Can be @c NULL.
622 * @param key A unique key. Can be @c NULL.
623 * @param key_length Should be the length of @p key (don't forget to count
625 * @param key_hash The hash that will always match key.
626 * @param data Data to associate with the string given by @p key.
627 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
629 * This function adds @p key to @p hash. @p hash, @p key and @p data
630 * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
631 * expected to be a unique string within the hash table. Otherwise,
632 * one cannot be sure which inserted data pointer will be accessed
633 * with @ref eina_hash_find, and removed with @ref eina_hash_del. This
634 * function does not make a copy of @p key so it must be a string
635 * constant or stored elsewhere (in the object being added). Do
636 * not forget to count '\\0' for string when setting the value of
637 * @p key_length. @p key_hash is expected to always match
638 * @p key. Otherwise, one cannot be sure to find it again with @ref
639 * eina_hash_find_by_hash. Key strings are case sensitive. If an error
640 * occurs, eina_error_get() should be used to determine if an
641 * allocation error occurred during this function. This function
642 * returns #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
644 EAPI Eina_Bool eina_hash_direct_add_by_hash(Eina_Hash *hash,
648 const void *data) EINA_ARG_NONNULL(1, 2, 5);
651 * @brief Remove the entry identified by a key and a key hash from the given
654 * @param hash The given hash table.
655 * @param key The key.
656 * @param key_length The length of the key.
657 * @param key_hash The hash that always match the key.
658 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
660 * This function removes the entry identified by @p key and
661 * @p key_hash from @p hash. If a free function was given to the
662 * callback on creation, it will be called for the data being
663 * deleted. Do not forget to count '\\0' for string when setting the
664 * value of @p key_length. If @p hash or @p key are @c NULL, the
665 * functions returns immediately #EINA_FALSE. This function returns
666 * #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
668 * @note if you don't have the key_hash, use eina_hash_del_by_key() instead.
669 * @note if you don't have the key, use eina_hash_del_by_data() instead.
671 EAPI Eina_Bool eina_hash_del_by_key_hash(Eina_Hash *hash,
674 int key_hash) EINA_ARG_NONNULL(1, 2);
677 * @brief Remove the entry identified by a key from the given hash table.
679 * This version will calculate key length and hash by using functions
680 * provided to hash creation function.
682 * @param hash The given hash table.
683 * @param key The key.
684 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
686 * This function removes the entry identified by @p key from @p
687 * hash. The key length and hash will be calculated automatically by
688 * using functiond provided to has creation function. If a free
689 * function was given to the callback on creation, it will be called
690 * for the data being deleted. If @p hash or @p key are @c NULL, the
691 * functions returns immediately #EINA_FALSE. This function returns
692 * #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
694 * @note if you already have the key_hash, use eina_hash_del_by_key_hash()
696 * @note if you don't have the key, use eina_hash_del_by_data() instead.
698 EAPI Eina_Bool eina_hash_del_by_key(Eina_Hash *hash,
699 const void *key) EINA_ARG_NONNULL(1, 2);
702 * @brief Remove the entry identified by a data from the given hash table.
704 * This version is slow since there is no quick access to nodes based on data.
706 * @param hash The given hash table.
707 * @param data The data value to search and remove.
708 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
711 * This function removes the entry identified by @p data from @p
712 * hash. If a free function was given to the callback on creation, it
713 * will be called for the data being deleted. If @p hash or @p data
714 * are @c NULL, the functions returns immediately #EINA_FALSE. This
715 * function returns #EINA_FALSE if an error occurred, #EINA_TRUE
718 * @note if you already have the key, use eina_hash_del_by_key() or
719 * eina_hash_del_by_key_hash() instead.
721 EAPI Eina_Bool eina_hash_del_by_data(Eina_Hash *hash,
722 const void *data) EINA_ARG_NONNULL(1, 2);
725 * @brief Remove the entry identified by a key and a key hash or a
726 * data from the given hash table.
728 * If @p key is @c NULL, then @p data is used to find a match to
731 * @param hash The given hash table.
732 * @param key The key.
733 * @param key_length The length of the key.
734 * @param key_hash The hash that always match the key.
735 * @param data The data pointer to remove if the key is @c NULL.
736 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
738 * This function removes the entry identified by @p key and
739 * @p key_hash, or @p data, from @p hash. If a free function was given to
740 * the callback on creation, it will be called for the data being
741 * deleted. If @p hash is @c NULL, the functions returns immediately
742 * #EINA_FALSE. If @p key is @c NULL, then @p key_hash and @p key_hash
743 * are ignored and @p data is used to find a match to remove,
744 * otherwise @p key and @p key_hash are used and @p data is not
745 * required and can be @c NULL. Do not forget to count '\\0' for
746 * string when setting the value of @p key_length. This function
747 * returns #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
749 * @note if you know you already have the key, use eina_hash_del_by_key_hash(),
750 * if you know you don't have the key, use eina_hash_del_by_data()
753 EAPI Eina_Bool eina_hash_del_by_hash(Eina_Hash *hash,
757 const void *data) EINA_ARG_NONNULL(1);
760 * @brief Retrieve a specific entry in the given hash table.
762 * @param hash The given hash table.
763 * @param key The key of the entry to find.
764 * @param key_length The length of the key.
765 * @param key_hash The hash that always match the key
766 * @return The data pointer for the stored entry on success, @c NULL
769 * This function retrieves the entry associated to @p key of length
770 * @p key_length in @p hash. @p key_hash is the hash that always match
771 * @p key. It is ignored if @p key is @c NULL. Do not forget to count
772 * '\\0' for string when setting the value of @p key_length. If
773 * @p hash is @c NULL, this function returns immediately @c NULL. This
774 * function returns the data pointer on success, @c NULL otherwise.
776 EAPI void *eina_hash_find_by_hash(const Eina_Hash *hash,
779 int key_hash) EINA_ARG_NONNULL(1, 2);
782 * @brief Modify the entry pointer at the specified key and returns
785 * @param hash The given hash table.
786 * @param key The key of the entry to modify.
787 * @param key_length Should be the length of @p key (don't forget to count
789 * @param key_hash The hash that always match the key. Ignored if @p key is
791 * @param data The data to replace the old entry, if it exists.
792 * @return The data pointer for the old stored entry, or @c NULL if not
793 * found. If an existing entry is not found, nothing is added to the
796 EAPI void *eina_hash_modify_by_hash(Eina_Hash *hash,
800 const void *data) EINA_ARG_NONNULL(1, 2, 5);
803 * @brief Returned a new iterator associated to hash keys.
805 * @param hash The hash.
806 * @return A new iterator.
808 * This function returns a newly allocated iterator associated to @p
809 * hash. If @p hash is not populated, this function still returns a
810 * valid iterator that will always return false on
811 * eina_iterator_next(), thus keeping API sane.
813 * If the memory can not be allocated, NULL is returned and
814 * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
817 * @warning if the hash structure changes then the iterator becomes
818 * invalid! That is, if you add or remove items this iterator
819 * behavior is undefined and your program may crash!
821 EAPI Eina_Iterator *eina_hash_iterator_key_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
824 * @brief Returned a new iterator associated to hash data.
826 * @param hash The hash.
827 * @return A new iterator.
829 * This function returns a newly allocated iterator associated to
830 * @p hash. If @p hash is not populated, this function still returns a
831 * valid iterator that will always return false on
832 * eina_iterator_next(), thus keeping API sane.
834 * If the memory can not be allocated, @c NULL is returned and
835 * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
838 * @warning if the hash structure changes then the iterator becomes
839 * invalid. That is, if you add or remove items this iterator behavior
840 * is undefined and your program may crash.
842 EAPI Eina_Iterator *eina_hash_iterator_data_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
845 * @brief Returned a new iterator associated to hash keys and data.
847 * @param hash The hash.
848 * @return A new iterator.
850 * This function returns a newly allocated iterator associated to @p
851 * hash. If @p hash is not populated, this function still returns a
852 * valid iterator that will always return false on
853 * eina_iterator_next(), thus keeping API sane.
855 * If the memory can not be allocated, NULL is returned and
856 * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
859 * @note iterator data will provide values as Eina_Hash_Tuple that should not
862 * @warning if the hash structure changes then the iterator becomes
863 * invalid! That is, if you add or remove items this iterator
864 * behavior is undefined and your program may crash!
866 EAPI Eina_Iterator *eina_hash_iterator_tuple_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
869 * @brief Call a function on every member stored in the hash table
871 * @param hash The hash table whose members will be walked
872 * @param func The function to call on each parameter
873 * @param fdata The data pointer to pass to the function being called
875 * This function goes through every entry in the hash table @p hash and calls
876 * the function @p func on each member. The function should @b not modify the
877 * hash table contents if it returns 1. @b If the hash table contents are
878 * modified by this function or the function wishes to stop processing it must
879 * return 0, otherwise return 1 to keep processing.
883 * extern Eina_Hash *hash;
885 * Eina_Bool hash_fn(const Eina_Hash *hash, const void *key,
886 * void *data, void *fdata)
888 * printf("Func data: %s, Hash entry: %s / %p\n",
889 * fdata, (const char *)key, data);
893 * int main(int argc, char **argv)
895 * char *hash_fn_data;
897 * hash_fn_data = strdup("Hello World");
898 * eina_hash_foreach(hash, hash_fn, hash_fn_data);
899 * free(hash_fn_data);
903 EAPI void eina_hash_foreach(const Eina_Hash *hash,
904 Eina_Hash_Foreach cb,
905 const void *fdata) EINA_ARG_NONNULL(1, 2);
906 /* Paul Hsieh (http://www.azillionmonkeys.com/qed/hash.html) hash function used by WebCore (http://webkit.org/blog/8/hashtables-part-2/) */
907 EAPI int eina_hash_superfast(const char *key,
908 int len) EINA_ARG_NONNULL(1);
909 /* Hash function first reported by dan bernstein many years ago in comp.lang.c */
910 static inline int eina_hash_djb2(const char *key,
911 int len) EINA_ARG_NONNULL(1);
912 static inline int eina_hash_djb2_len(const char *key,
913 int *plen) EINA_ARG_NONNULL(1, 2);
914 /* Hash function from http://www.concentric.net/~Ttwang/tech/inthash.htm */
915 static inline int eina_hash_int32(const unsigned int *pkey,
916 int len) EINA_ARG_NONNULL(1);
917 static inline int eina_hash_int64(const unsigned long int *pkey,
918 int len) EINA_ARG_NONNULL(1);
920 #include "eina_inline_hash.x"
938 #endif /*EINA_HASH_H_*/