eina: @since
[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  * @addtogroup Eina_Hash_Group Hash Table
122  *
123  * @brief Hash table management. Useful for mapping keys to values.
124  *
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
129  * details.
130  *
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.
133  *
134  * Eina hash tables can copy the keys when using eina_hash_add() or not when
135  * using eina_hash_direct_add().
136  *
137  * @section hashtable_algo Algorithm
138  *
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
147  * basic idea:
148  *
149  * @image html 01_hash-table.png
150  * @image latex 01_hash-table.eps
151  *
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.
156  *
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.
160  *
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.
164  *
165  * @section hashtable_perf Performance
166  *
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.
177  *
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).
184  *
185  * @section hashtable_tutorial Tutorial
186  *
187  * These examples show many Eina_Hash functions in action:
188  * @li @ref hash_01_example_page
189  * @li @ref hash_02_example_page
190  *
191  * @{
192  */
193
194 /**
195  * @addtogroup Eina_Data_Types_Group Data Types
196  *
197  * @{
198  */
199
200 /**
201  * @addtogroup Eina_Containers_Group Containers
202  *
203  * @{
204  */
205
206 /**
207  * @defgroup Eina_Hash_Group Hash Table
208  *
209  * @{
210  */
211
212 /**
213  * @typedef Eina_Hash
214  * Type for a generic hash table.
215  */
216 typedef struct _Eina_Hash       Eina_Hash;
217
218 typedef struct _Eina_Hash_Tuple Eina_Hash_Tuple;
219
220 struct _Eina_Hash_Tuple
221 {
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 */
225 };
226
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);
234
235
236 /**
237  * @brief Create a new hash table.
238  *
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.
246  *
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.
253  *
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().
259  */
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);
265
266 /**
267  * @brief Redefine the callback that clean the data of a hash
268  *
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.
272  * @since 1.1
273  */
274 EAPI void eina_hash_free_set(Eina_Hash *hash, Eina_Free_Cb data_free_cb) EINA_ARG_NONNULL(1);
275
276 /**
277  * @brief Create a new hash table using the djb2 algorithm.
278  *
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.
282  *
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.
287  */
288 EAPI Eina_Hash *eina_hash_string_djb2_new(Eina_Free_Cb data_free_cb);
289
290 /**
291  * @brief Create a new hash table for use with strings.
292  *
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.
296  *
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
301  * @c NULL.
302  */
303 EAPI Eina_Hash *eina_hash_string_superfast_new(Eina_Free_Cb data_free_cb);
304
305 /**
306  * @brief Create a new hash table for use with strings with small bucket size.
307  *
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.
311  *
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.
319  */
320 EAPI Eina_Hash *eina_hash_string_small_new(Eina_Free_Cb data_free_cb);
321
322 /**
323  * @brief Create a new hash table for use with 32bit integers.
324  *
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.
328  *
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.
337  */
338 EAPI Eina_Hash *eina_hash_int32_new(Eina_Free_Cb data_free_cb);
339
340 /**
341  * @brief Create a new hash table for use with 64bit integers.
342  *
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.
346  *
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.
354  */
355 EAPI Eina_Hash *eina_hash_int64_new(Eina_Free_Cb data_free_cb);
356
357 /**
358  * @brief Create a new hash table for use with pointers.
359  *
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.
363  *
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.
370  */
371 EAPI Eina_Hash *eina_hash_pointer_new(Eina_Free_Cb data_free_cb);
372
373 /**
374  * @brief Create a new hash table optimized for stringshared values.
375  *
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.
379  *
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.
384  *
385  * Excerpt of code that will NOT work with this type of hash:
386  *
387  * @code
388  * extern Eina_Hash *hash;
389  * extern const char *value;
390  * const char *a = eina_stringshare_add("key");
391  *
392  * eina_hash_add(hash, a, value);
393  * eina_hash_find(hash, "key")
394  * @endcode
395  */
396 EAPI Eina_Hash *eina_hash_stringshared_new(Eina_Free_Cb data_free_cb);
397
398 /**
399  * @brief Add an entry to the given hash table.
400  *
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.
405  *
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.
419  */
420 EAPI Eina_Bool  eina_hash_add(Eina_Hash  *hash,
421                               const void *key,
422                               const void *data) EINA_ARG_NONNULL(1, 2, 3);
423
424 /**
425  * @brief Add an entry to the given hash table without duplicating the string
426  * key.
427  *
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.
432  *
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.
448  */
449 EAPI Eina_Bool eina_hash_direct_add(Eina_Hash  *hash,
450                                     const void *key,
451                                     const void *data) EINA_ARG_NONNULL(1, 2, 3);
452
453 /**
454  * @brief Remove the entry identified by a key or a data from the given
455  * hash table.
456  *
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.
461  *
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.
470  *
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()
474  *       directly.
475  */
476 EAPI Eina_Bool eina_hash_del(Eina_Hash  *hash,
477                              const void *key,
478                              const void *data) EINA_ARG_NONNULL(1);
479
480 /**
481  * @brief Retrieve a specific entry in the given hash table.
482  *
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
486  * otherwise.
487  *
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
491  * otherwise.
492  */
493 EAPI void *eina_hash_find(const Eina_Hash *hash,
494                           const void      *key) EINA_ARG_NONNULL(1, 2);
495
496 /**
497  * @brief Modify the entry pointer at the specified key and return the old
498  * entry.
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
503  * @c NULL otherwise.
504  *
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.
508  */
509 EAPI void *eina_hash_modify(Eina_Hash  *hash,
510                             const void *key,
511                             const void *data) EINA_ARG_NONNULL(1, 2, 3);
512
513 /**
514  * @brief Modify the entry pointer at the specified key and return the
515  * old entry or add the entry if not found.
516  *
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
521  * otherwise.
522  *
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
527  * eina_error_get().
528  */
529 EAPI void *eina_hash_set(Eina_Hash  *hash,
530                          const void *key,
531                          const void *data) EINA_ARG_NONNULL(1, 2);
532
533 /**
534  * @brief Change the key associated with a data without triggering the
535  * free callback.
536  *
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.
541  *
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.
545  */
546 EAPI Eina_Bool eina_hash_move(Eina_Hash  *hash,
547                               const void *old_key,
548                               const void *new_key) EINA_ARG_NONNULL(1, 2, 3);
549
550 /**
551  * Free the given hash table resources.
552  *
553  * @param hash The hash table to be freed.
554  *
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.
563  *
564  * Example:
565  * @code
566  * extern Eina_Hash *hash;
567  *
568  * eina_hash_free(hash);
569  * hash = NULL;
570  * @endcode
571  */
572 EAPI void      eina_hash_free(Eina_Hash *hash) EINA_ARG_NONNULL(1);
573
574 /**
575  * Free the given hash table buckets resources.
576  *
577  * @param hash The hash table whose buckets have to be freed.
578  *
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.
585  */
586 EAPI void      eina_hash_free_buckets(Eina_Hash *hash) EINA_ARG_NONNULL(1);
587
588 /**
589  * @brief Returns the number of entries in the given hash table.
590  *
591  * @param hash The given hash table.
592  * @return The number of entries in the hash table.
593  *
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.
596  */
597 EAPI int       eina_hash_population(const Eina_Hash *hash) EINA_ARG_NONNULL(1);
598
599 /**
600  * @brief Add an entry to the given hash table.
601  *
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.
608  *
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.
621  */
622 EAPI Eina_Bool eina_hash_add_by_hash(Eina_Hash  *hash,
623                                      const void *key,
624                                      int         key_length,
625                                      int         key_hash,
626                                      const void *data) EINA_ARG_NONNULL(1, 2, 5);
627
628 /**
629  * @brief Add an entry to the given hash table and do not duplicate the string
630  * key.
631  *
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
635  * '\\0' for string).
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.
639  *
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.
654  */
655 EAPI Eina_Bool eina_hash_direct_add_by_hash(Eina_Hash  *hash,
656                                             const void *key,
657                                             int         key_length,
658                                             int         key_hash,
659                                             const void *data) EINA_ARG_NONNULL(1, 2, 5);
660
661 /**
662  * @brief Remove the entry identified by a key and a key hash from the given
663  * hash table.
664  *
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.
670  *
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.
678  *
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.
681  */
682 EAPI Eina_Bool eina_hash_del_by_key_hash(Eina_Hash  *hash,
683                                          const void *key,
684                                          int         key_length,
685                                          int         key_hash) EINA_ARG_NONNULL(1, 2);
686
687 /**
688  * @brief Remove the entry identified by a key from the given hash table.
689  *
690  * This version will calculate key length and hash by using functions
691  * provided to hash creation function.
692  *
693  * @param hash The given hash table.
694  * @param key  The key.
695  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
696  *
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.
704  *
705  * @note if you already have the key_hash, use eina_hash_del_by_key_hash()
706  * instead.
707  * @note if you don't have the key, use eina_hash_del_by_data() instead.
708  */
709 EAPI Eina_Bool eina_hash_del_by_key(Eina_Hash  *hash,
710                                     const void *key) EINA_ARG_NONNULL(1, 2);
711
712 /**
713  * @brief Remove the entry identified by a data from the given hash table.
714  *
715  * This version is slow since there is no quick access to nodes based on data.
716  *
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.
720  *          thing goes fine.
721  *
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
727  * otherwise.
728  *
729  * @note if you already have the key, use eina_hash_del_by_key() or
730  * eina_hash_del_by_key_hash() instead.
731  */
732 EAPI Eina_Bool eina_hash_del_by_data(Eina_Hash  *hash,
733                                      const void *data) EINA_ARG_NONNULL(1, 2);
734
735 /**
736  * @brief Remove the entry identified by a key and a key hash or a
737  * data from the given hash table.
738  *
739  * If @p key is @c NULL, then @p data is used to find a match to
740  * remove.
741  *
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.
748  *
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.
759  *
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()
762  *       directly.
763  */
764 EAPI Eina_Bool eina_hash_del_by_hash(Eina_Hash  *hash,
765                                      const void *key,
766                                      int         key_length,
767                                      int         key_hash,
768                                      const void *data) EINA_ARG_NONNULL(1);
769
770 /**
771  * @brief Retrieve a specific entry in the given hash table.
772  *
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
778  * otherwise.
779  *
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.
786  */
787 EAPI void *eina_hash_find_by_hash(const Eina_Hash *hash,
788                                   const void      *key,
789                                   int              key_length,
790                                   int              key_hash) EINA_ARG_NONNULL(1, 2);
791
792 /**
793  * @brief Modify the entry pointer at the specified key and returns
794  * the old entry.
795  *
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
799  * '\\0' for string).
800  * @param key_hash The hash that always match the key. Ignored if @p key is
801  *                 @c NULL.
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
805  *          hash.
806  */
807 EAPI void *eina_hash_modify_by_hash(Eina_Hash  *hash,
808                                     const void *key,
809                                     int         key_length,
810                                     int         key_hash,
811                                     const void *data) EINA_ARG_NONNULL(1, 2, 5);
812
813 /**
814  * @brief Returned a new iterator associated to hash keys.
815  *
816  * @param hash The hash.
817  * @return A new iterator.
818  *
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.
823  *
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
826  * returned.
827  *
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!
831  */
832 EAPI Eina_Iterator *eina_hash_iterator_key_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
833
834 /**
835  * @brief Returned a new iterator associated to hash data.
836  *
837  * @param hash The hash.
838  * @return A new iterator.
839  *
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.
844  *
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
847  * returned.
848  *
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.
852  */
853 EAPI Eina_Iterator *eina_hash_iterator_data_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
854
855 /**
856  * @brief Returned a new iterator associated to hash keys and data.
857  *
858  * @param hash The hash.
859  * @return A new iterator.
860  *
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.
865  *
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
868  * returned.
869  *
870  * @note iterator data will provide values as Eina_Hash_Tuple that should not
871  *   be modified!
872  *
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!
876  */
877 EAPI Eina_Iterator *eina_hash_iterator_tuple_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
878
879 /**
880  * @brief Call a function on every member stored in the hash table
881  *
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
885  *
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.
891  *
892  * Example:
893  * @code
894  * extern Eina_Hash *hash;
895  *
896  * Eina_Bool hash_fn(const Eina_Hash *hash, const void *key,
897  *                   void *data, void *fdata)
898  * {
899  *   printf("Func data: %s, Hash entry: %s / %p\n",
900  *          fdata, (const char *)key, data);
901  *   return 1;
902  * }
903  *
904  * int main(int argc, char **argv)
905  * {
906  *   char *hash_fn_data;
907  *
908  *   hash_fn_data = strdup("Hello World");
909  *   eina_hash_foreach(hash, hash_fn, hash_fn_data);
910  *   free(hash_fn_data);
911  * }
912  * @endcode
913  */
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);
930
931 #include "eina_inline_hash.x"
932
933 /**
934  * @}
935  */
936
937 /**
938  * @}
939  */
940
941 /**
942  * @}
943  */
944
945 /**
946  * @}
947  */
948
949 #endif /*EINA_HASH_H_*/