[eina] Add high-level documentation and examples for Eina_Hash.
[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.jpg
150  *
151  * Adding an element to the hash table is made of:
152  * @li calculating the hash for that key (using the specified hash function);
153  * @li calculate the array position [hash mod N];
154  * @li add the element to the rbtree on that position.
155  *
156  * The two first steps have constant time, proportional to the hash function
157  * being used. Adding the key to the rbtree will be proportional on the number
158  * of keys on that bucket.
159  *
160  * The average cost of lookup depends on the number of keys per
161  * bucket (load factor) of the table, if the distribution of keys is
162  * sufficiently uniform.
163  *
164  * @section hashtable_perf Performance
165  *
166  * As said before, the performance depends on the load factor. So trying to keep
167  * it as small as possible will improve the hash table performance. But
168  * increasing the buckets_power_size will also increase the memory consumption.
169  * The default hash table creation functions already have a good number of
170  * buckets, enough for most cases. Particularly for strings, if just a few keys
171  * (less than 30) will be added to the hash table, @ref
172  * eina_hash_string_small_new should be used. If just stringshared keys are
173  * being added, use @ref eina_hash_stringshared_new. If a lot of keys will be
174  * added to the hash table (e.g. more than 1000), then it's better to increase
175  * the buckets_power_size. See @ref eina_hash_new for more details.
176  *
177  * When adding a new key to a hash table, use @ref eina_hash_add or @ref
178  * eina_hash_direct_add (the latter if this key is already stored elsewhere). If
179  * the key may be already inside the hash table, instead of checking with
180  * @ref eina_hash_find and then doing @ref eina_hash_add, one can use just @ref
181  * eina_hash_set (this will change the data pointed by this key if it was
182  * already present in the table).
183  *
184  * @section hashtable_tutorial Tutorial
185  *
186  * These examples show many Eina_Hash functions in action:
187  * @li @ref hash_01_example_page
188  * @li @ref hash_02_example_page
189  *
190  * @{
191  */
192
193 /**
194  * @addtogroup Eina_Data_Types_Group Data Types
195  *
196  * @{
197  */
198
199 /**
200  * @addtogroup Eina_Containers_Group Containers
201  *
202  * @{
203  */
204
205 /**
206  * @defgroup Eina_Hash_Group Hash Table
207  *
208  * @{
209  */
210
211 /**
212  * @typedef Eina_Hash
213  * Type for a generic hash table.
214  */
215 typedef struct _Eina_Hash       Eina_Hash;
216
217 typedef struct _Eina_Hash_Tuple Eina_Hash_Tuple;
218
219 struct _Eina_Hash_Tuple
220 {
221    const void  *key; /**< The key */
222    void        *data; /**< The data associated to the key */
223    unsigned int key_length; /**< The length of the key */
224 };
225
226 typedef unsigned int (*Eina_Key_Length)(const void *key);
227 #define EINA_KEY_LENGTH(Function) ((Eina_Key_Length)Function)
228 typedef int          (*Eina_Key_Cmp)(const void *key1, int key1_length, const void *key2, int key2_length);
229 #define EINA_KEY_CMP(Function)    ((Eina_Key_Cmp)Function)
230 typedef int          (*Eina_Key_Hash)(const void *key, int key_length);
231 #define EINA_KEY_HASH(Function)   ((Eina_Key_Hash)Function)
232 typedef Eina_Bool    (*Eina_Hash_Foreach)(const Eina_Hash *hash, const void *key, void *data, void *fdata);
233
234
235 /**
236  * @brief Create a new hash table.
237  *
238  * @param key_length_cb The function called when getting the size of the key.
239  * @param key_cmp_cb The function called when comparing the keys.
240  * @param key_hash_cb The function called when getting the values.
241  * @param data_free_cb The function called on each value when the hash
242  * table is freed. @c NULL can be passed as callback.
243  * @param buckets_power_size The size of the buckets.
244  * @return The new hash table.
245  *
246  * This function creates a new hash table using user-defined callbacks
247  * to manage the hash table. On failure, @c NULL is returned and
248  * #EINA_ERROR_OUT_OF_MEMORY is set. If @p key_cmp_cb or @p key_hash_cb
249  * are @c NULL, @c NULL is returned. If @p buckets_power_size is
250  * smaller or equal than 2, or if it is greater or equal than 17,
251  * @c NULL is returned.
252  *
253  * Pre-defined functions are available to create a hash table. See
254  * eina_hash_string_djb2_new(), eina_hash_string_superfast_new(),
255  * eina_hash_string_small_new(), eina_hash_int32_new(),
256  * eina_hash_int64_new(), eina_hash_pointer_new() and
257  * eina_hash_stringshared_new().
258  */
259 EAPI Eina_Hash *eina_hash_new(Eina_Key_Length key_length_cb,
260                               Eina_Key_Cmp    key_cmp_cb,
261                               Eina_Key_Hash   key_hash_cb,
262                               Eina_Free_Cb    data_free_cb,
263                               int             buckets_power_size) EINA_MALLOC EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(2, 3);
264
265 /**
266  * @brief Create a new hash table using the djb2 algorithm.
267  *
268  * @param data_free_cb The function called on each value when the hash table
269  * is freed. @c NULL can be passed as callback.
270  * @return The new hash table.
271  *
272  * This function creates a new hash table using the djb2 algorithm for
273  * table management and strcmp() to compare the keys. Values can then
274  * be looked up with pointers other than the original key pointer that
275  * was used to add values. On failure, this function returns @c NULL.
276  */
277 EAPI Eina_Hash *eina_hash_string_djb2_new(Eina_Free_Cb data_free_cb);
278
279 /**
280  * @brief Create a new hash table for use with strings.
281  *
282  * @param data_free_cb The function called on each value when the hash table
283  * is freed. @c NULL can be passed as callback.
284  * @return The new hash table.
285  *
286  * This function creates a new hash table using the superfast algorithm
287  * for table management and strcmp() to compare the keys. Values can
288  * then be looked up with pointers other than the original key pointer
289  * that was used to add values. On failure, this function returns
290  * @c NULL.
291  */
292 EAPI Eina_Hash *eina_hash_string_superfast_new(Eina_Free_Cb data_free_cb);
293
294 /**
295  * @brief Create a new hash table for use with strings with small bucket size.
296  *
297  * @param data_free_cb  The function called on each value when the hash table
298  * is freed. @c NULL can be passed as callback.
299  * @return  The new hash table.
300  *
301  * This function creates a new hash table using the superfast algorithm
302  * for table management and strcmp() to compare the keys, but with a
303  * smaller bucket size (compared to eina_hash_string_superfast_new())
304  * which will minimize the memory used by the returned hash
305  * table. Values can then be looked up with pointers other than the
306  * original key pointer that was used to add values. On failure, this
307  * function returns @c NULL.
308  */
309 EAPI Eina_Hash *eina_hash_string_small_new(Eina_Free_Cb data_free_cb);
310
311 /**
312  * @brief Create a new hash table for use with 32bit integers.
313  *
314  * @param data_free_cb  The function called on each value when the hash table
315  * is freed. @c NULL can be passed as callback.
316  * @return  The new hash table.
317  *
318  * This function creates a new hash table where keys are 32bit integers.
319  * When adding or looking up in the hash table, pointers to 32bit integers
320  * must be passed. They can be addresses on the stack if you let the
321  * eina_hash copy the key. Values can then
322  * be looked up with pointers other than the original key pointer that was
323  * used to add values. This method is not suitable to match string keys as
324  * it would only match the first character.
325  * On failure, this function returns @c NULL.
326  */
327 EAPI Eina_Hash *eina_hash_int32_new(Eina_Free_Cb data_free_cb);
328
329 /**
330  * @brief Create a new hash table for use with 64bit integers.
331  *
332  * @param data_free_cb  The function called on each value when the hash table
333  * is freed. @c NULL can be passed as callback.
334  * @return  The new hash table.
335  *
336  * This function creates a new hash table where keys are 64bit integers.
337  * When adding or looking up in the hash table, pointers to 64bit integers
338  * must be passed. They can be addresses on the stack. Values can then
339  * be looked up with pointers other than the original key pointer that was
340  * used to add values. This method is not suitable to match string keys as
341  * it would only match the first character.
342  * On failure, this function returns @c NULL.
343  */
344 EAPI Eina_Hash *eina_hash_int64_new(Eina_Free_Cb data_free_cb);
345
346 /**
347  * @brief Create a new hash table for use with pointers.
348  *
349  * @param data_free_cb  The function called on each value when the hash table
350  * is freed. @c NULL can be passed as callback.
351  * @return  The new hash table.
352  *
353  * This function creates a new hash table using the int64/int32 algorithm for
354  * table management and dereferenced pointers to compare the
355  * keys. Values can then be looked up with pointers other than the
356  * original key pointer that was used to add values. This method may
357  * appear to be able to match string keys, actually it only matches
358  * the first character. On failure, this function returns @c NULL.
359  */
360 EAPI Eina_Hash *eina_hash_pointer_new(Eina_Free_Cb data_free_cb);
361
362 /**
363  * @brief Create a new hash table optimized for stringshared values.
364  *
365  * @param data_free_cb  The function called on each value when the hash table
366  * is freed. @c NULL can be passed as callback.
367  * @return  The new hash table.
368  *
369  * This function creates a new hash table optimized for stringshared
370  * values. Values CAN NOT be looked up with pointers not
371  * equal to the original key pointer that was used to add a value. On failure,
372  * this function returns @c NULL.
373  *
374  * Excerpt of code that will NOT work with this type of hash:
375  *
376  * @code
377  * extern Eina_Hash *hash;
378  * extern const char *value;
379  * const char *a = eina_stringshare_add("key");
380  *
381  * eina_hash_add(hash, a, value);
382  * eina_hash_find(hash, "key")
383  * @endcode
384  */
385 EAPI Eina_Hash *eina_hash_stringshared_new(Eina_Free_Cb data_free_cb);
386
387 /**
388  * @brief Add an entry to the given hash table.
389  *
390  * @param hash The given hash table.
391  * @param key A unique key.
392  * @param data Data to associate with the string given by @p key.
393  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
394  *
395  * This function adds @p key to @p hash. @p hash, @p key and @p data
396  * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
397  * expected to be unique within the hash table. Key uniqueness varies
398  * depending on the type of @p hash: a stringshared @ref Eina_Hash
399  * need only have unique pointers for keys, but the strings in the
400  * pointers may be identical. All other hash types require the strings
401  * themselves to be unique. Failure to use sufficient uniqueness will
402  * result in unexpected results when inserting data pointers accessed
403  * with eina_hash_find(), and removed with eina_hash_del(). Key
404  * strings are case sensitive. If an error occurs, eina_error_get()
405  * should be used to determine if an allocation error occurred during
406  * this function. This function returns #EINA_FALSE if an error
407  * occurred, #EINA_TRUE otherwise.
408  */
409 EAPI Eina_Bool  eina_hash_add(Eina_Hash  *hash,
410                               const void *key,
411                               const void *data) EINA_ARG_NONNULL(1, 2, 3);
412
413 /**
414  * @brief Add an entry to the given hash table without duplicating the string
415  * key.
416  *
417  * @param hash The given hash table.  Can be @c NULL.
418  * @param key A unique key.  Can be @c NULL.
419  * @param data Data to associate with the string given by @p key.
420  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
421  *
422  * This function adds @p key to @p hash. @p hash, @p key and @p data
423  * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
424  * expected to be unique within the hash table. Key uniqueness varies
425  * depending on the type of @p hash: a stringshared @ref Eina_Hash
426  * need only have unique pointers for keys, but the strings in the
427  * pointers may be identical. All other hash types require the strings
428  * themselves to be unique. Failure to use sufficient uniqueness will
429  * result in unexpected results when inserting data pointers accessed
430  * with eina_hash_find(), and removed with eina_hash_del(). This
431  * function does not make a copy of @p key, so it must be a string
432  * constant or stored elsewhere ( in the object being added). Key
433  * strings are case sensitive. If an error occurs, eina_error_get()
434  * should be used to determine if an allocation error occurred during
435  * this function. This function returns #EINA_FALSE if an error
436  * occurred, #EINA_TRUE otherwise.
437  */
438 EAPI Eina_Bool eina_hash_direct_add(Eina_Hash  *hash,
439                                     const void *key,
440                                     const void *data) EINA_ARG_NONNULL(1, 2, 3);
441
442 /**
443  * @brief Remove the entry identified by a key or a data from the given
444  * hash table.
445  *
446  * @param hash The given hash table.
447  * @param key  The key.
448  * @param data The data pointer to remove if the key is @c NULL.
449  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
450  *
451  * This function removes the entry identified by @p key or @p data
452  * from @p hash. If a free function was given to the
453  * callback on creation, it will be called for the data being
454  * deleted. If @p hash is @c NULL, the functions returns immediately
455  * #EINA_FALSE. If @p key is @c NULL, then @p data is used to find the a
456  * match to remove, otherwise @p key is used and @p data is not
457  * required and can be @c NULL. This function returns #EINA_FALSE if
458  * an error occurred, #EINA_TRUE otherwise.
459  *
460  * @note if you know you already have the key, use
461  *       eina_hash_del_by_key() or eina_hash_del_by_key_hash(). If you
462  *       know you don't have the key, use eina_hash_del_by_data()
463  *       directly.
464  */
465 EAPI Eina_Bool eina_hash_del(Eina_Hash  *hash,
466                              const void *key,
467                              const void *data) EINA_ARG_NONNULL(1);
468
469 /**
470  * @brief Retrieve a specific entry in the given hash table.
471  *
472  * @param hash The given hash table.
473  * @param key The key of the entry to find.
474  * @return The data pointer for the stored entry on success, @c NULL
475  * otherwise.
476  *
477  * This function retrieves the entry associated to @p key in
478  * @p hash. If @p hash is @c NULL, this function returns immediately
479  * @c NULL. This function returns the data pointer on success, @c NULL
480  * otherwise.
481  */
482 EAPI void *eina_hash_find(const Eina_Hash *hash,
483                           const void      *key) EINA_ARG_NONNULL(1, 2);
484
485 /**
486  * @brief Modify the entry pointer at the specified key and return the old
487  * entry.
488  * @param hash The given hash table.
489  * @param key The key of the entry to modify.
490  * @param data The data to replace the old entry.
491  * @return The data pointer for the old stored entry on success, or
492  * @c NULL otherwise.
493  *
494  * This function modifies the data of @p key with @p data in @p
495  * hash. If no entry is found, nothing is added to @p hash. On success
496  * this function returns the old entry, otherwise it returns @c NULL.
497  */
498 EAPI void *eina_hash_modify(Eina_Hash  *hash,
499                             const void *key,
500                             const void *data) EINA_ARG_NONNULL(1, 2, 3);
501
502 /**
503  * @brief Modify the entry pointer at the specified key and return the
504  * old entry or add the entry if not found.
505  *
506  * @param hash The given hash table.
507  * @param key The key of the entry to modify.
508  * @param data The data to replace the old entry
509  * @return The data pointer for the old stored entry, or @c NULL
510  * otherwise.
511  *
512  * This function modifies the data of @p key with @p data in @p
513  * hash. If no entry is found, @p data is added to @p hash with the
514  * key @p key. On success this function returns the old entry,
515  * otherwise it returns @c NULL. To check for errors, use
516  * eina_error_get().
517  */
518 EAPI void *eina_hash_set(Eina_Hash  *hash,
519                          const void *key,
520                          const void *data) EINA_ARG_NONNULL(1, 2);
521
522 /**
523  * @brief Change the key associated with a data without triggering the
524  * free callback.
525  *
526  * @param hash    The given hash table.
527  * @param old_key The current key associated with the data
528  * @param new_key The new key to associate data with
529  * @return EINA_FALSE in any case but success, EINA_TRUE on success.
530  *
531  * This function allows for the move of data from one key to another,
532  * but does not call the Eina_Free_Cb associated with the hash table
533  * when destroying the old key.
534  */
535 EAPI Eina_Bool eina_hash_move(Eina_Hash  *hash,
536                               const void *old_key,
537                               const void *new_key) EINA_ARG_NONNULL(1, 2, 3);
538
539 /**
540  * Free the given hash table resources.
541  *
542  * @param hash The hash table to be freed.
543  *
544  * This function frees up all the memory allocated to storing @p hash,
545  * and call the free callback if it has been passed to the hash table
546  * at creation time. If no free callback has been passed, any entries
547  * in the table that the program has no more pointers for elsewhere
548  * may now be lost, so this should only be called if the program has
549  * already freed any allocated data in the hash table or has the
550  * pointers for data in the table stored elsewhere as well. If @p hash
551  * is @c NULL, the function returns immediately.
552  *
553  * Example:
554  * @code
555  * extern Eina_Hash *hash;
556  *
557  * eina_hash_free(hash);
558  * hash = NULL;
559  * @endcode
560  */
561 EAPI void      eina_hash_free(Eina_Hash *hash) EINA_ARG_NONNULL(1);
562
563 /**
564  * Free the given hash table buckets resources.
565  *
566  * @param hash The hash table whose buckets have to be freed.
567  *
568  * This function frees up all the memory allocated to storing the
569  * buckets of @p hash, and calls the free callback on all hash table
570  * buckets if it has been passed to the hash table at creation time,
571  * then frees the buckets. If no free callback has been passed, no
572  * buckets value will be freed. If @p hash is @c NULL, the function
573  * returns immediately.
574  */
575 EAPI void      eina_hash_free_buckets(Eina_Hash *hash) EINA_ARG_NONNULL(1);
576
577 /**
578  * @brief Returns the number of entries in the given hash table.
579  *
580  * @param hash The given hash table.
581  * @return The number of entries in the hash table.
582  *
583  * This function returns the number of entries in @p hash, or 0 on
584  * error. If @p hash is @c NULL, 0 is returned.
585  */
586 EAPI int       eina_hash_population(const Eina_Hash *hash) EINA_ARG_NONNULL(1);
587
588 /**
589  * @brief Add an entry to the given hash table.
590  *
591  * @param hash The given hash table.
592  * @param key A unique key.
593  * @param key_length The length of the key.
594  * @param key_hash The hash that will always match key.
595  * @param data The data to associate with the string given by the key.
596  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
597  *
598  * This function adds @p key to @p hash. @p hash, @p key and @p data
599  * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
600  * expected to be a unique string within the hash table. Otherwise,
601  * one cannot be sure which inserted data pointer will be accessed
602  * with @ref eina_hash_find, and removed with @ref eina_hash_del. Do
603  * not forget to count '\\0' for string when setting the value of
604  * @p key_length. @p key_hash is expected to always match
605  * @p key. Otherwise, one cannot be sure to find it again with @ref
606  * eina_hash_find_by_hash. Key strings are case sensitive. If an error
607  * occurs, eina_error_get() should be used to determine if an
608  * allocation error occurred during this function. This function
609  * returns #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
610  */
611 EAPI Eina_Bool eina_hash_add_by_hash(Eina_Hash  *hash,
612                                      const void *key,
613                                      int         key_length,
614                                      int         key_hash,
615                                      const void *data) EINA_ARG_NONNULL(1, 2, 5);
616
617 /**
618  * @brief Add an entry to the given hash table and do not duplicate the string
619  * key.
620  *
621  * @param hash The given hash table.  Can be @c NULL.
622  * @param key A unique key.  Can be @c NULL.
623  * @param key_length Should be the length of @p key (don't forget to count
624  * '\\0' for string).
625  * @param key_hash The hash that will always match key.
626  * @param data Data to associate with the string given by @p key.
627  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
628  *
629  * This function adds @p key to @p hash. @p hash, @p key and @p data
630  * can be @c NULL, in that case #EINA_FALSE is returned. @p key is
631  * expected to be a unique string within the hash table. Otherwise,
632  * one cannot be sure which inserted data pointer will be accessed
633  * with @ref eina_hash_find, and removed with @ref eina_hash_del. This
634  * function does not make a copy of @p key so it must be a string
635  * constant or stored elsewhere (in the object being added). Do
636  * not forget to count '\\0' for string when setting the value of
637  * @p key_length. @p key_hash is expected to always match
638  * @p key. Otherwise, one cannot be sure to find it again with @ref
639  * eina_hash_find_by_hash. Key strings are case sensitive. If an error
640  * occurs, eina_error_get() should be used to determine if an
641  * allocation error occurred during this function. This function
642  * returns #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
643  */
644 EAPI Eina_Bool eina_hash_direct_add_by_hash(Eina_Hash  *hash,
645                                             const void *key,
646                                             int         key_length,
647                                             int         key_hash,
648                                             const void *data) EINA_ARG_NONNULL(1, 2, 5);
649
650 /**
651  * @brief Remove the entry identified by a key and a key hash from the given
652  * hash table.
653  *
654  * @param hash The given hash table.
655  * @param key The key.
656  * @param key_length The length of the key.
657  * @param key_hash The hash that always match the key.
658  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
659  *
660  * This function removes the entry identified by @p key and
661  * @p key_hash from @p hash. If a free function was given to the
662  * callback on creation, it will be called for the data being
663  * deleted. Do not forget to count '\\0' for string when setting the
664  * value of @p key_length. If @p hash or @p key are @c NULL, the
665  * functions returns immediately #EINA_FALSE. This function returns
666  * #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
667  *
668  * @note if you don't have the key_hash, use eina_hash_del_by_key() instead.
669  * @note if you don't have the key, use eina_hash_del_by_data() instead.
670  */
671 EAPI Eina_Bool eina_hash_del_by_key_hash(Eina_Hash  *hash,
672                                          const void *key,
673                                          int         key_length,
674                                          int         key_hash) EINA_ARG_NONNULL(1, 2);
675
676 /**
677  * @brief Remove the entry identified by a key from the given hash table.
678  *
679  * This version will calculate key length and hash by using functions
680  * provided to hash creation function.
681  *
682  * @param hash The given hash table.
683  * @param key  The key.
684  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
685  *
686  * This function removes the entry identified by @p key from @p
687  * hash. The key length and hash will be calculated automatically by
688  * using functiond provided to has creation function. If a free
689  * function was given to the callback on creation, it will be called
690  * for the data being deleted. If @p hash or @p key are @c NULL, the
691  * functions returns immediately #EINA_FALSE. This function returns
692  * #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
693  *
694  * @note if you already have the key_hash, use eina_hash_del_by_key_hash()
695  * instead.
696  * @note if you don't have the key, use eina_hash_del_by_data() instead.
697  */
698 EAPI Eina_Bool eina_hash_del_by_key(Eina_Hash  *hash,
699                                     const void *key) EINA_ARG_NONNULL(1, 2);
700
701 /**
702  * @brief Remove the entry identified by a data from the given hash table.
703  *
704  * This version is slow since there is no quick access to nodes based on data.
705  *
706  * @param hash The given hash table.
707  * @param data The data value to search and remove.
708  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
709  *          thing goes fine.
710  *
711  * This function removes the entry identified by @p data from @p
712  * hash. If a free function was given to the callback on creation, it
713  * will be called for the data being deleted. If @p hash or @p data
714  * are @c NULL, the functions returns immediately #EINA_FALSE. This
715  * function returns #EINA_FALSE if an error occurred, #EINA_TRUE
716  * otherwise.
717  *
718  * @note if you already have the key, use eina_hash_del_by_key() or
719  * eina_hash_del_by_key_hash() instead.
720  */
721 EAPI Eina_Bool eina_hash_del_by_data(Eina_Hash  *hash,
722                                      const void *data) EINA_ARG_NONNULL(1, 2);
723
724 /**
725  * @brief Remove the entry identified by a key and a key hash or a
726  * data from the given hash table.
727  *
728  * If @p key is @c NULL, then @p data is used to find a match to
729  * remove.
730  *
731  * @param hash The given hash table.
732  * @param key The key.
733  * @param key_length The length of the key.
734  * @param key_hash The hash that always match the key.
735  * @param data The data pointer to remove if the key is @c NULL.
736  * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
737  *
738  * This function removes the entry identified by @p key and
739  * @p key_hash, or @p data, from @p hash. If a free function was given to
740  * the  callback on creation, it will be called for the data being
741  * deleted. If @p hash is @c NULL, the functions returns immediately
742  * #EINA_FALSE. If @p key is @c NULL, then @p key_hash and @p key_hash
743  * are ignored and @p data is used to find a match to remove,
744  * otherwise @p key and @p key_hash are used and @p data is not
745  * required and can be @c NULL. Do not forget to count '\\0' for
746  * string when setting the value of @p key_length. This function
747  * returns #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
748  *
749  * @note if you know you already have the key, use eina_hash_del_by_key_hash(),
750  *       if you know you don't have the key, use eina_hash_del_by_data()
751  *       directly.
752  */
753 EAPI Eina_Bool eina_hash_del_by_hash(Eina_Hash  *hash,
754                                      const void *key,
755                                      int         key_length,
756                                      int         key_hash,
757                                      const void *data) EINA_ARG_NONNULL(1);
758
759 /**
760  * @brief Retrieve a specific entry in the given hash table.
761  *
762  * @param hash The given hash table.
763  * @param key The key of the entry to find.
764  * @param key_length The length of the key.
765  * @param key_hash The hash that always match the key
766  * @return The data pointer for the stored entry on success, @c NULL
767  * otherwise.
768  *
769  * This function retrieves the entry associated to @p key of length
770  * @p key_length in @p hash. @p key_hash is the hash that always match
771  * @p key. It is ignored if @p key is @c NULL. Do not forget to count
772  * '\\0' for string when setting the value of @p key_length. If
773  * @p hash is @c NULL, this function returns immediately @c NULL. This
774  * function returns the data pointer on success, @c NULL otherwise.
775  */
776 EAPI void *eina_hash_find_by_hash(const Eina_Hash *hash,
777                                   const void      *key,
778                                   int              key_length,
779                                   int              key_hash) EINA_ARG_NONNULL(1, 2);
780
781 /**
782  * @brief Modify the entry pointer at the specified key and returns
783  * the old entry.
784  *
785  * @param hash The given hash table.
786  * @param key The key of the entry to modify.
787  * @param key_length Should be the length of @p key (don't forget to count
788  * '\\0' for string).
789  * @param key_hash The hash that always match the key. Ignored if @p key is
790  *                 @c NULL.
791  * @param data The data to replace the old entry, if it exists.
792  * @return The data pointer for the old stored entry, or @c NULL if not
793  *          found. If an existing entry is not found, nothing is added to the
794  *          hash.
795  */
796 EAPI void *eina_hash_modify_by_hash(Eina_Hash  *hash,
797                                     const void *key,
798                                     int         key_length,
799                                     int         key_hash,
800                                     const void *data) EINA_ARG_NONNULL(1, 2, 5);
801
802 /**
803  * @brief Returned a new iterator associated to hash keys.
804  *
805  * @param hash The hash.
806  * @return A new iterator.
807  *
808  * This function returns a newly allocated iterator associated to @p
809  * hash. If @p hash is not populated, this function still returns a
810  * valid iterator that will always return false on
811  * eina_iterator_next(), thus keeping API sane.
812  *
813  * If the memory can not be allocated, NULL is returned and
814  * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
815  * returned.
816  *
817  * @warning if the hash structure changes then the iterator becomes
818  *    invalid! That is, if you add or remove items this iterator
819  *    behavior is undefined and your program may crash!
820  */
821 EAPI Eina_Iterator *eina_hash_iterator_key_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
822
823 /**
824  * @brief Returned a new iterator associated to hash data.
825  *
826  * @param hash The hash.
827  * @return A new iterator.
828  *
829  * This function returns a newly allocated iterator associated to
830  * @p hash. If @p hash is not populated, this function still returns a
831  * valid iterator that will always return false on
832  * eina_iterator_next(), thus keeping API sane.
833  *
834  * If the memory can not be allocated, @c NULL is returned and
835  * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
836  * returned.
837  *
838  * @warning if the hash structure changes then the iterator becomes
839  * invalid. That is, if you add or remove items this iterator behavior
840  * is undefined and your program may crash.
841  */
842 EAPI Eina_Iterator *eina_hash_iterator_data_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
843
844 /**
845  * @brief Returned a new iterator associated to hash keys and data.
846  *
847  * @param hash The hash.
848  * @return A new iterator.
849  *
850  * This function returns a newly allocated iterator associated to @p
851  * hash. If @p hash is not populated, this function still returns a
852  * valid iterator that will always return false on
853  * eina_iterator_next(), thus keeping API sane.
854  *
855  * If the memory can not be allocated, NULL is returned and
856  * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
857  * returned.
858  *
859  * @note iterator data will provide values as Eina_Hash_Tuple that should not
860  *   be modified!
861  *
862  * @warning if the hash structure changes then the iterator becomes
863  *    invalid! That is, if you add or remove items this iterator
864  *    behavior is undefined and your program may crash!
865  */
866 EAPI Eina_Iterator *eina_hash_iterator_tuple_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
867
868 /**
869  * @brief Call a function on every member stored in the hash table
870  *
871  * @param hash The hash table whose members will be walked
872  * @param func The function to call on each parameter
873  * @param fdata The data pointer to pass to the function being called
874  *
875  * This function goes through every entry in the hash table @p hash and calls
876  * the function @p func on each member. The function should @b not modify the
877  * hash table contents if it returns 1. @b If the hash table contents are
878  * modified by this function or the function wishes to stop processing it must
879  * return 0, otherwise return 1 to keep processing.
880  *
881  * Example:
882  * @code
883  * extern Eina_Hash *hash;
884  *
885  * Eina_Bool hash_fn(const Eina_Hash *hash, const void *key,
886  *                   void *data, void *fdata)
887  * {
888  *   printf("Func data: %s, Hash entry: %s / %p\n",
889  *          fdata, (const char *)key, data);
890  *   return 1;
891  * }
892  *
893  * int main(int argc, char **argv)
894  * {
895  *   char *hash_fn_data;
896  *
897  *   hash_fn_data = strdup("Hello World");
898  *   eina_hash_foreach(hash, hash_fn, hash_fn_data);
899  *   free(hash_fn_data);
900  * }
901  * @endcode
902  */
903 EAPI void           eina_hash_foreach(const Eina_Hash  *hash,
904                                       Eina_Hash_Foreach cb,
905                                       const void       *fdata) EINA_ARG_NONNULL(1, 2);
906 /* Paul Hsieh (http://www.azillionmonkeys.com/qed/hash.html) hash function used by WebCore (http://webkit.org/blog/8/hashtables-part-2/) */
907 EAPI int eina_hash_superfast(const char *key,
908                              int         len) EINA_ARG_NONNULL(1);
909 /* Hash function first reported by dan bernstein many years ago in comp.lang.c */
910 static inline int eina_hash_djb2(const char *key,
911                                  int         len) EINA_ARG_NONNULL(1);
912 static inline int eina_hash_djb2_len(const char *key,
913                                      int        *plen) EINA_ARG_NONNULL(1, 2);
914 /* Hash function from http://www.concentric.net/~Ttwang/tech/inthash.htm */
915 static inline int eina_hash_int32(const unsigned int *pkey,
916                                   int                 len) EINA_ARG_NONNULL(1);
917 static inline int eina_hash_int64(const unsigned long int *pkey,
918                                   int                      len) EINA_ARG_NONNULL(1);
919
920 #include "eina_inline_hash.x"
921
922 /**
923  * @}
924  */
925
926 /**
927  * @}
928  */
929
930 /**
931  * @}
932  */
933
934 /**
935  * @}
936  */
937
938 #endif /*EINA_HASH_H_*/