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.png
150 * @image latex 01_hash-table.eps
152 * Adding an element to the hash table is made of:
153 * @li calculating the hash for that key (using the specified hash function);
154 * @li calculate the array position [hash mod N];
155 * @li add the element to the rbtree on that position.
157 * The two first steps have constant time, proportional to the hash function
158 * being used. Adding the key to the rbtree will be proportional on the number
159 * of keys on that bucket.
161 * The average cost of lookup depends on the number of keys per
162 * bucket (load factor) of the table, if the distribution of keys is
163 * sufficiently uniform.
165 * @section hashtable_perf Performance
167 * As said before, the performance depends on the load factor. So trying to keep
168 * it as small as possible will improve the hash table performance. But
169 * increasing the buckets_power_size will also increase the memory consumption.
170 * The default hash table creation functions already have a good number of
171 * buckets, enough for most cases. Particularly for strings, if just a few keys
172 * (less than 30) will be added to the hash table, @ref
173 * eina_hash_string_small_new should be used. If just stringshared keys are
174 * being added, use @ref eina_hash_stringshared_new. If a lot of keys will be
175 * added to the hash table (e.g. more than 1000), then it's better to increase
176 * the buckets_power_size. See @ref eina_hash_new for more details.
178 * When adding a new key to a hash table, use @ref eina_hash_add or @ref
179 * eina_hash_direct_add (the latter if this key is already stored elsewhere). If
180 * the key may be already inside the hash table, instead of checking with
181 * @ref eina_hash_find and then doing @ref eina_hash_add, one can use just @ref
182 * eina_hash_set (this will change the data pointed by this key if it was
183 * already present in the table).
185 * @section hashtable_tutorial Tutorial
187 * These examples show many Eina_Hash functions in action:
188 * @li @ref hash_01_example_page
189 * @li @ref hash_02_example_page
195 * @addtogroup Eina_Data_Types_Group Data Types
201 * @addtogroup Eina_Containers_Group Containers
207 * @defgroup Eina_Hash_Group Hash Table
214 * Type for a generic hash table.
216 typedef struct _Eina_Hash Eina_Hash;
218 typedef struct _Eina_Hash_Tuple Eina_Hash_Tuple;
220 struct _Eina_Hash_Tuple
222 const void *key; /**< The key */
223 void *data; /**< The data associated to the key */
224 unsigned int key_length; /**< The length of the key */
227 typedef unsigned int (*Eina_Key_Length)(const void *key);
228 #define EINA_KEY_LENGTH(Function) ((Eina_Key_Length)Function)
229 typedef int (*Eina_Key_Cmp)(const void *key1, int key1_length, const void *key2, int key2_length);
230 #define EINA_KEY_CMP(Function) ((Eina_Key_Cmp)Function)
231 typedef int (*Eina_Key_Hash)(const void *key, int key_length);
232 #define EINA_KEY_HASH(Function) ((Eina_Key_Hash)Function)
233 typedef Eina_Bool (*Eina_Hash_Foreach)(const Eina_Hash *hash, const void *key, void *data, void *fdata);
237 * @brief Create a new hash table.
239 * @param key_length_cb The function called when getting the size of the key.
240 * @param key_cmp_cb The function called when comparing the keys.
241 * @param key_hash_cb The function called when getting the values.
242 * @param data_free_cb The function called on each value when the hash
243 * table is freed. @c NULL can be passed as callback.
244 * @param buckets_power_size The size of the buckets.
245 * @return The new hash table.
247 * This function creates a new hash table using user-defined callbacks
248 * to manage the hash table. On failure, @c NULL is returned and
249 * #EINA_ERROR_OUT_OF_MEMORY is set. If @p key_cmp_cb or @p key_hash_cb
250 * are @c NULL, @c NULL is returned. If @p buckets_power_size is
251 * smaller or equal than 2, or if it is greater or equal than 17,
252 * @c NULL is returned.
254 * Pre-defined functions are available to create a hash table. See
255 * eina_hash_string_djb2_new(), eina_hash_string_superfast_new(),
256 * eina_hash_string_small_new(), eina_hash_int32_new(),
257 * eina_hash_int64_new(), eina_hash_pointer_new() and
258 * eina_hash_stringshared_new().
260 EAPI Eina_Hash *eina_hash_new(Eina_Key_Length key_length_cb,
261 Eina_Key_Cmp key_cmp_cb,
262 Eina_Key_Hash key_hash_cb,
263 Eina_Free_Cb data_free_cb,
264 int buckets_power_size) EINA_MALLOC EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(2, 3);
267 * @brief Create a new hash table using the djb2 algorithm.
269 * @param data_free_cb The function called on each value when the hash table
270 * is freed. @c NULL can be passed as callback.
271 * @return The new hash table.
273 * This function creates a new hash table using the djb2 algorithm for
274 * table management and strcmp() to compare the keys. Values can then
275 * be looked up with pointers other than the original key pointer that
276 * was used to add values. On failure, this function returns @c NULL.
278 EAPI Eina_Hash *eina_hash_string_djb2_new(Eina_Free_Cb data_free_cb);
281 * @brief Create a new hash table for use with strings.
283 * @param data_free_cb The function called on each value when the hash table
284 * is freed. @c NULL can be passed as callback.
285 * @return The new hash table.
287 * This function creates a new hash table using the superfast algorithm
288 * for table management and strcmp() to compare the keys. Values can
289 * then be looked up with pointers other than the original key pointer
290 * that was used to add values. On failure, this function returns
293 EAPI Eina_Hash *eina_hash_string_superfast_new(Eina_Free_Cb data_free_cb);
296 * @brief Create a new hash table for use with strings with small bucket size.
298 * @param data_free_cb The function called on each value when the hash table
299 * is freed. @c NULL can be passed as callback.
300 * @return The new hash table.
302 * This function creates a new hash table using the superfast algorithm
303 * for table management and strcmp() to compare the keys, but with a
304 * smaller bucket size (compared to eina_hash_string_superfast_new())
305 * which will minimize the memory used by the returned hash
306 * table. Values can then be looked up with pointers other than the
307 * original key pointer that was used to add values. On failure, this
308 * function returns @c NULL.
310 EAPI Eina_Hash *eina_hash_string_small_new(Eina_Free_Cb data_free_cb);
313 * @brief Create a new hash table for use with 32bit integers.
315 * @param data_free_cb The function called on each value when the hash table
316 * is freed. @c NULL can be passed as callback.
317 * @return The new hash table.
319 * This function creates a new hash table where keys are 32bit integers.
320 * When adding or looking up in the hash table, pointers to 32bit integers
321 * must be passed. They can be addresses on the stack if you let the
322 * eina_hash copy the key. Values can then
323 * be looked up with pointers other than the original key pointer that was
324 * used to add values. This method is not suitable to match string keys as
325 * it would only match the first character.
326 * On failure, this function returns @c NULL.
328 EAPI Eina_Hash *eina_hash_int32_new(Eina_Free_Cb data_free_cb);
331 * @brief Create a new hash table for use with 64bit integers.
333 * @param data_free_cb The function called on each value when the hash table
334 * is freed. @c NULL can be passed as callback.
335 * @return The new hash table.
337 * This function creates a new hash table where keys are 64bit integers.
338 * When adding or looking up in the hash table, pointers to 64bit integers
339 * must be passed. They can be addresses on the stack. Values can then
340 * be looked up with pointers other than the original key pointer that was
341 * used to add values. This method is not suitable to match string keys as
342 * it would only match the first character.
343 * On failure, this function returns @c NULL.
345 EAPI Eina_Hash *eina_hash_int64_new(Eina_Free_Cb data_free_cb);
348 * @brief Create a new hash table for use with pointers.
350 * @param data_free_cb The function called on each value when the hash table
351 * is freed. @c NULL can be passed as callback.
352 * @return The new hash table.
354 * This function creates a new hash table using the int64/int32 algorithm for
355 * table management and dereferenced pointers to compare the
356 * keys. Values can then be looked up with pointers other than the
357 * original key pointer that was used to add values. This method may
358 * appear to be able to match string keys, actually it only matches
359 * the first character. On failure, this function returns @c NULL.
361 EAPI Eina_Hash *eina_hash_pointer_new(Eina_Free_Cb data_free_cb);
364 * @brief Create a new hash table optimized for stringshared values.
366 * @param data_free_cb The function called on each value when the hash table
367 * is freed. @c NULL can be passed as callback.
368 * @return The new hash table.
370 * This function creates a new hash table optimized for stringshared
371 * values. Values CAN NOT be looked up with pointers not
372 * equal to the original key pointer that was used to add a value. On failure,
373 * this function returns @c NULL.
375 * Excerpt of code that will NOT work with this type of hash:
378 * extern Eina_Hash *hash;
379 * extern const char *value;
380 * const char *a = eina_stringshare_add("key");
382 * eina_hash_add(hash, a, value);
383 * eina_hash_find(hash, "key")
386 EAPI Eina_Hash *eina_hash_stringshared_new(Eina_Free_Cb data_free_cb);
389 * @brief Add an entry to the given hash table.
391 * @param hash The given hash table.
392 * @param key A unique key.
393 * @param data Data to associate with the string given by @p key.
394 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
396 * This function adds @p key to @p hash. @p hash, @p key and @p data
397 * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
398 * expected to be unique within the hash table. Key uniqueness varies
399 * depending on the type of @p hash: a stringshared @ref Eina_Hash
400 * need only have unique pointers for keys, but the strings in the
401 * pointers may be identical. All other hash types require the strings
402 * themselves to be unique. Failure to use sufficient uniqueness will
403 * result in unexpected results when inserting data pointers accessed
404 * with eina_hash_find(), and removed with eina_hash_del(). Key
405 * strings are case sensitive. If an error occurs, eina_error_get()
406 * should be used to determine if an allocation error occurred during
407 * this function. This function returns #EINA_FALSE if an error
408 * occurred, #EINA_TRUE otherwise.
410 EAPI Eina_Bool eina_hash_add(Eina_Hash *hash,
412 const void *data) EINA_ARG_NONNULL(1, 2, 3);
415 * @brief Add an entry to the given hash table without duplicating the string
418 * @param hash The given hash table. Can be @c NULL.
419 * @param key A unique key. Can be @c NULL.
420 * @param data Data to associate with the string given by @p key.
421 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
423 * This function adds @p key to @p hash. @p hash, @p key and @p data
424 * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
425 * expected to be unique within the hash table. Key uniqueness varies
426 * depending on the type of @p hash: a stringshared @ref Eina_Hash
427 * need only have unique pointers for keys, but the strings in the
428 * pointers may be identical. All other hash types require the strings
429 * themselves to be unique. Failure to use sufficient uniqueness will
430 * result in unexpected results when inserting data pointers accessed
431 * with eina_hash_find(), and removed with eina_hash_del(). This
432 * function does not make a copy of @p key, so it must be a string
433 * constant or stored elsewhere ( in the object being added). Key
434 * strings are case sensitive. If an error occurs, eina_error_get()
435 * should be used to determine if an allocation error occurred during
436 * this function. This function returns #EINA_FALSE if an error
437 * occurred, #EINA_TRUE otherwise.
439 EAPI Eina_Bool eina_hash_direct_add(Eina_Hash *hash,
441 const void *data) EINA_ARG_NONNULL(1, 2, 3);
444 * @brief Remove the entry identified by a key or a data from the given
447 * @param hash The given hash table.
448 * @param key The key.
449 * @param data The data pointer to remove if the key is @c NULL.
450 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
452 * This function removes the entry identified by @p key or @p data
453 * from @p hash. If a free function was given to the
454 * callback on creation, it will be called for the data being
455 * deleted. If @p hash is @c NULL, the functions returns immediately
456 * #EINA_FALSE. If @p key is @c NULL, then @p data is used to find the a
457 * match to remove, otherwise @p key is used and @p data is not
458 * required and can be @c NULL. This function returns #EINA_FALSE if
459 * an error occurred, #EINA_TRUE otherwise.
461 * @note if you know you already have the key, use
462 * eina_hash_del_by_key() or eina_hash_del_by_key_hash(). If you
463 * know you don't have the key, use eina_hash_del_by_data()
466 EAPI Eina_Bool eina_hash_del(Eina_Hash *hash,
468 const void *data) EINA_ARG_NONNULL(1);
471 * @brief Retrieve a specific entry in the given hash table.
473 * @param hash The given hash table.
474 * @param key The key of the entry to find.
475 * @return The data pointer for the stored entry on success, @c NULL
478 * This function retrieves the entry associated to @p key in
479 * @p hash. If @p hash is @c NULL, this function returns immediately
480 * @c NULL. This function returns the data pointer on success, @c NULL
483 EAPI void *eina_hash_find(const Eina_Hash *hash,
484 const void *key) EINA_ARG_NONNULL(1, 2);
487 * @brief Modify the entry pointer at the specified key and return the old
489 * @param hash The given hash table.
490 * @param key The key of the entry to modify.
491 * @param data The data to replace the old entry.
492 * @return The data pointer for the old stored entry on success, or
495 * This function modifies the data of @p key with @p data in @p
496 * hash. If no entry is found, nothing is added to @p hash. On success
497 * this function returns the old entry, otherwise it returns @c NULL.
499 EAPI void *eina_hash_modify(Eina_Hash *hash,
501 const void *data) EINA_ARG_NONNULL(1, 2, 3);
504 * @brief Modify the entry pointer at the specified key and return the
505 * old entry or add the entry if not found.
507 * @param hash The given hash table.
508 * @param key The key of the entry to modify.
509 * @param data The data to replace the old entry
510 * @return The data pointer for the old stored entry, or @c NULL
513 * This function modifies the data of @p key with @p data in @p
514 * hash. If no entry is found, @p data is added to @p hash with the
515 * key @p key. On success this function returns the old entry,
516 * otherwise it returns @c NULL. To check for errors, use
519 EAPI void *eina_hash_set(Eina_Hash *hash,
521 const void *data) EINA_ARG_NONNULL(1, 2);
524 * @brief Change the key associated with a data without triggering the
527 * @param hash The given hash table.
528 * @param old_key The current key associated with the data
529 * @param new_key The new key to associate data with
530 * @return EINA_FALSE in any case but success, EINA_TRUE on success.
532 * This function allows for the move of data from one key to another,
533 * but does not call the Eina_Free_Cb associated with the hash table
534 * when destroying the old key.
536 EAPI Eina_Bool eina_hash_move(Eina_Hash *hash,
538 const void *new_key) EINA_ARG_NONNULL(1, 2, 3);
541 * Free the given hash table resources.
543 * @param hash The hash table to be freed.
545 * This function frees up all the memory allocated to storing @p hash,
546 * and call the free callback if it has been passed to the hash table
547 * at creation time. If no free callback has been passed, any entries
548 * in the table that the program has no more pointers for elsewhere
549 * may now be lost, so this should only be called if the program has
550 * already freed any allocated data in the hash table or has the
551 * pointers for data in the table stored elsewhere as well. If @p hash
552 * is @c NULL, the function returns immediately.
556 * extern Eina_Hash *hash;
558 * eina_hash_free(hash);
562 EAPI void eina_hash_free(Eina_Hash *hash) EINA_ARG_NONNULL(1);
565 * Free the given hash table buckets resources.
567 * @param hash The hash table whose buckets have to be freed.
569 * This function frees up all the memory allocated to storing the
570 * buckets of @p hash, and calls the free callback on all hash table
571 * buckets if it has been passed to the hash table at creation time,
572 * then frees the buckets. If no free callback has been passed, no
573 * buckets value will be freed. If @p hash is @c NULL, the function
574 * returns immediately.
576 EAPI void eina_hash_free_buckets(Eina_Hash *hash) EINA_ARG_NONNULL(1);
579 * @brief Returns the number of entries in the given hash table.
581 * @param hash The given hash table.
582 * @return The number of entries in the hash table.
584 * This function returns the number of entries in @p hash, or 0 on
585 * error. If @p hash is @c NULL, 0 is returned.
587 EAPI int eina_hash_population(const Eina_Hash *hash) EINA_ARG_NONNULL(1);
590 * @brief Add an entry to the given hash table.
592 * @param hash The given hash table.
593 * @param key A unique key.
594 * @param key_length The length of the key.
595 * @param key_hash The hash that will always match key.
596 * @param data The data to associate with the string given by the key.
597 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
599 * This function adds @p key to @p hash. @p hash, @p key and @p data
600 * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
601 * expected to be a unique string within the hash table. Otherwise,
602 * one cannot be sure which inserted data pointer will be accessed
603 * with @ref eina_hash_find, and removed with @ref eina_hash_del. Do
604 * not forget to count '\\0' for string when setting the value of
605 * @p key_length. @p key_hash is expected to always match
606 * @p key. Otherwise, one cannot be sure to find it again with @ref
607 * eina_hash_find_by_hash. Key strings are case sensitive. If an error
608 * occurs, eina_error_get() should be used to determine if an
609 * allocation error occurred during this function. This function
610 * returns #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
612 EAPI Eina_Bool eina_hash_add_by_hash(Eina_Hash *hash,
616 const void *data) EINA_ARG_NONNULL(1, 2, 5);
619 * @brief Add an entry to the given hash table and do not duplicate the string
622 * @param hash The given hash table. Can be @c NULL.
623 * @param key A unique key. Can be @c NULL.
624 * @param key_length Should be the length of @p key (don't forget to count
626 * @param key_hash The hash that will always match key.
627 * @param data Data to associate with the string given by @p key.
628 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
630 * This function adds @p key to @p hash. @p hash, @p key and @p data
631 * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
632 * expected to be a unique string within the hash table. Otherwise,
633 * one cannot be sure which inserted data pointer will be accessed
634 * with @ref eina_hash_find, and removed with @ref eina_hash_del. This
635 * function does not make a copy of @p key so it must be a string
636 * constant or stored elsewhere (in the object being added). Do
637 * not forget to count '\\0' for string when setting the value of
638 * @p key_length. @p key_hash is expected to always match
639 * @p key. Otherwise, one cannot be sure to find it again with @ref
640 * eina_hash_find_by_hash. Key strings are case sensitive. If an error
641 * occurs, eina_error_get() should be used to determine if an
642 * allocation error occurred during this function. This function
643 * returns #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
645 EAPI Eina_Bool eina_hash_direct_add_by_hash(Eina_Hash *hash,
649 const void *data) EINA_ARG_NONNULL(1, 2, 5);
652 * @brief Remove the entry identified by a key and a key hash from the given
655 * @param hash The given hash table.
656 * @param key The key.
657 * @param key_length The length of the key.
658 * @param key_hash The hash that always match the key.
659 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
661 * This function removes the entry identified by @p key and
662 * @p key_hash from @p hash. If a free function was given to the
663 * callback on creation, it will be called for the data being
664 * deleted. Do not forget to count '\\0' for string when setting the
665 * value of @p key_length. If @p hash or @p key are @c NULL, the
666 * functions returns immediately #EINA_FALSE. This function returns
667 * #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
669 * @note if you don't have the key_hash, use eina_hash_del_by_key() instead.
670 * @note if you don't have the key, use eina_hash_del_by_data() instead.
672 EAPI Eina_Bool eina_hash_del_by_key_hash(Eina_Hash *hash,
675 int key_hash) EINA_ARG_NONNULL(1, 2);
678 * @brief Remove the entry identified by a key from the given hash table.
680 * This version will calculate key length and hash by using functions
681 * provided to hash creation function.
683 * @param hash The given hash table.
684 * @param key The key.
685 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
687 * This function removes the entry identified by @p key from @p
688 * hash. The key length and hash will be calculated automatically by
689 * using functiond provided to has creation function. If a free
690 * function was given to the callback on creation, it will be called
691 * for the data being deleted. If @p hash or @p key are @c NULL, the
692 * functions returns immediately #EINA_FALSE. This function returns
693 * #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
695 * @note if you already have the key_hash, use eina_hash_del_by_key_hash()
697 * @note if you don't have the key, use eina_hash_del_by_data() instead.
699 EAPI Eina_Bool eina_hash_del_by_key(Eina_Hash *hash,
700 const void *key) EINA_ARG_NONNULL(1, 2);
703 * @brief Remove the entry identified by a data from the given hash table.
705 * This version is slow since there is no quick access to nodes based on data.
707 * @param hash The given hash table.
708 * @param data The data value to search and remove.
709 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
712 * This function removes the entry identified by @p data from @p
713 * hash. If a free function was given to the callback on creation, it
714 * will be called for the data being deleted. If @p hash or @p data
715 * are @c NULL, the functions returns immediately #EINA_FALSE. This
716 * function returns #EINA_FALSE if an error occurred, #EINA_TRUE
719 * @note if you already have the key, use eina_hash_del_by_key() or
720 * eina_hash_del_by_key_hash() instead.
722 EAPI Eina_Bool eina_hash_del_by_data(Eina_Hash *hash,
723 const void *data) EINA_ARG_NONNULL(1, 2);
726 * @brief Remove the entry identified by a key and a key hash or a
727 * data from the given hash table.
729 * If @p key is @c NULL, then @p data is used to find a match to
732 * @param hash The given hash table.
733 * @param key The key.
734 * @param key_length The length of the key.
735 * @param key_hash The hash that always match the key.
736 * @param data The data pointer to remove if the key is @c NULL.
737 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
739 * This function removes the entry identified by @p key and
740 * @p key_hash, or @p data, from @p hash. If a free function was given to
741 * the callback on creation, it will be called for the data being
742 * deleted. If @p hash is @c NULL, the functions returns immediately
743 * #EINA_FALSE. If @p key is @c NULL, then @p key_hash and @p key_hash
744 * are ignored and @p data is used to find a match to remove,
745 * otherwise @p key and @p key_hash are used and @p data is not
746 * required and can be @c NULL. Do not forget to count '\\0' for
747 * string when setting the value of @p key_length. This function
748 * returns #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
750 * @note if you know you already have the key, use eina_hash_del_by_key_hash(),
751 * if you know you don't have the key, use eina_hash_del_by_data()
754 EAPI Eina_Bool eina_hash_del_by_hash(Eina_Hash *hash,
758 const void *data) EINA_ARG_NONNULL(1);
761 * @brief Retrieve a specific entry in the given hash table.
763 * @param hash The given hash table.
764 * @param key The key of the entry to find.
765 * @param key_length The length of the key.
766 * @param key_hash The hash that always match the key
767 * @return The data pointer for the stored entry on success, @c NULL
770 * This function retrieves the entry associated to @p key of length
771 * @p key_length in @p hash. @p key_hash is the hash that always match
772 * @p key. It is ignored if @p key is @c NULL. Do not forget to count
773 * '\\0' for string when setting the value of @p key_length. If
774 * @p hash is @c NULL, this function returns immediately @c NULL. This
775 * function returns the data pointer on success, @c NULL otherwise.
777 EAPI void *eina_hash_find_by_hash(const Eina_Hash *hash,
780 int key_hash) EINA_ARG_NONNULL(1, 2);
783 * @brief Modify the entry pointer at the specified key and returns
786 * @param hash The given hash table.
787 * @param key The key of the entry to modify.
788 * @param key_length Should be the length of @p key (don't forget to count
790 * @param key_hash The hash that always match the key. Ignored if @p key is
792 * @param data The data to replace the old entry, if it exists.
793 * @return The data pointer for the old stored entry, or @c NULL if not
794 * found. If an existing entry is not found, nothing is added to the
797 EAPI void *eina_hash_modify_by_hash(Eina_Hash *hash,
801 const void *data) EINA_ARG_NONNULL(1, 2, 5);
804 * @brief Returned a new iterator associated to hash keys.
806 * @param hash The hash.
807 * @return A new iterator.
809 * This function returns a newly allocated iterator associated to @p
810 * hash. If @p hash is not populated, this function still returns a
811 * valid iterator that will always return false on
812 * eina_iterator_next(), thus keeping API sane.
814 * If the memory can not be allocated, NULL is returned and
815 * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
818 * @warning if the hash structure changes then the iterator becomes
819 * invalid! That is, if you add or remove items this iterator
820 * behavior is undefined and your program may crash!
822 EAPI Eina_Iterator *eina_hash_iterator_key_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
825 * @brief Returned a new iterator associated to hash data.
827 * @param hash The hash.
828 * @return A new iterator.
830 * This function returns a newly allocated iterator associated to
831 * @p hash. If @p hash is not populated, this function still returns a
832 * valid iterator that will always return false on
833 * eina_iterator_next(), thus keeping API sane.
835 * If the memory can not be allocated, @c NULL is returned and
836 * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
839 * @warning if the hash structure changes then the iterator becomes
840 * invalid. That is, if you add or remove items this iterator behavior
841 * is undefined and your program may crash.
843 EAPI Eina_Iterator *eina_hash_iterator_data_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
846 * @brief Returned a new iterator associated to hash keys and data.
848 * @param hash The hash.
849 * @return A new iterator.
851 * This function returns a newly allocated iterator associated to @p
852 * hash. If @p hash is not populated, this function still returns a
853 * valid iterator that will always return false on
854 * eina_iterator_next(), thus keeping API sane.
856 * If the memory can not be allocated, NULL is returned and
857 * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
860 * @note iterator data will provide values as Eina_Hash_Tuple that should not
863 * @warning if the hash structure changes then the iterator becomes
864 * invalid! That is, if you add or remove items this iterator
865 * behavior is undefined and your program may crash!
867 EAPI Eina_Iterator *eina_hash_iterator_tuple_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
870 * @brief Call a function on every member stored in the hash table
872 * @param hash The hash table whose members will be walked
873 * @param func The function to call on each parameter
874 * @param fdata The data pointer to pass to the function being called
876 * This function goes through every entry in the hash table @p hash and calls
877 * the function @p func on each member. The function should @b not modify the
878 * hash table contents if it returns 1. @b If the hash table contents are
879 * modified by this function or the function wishes to stop processing it must
880 * return 0, otherwise return 1 to keep processing.
884 * extern Eina_Hash *hash;
886 * Eina_Bool hash_fn(const Eina_Hash *hash, const void *key,
887 * void *data, void *fdata)
889 * printf("Func data: %s, Hash entry: %s / %p\n",
890 * fdata, (const char *)key, data);
894 * int main(int argc, char **argv)
896 * char *hash_fn_data;
898 * hash_fn_data = strdup("Hello World");
899 * eina_hash_foreach(hash, hash_fn, hash_fn_data);
900 * free(hash_fn_data);
904 EAPI void eina_hash_foreach(const Eina_Hash *hash,
905 Eina_Hash_Foreach cb,
906 const void *fdata) EINA_ARG_NONNULL(1, 2);
907 /* Paul Hsieh (http://www.azillionmonkeys.com/qed/hash.html) hash function used by WebCore (http://webkit.org/blog/8/hashtables-part-2/) */
908 EAPI int eina_hash_superfast(const char *key,
909 int len) EINA_ARG_NONNULL(1);
910 /* Hash function first reported by dan bernstein many years ago in comp.lang.c */
911 static inline int eina_hash_djb2(const char *key,
912 int len) EINA_ARG_NONNULL(1);
913 static inline int eina_hash_djb2_len(const char *key,
914 int *plen) EINA_ARG_NONNULL(1, 2);
915 /* Hash function from http://www.concentric.net/~Ttwang/tech/inthash.htm */
916 static inline int eina_hash_int32(const unsigned int *pkey,
917 int len) EINA_ARG_NONNULL(1);
918 static inline int eina_hash_int64(const unsigned long int *pkey,
919 int len) EINA_ARG_NONNULL(1);
921 #include "eina_inline_hash.x"
939 #endif /*EINA_HASH_H_*/