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 Redefine the callback that clean the data of a hash
269 * @param hash The given hash table
270 * @param data_free_cb The function called on each value when the hash
271 * table is freed. @c NULL can be passed as callback.
274 EAPI void eina_hash_free_set(Eina_Hash *hash, Eina_Free_Cb data_free_cb) EINA_ARG_NONNULL(1);
277 * @brief Create a new hash table using the djb2 algorithm.
279 * @param data_free_cb The function called on each value when the hash table
280 * is freed. @c NULL can be passed as callback.
281 * @return The new hash table.
283 * This function creates a new hash table using the djb2 algorithm for
284 * table management and strcmp() to compare the keys. Values can then
285 * be looked up with pointers other than the original key pointer that
286 * was used to add values. On failure, this function returns @c NULL.
288 EAPI Eina_Hash *eina_hash_string_djb2_new(Eina_Free_Cb data_free_cb);
291 * @brief Create a new hash table for use with strings.
293 * @param data_free_cb The function called on each value when the hash table
294 * is freed. @c NULL can be passed as callback.
295 * @return The new hash table.
297 * This function creates a new hash table using the superfast algorithm
298 * for table management and strcmp() to compare the keys. Values can
299 * then be looked up with pointers other than the original key pointer
300 * that was used to add values. On failure, this function returns
303 EAPI Eina_Hash *eina_hash_string_superfast_new(Eina_Free_Cb data_free_cb);
306 * @brief Create a new hash table for use with strings with small bucket size.
308 * @param data_free_cb The function called on each value when the hash table
309 * is freed. @c NULL can be passed as callback.
310 * @return The new hash table.
312 * This function creates a new hash table using the superfast algorithm
313 * for table management and strcmp() to compare the keys, but with a
314 * smaller bucket size (compared to eina_hash_string_superfast_new())
315 * which will minimize the memory used by the returned hash
316 * table. Values can then be looked up with pointers other than the
317 * original key pointer that was used to add values. On failure, this
318 * function returns @c NULL.
320 EAPI Eina_Hash *eina_hash_string_small_new(Eina_Free_Cb data_free_cb);
323 * @brief Create a new hash table for use with 32bit integers.
325 * @param data_free_cb The function called on each value when the hash table
326 * is freed. @c NULL can be passed as callback.
327 * @return The new hash table.
329 * This function creates a new hash table where keys are 32bit integers.
330 * When adding or looking up in the hash table, pointers to 32bit integers
331 * must be passed. They can be addresses on the stack if you let the
332 * eina_hash copy the key. Values can then
333 * be looked up with pointers other than the original key pointer that was
334 * used to add values. This method is not suitable to match string keys as
335 * it would only match the first character.
336 * On failure, this function returns @c NULL.
338 EAPI Eina_Hash *eina_hash_int32_new(Eina_Free_Cb data_free_cb);
341 * @brief Create a new hash table for use with 64bit integers.
343 * @param data_free_cb The function called on each value when the hash table
344 * is freed. @c NULL can be passed as callback.
345 * @return The new hash table.
347 * This function creates a new hash table where keys are 64bit integers.
348 * When adding or looking up in the hash table, pointers to 64bit integers
349 * must be passed. They can be addresses on the stack. Values can then
350 * be looked up with pointers other than the original key pointer that was
351 * used to add values. This method is not suitable to match string keys as
352 * it would only match the first character.
353 * On failure, this function returns @c NULL.
355 EAPI Eina_Hash *eina_hash_int64_new(Eina_Free_Cb data_free_cb);
358 * @brief Create a new hash table for use with pointers.
360 * @param data_free_cb The function called on each value when the hash table
361 * is freed. @c NULL can be passed as callback.
362 * @return The new hash table.
364 * This function creates a new hash table using the int64/int32 algorithm for
365 * table management and dereferenced pointers to compare the
366 * keys. Values can then be looked up with pointers other than the
367 * original key pointer that was used to add values. This method may
368 * appear to be able to match string keys, actually it only matches
369 * the first character. On failure, this function returns @c NULL.
371 EAPI Eina_Hash *eina_hash_pointer_new(Eina_Free_Cb data_free_cb);
374 * @brief Create a new hash table optimized for stringshared values.
376 * @param data_free_cb The function called on each value when the hash table
377 * is freed. @c NULL can be passed as callback.
378 * @return The new hash table.
380 * This function creates a new hash table optimized for stringshared
381 * values. Values CAN NOT be looked up with pointers not
382 * equal to the original key pointer that was used to add a value. On failure,
383 * this function returns @c NULL.
385 * Excerpt of code that will NOT work with this type of hash:
388 * extern Eina_Hash *hash;
389 * extern const char *value;
390 * const char *a = eina_stringshare_add("key");
392 * eina_hash_add(hash, a, value);
393 * eina_hash_find(hash, "key")
396 EAPI Eina_Hash *eina_hash_stringshared_new(Eina_Free_Cb data_free_cb);
399 * @brief Add an entry to the given hash table.
401 * @param hash The given hash table.
402 * @param key A unique key.
403 * @param data Data to associate with the string given by @p key.
404 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
406 * This function adds @p key to @p hash. @p hash, @p key and @p data
407 * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
408 * expected to be unique within the hash table. Key uniqueness varies
409 * depending on the type of @p hash: a stringshared @ref Eina_Hash
410 * need only have unique pointers for keys, but the strings in the
411 * pointers may be identical. All other hash types require the strings
412 * themselves to be unique. Failure to use sufficient uniqueness will
413 * result in unexpected results when inserting data pointers accessed
414 * with eina_hash_find(), and removed with eina_hash_del(). Key
415 * strings are case sensitive. If an error occurs, eina_error_get()
416 * should be used to determine if an allocation error occurred during
417 * this function. This function returns #EINA_FALSE if an error
418 * occurred, #EINA_TRUE otherwise.
420 EAPI Eina_Bool eina_hash_add(Eina_Hash *hash,
422 const void *data) EINA_ARG_NONNULL(1, 2, 3);
425 * @brief Add an entry to the given hash table without duplicating the string
428 * @param hash The given hash table. Can be @c NULL.
429 * @param key A unique key. Can be @c NULL.
430 * @param data Data to associate with the string given by @p key.
431 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
433 * This function adds @p key to @p hash. @p hash, @p key and @p data
434 * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
435 * expected to be unique within the hash table. Key uniqueness varies
436 * depending on the type of @p hash: a stringshared @ref Eina_Hash
437 * need only have unique pointers for keys, but the strings in the
438 * pointers may be identical. All other hash types require the strings
439 * themselves to be unique. Failure to use sufficient uniqueness will
440 * result in unexpected results when inserting data pointers accessed
441 * with eina_hash_find(), and removed with eina_hash_del(). This
442 * function does not make a copy of @p key, so it must be a string
443 * constant or stored elsewhere ( in the object being added). Key
444 * strings are case sensitive. If an error occurs, eina_error_get()
445 * should be used to determine if an allocation error occurred during
446 * this function. This function returns #EINA_FALSE if an error
447 * occurred, #EINA_TRUE otherwise.
449 EAPI Eina_Bool eina_hash_direct_add(Eina_Hash *hash,
451 const void *data) EINA_ARG_NONNULL(1, 2, 3);
454 * @brief Remove the entry identified by a key or a data from the given
457 * @param hash The given hash table.
458 * @param key The key.
459 * @param data The data pointer to remove if the key is @c NULL.
460 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
462 * This function removes the entry identified by @p key or @p data
463 * from @p hash. If a free function was given to the
464 * callback on creation, it will be called for the data being
465 * deleted. If @p hash is @c NULL, the functions returns immediately
466 * #EINA_FALSE. If @p key is @c NULL, then @p data is used to find the a
467 * match to remove, otherwise @p key is used and @p data is not
468 * required and can be @c NULL. This function returns #EINA_FALSE if
469 * an error occurred, #EINA_TRUE otherwise.
471 * @note if you know you already have the key, use
472 * eina_hash_del_by_key() or eina_hash_del_by_key_hash(). If you
473 * know you don't have the key, use eina_hash_del_by_data()
476 EAPI Eina_Bool eina_hash_del(Eina_Hash *hash,
478 const void *data) EINA_ARG_NONNULL(1);
481 * @brief Retrieve a specific entry in the given hash table.
483 * @param hash The given hash table.
484 * @param key The key of the entry to find.
485 * @return The data pointer for the stored entry on success, @c NULL
488 * This function retrieves the entry associated to @p key in
489 * @p hash. If @p hash is @c NULL, this function returns immediately
490 * @c NULL. This function returns the data pointer on success, @c NULL
493 EAPI void *eina_hash_find(const Eina_Hash *hash,
494 const void *key) EINA_ARG_NONNULL(1, 2);
497 * @brief Modify the entry pointer at the specified key and return the old
499 * @param hash The given hash table.
500 * @param key The key of the entry to modify.
501 * @param data The data to replace the old entry.
502 * @return The data pointer for the old stored entry on success, or
505 * This function modifies the data of @p key with @p data in @p
506 * hash. If no entry is found, nothing is added to @p hash. On success
507 * this function returns the old entry, otherwise it returns @c NULL.
509 EAPI void *eina_hash_modify(Eina_Hash *hash,
511 const void *data) EINA_ARG_NONNULL(1, 2, 3);
514 * @brief Modify the entry pointer at the specified key and return the
515 * old entry or add the entry if not found.
517 * @param hash The given hash table.
518 * @param key The key of the entry to modify.
519 * @param data The data to replace the old entry
520 * @return The data pointer for the old stored entry, or @c NULL
523 * This function modifies the data of @p key with @p data in @p
524 * hash. If no entry is found, @p data is added to @p hash with the
525 * key @p key. On success this function returns the old entry,
526 * otherwise it returns @c NULL. To check for errors, use
529 EAPI void *eina_hash_set(Eina_Hash *hash,
531 const void *data) EINA_ARG_NONNULL(1, 2);
534 * @brief Change the key associated with a data without triggering the
537 * @param hash The given hash table.
538 * @param old_key The current key associated with the data
539 * @param new_key The new key to associate data with
540 * @return EINA_FALSE in any case but success, EINA_TRUE on success.
542 * This function allows for the move of data from one key to another,
543 * but does not call the Eina_Free_Cb associated with the hash table
544 * when destroying the old key.
546 EAPI Eina_Bool eina_hash_move(Eina_Hash *hash,
548 const void *new_key) EINA_ARG_NONNULL(1, 2, 3);
551 * Free the given hash table resources.
553 * @param hash The hash table to be freed.
555 * This function frees up all the memory allocated to storing @p hash,
556 * and call the free callback if it has been passed to the hash table
557 * at creation time. If no free callback has been passed, any entries
558 * in the table that the program has no more pointers for elsewhere
559 * may now be lost, so this should only be called if the program has
560 * already freed any allocated data in the hash table or has the
561 * pointers for data in the table stored elsewhere as well. If @p hash
562 * is @c NULL, the function returns immediately.
566 * extern Eina_Hash *hash;
568 * eina_hash_free(hash);
572 EAPI void eina_hash_free(Eina_Hash *hash) EINA_ARG_NONNULL(1);
575 * Free the given hash table buckets resources.
577 * @param hash The hash table whose buckets have to be freed.
579 * This function frees up all the memory allocated to storing the
580 * buckets of @p hash, and calls the free callback on all hash table
581 * buckets if it has been passed to the hash table at creation time,
582 * then frees the buckets. If no free callback has been passed, no
583 * buckets value will be freed. If @p hash is @c NULL, the function
584 * returns immediately.
586 EAPI void eina_hash_free_buckets(Eina_Hash *hash) EINA_ARG_NONNULL(1);
589 * @brief Returns the number of entries in the given hash table.
591 * @param hash The given hash table.
592 * @return The number of entries in the hash table.
594 * This function returns the number of entries in @p hash, or 0 on
595 * error. If @p hash is @c NULL, 0 is returned.
597 EAPI int eina_hash_population(const Eina_Hash *hash) EINA_ARG_NONNULL(1);
600 * @brief Add an entry to the given hash table.
602 * @param hash The given hash table.
603 * @param key A unique key.
604 * @param key_length The length of the key.
605 * @param key_hash The hash that will always match key.
606 * @param data The data to associate with the string given by the key.
607 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
609 * This function adds @p key to @p hash. @p hash, @p key and @p data
610 * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
611 * expected to be a unique string within the hash table. Otherwise,
612 * one cannot be sure which inserted data pointer will be accessed
613 * with @ref eina_hash_find, and removed with @ref eina_hash_del. Do
614 * not forget to count '\\0' for string when setting the value of
615 * @p key_length. @p key_hash is expected to always match
616 * @p key. Otherwise, one cannot be sure to find it again with @ref
617 * eina_hash_find_by_hash. Key strings are case sensitive. If an error
618 * occurs, eina_error_get() should be used to determine if an
619 * allocation error occurred during this function. This function
620 * returns #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
622 EAPI Eina_Bool eina_hash_add_by_hash(Eina_Hash *hash,
626 const void *data) EINA_ARG_NONNULL(1, 2, 5);
629 * @brief Add an entry to the given hash table and do not duplicate the string
632 * @param hash The given hash table. Can be @c NULL.
633 * @param key A unique key. Can be @c NULL.
634 * @param key_length Should be the length of @p key (don't forget to count
636 * @param key_hash The hash that will always match key.
637 * @param data Data to associate with the string given by @p key.
638 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
640 * This function adds @p key to @p hash. @p hash, @p key and @p data
641 * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
642 * expected to be a unique string within the hash table. Otherwise,
643 * one cannot be sure which inserted data pointer will be accessed
644 * with @ref eina_hash_find, and removed with @ref eina_hash_del. This
645 * function does not make a copy of @p key so it must be a string
646 * constant or stored elsewhere (in the object being added). Do
647 * not forget to count '\\0' for string when setting the value of
648 * @p key_length. @p key_hash is expected to always match
649 * @p key. Otherwise, one cannot be sure to find it again with @ref
650 * eina_hash_find_by_hash. Key strings are case sensitive. If an error
651 * occurs, eina_error_get() should be used to determine if an
652 * allocation error occurred during this function. This function
653 * returns #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
655 EAPI Eina_Bool eina_hash_direct_add_by_hash(Eina_Hash *hash,
659 const void *data) EINA_ARG_NONNULL(1, 2, 5);
662 * @brief Remove the entry identified by a key and a key hash from the given
665 * @param hash The given hash table.
666 * @param key The key.
667 * @param key_length The length of the key.
668 * @param key_hash The hash that always match the key.
669 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
671 * This function removes the entry identified by @p key and
672 * @p key_hash from @p hash. If a free function was given to the
673 * callback on creation, it will be called for the data being
674 * deleted. Do not forget to count '\\0' for string when setting the
675 * value of @p key_length. If @p hash or @p key are @c NULL, the
676 * functions returns immediately #EINA_FALSE. This function returns
677 * #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
679 * @note if you don't have the key_hash, use eina_hash_del_by_key() instead.
680 * @note if you don't have the key, use eina_hash_del_by_data() instead.
682 EAPI Eina_Bool eina_hash_del_by_key_hash(Eina_Hash *hash,
685 int key_hash) EINA_ARG_NONNULL(1, 2);
688 * @brief Remove the entry identified by a key from the given hash table.
690 * This version will calculate key length and hash by using functions
691 * provided to hash creation function.
693 * @param hash The given hash table.
694 * @param key The key.
695 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
697 * This function removes the entry identified by @p key from @p
698 * hash. The key length and hash will be calculated automatically by
699 * using functiond provided to has creation function. If a free
700 * function was given to the callback on creation, it will be called
701 * for the data being deleted. If @p hash or @p key are @c NULL, the
702 * functions returns immediately #EINA_FALSE. This function returns
703 * #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
705 * @note if you already have the key_hash, use eina_hash_del_by_key_hash()
707 * @note if you don't have the key, use eina_hash_del_by_data() instead.
709 EAPI Eina_Bool eina_hash_del_by_key(Eina_Hash *hash,
710 const void *key) EINA_ARG_NONNULL(1, 2);
713 * @brief Remove the entry identified by a data from the given hash table.
715 * This version is slow since there is no quick access to nodes based on data.
717 * @param hash The given hash table.
718 * @param data The data value to search and remove.
719 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
722 * This function removes the entry identified by @p data from @p
723 * hash. If a free function was given to the callback on creation, it
724 * will be called for the data being deleted. If @p hash or @p data
725 * are @c NULL, the functions returns immediately #EINA_FALSE. This
726 * function returns #EINA_FALSE if an error occurred, #EINA_TRUE
729 * @note if you already have the key, use eina_hash_del_by_key() or
730 * eina_hash_del_by_key_hash() instead.
732 EAPI Eina_Bool eina_hash_del_by_data(Eina_Hash *hash,
733 const void *data) EINA_ARG_NONNULL(1, 2);
736 * @brief Remove the entry identified by a key and a key hash or a
737 * data from the given hash table.
739 * If @p key is @c NULL, then @p data is used to find a match to
742 * @param hash The given hash table.
743 * @param key The key.
744 * @param key_length The length of the key.
745 * @param key_hash The hash that always match the key.
746 * @param data The data pointer to remove if the key is @c NULL.
747 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
749 * This function removes the entry identified by @p key and
750 * @p key_hash, or @p data, from @p hash. If a free function was given to
751 * the callback on creation, it will be called for the data being
752 * deleted. If @p hash is @c NULL, the functions returns immediately
753 * #EINA_FALSE. If @p key is @c NULL, then @p key_hash and @p key_hash
754 * are ignored and @p data is used to find a match to remove,
755 * otherwise @p key and @p key_hash are used and @p data is not
756 * required and can be @c NULL. Do not forget to count '\\0' for
757 * string when setting the value of @p key_length. This function
758 * returns #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
760 * @note if you know you already have the key, use eina_hash_del_by_key_hash(),
761 * if you know you don't have the key, use eina_hash_del_by_data()
764 EAPI Eina_Bool eina_hash_del_by_hash(Eina_Hash *hash,
768 const void *data) EINA_ARG_NONNULL(1);
771 * @brief Retrieve a specific entry in the given hash table.
773 * @param hash The given hash table.
774 * @param key The key of the entry to find.
775 * @param key_length The length of the key.
776 * @param key_hash The hash that always match the key
777 * @return The data pointer for the stored entry on success, @c NULL
780 * This function retrieves the entry associated to @p key of length
781 * @p key_length in @p hash. @p key_hash is the hash that always match
782 * @p key. It is ignored if @p key is @c NULL. Do not forget to count
783 * '\\0' for string when setting the value of @p key_length. If
784 * @p hash is @c NULL, this function returns immediately @c NULL. This
785 * function returns the data pointer on success, @c NULL otherwise.
787 EAPI void *eina_hash_find_by_hash(const Eina_Hash *hash,
790 int key_hash) EINA_ARG_NONNULL(1, 2);
793 * @brief Modify the entry pointer at the specified key and returns
796 * @param hash The given hash table.
797 * @param key The key of the entry to modify.
798 * @param key_length Should be the length of @p key (don't forget to count
800 * @param key_hash The hash that always match the key. Ignored if @p key is
802 * @param data The data to replace the old entry, if it exists.
803 * @return The data pointer for the old stored entry, or @c NULL if not
804 * found. If an existing entry is not found, nothing is added to the
807 EAPI void *eina_hash_modify_by_hash(Eina_Hash *hash,
811 const void *data) EINA_ARG_NONNULL(1, 2, 5);
814 * @brief Returned a new iterator associated to hash keys.
816 * @param hash The hash.
817 * @return A new iterator.
819 * This function returns a newly allocated iterator associated to @p
820 * hash. If @p hash is not populated, this function still returns a
821 * valid iterator that will always return false on
822 * eina_iterator_next(), thus keeping API sane.
824 * If the memory can not be allocated, NULL is returned and
825 * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
828 * @warning if the hash structure changes then the iterator becomes
829 * invalid! That is, if you add or remove items this iterator
830 * behavior is undefined and your program may crash!
832 EAPI Eina_Iterator *eina_hash_iterator_key_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
835 * @brief Returned a new iterator associated to hash data.
837 * @param hash The hash.
838 * @return A new iterator.
840 * This function returns a newly allocated iterator associated to
841 * @p hash. If @p hash is not populated, this function still returns a
842 * valid iterator that will always return false on
843 * eina_iterator_next(), thus keeping API sane.
845 * If the memory can not be allocated, @c NULL is returned and
846 * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
849 * @warning if the hash structure changes then the iterator becomes
850 * invalid. That is, if you add or remove items this iterator behavior
851 * is undefined and your program may crash.
853 EAPI Eina_Iterator *eina_hash_iterator_data_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
856 * @brief Returned a new iterator associated to hash keys and data.
858 * @param hash The hash.
859 * @return A new iterator.
861 * This function returns a newly allocated iterator associated to @p
862 * hash. If @p hash is not populated, this function still returns a
863 * valid iterator that will always return false on
864 * eina_iterator_next(), thus keeping API sane.
866 * If the memory can not be allocated, NULL is returned and
867 * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
870 * @note iterator data will provide values as Eina_Hash_Tuple that should not
873 * @warning if the hash structure changes then the iterator becomes
874 * invalid! That is, if you add or remove items this iterator
875 * behavior is undefined and your program may crash!
877 EAPI Eina_Iterator *eina_hash_iterator_tuple_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
880 * @brief Call a function on every member stored in the hash table
882 * @param hash The hash table whose members will be walked
883 * @param func The function to call on each parameter
884 * @param fdata The data pointer to pass to the function being called
886 * This function goes through every entry in the hash table @p hash and calls
887 * the function @p func on each member. The function should @b not modify the
888 * hash table contents if it returns 1. @b If the hash table contents are
889 * modified by this function or the function wishes to stop processing it must
890 * return 0, otherwise return 1 to keep processing.
894 * extern Eina_Hash *hash;
896 * Eina_Bool hash_fn(const Eina_Hash *hash, const void *key,
897 * void *data, void *fdata)
899 * printf("Func data: %s, Hash entry: %s / %p\n",
900 * fdata, (const char *)key, data);
904 * int main(int argc, char **argv)
906 * char *hash_fn_data;
908 * hash_fn_data = strdup("Hello World");
909 * eina_hash_foreach(hash, hash_fn, hash_fn_data);
910 * free(hash_fn_data);
914 EAPI void eina_hash_foreach(const Eina_Hash *hash,
915 Eina_Hash_Foreach cb,
916 const void *fdata) EINA_ARG_NONNULL(1, 2);
917 /* Paul Hsieh (http://www.azillionmonkeys.com/qed/hash.html) hash function used by WebCore (http://webkit.org/blog/8/hashtables-part-2/) */
918 EAPI int eina_hash_superfast(const char *key,
919 int len) EINA_ARG_NONNULL(1);
920 /* Hash function first reported by dan bernstein many years ago in comp.lang.c */
921 static inline int eina_hash_djb2(const char *key,
922 int len) EINA_ARG_NONNULL(1);
923 static inline int eina_hash_djb2_len(const char *key,
924 int *plen) EINA_ARG_NONNULL(1, 2);
925 /* Hash function from http://www.concentric.net/~Ttwang/tech/inthash.htm */
926 static inline int eina_hash_int32(const unsigned int *pkey,
927 int len) EINA_ARG_NONNULL(1);
928 static inline int eina_hash_int64(const unsigned long int *pkey,
929 int len) EINA_ARG_NONNULL(1);
931 #include "eina_inline_hash.x"
949 #endif /*EINA_HASH_H_*/