df6a5a4b0ba6473ec719fe5bb3e8efd4b104c02d
[profile/ivi/eina.git] / src / include / eina_hash.h
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
4  *
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.
9  *
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.
14  *
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/>.
18  */
19
20 #ifndef EINA_HASH_H_
21 #define EINA_HASH_H_
22
23 #include "eina_types.h"
24 #include "eina_iterator.h"
25
26 /**
27  * @page hash_01_example_page Eina_Hash in action
28  * @dontinclude eina_hash_01.c
29  *
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:
33  * @skip _Phone_Entry
34  * @until // _start_entries
35  *
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:
39  *
40  * @skip static
41  * @until }
42  *
43  * We also need a callback to iterate over the elements of the list later, so
44  * we are defining it now:
45  *
46  * @skip Eina_Bool
47  * @until }
48  *
49  * Now let's create our @ref Eina_Hash using @ref
50  * eina_hash_string_superfast_new :
51  *
52  * @skip eina_init
53  * @until phone_book
54  *
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).
58  *
59  * @skip for
60  * @until }
61  *
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:
67  *
68  * @skip Look for
69  * @until free(
70  *
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:
75  *
76  * @skip Modify
77  * @until printf("\n");
78  *
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 .
81  *
82  * @skip List of phones
83  * @until eina_iterator_free(it);
84  *
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:
87  *
88  * @skipline eina_hash_move
89  *
90  * We can remove all the elements from the table without free the table itself:
91  *
92  * @skip Empty the phone book
93  * @until eina_hash_population
94  *
95  * Or free the the entire table with its content:
96  *
97  * @skipline eina_hash_free
98  *
99  *
100  * The full code for this example can be seen here: @ref eina_hash_01_c
101  */
102
103 /**
104  * @page eina_hash_01_c Hash table in action
105  *
106  * @include eina_hash_01.c
107  * @example eina_hash_01.c
108  */
109
110 /**
111  * @page hash_02_example_page Different types of tables
112  *
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
115  * eina_hash_new.
116  * @include eina_hash_02.c
117  * @example eina_hash_02.c
118  */
119
120 /**
121  * @example eina_hash_03.c
122  * Same example as @ref hash_01_example_page but using a "string small" hash
123  * table instead of "string superfast".
124  */
125
126 /**
127  * @example eina_hash_04.c
128  * Same example as @ref hash_01_example_page but using a "string djb2" hash
129  * table instead of "string superfast".
130  */
131
132 /**
133  * @example eina_hash_05.c
134  * Same example as @ref hash_01_example_page but using a "int32" hash
135  * table instead of "string superfast".
136  */
137
138 /**
139  * @example eina_hash_06.c
140  * Same example as @ref hash_01_example_page but using a "int64" hash
141  * table instead of "string superfast".
142  */
143
144 /**
145  * @example eina_hash_07.c
146  * Same example as @ref hash_01_example_page but using a "pointer" hash
147  * table instead of "string superfast".
148  */
149
150 /**
151  * @example eina_hash_08.c
152  * This example shows the the usage of eina_hash_add(), eina_hash_add_by_hash(),
153  * eina_hash_direct_add_by_hash(), eina_hash_del(), eina_hash_del_by_key_hash(),
154  * eina_hash_del_by_key(), eina_hash_del_by_data(), eina_hash_find_by_hash() and
155  * eina_hash_modify_by_hash().
156  */
157
158 /**
159  * @addtogroup Eina_Hash_Group Hash Table
160  *
161  * @brief Hash table management. Useful for mapping keys to values.
162  *
163  * The hash table is useful for when one wants to implement a table that maps
164  * keys (usually strings) to data, and have relatively fast access time. The
165  * performance is proportional to the load factor of the table (number of
166  * elements / number of buckets). See @ref hashtable_algo for implementation
167  * details.
168  *
169  * Different implementations exists depending on what kind of key will be used
170  * to access the data: strings, integers, pointers, stringshared or your own.
171  *
172  * Eina hash tables can copy the keys when using eina_hash_add() or not when
173  * using eina_hash_direct_add().
174  *
175  * @section hashtable_algo Algorithm
176  *
177  * The Eina_Hash is implemented using an array of N "buckets", where each
178  * bucket is a pointer to a structure that is the head of a <a
179  * href="http://en.wikipedia.org/wiki/Red-black_tree">red-black tree</a>. The
180  * array can then be indexed by the [hash_of_element mod N]. The
181  * hash_of_element is calculated using the hashing function, passed as
182  * parameter to the @ref eina_hash_new function. N is the number of buckets
183  * (array positions), and is calculated based on the buckets_power_size
184  * (argument of @ref eina_hash_new too). The following picture ilustrates the
185  * basic idea:
186  *
187  * @htmlonly
188  * <img src="01_hash-table.png" width="500" />
189  * @endhtmlonly
190  * @image latex 01_hash-table.eps
191  *
192  * Adding an element to the hash table is made of:
193  * @li calculating the hash for that key (using the specified hash function);
194  * @li calculate the array position [hash mod N];
195  * @li add the element to the rbtree on that position.
196  *
197  * The two first steps have constant time, proportional to the hash function
198  * being used. Adding the key to the rbtree will be proportional on the number
199  * of keys on that bucket.
200  *
201  * The average cost of lookup depends on the number of keys per
202  * bucket (load factor) of the table, if the distribution of keys is
203  * sufficiently uniform.
204  *
205  * @section hashtable_perf Performance
206  *
207  * As said before, the performance depends on the load factor. So trying to keep
208  * the load factor as small as possible will improve the hash table performance. But
209  * increasing the buckets_power_size will also increase the memory consumption.
210  * The default hash table creation functions already have a good number of
211  * buckets, enough for most cases. Particularly for strings, if just a few keys
212  * (less than 30) will be added to the hash table, @ref
213  * eina_hash_string_small_new should be used, since it will reduce the memory
214  * consumption for the buckets, and you still won't have many collisions.
215  * However, @ref eina_hash_string_small_new still uses the same hash calculation
216  * function that @ref eina_hash_string_superfast_new, which is more complex than
217  * @ref eina_hash_string_djb2_new. The latter has a faster hash computation
218  * function, but that will imply on a not so good distribution. But if just a
219  * few keys are being added, this is not a problem, it will still have not many
220  * collisions and be faster to calculate the hash than in a hash created with
221  * @ref eina_hash_string_small_new and @ref eina_hash_string_superfast_new.
222  *
223  * A simple comparison between them would be:
224  *
225  * @li @c djb2 - faster hash function - 256 buckets (higher memory consumption)
226  * @li @c string_small - slower hash function but less collisions - 32 buckets
227  * (lower memory consumption)
228  * @li @c string_superfast - slower hash function but less collisions - 256 buckets
229  * (higher memory consumption)
230  *
231  * Basically for a very small number of keys (10 or less), @c djb2 should be
232  * used, or @c string_small if you have a restriction on memory usage. And for a
233  * higher number of keys, @c string_superfast should be always preferred.
234  *
235  * If just stringshared keys are being added, use @ref
236  * eina_hash_stringshared_new. If a lot of keys will be added to the hash table
237  * (e.g. more than 1000), then it's better to increase the buckets_power_size.
238  * See @ref eina_hash_new for more details.
239  *
240  * When adding a new key to a hash table, use @ref eina_hash_add or @ref
241  * eina_hash_direct_add (the latter if this key is already stored elsewhere). If
242  * the key may be already inside the hash table, instead of checking with
243  * @ref eina_hash_find and then doing @ref eina_hash_add, one can use just @ref
244  * eina_hash_set (this will change the data pointed by this key if it was
245  * already present in the table).
246  *
247  * @section hashtable_tutorial Tutorial
248  *
249  * These examples show many Eina_Hash functions in action:
250  * <ul>
251  * <li> @ref hash_01_example_page
252  * <li> @ref hash_02_example_page
253  * <li> Different types of hash in use:
254  *      <ul>
255  *      <li> @ref eina_hash_03.c "string small"
256  *      <li> @ref eina_hash_04.c "string djb2"
257  *      <li> @ref eina_hash_05.c "int32"
258  *      <li> @ref eina_hash_06.c "int64"
259  *      <li> @ref eina_hash_07.c "pointer"
260  *      </ul>
261  * <li> @ref eina_hash_08.c "Different add and delete functions"
262  * </ul>
263  */
264
265 /**
266  * @addtogroup Eina_Data_Types_Group Data Types
267  *
268  * @{
269  */
270
271 /**
272  * @addtogroup Eina_Containers_Group Containers
273  *
274  * @{
275  */
276
277 /**
278  * @defgroup Eina_Hash_Group Hash Table
279  *
280  * @{
281  */
282
283 /**
284  * @typedef Eina_Hash
285  * Type for a generic hash table.
286  */
287 typedef struct _Eina_Hash       Eina_Hash;
288
289 typedef struct _Eina_Hash_Tuple Eina_Hash_Tuple;
290
291 struct _Eina_Hash_Tuple
292 {
293    const void  *key; /**< The key */
294    void        *data; /**< The data associated to the key */
295    unsigned int key_length; /**< The length of the key */
296 };
297
298 typedef unsigned int (*Eina_Key_Length)(const void *key);
299 /**
300  * @def EINA_KEY_LENGTH
301  * @param Function The function used to hash calculation.
302  */
303 #define EINA_KEY_LENGTH(Function) ((Eina_Key_Length)Function)
304 typedef int          (*Eina_Key_Cmp)(const void *key1, int key1_length, const void *key2, int key2_length);
305 /**
306  * @def EINA_KEY_CMP
307  * @param Function The function used to hash calculation.
308  */
309 #define EINA_KEY_CMP(Function)    ((Eina_Key_Cmp)Function)
310 typedef int          (*Eina_Key_Hash)(const void *key, int key_length);
311 /**
312  * @def EINA_KEY_HASH
313  * @param Function The function used to hash calculation.
314  */
315 #define EINA_KEY_HASH(Function)   ((Eina_Key_Hash)Function)
316 typedef Eina_Bool    (*Eina_Hash_Foreach)(const Eina_Hash *hash, const void *key, void *data, void *fdata);
317
318
319 /**
320  * @brief Create a new hash table.
321  *
322  * @param key_length_cb The function called when getting the size of the key.
323  * @param key_cmp_cb The function called when comparing the keys.
324  * @param key_hash_cb The function called when getting the values.
325  * @param data_free_cb The function called on each value when the hash table is
326  * freed, or when an item is deleted from it. @c NULL can be passed as
327  * callback.
328  * @param buckets_power_size The size of the buckets.
329  * @return The new hash table.
330  *
331  * This function creates a new hash table using user-defined callbacks
332  * to manage the hash table. On failure, @c NULL is returned
333  * and #EINA_ERROR_OUT_OF_MEMORY is set. If @p key_cmp_cb or @p key_hash_cb
334  * are @c NULL, @c NULL is returned. If @p buckets_power_size is
335  * smaller or equal than 2, or if it is greater or equal than 17,
336  * @c NULL is returned.
337  *
338  * The number of buckets created will be 2 ^ @p buckets_power_size. This means
339  * that if @p buckets_power_size is 5, there will be created 32 buckets. for a
340  * @p buckets_power_size of 8, there will be 256 buckets.
341  *
342  * Pre-defined functions are available to create a hash table. See
343  * eina_hash_string_djb2_new(), eina_hash_string_superfast_new(),
344  * eina_hash_string_small_new(), eina_hash_int32_new(),
345  * eina_hash_int64_new(), eina_hash_pointer_new() and
346  * eina_hash_stringshared_new().
347  */
348 EAPI Eina_Hash *eina_hash_new(Eina_Key_Length key_length_cb,
349                               Eina_Key_Cmp    key_cmp_cb,
350                               Eina_Key_Hash   key_hash_cb,
351                               Eina_Free_Cb    data_free_cb,
352                               int             buckets_power_size) EINA_MALLOC EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(2, 3);
353
354 /**
355  * @brief Redefine the callback that clean the data of a hash
356  *
357  * @param hash The given hash table
358  * @param data_free_cb The function called on each value when the hash
359  * table is freed, or when an item is deleted from it. @c NULL can be passed as
360  * callback.
361  * @since 1.1
362  * See @ref eina_hash_new.
363  */
364 EAPI void eina_hash_free_cb_set(Eina_Hash *hash, Eina_Free_Cb data_free_cb) EINA_ARG_NONNULL(1);
365
366 /**
367  * @brief Create a new hash table using the djb2 algorithm.
368  *
369  * @param data_free_cb The function called on each value when the hash table
370  * is freed, or when an item is deleted from it. @c NULL can be passed as
371  * callback.
372  * @return The new hash table.
373  *
374  * This function creates a new hash table using the djb2 algorithm for
375  * table management and strcmp() to compare the keys. Values can then
376  * be looked up with pointers other than the original key pointer that
377  * was used to add values. On failure, this function returns @c NULL.
378  */
379 EAPI Eina_Hash *eina_hash_string_djb2_new(Eina_Free_Cb data_free_cb);
380
381 /**
382  * @brief Create a new hash table for use with strings.
383  *
384  * @param data_free_cb The function called on each value when the hash table
385  * is freed, or when an item is deleted from it. @c NULL can be passed as
386  * callback.
387  * @return The new hash table.
388  *
389  * This function creates a new hash table using the superfast algorithm
390  * for table management and strcmp() to compare the keys. Values can
391  * then be looked up with pointers other than the original key pointer
392  * that was used to add values. On failure, this function returns
393  * @c NULL.
394  */
395 EAPI Eina_Hash *eina_hash_string_superfast_new(Eina_Free_Cb data_free_cb);
396
397 /**
398  * @brief Create a new hash table for use with strings with small bucket size.
399  *
400  * @param data_free_cb  The function called on each value when the hash table
401  * is freed, or when an item is deleted from it. @c NULL can be passed as
402  * callback.
403  * @return  The new hash table.
404  *
405  * This function creates a new hash table using the superfast algorithm
406  * for table management and strcmp() to compare the keys, but with a
407  * smaller bucket size (compared to eina_hash_string_superfast_new())
408  * which will minimize the memory used by the returned hash
409  * table. Values can then be looked up with pointers other than the
410  * original key pointer that was used to add values. On failure, this
411  * function returns @c NULL.
412  */
413 EAPI Eina_Hash *eina_hash_string_small_new(Eina_Free_Cb data_free_cb);
414
415 /**
416  * @brief Create a new hash table for use with 32bit integers.
417  *
418  * @param data_free_cb  The function called on each value when the hash table
419  * is freed, or when an item is deleted from it. @c NULL can be passed as
420  * callback.
421  * @return  The new hash table.
422  *
423  * This function creates a new hash table where keys are 32bit integers.
424  * When adding or looking up in the hash table, pointers to 32bit integers
425  * must be passed. They can be addresses on the stack if you let the
426  * eina_hash copy the key. Values can then
427  * be looked up with pointers other than the original key pointer that was
428  * used to add values. This method is not suitable to match string keys as
429  * it would only match the first character.
430  * On failure, this function returns @c NULL.
431  */
432 EAPI Eina_Hash *eina_hash_int32_new(Eina_Free_Cb data_free_cb);
433
434 /**
435  * @brief Create a new hash table for use with 64bit integers.
436  *
437  * @param data_free_cb  The function called on each value when the hash table
438  * is freed, or when an item is deleted from it. @c NULL can be passed as
439  * callback.
440  * @return  The new hash table.
441  *
442  * This function creates a new hash table where keys are 64bit integers.
443  * When adding or looking up in the hash table, pointers to 64bit integers
444  * must be passed. They can be addresses on the stack. Values can then
445  * be looked up with pointers other than the original key pointer that was
446  * used to add values. This method is not suitable to match string keys as
447  * it would only match the first character.
448  * On failure, this function returns @c NULL.
449  */
450 EAPI Eina_Hash *eina_hash_int64_new(Eina_Free_Cb data_free_cb);
451
452 /**
453  * @brief Create a new hash table for use with pointers.
454  *
455  * @param data_free_cb  The function called on each value when the hash table
456  * is freed, or when an item is deleted from it. @c NULL can be passed as
457  * callback.
458  * @return  The new hash table.
459  *
460  * This function creates a new hash table using the int64/int32 algorithm for
461  * table management and dereferenced pointers to compare the
462  * keys. Values can then be looked up with pointers other than the
463  * original key pointer that was used to add values. This method may
464  * appear to be able to match string keys, actually it only matches
465  * the first character. On failure, this function returns @c NULL.
466  */
467 EAPI Eina_Hash *eina_hash_pointer_new(Eina_Free_Cb data_free_cb);
468
469 /**
470  * @brief Create a new hash table optimized for stringshared values.
471  *
472  * @param data_free_cb  The function called on each value when the hash table
473  * is freed, or when an item is deleted from it. @c NULL can be passed as
474  * callback.
475  * @return  The new hash table.
476  *
477  * This function creates a new hash table optimized for stringshared
478  * values. Values CAN NOT be looked up with pointers not
479  * equal to the original key pointer that was used to add a value. On failure,
480  * this function returns @c NULL.
481  *
482  * Excerpt of code that will NOT work with this type of hash:
483  *
484  * @code
485  * extern Eina_Hash *hash;
486  * extern const char *value;
487  * const char *a = eina_stringshare_add("key");
488  *
489  * eina_hash_add(hash, a, value);
490  * eina_hash_find(hash, "key")
491  * @endcode
492  */
493 EAPI Eina_Hash *eina_hash_stringshared_new(Eina_Free_Cb data_free_cb);
494
495 /**
496  * @brief Add an entry to the given hash table.
497  *
498  * @param hash The given hash table. Cannot be @c NULL.
499  * @param key A unique key. Cannot be @c NULL.
500  * @param data Data to associate with the string given by @p key. Cannot be @c
501  * NULL.
502  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
503  *
504  * This function adds @p key to @p hash. @p key is
505  * expected to be unique within the hash table. Key uniqueness varies
506  * depending on the type of @p hash: a stringshared @ref Eina_Hash
507  * need to have unique pointers (which implies unique strings).
508  * All other string hash types require the strings
509  * themselves to be unique. Pointer, int32 and int64 hashes need to have these
510  * values as unique. Failure to use sufficient uniqueness will
511  * result in unexpected results when inserting data pointers accessed
512  * with eina_hash_find(), and removed with eina_hash_del(). Key
513  * strings are case sensitive. If an error occurs, eina_error_get()
514  * should be used to determine if an allocation error occurred during
515  * this function. This function returns #EINA_FALSE if an error
516  * occurred, #EINA_TRUE otherwise.
517  */
518 EAPI Eina_Bool  eina_hash_add(Eina_Hash  *hash,
519                               const void *key,
520                               const void *data) EINA_ARG_NONNULL(1, 2, 3);
521
522 /**
523  * @brief Add an entry to the given hash table without duplicating the string
524  * key.
525  *
526  * @param hash The given hash table. Cannot be @c NULL.
527  * @param key A unique key. Cannot be @c NULL.
528  * @param data Data to associate with the string given by @p key. Cannot be @c
529  * NULL
530  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
531  *
532  * This function adds @p key to @p hash. @p key is
533  * expected to be unique within the hash table. Key uniqueness varies
534  * depending on the type of @p hash: a stringshared @ref Eina_Hash
535  * need have unique pointers (which implies unique strings).
536  * All other string hash types require the strings
537  * themselves to be unique. Pointer, int32 and int64 hashes need to have these
538  * values as unique. Failure to use sufficient uniqueness will
539  * result in unexpected results when inserting data pointers accessed
540  * with eina_hash_find(), and removed with eina_hash_del(). This
541  * function does not make a copy of @p key, so it must be a string
542  * constant or stored elsewhere ( in the object being added). Key
543  * strings are case sensitive. If an error occurs, eina_error_get()
544  * should be used to determine if an allocation error occurred during
545  * this function. This function returns #EINA_FALSE if an error
546  * occurred, #EINA_TRUE otherwise.
547  */
548 EAPI Eina_Bool eina_hash_direct_add(Eina_Hash  *hash,
549                                     const void *key,
550                                     const void *data) EINA_ARG_NONNULL(1, 2, 3);
551
552 /**
553  * @brief Remove the entry identified by a key or a data from the given
554  * hash table.
555  *
556  * @param hash The given hash table.
557  * @param key  The key.
558  * @param data The data pointer to remove if the key is @c NULL.
559  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
560  *
561  * This function removes the entry identified by @p key or @p data
562  * from @p hash. If a free function was given to the
563  * callback on creation, it will be called for the data being
564  * deleted. If @p hash is @c NULL, the functions returns immediately #EINA_FALSE.
565  * If @p key is @c NULL, then @p data is used to find the a
566  * match to remove, otherwise @p key is used and @p data is not
567  * required and can be @c NULL. This function returns #EINA_FALSE if
568  * an error occurred, #EINA_TRUE otherwise.
569  *
570  * @note if you know you already have the key, use
571  *       eina_hash_del_by_key() or eina_hash_del_by_key_hash(). If you
572  *       know you don't have the key, use eina_hash_del_by_data()
573  *       directly.
574  */
575 EAPI Eina_Bool eina_hash_del(Eina_Hash  *hash,
576                              const void *key,
577                              const void *data) EINA_ARG_NONNULL(1);
578
579 /**
580  * @brief Retrieve a specific entry in the given hash table.
581  *
582  * @param hash The given hash table.
583  * @param key The key of the entry to find.
584  * @return The data pointer for the stored entry on success, @c NULL
585  * otherwise.
586  *
587  * This function retrieves the entry associated to @p key in
588  * @p hash. If @p hash is @c NULL, this function returns immediately
589  * @c NULL. This function returns the data pointer on success, @c NULL
590  * otherwise.
591  */
592 EAPI void *eina_hash_find(const Eina_Hash *hash,
593                           const void      *key) EINA_ARG_NONNULL(2);
594
595 /**
596  * @brief Modify the entry pointer at the specified key and return the old
597  * entry.
598  * @param hash The given hash table.
599  * @param key The key of the entry to modify.
600  * @param data The data to replace the old entry.
601  * @return The data pointer for the old stored entry on success, or
602  * @c NULL otherwise.
603  *
604  * This function modifies the data of @p key with @p data in @p
605  * hash. If no entry is found, nothing is added to @p hash. On success
606  * this function returns the old entry, otherwise it returns @c NULL.
607  */
608 EAPI void *eina_hash_modify(Eina_Hash  *hash,
609                             const void *key,
610                             const void *data) EINA_ARG_NONNULL(1, 2, 3);
611
612 /**
613  * @brief Modify the entry pointer at the specified key and return the
614  * old entry or add the entry if not found.
615  *
616  * @param hash The given hash table.
617  * @param key The key of the entry to modify.
618  * @param data The data to replace the old entry
619  * @return The data pointer for the old stored entry, or @c NULL
620  * otherwise.
621  *
622  * This function modifies the data of @p key with @p data in @p
623  * hash. If no entry is found, @p data is added to @p hash with the
624  * key @p key. On success this function returns the old entry,
625  * otherwise it returns @c NULL. To check for errors, use
626  * eina_error_get().
627  */
628 EAPI void *eina_hash_set(Eina_Hash  *hash,
629                          const void *key,
630                          const void *data) EINA_ARG_NONNULL(1, 2);
631
632 /**
633  * @brief Change the key associated with a data without triggering the
634  * free callback.
635  *
636  * @param hash    The given hash table.
637  * @param old_key The current key associated with the data
638  * @param new_key The new key to associate data with
639  * @return #EINA_FALSE in any case but success, #EINA_TRUE on success.
640  *
641  * This function allows for the move of data from one key to another,
642  * but does not call the Eina_Free_Cb associated with the hash table
643  * when destroying the old key.
644  */
645 EAPI Eina_Bool eina_hash_move(Eina_Hash  *hash,
646                               const void *old_key,
647                               const void *new_key) EINA_ARG_NONNULL(1, 2, 3);
648
649 /**
650  * Free the given hash table resources.
651  *
652  * @param hash The hash table to be freed.
653  *
654  * This function frees up all the memory allocated to storing @p hash,
655  * and call the free callback if it has been passed to the hash table
656  * at creation time. If no free callback has been passed, any entries
657  * in the table that the program has no more pointers for elsewhere
658  * may now be lost, so this should only be called if the program has
659  * already freed any allocated data in the hash table or has the
660  * pointers for data in the table stored elsewhere as well. If @p hash
661  * is @c NULL, the function returns immediately.
662  *
663  * Example:
664  * @code
665  * extern Eina_Hash *hash;
666  *
667  * eina_hash_free(hash);
668  * hash = NULL;
669  * @endcode
670  */
671 EAPI void      eina_hash_free(Eina_Hash *hash) EINA_ARG_NONNULL(1);
672
673 /**
674  * Free the given hash table buckets resources.
675  *
676  * @param hash The hash table whose buckets have to be freed.
677  *
678  * This function frees up all the memory allocated to storing the
679  * buckets of @p hash, and calls the free callback on all hash table
680  * buckets if it has been passed to the hash table at creation time,
681  * then frees the buckets. If no free callback has been passed, no
682  * buckets value will be freed. If @p hash is @c NULL, the function
683  * returns immediately.
684  */
685 EAPI void      eina_hash_free_buckets(Eina_Hash *hash) EINA_ARG_NONNULL(1);
686
687 /**
688  * @brief Returns the number of entries in the given hash table.
689  *
690  * @param hash The given hash table.
691  * @return The number of entries in the hash table.
692  *
693  * This function returns the number of entries in @p hash, or 0 on
694  * error. If @p hash is @c NULL, @c 0 is returned.
695  */
696 EAPI int       eina_hash_population(const Eina_Hash *hash) EINA_ARG_NONNULL(1);
697
698 /**
699  * @brief Add an entry to the given hash table.
700  *
701  * @param hash The given hash table. Cannot be @c NULL.
702  * @param key A unique key. Cannot be @c NULL.
703  * @param key_length The length of the key.
704  * @param key_hash The hash that will always match key.
705  * @param data The data to associate with the string given by the key. Cannot be
706  * @c NULL.
707  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
708  *
709  * This function adds @p key to @p hash. @p hash, @p key and @p data
710  * cannot be @c NULL, in that case #EINA_FALSE is returned. @p key is
711  * expected to be a unique within the hash table. Otherwise,
712  * one cannot be sure which inserted data pointer will be accessed
713  * with @ref eina_hash_find, and removed with @ref eina_hash_del. Do
714  * not forget to count '\\0' for string when setting the value of
715  * @p key_length. @p key_hash is expected to always match
716  * @p key. Otherwise, one cannot be sure to find it again with @ref
717  * eina_hash_find_by_hash. Key strings are case sensitive. If an error
718  * occurs, eina_error_get() should be used to determine if an
719  * allocation error occurred during this function. This function
720  * returns #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
721  *
722  * @see eina_hash_add()
723  */
724 EAPI Eina_Bool eina_hash_add_by_hash(Eina_Hash  *hash,
725                                      const void *key,
726                                      int         key_length,
727                                      int         key_hash,
728                                      const void *data) EINA_ARG_NONNULL(1, 2, 5);
729
730 /**
731  * @brief Add an entry to the given hash table and do not duplicate the string
732  * key.
733  *
734  * @param hash The given hash table. Cannot be @c NULL.
735  * @param key A unique key. Cannot be @c NULL.
736  * @param key_length Should be the length of @p key (don't forget to count
737  * '\\0' for string).
738  * @param key_hash The hash that will always match key.
739  * @param data Data to associate with the string given by @p key. Cannot be @c
740  * NULL.
741  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
742  *
743  * This function adds @p key to @p hash. @p hash, @p key and @p data
744  * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
745  * expected to be unique within the hash table. Otherwise,
746  * one cannot be sure which inserted data pointer will be accessed
747  * with @ref eina_hash_find, and removed with @ref eina_hash_del. This
748  * function does not make a copy of @p key so it must be a string
749  * constant or stored elsewhere (in the object being added). Do
750  * not forget to count '\\0' for string when setting the value of
751  * @p key_length. @p key_hash is expected to always match
752  * @p key. Otherwise, one cannot be sure to find it again with @ref
753  * eina_hash_find_by_hash. Key strings are case sensitive. If an error
754  * occurs, eina_error_get() should be used to determine if an
755  * allocation error occurred during this function. This function
756  * returns #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
757  *
758  * @see eina_hash_direct_add()
759  */
760 EAPI Eina_Bool eina_hash_direct_add_by_hash(Eina_Hash  *hash,
761                                             const void *key,
762                                             int         key_length,
763                                             int         key_hash,
764                                             const void *data) EINA_ARG_NONNULL(1, 2, 5);
765
766 /**
767  * @brief Remove the entry identified by a key and a key hash from the given
768  * hash table.
769  *
770  * @param hash The given hash table. Cannot be @c NULL.
771  * @param key The key. Cannot be @c NULL.
772  * @param key_length The length of the key.
773  * @param key_hash The hash that always match the key.
774  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
775  *
776  * This function removes the entry identified by @p key and
777  * @p key_hash from @p hash. If a free function was given to the
778  * callback on creation, it will be called for the data being
779  * deleted. Do not forget to count '\\0' for string when setting the
780  * value of @p key_length. If @p hash or @p key are @c NULL, the
781  * functions returns immediately #EINA_FALSE. This function
782  * returns #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
783  *
784  * @note if you don't have the key_hash, use eina_hash_del_by_key() instead.
785  * @note if you don't have the key, use eina_hash_del_by_data() instead.
786  */
787 EAPI Eina_Bool eina_hash_del_by_key_hash(Eina_Hash  *hash,
788                                          const void *key,
789                                          int         key_length,
790                                          int         key_hash) EINA_ARG_NONNULL(1, 2);
791
792 /**
793  * @brief Remove the entry identified by a key from the given hash table.
794  *
795  * This version will calculate key length and hash by using functions
796  * provided to hash creation function.
797  *
798  * @param hash The given hash table. Cannot be @c NULL.
799  * @param key  The key. Cannot be @c NULL.
800  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
801  *
802  * This function removes the entry identified by @p key from @p
803  * hash. The key length and hash will be calculated automatically by
804  * using functiond provided to has creation function. If a free
805  * function was given to the callback on creation, it will be called
806  * for the data being deleted. If @p hash or @p key are @c NULL, the
807  * functions returns immediately #EINA_FALSE. This function
808  * returns #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
809  *
810  * @note if you already have the key_hash, use eina_hash_del_by_key_hash()
811  * instead.
812  * @note if you don't have the key, use eina_hash_del_by_data() instead.
813  */
814 EAPI Eina_Bool eina_hash_del_by_key(Eina_Hash  *hash,
815                                     const void *key) EINA_ARG_NONNULL(1, 2);
816
817 /**
818  * @brief Remove the entry identified by a data from the given hash table.
819  *
820  * This version is slow since there is no quick access to nodes based on data.
821  *
822  * @param hash The given hash table. Cannot be @c NULL.
823  * @param data The data value to search and remove. Cannot be @c NULL.
824  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
825  *          thing goes fine.
826  *
827  * This function removes the entry identified by @p data from @p
828  * hash. If a free function was given to the callback on creation, it
829  * will be called for the data being deleted. If @p hash or @p data
830  * are @c NULL, the functions returns immediately #EINA_FALSE. This
831  * function returns #EINA_FALSE if an error occurred, #EINA_TRUE
832  * otherwise.
833  *
834  * @note if you already have the key, use eina_hash_del_by_key() or
835  * eina_hash_del_by_key_hash() instead.
836  */
837 EAPI Eina_Bool eina_hash_del_by_data(Eina_Hash  *hash,
838                                      const void *data) EINA_ARG_NONNULL(1, 2);
839
840 /**
841  * @brief Remove the entry identified by a key and a key hash or a
842  * data from the given hash table.
843  *
844  * If @p key is @c NULL, then @p data is used to find a match to
845  * remove.
846  *
847  * @param hash The given hash table. Cannot be @c NULL.
848  * @param key The key.
849  * @param key_length The length of the key.
850  * @param key_hash The hash that always match the key.
851  * @param data The data pointer to remove if the key is @c NULL.
852  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
853  *
854  * This function removes the entry identified by @p key and
855  * @p key_hash, or @p data, from @p hash. If a free function was given to
856  * the  callback on creation, it will be called for the data being
857  * deleted. If @p hash is @c NULL, the functions returns immediately #EINA_FALSE.
858  * If @p key is @c NULL, then @p key_length and @p key_hash
859  * are ignored and @p data is used to find a match to remove,
860  * otherwise @p key and @p key_hash are used and @p data is not
861  * required and can be @c NULL. Do not forget to count '\\0' for
862  * string when setting the value of @p key_length. This function
863  * returns #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
864  *
865  * @note if you know you already have the key, use eina_hash_del_by_key_hash(),
866  *       if you know you don't have the key, use eina_hash_del_by_data()
867  *       directly.
868  */
869 EAPI Eina_Bool eina_hash_del_by_hash(Eina_Hash  *hash,
870                                      const void *key,
871                                      int         key_length,
872                                      int         key_hash,
873                                      const void *data) EINA_ARG_NONNULL(1);
874
875 /**
876  * @brief Retrieve a specific entry in the given hash table.
877  *
878  * @param hash The given hash table. Cannot be @c NULL.
879  * @param key The key of the entry to find.
880  * @param key_length The length of the key.
881  * @param key_hash The hash that always match the key
882  * @return The data pointer for the stored entry on success, @c NULL
883  * otherwise.
884  *
885  * This function retrieves the entry associated to @p key of length
886  * @p key_length in @p hash. @p key_hash is the hash that always match
887  * @p key. It is ignored if @p key is @c NULL. Do not forget to count
888  * '\\0' for string when setting the value of @p key_length. If
889  * @p hash is @c NULL, this function returns immediately @c NULL. This
890  * function returns the data pointer on success, @c NULL otherwise.
891  */
892 EAPI void *eina_hash_find_by_hash(const Eina_Hash *hash,
893                                   const void      *key,
894                                   int              key_length,
895                                   int              key_hash) EINA_ARG_NONNULL(1, 2);
896
897 /**
898  * @brief Modify the entry pointer at the specified key and returns
899  * the old entry.
900  *
901  * @param hash The given hash table.
902  * @param key The key of the entry to modify.
903  * @param key_length Should be the length of @p key (don't forget to count
904  * '\\0' for string).
905  * @param key_hash The hash that always match the key. Ignored if @p key is
906  *                 @c NULL.
907  * @param data The data to replace the old entry, if it exists.
908  * @return The data pointer for the old stored entry, or @c NULL if not
909  *          found. If an existing entry is not found, nothing is added to the
910  *          hash.
911  */
912 EAPI void *eina_hash_modify_by_hash(Eina_Hash  *hash,
913                                     const void *key,
914                                     int         key_length,
915                                     int         key_hash,
916                                     const void *data) EINA_ARG_NONNULL(1, 2, 5);
917
918 /**
919  * @brief Returned a new iterator associated to hash keys.
920  *
921  * @param hash The hash.
922  * @return A new iterator.
923  *
924  * This function returns a newly allocated iterator associated to @p
925  * hash. If @p hash is not populated, this function still returns a
926  * valid iterator that will always return false on
927  * eina_iterator_next(), thus keeping API sane.
928  *
929  * If the memory can not be allocated, NULL is returned
930  * and #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
931  * returned.
932  *
933  * @warning if the hash structure changes then the iterator becomes
934  *    invalid! That is, if you add or remove items this iterator
935  *    behavior is undefined and your program may crash!
936  */
937 EAPI Eina_Iterator *eina_hash_iterator_key_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
938
939 /**
940  * @brief Returned a new iterator associated to hash data.
941  *
942  * @param hash The hash.
943  * @return A new iterator.
944  *
945  * This function returns a newly allocated iterator associated to
946  * @p hash. If @p hash is not populated, this function still returns a
947  * valid iterator that will always return false on
948  * eina_iterator_next(), thus keeping API sane.
949  *
950  * If the memory can not be allocated, @c NULL is returned
951  * and #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
952  * returned.
953  *
954  * @warning if the hash structure changes then the iterator becomes
955  * invalid. That is, if you add or remove items this iterator behavior
956  * is undefined and your program may crash.
957  */
958 EAPI Eina_Iterator *eina_hash_iterator_data_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
959
960 /**
961  * @brief Returned a new iterator associated to hash keys and data.
962  *
963  * @param hash The hash.
964  * @return A new iterator.
965  *
966  * This function returns a newly allocated iterator associated to @p
967  * hash. If @p hash is not populated, this function still returns a
968  * valid iterator that will always return false on
969  * eina_iterator_next(), thus keeping API sane.
970  *
971  * If the memory can not be allocated, @c NULL is returned
972  * and #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
973  * returned.
974  *
975  * @note iterator data will provide values as Eina_Hash_Tuple that should not
976  *   be modified!
977  *
978  * @warning if the hash structure changes then the iterator becomes
979  *    invalid! That is, if you add or remove items this iterator
980  *    behavior is undefined and your program may crash!
981  */
982 EAPI Eina_Iterator *eina_hash_iterator_tuple_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
983
984 /**
985  * @brief Call a function on every member stored in the hash table
986  *
987  * @param hash The hash table whose members will be walked
988  * @param func The function to call on each parameter
989  * @param fdata The data pointer to pass to the function being called
990  *
991  * This function goes through every entry in the hash table @p hash and calls
992  * the function @p func on each member. The function should @b not modify the
993  * hash table contents if it returns @c 1. @b If the hash table contents are
994  * modified by this function or the function wishes to stop processing it must
995  * return @c 0, otherwise return @c 1 to keep processing.
996  *
997  * Example:
998  * @code
999  * extern Eina_Hash *hash;
1000  *
1001  * Eina_Bool hash_fn(const Eina_Hash *hash, const void *key,
1002  *                   void *data, void *fdata)
1003  * {
1004  *   printf("Func data: %s, Hash entry: %s / %p\n",
1005  *          fdata, (const char *)key, data);
1006  *   return 1;
1007  * }
1008  *
1009  * int main(int argc, char **argv)
1010  * {
1011  *   char *hash_fn_data;
1012  *
1013  *   hash_fn_data = strdup("Hello World");
1014  *   eina_hash_foreach(hash, hash_fn, hash_fn_data);
1015  *   free(hash_fn_data);
1016  * }
1017  * @endcode
1018  */
1019 EAPI void           eina_hash_foreach(const Eina_Hash  *hash,
1020                                       Eina_Hash_Foreach func,
1021                                       const void       *fdata) EINA_ARG_NONNULL(1, 2);
1022 /* Paul Hsieh (http://www.azillionmonkeys.com/qed/hash.html) hash function used by WebCore (http://webkit.org/blog/8/hashtables-part-2/) */
1023 EAPI int eina_hash_superfast(const char *key,
1024                              int         len) EINA_ARG_NONNULL(1);
1025 /* Hash function first reported by dan bernstein many years ago in comp.lang.c */
1026 static inline int eina_hash_djb2(const char *key,
1027                                  int         len) EINA_ARG_NONNULL(1);
1028 static inline int eina_hash_djb2_len(const char *key,
1029                                      int        *plen) EINA_ARG_NONNULL(1, 2);
1030 /* Hash function from http://www.concentric.net/~Ttwang/tech/inthash.htm */
1031 static inline int eina_hash_int32(const unsigned int *pkey,
1032                                   int                 len) EINA_ARG_NONNULL(1);
1033 static inline int eina_hash_int64(const unsigned long int *pkey,
1034                                   int                      len) EINA_ARG_NONNULL(1);
1035 /* http://sites.google.com/site/murmurhash/ */
1036 static inline int eina_hash_murmur3(const char *key,
1037                            int         len) EINA_ARG_NONNULL(1);
1038 #include "eina_inline_hash.x"
1039
1040 /**
1041  * @}
1042  */
1043
1044 /**
1045  * @}
1046  */
1047
1048 /**
1049  * @}
1050  */
1051
1052 #endif /*EINA_HASH_H_*/