eina: fix eina_hash_set to act when data == NULL like eina_hash_del.
[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  * @addtogroup Eina_Hash_Group Hash Table
28  *
29  * @brief give a small description here : what it is for, what it does
30  * , etc...
31  * Different implementations exists depending on what to store: strings,
32  * integers, pointers, stringshared or your own...
33  * Eina hash tables can copy the keys when using eina_hash_add() or not when
34  * using eina_hash_direct_add().
35  *
36  * Hash API. Give some hints about the use (functions that must be
37  * used like init / shutdown), general use, etc... Give also a link to
38  * tutorial below.
39  *
40  * @section hashtable_algo Algorithm
41  *
42  * Give here the algorithm used in the implementation
43  *
44  * @section hashtable_perf Performance
45  *
46  * Give some hints about performance if it is possible, and an image !
47  *
48  * @section hashtable_tutorial Tutorial
49  *
50  * Here is a fantastic tutorial about our hash table
51  *
52  * @{
53  */
54
55 /**
56  * @addtogroup Eina_Data_Types_Group Data Types
57  *
58  * @{
59  */
60
61 /**
62  * @addtogroup Eina_Containers_Group Containers
63  *
64  * @{
65  */
66
67 /**
68  * @defgroup Eina_Hash_Group Hash Table
69  *
70  * @{
71  */
72
73 /**
74  * @typedef Eina_Hash
75  * Type for a generic hash table.
76  */
77 typedef struct _Eina_Hash       Eina_Hash;
78
79 typedef struct _Eina_Hash_Tuple Eina_Hash_Tuple;
80
81 struct _Eina_Hash_Tuple
82 {
83    const void  *key; /**< The key */
84    void        *data; /**< The data associated to the key */
85    unsigned int key_length; /**< The length of the key */
86 };
87
88 typedef unsigned int (*Eina_Key_Length)(const void *key);
89 #define EINA_KEY_LENGTH(Function) ((Eina_Key_Length)Function)
90 typedef int          (*Eina_Key_Cmp)(const void *key1, int key1_length, const void *key2, int key2_length);
91 #define EINA_KEY_CMP(Function)    ((Eina_Key_Cmp)Function)
92 typedef int          (*Eina_Key_Hash)(const void *key, int key_length);
93 #define EINA_KEY_HASH(Function)   ((Eina_Key_Hash)Function)
94 typedef Eina_Bool    (*Eina_Hash_Foreach)(const Eina_Hash *hash, const void *key, void *data, void *fdata);
95
96
97 /**
98  * @brief Create a new hash table.
99  *
100  * @param key_length_cb The function called when getting the size of the key.
101  * @param key_cmp_cb The function called when comparing the keys.
102  * @param key_hash_cb The function called when getting the values.
103  * @param data_free_cb The function called on each value when the hash
104  * table is freed. @c NULL can be passed as callback.
105  * @param buckets_power_size The size of the buckets.
106  * @return The new hash table.
107  *
108  * This function creates a new hash table using user-defined callbacks
109  * to manage the hash table. On failure, @c NULL is returned and
110  * #EINA_ERROR_OUT_OF_MEMORY is set. If @p key_cmp_cb or @p key_hash_cb
111  * are @c NULL, @c NULL is returned. If @p buckets_power_size is
112  * smaller or equal than 2, or if it is greater or equal than 17,
113  * @c NULL is returned.
114  *
115  * Pre-defined functions are available to create a hash table. See
116  * eina_hash_string_djb2_new(), eina_hash_string_superfast_new(),
117  * eina_hash_string_small_new(), eina_hash_int32_new(),
118  * eina_hash_int64_new(), eina_hash_pointer_new() and
119  * eina_hash_stringshared_new().
120  */
121 EAPI Eina_Hash *eina_hash_new(Eina_Key_Length key_length_cb,
122                               Eina_Key_Cmp    key_cmp_cb,
123                               Eina_Key_Hash   key_hash_cb,
124                               Eina_Free_Cb    data_free_cb,
125                               int             buckets_power_size) EINA_MALLOC EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(2, 3);
126
127 /**
128  * @brief Create a new hash table using the djb2 algorithm.
129  *
130  * @param data_free_cb The function called on each value when the hash table
131  * is freed. @c NULL can be passed as callback.
132  * @return The new hash table.
133  *
134  * This function creates a new hash table using the djb2 algorithm for
135  * table management and strcmp() to compare the keys. Values can then
136  * be looked up with pointers other than the original key pointer that
137  * was used to add values. On failure, this function returns @c NULL.
138  */
139 EAPI Eina_Hash *eina_hash_string_djb2_new(Eina_Free_Cb data_free_cb);
140
141 /**
142  * @brief Create a new hash table for use with strings.
143  *
144  * @param data_free_cb The function called on each value when the hash table
145  * is freed. @c NULL can be passed as callback.
146  * @return The new hash table.
147  *
148  * This function creates a new hash table using the superfast algorithm
149  * for table management and strcmp() to compare the keys. Values can
150  * then be looked up with pointers other than the original key pointer
151  * that was used to add values. On failure, this function returns
152  * @c NULL.
153  */
154 EAPI Eina_Hash *eina_hash_string_superfast_new(Eina_Free_Cb data_free_cb);
155
156 /**
157  * @brief Create a new hash table for use with strings with small bucket size.
158  *
159  * @param data_free_cb  The function called on each value when the hash table
160  * is freed. @c NULL can be passed as callback.
161  * @return  The new hash table.
162  *
163  * This function creates a new hash table using the superfast algorithm
164  * for table management and strcmp() to compare the keys, but with a
165  * smaller bucket size (compared to eina_hash_string_superfast_new())
166  * which will minimize the memory used by the returned hash
167  * table. Values can then be looked up with pointers other than the
168  * original key pointer that was used to add values. On failure, this
169  * function returns @c NULL.
170  */
171 EAPI Eina_Hash *eina_hash_string_small_new(Eina_Free_Cb data_free_cb);
172
173 /**
174  * @brief Create a new hash table for use with 32bit integers.
175  *
176  * @param data_free_cb  The function called on each value when the hash table
177  * is freed. @c NULL can be passed as callback.
178  * @return  The new hash table.
179  *
180  * This function creates a new hash table where keys are 32bit integers.
181  * When adding or looking up in the hash table, pointers to 32bit integers
182  * must be passed. They can be addresses on the stack if you let the
183  * eina_hash copy the key. Values can then
184  * be looked up with pointers other than the original key pointer that was
185  * used to add values. This method is not suitable to match string keys as
186  * it would only match the first character.
187  * On failure, this function returns @c NULL.
188  */
189 EAPI Eina_Hash *eina_hash_int32_new(Eina_Free_Cb data_free_cb);
190
191 /**
192  * @brief Create a new hash table for use with 64bit integers.
193  *
194  * @param data_free_cb  The function called on each value when the hash table
195  * is freed. @c NULL can be passed as callback.
196  * @return  The new hash table.
197  *
198  * This function creates a new hash table where keys are 64bit integers.
199  * When adding or looking up in the hash table, pointers to 64bit integers
200  * must be passed. They can be addresses on the stack. Values can then
201  * be looked up with pointers other than the original key pointer that was
202  * used to add values. This method is not suitable to match string keys as
203  * it would only match the first character.
204  * On failure, this function returns @c NULL.
205  */
206 EAPI Eina_Hash *eina_hash_int64_new(Eina_Free_Cb data_free_cb);
207
208 /**
209  * @brief Create a new hash table for use with pointers.
210  *
211  * @param data_free_cb  The function called on each value when the hash table
212  * is freed. @c NULL can be passed as callback.
213  * @return  The new hash table.
214  *
215  * This function creates a new hash table using the int64/int32 algorithm for
216  * table management and dereferenced pointers to compare the
217  * keys. Values can then be looked up with pointers other than the
218  * original key pointer that was used to add values. This method may
219  * appear to be able to match string keys, actually it only matches
220  * the first character. On failure, this function returns @c NULL.
221  */
222 EAPI Eina_Hash *eina_hash_pointer_new(Eina_Free_Cb data_free_cb);
223
224 /**
225  * @brief Create a new hash table optimized for stringshared values.
226  *
227  * @param data_free_cb  The function called on each value when the hash table
228  * is freed. @c NULL can be passed as callback.
229  * @return  The new hash table.
230  *
231  * This function creates a new hash table optimized for stringshared
232  * values. Values CAN NOT be looked up with pointers not
233  * equal to the original key pointer that was used to add a value. On failure,
234  * this function returns @c NULL.
235  *
236  * Excerpt of code that will NOT work with this type of hash:
237  *
238  * @code
239  * extern Eina_Hash *hash;
240  * extern const char *value;
241  * const char *a = eina_stringshare_add("key");
242  *
243  * eina_hash_add(hash, a, value);
244  * eina_hash_find(hash, "key")
245  * @endcode
246  */
247 EAPI Eina_Hash *eina_hash_stringshared_new(Eina_Free_Cb data_free_cb);
248
249 /**
250  * @brief Add an entry to the given hash table.
251  *
252  * @param hash The given hash table.
253  * @param key A unique key.
254  * @param data Data to associate with the string given by @p key.
255  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
256  *
257  * This function adds @p key to @p hash. @p hash, @p key and @p data
258  * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
259  * expected to be unique within the hash table. Key uniqueness varies
260  * depending on the type of @p hash: a stringshared @ref Eina_Hash
261  * need only have unique pointers for keys, but the strings in the
262  * pointers may be identical. All other hash types require the strings
263  * themselves to be unique. Failure to use sufficient uniqueness will
264  * result in unexpected results when inserting data pointers accessed
265  * with eina_hash_find(), and removed with eina_hash_del(). Key
266  * strings are case sensitive. If an error occurs, eina_error_get()
267  * should be used to determine if an allocation error occurred during
268  * this function. This function returns #EINA_FALSE if an error
269  * occurred, #EINA_TRUE otherwise.
270  */
271 EAPI Eina_Bool  eina_hash_add(Eina_Hash  *hash,
272                               const void *key,
273                               const void *data) EINA_ARG_NONNULL(1, 2, 3);
274
275 /**
276  * @brief Add an entry to the given hash table without duplicating the string
277  * key.
278  *
279  * @param hash The given hash table.  Can be @c NULL.
280  * @param key A unique key.  Can be @c NULL.
281  * @param data Data to associate with the string given by @p key.
282  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
283  *
284  * This function adds @p key to @p hash. @p hash, @p key and @p data
285  * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
286  * expected to be unique within the hash table. Key uniqueness varies
287  * depending on the type of @p hash: a stringshared @ref Eina_Hash
288  * need only have unique pointers for keys, but the strings in the
289  * pointers may be identical. All other hash types require the strings
290  * themselves to be unique. Failure to use sufficient uniqueness will
291  * result in unexpected results when inserting data pointers accessed
292  * with eina_hash_find(), and removed with eina_hash_del(). This
293  * function does not make a copy of @p key, so it must be a string
294  * constant or stored elsewhere ( in the object being added). Key
295  * strings are case sensitive. If an error occurs, eina_error_get()
296  * should be used to determine if an allocation error occurred during
297  * this function. This function returns #EINA_FALSE if an error
298  * occurred, #EINA_TRUE otherwise.
299  */
300 EAPI Eina_Bool eina_hash_direct_add(Eina_Hash  *hash,
301                                     const void *key,
302                                     const void *data) EINA_ARG_NONNULL(1, 2, 3);
303
304 /**
305  * @brief Remove the entry identified by a key or a data from the given
306  * hash table.
307  *
308  * @param hash The given hash table.
309  * @param key  The key.
310  * @param data The data pointer to remove if the key is @c NULL.
311  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
312  *
313  * This function removes the entry identified by @p key or @p data
314  * from @p hash. If a free function was given to the
315  * callback on creation, it will be called for the data being
316  * deleted. If @p hash is @c NULL, the functions returns immediately
317  * #EINA_FALSE. If @p key is @c NULL, then @p data is used to find the a
318  * match to remove, otherwise @p key is used and @p data is not
319  * required and can be @c NULL. This function returns #EINA_FALSE if
320  * an error occurred, #EINA_TRUE otherwise.
321  *
322  * @note if you know you already have the key, use
323  *       eina_hash_del_by_key() or eina_hash_del_by_key_hash(). If you
324  *       know you don't have the key, use eina_hash_del_by_data()
325  *       directly.
326  */
327 EAPI Eina_Bool eina_hash_del(Eina_Hash  *hash,
328                              const void *key,
329                              const void *data) EINA_ARG_NONNULL(1);
330
331 /**
332  * @brief Retrieve a specific entry in the given hash table.
333  *
334  * @param hash The given hash table.
335  * @param key The key of the entry to find.
336  * @return The data pointer for the stored entry on success, @c NULL
337  * otherwise.
338  *
339  * This function retrieves the entry associated to @p key in
340  * @p hash. If @p hash is @c NULL, this function returns immediately
341  * @c NULL. This function returns the data pointer on success, @c NULL
342  * otherwise.
343  */
344 EAPI void *eina_hash_find(const Eina_Hash *hash,
345                           const void      *key) EINA_ARG_NONNULL(1, 2);
346
347 /**
348  * @brief Modify the entry pointer at the specified key and return the old
349  * entry.
350  * @param hash The given hash table.
351  * @param key The key of the entry to modify.
352  * @param data The data to replace the old entry.
353  * @return The data pointer for the old stored entry on success, or
354  * @c NULL otherwise.
355  *
356  * This function modifies the data of @p key with @p data in @p
357  * hash. If no entry is found, nothing is added to @p hash. On success
358  * this function returns the old entry, otherwise it returns @c NULL.
359  */
360 EAPI void *eina_hash_modify(Eina_Hash  *hash,
361                             const void *key,
362                             const void *data) EINA_ARG_NONNULL(1, 2, 3);
363
364 /**
365  * @brief Modify the entry pointer at the specified key and return the
366  * old entry or add the entry if not found.
367  *
368  * @param hash The given hash table.
369  * @param key The key of the entry to modify.
370  * @param data The data to replace the old entry
371  * @return The data pointer for the old stored entry, or @c NULL
372  * otherwise.
373  *
374  * This function modifies the data of @p key with @p data in @p
375  * hash. If no entry is found, @p data is added to @p hash with the
376  * key @p key. On success this function returns the old entry,
377  * otherwise it returns @c NULL. To check for errors, use
378  * eina_error_get().
379  */
380 EAPI void *eina_hash_set(Eina_Hash  *hash,
381                          const void *key,
382                          const void *data) EINA_ARG_NONNULL(1, 2);
383
384 /**
385  * @brief Change the key associated with a data without triggering the
386  * free callback.
387  *
388  * @param hash    The given hash table.
389  * @param old_key The current key associated with the data
390  * @param new_key The new key to associate data with
391  * @return EINA_FALSE in any case but success, EINA_TRUE on success.
392  *
393  * This function allows for the move of data from one key to another,
394  * but does not call the Eina_Free_Cb associated with the hash table
395  * when destroying the old key.
396  */
397 EAPI Eina_Bool eina_hash_move(Eina_Hash  *hash,
398                               const void *old_key,
399                               const void *new_key) EINA_ARG_NONNULL(1, 2, 3);
400
401 /**
402  * Free the given hash table resources.
403  *
404  * @param hash The hash table to be freed.
405  *
406  * This function frees up all the memory allocated to storing @p hash,
407  * and call the free callback if it has been passed to the hash table
408  * at creation time. If no free callback has been passed, any entries
409  * in the table that the program has no more pointers for elsewhere
410  * may now be lost, so this should only be called if the program has
411  * already freed any allocated data in the hash table or has the
412  * pointers for data in the table stored elsewhere as well. If @p hash
413  * is @c NULL, the function returns immediately.
414  *
415  * Example:
416  * @code
417  * extern Eina_Hash *hash;
418  *
419  * eina_hash_free(hash);
420  * hash = NULL;
421  * @endcode
422  */
423 EAPI void      eina_hash_free(Eina_Hash *hash) EINA_ARG_NONNULL(1);
424
425 /**
426  * Free the given hash table buckets resources.
427  *
428  * @param hash The hash table whose buckets have to be freed.
429  *
430  * This function frees up all the memory allocated to storing the
431  * buckets of @p hash, and calls the free callback on all hash table
432  * buckets if it has been passed to the hash table at creation time,
433  * then frees the buckets. If no free callback has been passed, no
434  * buckets value will be freed. If @p hash is @c NULL, the function
435  * returns immediately.
436  */
437 EAPI void      eina_hash_free_buckets(Eina_Hash *hash) EINA_ARG_NONNULL(1);
438
439 /**
440  * @brief Returns the number of entries in the given hash table.
441  *
442  * @param hash The given hash table.
443  * @return The number of entries in the hash table.
444  *
445  * This function returns the number of entries in @p hash, or 0 on
446  * error. If @p hash is @c NULL, 0 is returned.
447  */
448 EAPI int       eina_hash_population(const Eina_Hash *hash) EINA_ARG_NONNULL(1);
449
450 /**
451  * @brief Add an entry to the given hash table.
452  *
453  * @param hash The given hash table.
454  * @param key A unique key.
455  * @param key_length The length of the key.
456  * @param key_hash The hash that will always match key.
457  * @param data The data to associate with the string given by the key.
458  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
459  *
460  * This function adds @p key to @p hash. @p hash, @p key and @p data
461  * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
462  * expected to be a unique string within the hash table. Otherwise,
463  * one cannot be sure which inserted data pointer will be accessed
464  * with @ref eina_hash_find, and removed with @ref eina_hash_del. Do
465  * not forget to count '\\0' for string when setting the value of
466  * @p key_length. @p key_hash is expected to always match
467  * @p key. Otherwise, one cannot be sure to find it again with @ref
468  * eina_hash_find_by_hash. Key strings are case sensitive. If an error
469  * occurs, eina_error_get() should be used to determine if an
470  * allocation error occurred during this function. This function
471  * returns #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
472  */
473 EAPI Eina_Bool eina_hash_add_by_hash(Eina_Hash  *hash,
474                                      const void *key,
475                                      int         key_length,
476                                      int         key_hash,
477                                      const void *data) EINA_ARG_NONNULL(1, 2, 5);
478
479 /**
480  * @brief Add an entry to the given hash table and do not duplicate the string
481  * key.
482  *
483  * @param hash The given hash table.  Can be @c NULL.
484  * @param key A unique key.  Can be @c NULL.
485  * @param key_length Should be the length of @p key (don't forget to count
486  * '\\0' for string).
487  * @param key_hash The hash that will always match key.
488  * @param data Data to associate with the string given by @p key.
489  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
490  *
491  * This function adds @p key to @p hash. @p hash, @p key and @p data
492  * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
493  * expected to be a unique string within the hash table. Otherwise,
494  * one cannot be sure which inserted data pointer will be accessed
495  * with @ref eina_hash_find, and removed with @ref eina_hash_del. This
496  * function does not make a copy of @p key so it must be a string
497  * constant or stored elsewhere (in the object being added). Do
498  * not forget to count '\\0' for string when setting the value of
499  * @p key_length. @p key_hash is expected to always match
500  * @p key. Otherwise, one cannot be sure to find it again with @ref
501  * eina_hash_find_by_hash. Key strings are case sensitive. If an error
502  * occurs, eina_error_get() should be used to determine if an
503  * allocation error occurred during this function. This function
504  * returns #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
505  */
506 EAPI Eina_Bool eina_hash_direct_add_by_hash(Eina_Hash  *hash,
507                                             const void *key,
508                                             int         key_length,
509                                             int         key_hash,
510                                             const void *data) EINA_ARG_NONNULL(1, 2, 5);
511
512 /**
513  * @brief Remove the entry identified by a key and a key hash from the given
514  * hash table.
515  *
516  * @param hash The given hash table.
517  * @param key The key.
518  * @param key_length The length of the key.
519  * @param key_hash The hash that always match the key.
520  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
521  *
522  * This function removes the entry identified by @p key and
523  * @p key_hash from @p hash. If a free function was given to the
524  * callback on creation, it will be called for the data being
525  * deleted. Do not forget to count '\\0' for string when setting the
526  * value of @p key_length. If @p hash or @p key are @c NULL, the
527  * functions returns immediately #EINA_FALSE. This function returns
528  * #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
529  *
530  * @note if you don't have the key_hash, use eina_hash_del_by_key() instead.
531  * @note if you don't have the key, use eina_hash_del_by_data() instead.
532  */
533 EAPI Eina_Bool eina_hash_del_by_key_hash(Eina_Hash  *hash,
534                                          const void *key,
535                                          int         key_length,
536                                          int         key_hash) EINA_ARG_NONNULL(1, 2);
537
538 /**
539  * @brief Remove the entry identified by a key from the given hash table.
540  *
541  * This version will calculate key length and hash by using functions
542  * provided to hash creation function.
543  *
544  * @param hash The given hash table.
545  * @param key  The key.
546  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
547  *
548  * This function removes the entry identified by @p key from @p
549  * hash. The key length and hash will be calculated automatically by
550  * using functiond provided to has creation function. If a free
551  * function was given to the callback on creation, it will be called
552  * for the data being deleted. If @p hash or @p key are @c NULL, the
553  * functions returns immediately #EINA_FALSE. This function returns
554  * #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
555  *
556  * @note if you already have the key_hash, use eina_hash_del_by_key_hash()
557  * instead.
558  * @note if you don't have the key, use eina_hash_del_by_data() instead.
559  */
560 EAPI Eina_Bool eina_hash_del_by_key(Eina_Hash  *hash,
561                                     const void *key) EINA_ARG_NONNULL(1, 2);
562
563 /**
564  * @brief Remove the entry identified by a data from the given hash table.
565  *
566  * This version is slow since there is no quick access to nodes based on data.
567  *
568  * @param hash The given hash table.
569  * @param data The data value to search and remove.
570  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
571  *          thing goes fine.
572  *
573  * This function removes the entry identified by @p data from @p
574  * hash. If a free function was given to the callback on creation, it
575  * will be called for the data being deleted. If @p hash or @p data
576  * are @c NULL, the functions returns immediately #EINA_FALSE. This
577  * function returns #EINA_FALSE if an error occurred, #EINA_TRUE
578  * otherwise.
579  *
580  * @note if you already have the key, use eina_hash_del_by_key() or
581  * eina_hash_del_by_key_hash() instead.
582  */
583 EAPI Eina_Bool eina_hash_del_by_data(Eina_Hash  *hash,
584                                      const void *data) EINA_ARG_NONNULL(1, 2);
585
586 /**
587  * @brief Remove the entry identified by a key and a key hash or a
588  * data from the given hash table.
589  *
590  * If @p key is @c NULL, then @p data is used to find a match to
591  * remove.
592  *
593  * @param hash The given hash table.
594  * @param key The key.
595  * @param key_length The length of the key.
596  * @param key_hash The hash that always match the key.
597  * @param data The data pointer to remove if the key is @c NULL.
598  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
599  *
600  * This function removes the entry identified by @p key and
601  * @p key_hash, or @p data, from @p hash. If a free function was given to
602  * the  callback on creation, it will be called for the data being
603  * deleted. If @p hash is @c NULL, the functions returns immediately
604  * #EINA_FALSE. If @p key is @c NULL, then @p key_hash and @p key_hash
605  * are ignored and @p data is used to find a match to remove,
606  * otherwise @p key and @p key_hash are used and @p data is not
607  * required and can be @c NULL. Do not forget to count '\\0' for
608  * string when setting the value of @p key_length. This function
609  * returns #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
610  *
611  * @note if you know you already have the key, use eina_hash_del_by_key_hash(),
612  *       if you know you don't have the key, use eina_hash_del_by_data()
613  *       directly.
614  */
615 EAPI Eina_Bool eina_hash_del_by_hash(Eina_Hash  *hash,
616                                      const void *key,
617                                      int         key_length,
618                                      int         key_hash,
619                                      const void *data) EINA_ARG_NONNULL(1);
620
621 /**
622  * @brief Retrieve a specific entry in the given hash table.
623  *
624  * @param hash The given hash table.
625  * @param key The key of the entry to find.
626  * @param key_length The length of the key.
627  * @param key_hash The hash that always match the key
628  * @return The data pointer for the stored entry on success, @c NULL
629  * otherwise.
630  *
631  * This function retrieves the entry associated to @p key of length
632  * @p key_length in @p hash. @p key_hash is the hash that always match
633  * @p key. It is ignored if @p key is @c NULL. Do not forget to count
634  * '\\0' for string when setting the value of @p key_length. If
635  * @p hash is @c NULL, this function returns immediately @c NULL. This
636  * function returns the data pointer on success, @c NULL otherwise.
637  */
638 EAPI void *eina_hash_find_by_hash(const Eina_Hash *hash,
639                                   const void      *key,
640                                   int              key_length,
641                                   int              key_hash) EINA_ARG_NONNULL(1, 2);
642
643 /**
644  * @brief Modify the entry pointer at the specified key and returns
645  * the old entry.
646  *
647  * @param hash The given hash table.
648  * @param key The key of the entry to modify.
649  * @param key_length Should be the length of @p key (don't forget to count
650  * '\\0' for string).
651  * @param key_hash The hash that always match the key. Ignored if @p key is
652  *                 @c NULL.
653  * @param data The data to replace the old entry, if it exists.
654  * @return The data pointer for the old stored entry, or @c NULL if not
655  *          found. If an existing entry is not found, nothing is added to the
656  *          hash.
657  */
658 EAPI void *eina_hash_modify_by_hash(Eina_Hash  *hash,
659                                     const void *key,
660                                     int         key_length,
661                                     int         key_hash,
662                                     const void *data) EINA_ARG_NONNULL(1, 2, 5);
663
664 /**
665  * @brief Returned a new iterator associated to hash keys.
666  *
667  * @param hash The hash.
668  * @return A new iterator.
669  *
670  * This function returns a newly allocated iterator associated to @p
671  * hash. If @p hash is not populated, this function still returns a
672  * valid iterator that will always return false on
673  * eina_iterator_next(), thus keeping API sane.
674  *
675  * If the memory can not be allocated, NULL is returned and
676  * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
677  * returned.
678  *
679  * @warning if the hash structure changes then the iterator becomes
680  *    invalid! That is, if you add or remove items this iterator
681  *    behavior is undefined and your program may crash!
682  */
683 EAPI Eina_Iterator *eina_hash_iterator_key_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
684
685 /**
686  * @brief Returned a new iterator associated to hash data.
687  *
688  * @param hash The hash.
689  * @return A new iterator.
690  *
691  * This function returns a newly allocated iterator associated to
692  * @p hash. If @p hash is not populated, this function still returns a
693  * valid iterator that will always return false on
694  * eina_iterator_next(), thus keeping API sane.
695  *
696  * If the memory can not be allocated, @c NULL is returned and
697  * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
698  * returned.
699  *
700  * @warning if the hash structure changes then the iterator becomes
701  * invalid. That is, if you add or remove items this iterator behavior
702  * is undefined and your program may crash.
703  */
704 EAPI Eina_Iterator *eina_hash_iterator_data_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
705
706 /**
707  * @brief Returned a new iterator associated to hash keys and data.
708  *
709  * @param hash The hash.
710  * @return A new iterator.
711  *
712  * This function returns a newly allocated iterator associated to @p
713  * hash. If @p hash is not populated, this function still returns a
714  * valid iterator that will always return false on
715  * eina_iterator_next(), thus keeping API sane.
716  *
717  * If the memory can not be allocated, NULL is returned and
718  * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
719  * returned.
720  *
721  * @note iterator data will provide values as Eina_Hash_Tuple that should not
722  *   be modified!
723  *
724  * @warning if the hash structure changes then the iterator becomes
725  *    invalid! That is, if you add or remove items this iterator
726  *    behavior is undefined and your program may crash!
727  */
728 EAPI Eina_Iterator *eina_hash_iterator_tuple_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
729
730 /**
731  * @brief Call a function on every member stored in the hash table
732  *
733  * @param hash The hash table whose members will be walked
734  * @param func The function to call on each parameter
735  * @param fdata The data pointer to pass to the function being called
736  *
737  * This function goes through every entry in the hash table @p hash and calls
738  * the function @p func on each member. The function should @b not modify the
739  * hash table contents if it returns 1. @b If the hash table contents are
740  * modified by this function or the function wishes to stop processing it must
741  * return 0, otherwise return 1 to keep processing.
742  *
743  * Example:
744  * @code
745  * extern Eina_Hash *hash;
746  *
747  * Eina_Bool hash_fn(const Eina_Hash *hash, const void *key,
748  *                   void *data, void *fdata)
749  * {
750  *   printf("Func data: %s, Hash entry: %s / %p\n",
751  *          fdata, (const char *)key, data);
752  *   return 1;
753  * }
754  *
755  * int main(int argc, char **argv)
756  * {
757  *   char *hash_fn_data;
758  *
759  *   hash_fn_data = strdup("Hello World");
760  *   eina_hash_foreach(hash, hash_fn, hash_fn_data);
761  *   free(hash_fn_data);
762  * }
763  * @endcode
764  */
765 EAPI void           eina_hash_foreach(const Eina_Hash  *hash,
766                                       Eina_Hash_Foreach cb,
767                                       const void       *fdata) EINA_ARG_NONNULL(1, 2);
768 /* Paul Hsieh (http://www.azillionmonkeys.com/qed/hash.html) hash function used by WebCore (http://webkit.org/blog/8/hashtables-part-2/) */
769 EAPI int eina_hash_superfast(const char *key,
770                              int         len) EINA_ARG_NONNULL(1);
771 /* Hash function first reported by dan bernstein many years ago in comp.lang.c */
772 static inline int eina_hash_djb2(const char *key,
773                                  int         len) EINA_ARG_NONNULL(1);
774 static inline int eina_hash_djb2_len(const char *key,
775                                      int        *plen) EINA_ARG_NONNULL(1, 2);
776 /* Hash function from http://www.concentric.net/~Ttwang/tech/inthash.htm */
777 static inline int eina_hash_int32(const unsigned int *pkey,
778                                   int                 len) EINA_ARG_NONNULL(1);
779 static inline int eina_hash_int64(const unsigned long int *pkey,
780                                   int                      len) EINA_ARG_NONNULL(1);
781
782 #include "eina_inline_hash.x"
783
784 /**
785  * @}
786  */
787
788 /**
789  * @}
790  */
791
792 /**
793  * @}
794  */
795
796 /**
797  * @}
798  */
799
800 #endif /*EINA_HASH_H_*/