1 /* EINA - EFL data type library
2 * Copyright (C) 2002-2008 Carsten Haitzler, Gustavo Sverzut Barbieri,
3 * Vincent Torri, Jorge Luis Zapata Muga, Cedric Bail
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library;
17 * if not, see <http://www.gnu.org/licenses/>.
23 #include "eina_types.h"
24 #include "eina_iterator.h"
27 * @addtogroup Eina_Hash_Group Hash Table
29 * @brief give a small description here : what it is for, what it does
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().
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
40 * @section hashtable_algo Algorithm
42 * Give here the algorithm used in the implementation
44 * @section hashtable_perf Performance
46 * Give some hints about performance if it is possible, and an image !
48 * @section hashtable_tutorial Tutorial
50 * Here is a fantastic tutorial about our hash table
56 * @addtogroup Eina_Data_Types_Group Data Types
62 * @addtogroup Eina_Containers_Group Containers
68 * @defgroup Eina_Hash_Group Hash Table
75 * Type for a generic hash table.
77 typedef struct _Eina_Hash Eina_Hash;
79 typedef struct _Eina_Hash_Tuple Eina_Hash_Tuple;
81 struct _Eina_Hash_Tuple
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 */
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);
98 * @brief Create a new hash table.
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.
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.
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().
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);
128 * @brief Create a new hash table using the djb2 algorithm.
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.
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.
139 EAPI Eina_Hash *eina_hash_string_djb2_new(Eina_Free_Cb data_free_cb);
142 * @brief Create a new hash table for use with strings.
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.
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
154 EAPI Eina_Hash *eina_hash_string_superfast_new(Eina_Free_Cb data_free_cb);
157 * @brief Create a new hash table for use with strings with small bucket size.
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.
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.
171 EAPI Eina_Hash *eina_hash_string_small_new(Eina_Free_Cb data_free_cb);
174 * @brief Create a new hash table for use with 32bit integers.
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.
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.
189 EAPI Eina_Hash *eina_hash_int32_new(Eina_Free_Cb data_free_cb);
192 * @brief Create a new hash table for use with 64bit integers.
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.
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.
206 EAPI Eina_Hash *eina_hash_int64_new(Eina_Free_Cb data_free_cb);
209 * @brief Create a new hash table for use with pointers.
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.
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.
222 EAPI Eina_Hash *eina_hash_pointer_new(Eina_Free_Cb data_free_cb);
225 * @brief Create a new hash table optimized for stringshared values.
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.
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.
236 * Excerpt of code that will NOT work with this type of hash:
239 * extern Eina_Hash *hash;
240 * extern const char *value;
241 * const char *a = eina_stringshare_add("key");
243 * eina_hash_add(hash, a, value);
244 * eina_hash_find(hash, "key")
247 EAPI Eina_Hash *eina_hash_stringshared_new(Eina_Free_Cb data_free_cb);
250 * @brief Add an entry to the given hash table.
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.
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.
271 EAPI Eina_Bool eina_hash_add(Eina_Hash *hash,
273 const void *data) EINA_ARG_NONNULL(1, 2, 3);
276 * @brief Add an entry to the given hash table without duplicating the string
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.
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.
300 EAPI Eina_Bool eina_hash_direct_add(Eina_Hash *hash,
302 const void *data) EINA_ARG_NONNULL(1, 2, 3);
305 * @brief Remove the entry identified by a key or a data from the given
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.
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.
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()
327 EAPI Eina_Bool eina_hash_del(Eina_Hash *hash,
329 const void *data) EINA_ARG_NONNULL(1);
332 * @brief Retrieve a specific entry in the given hash table.
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
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
344 EAPI void *eina_hash_find(const Eina_Hash *hash,
345 const void *key) EINA_ARG_NONNULL(1, 2);
348 * @brief Modify the entry pointer at the specified key and return the old
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
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.
360 EAPI void *eina_hash_modify(Eina_Hash *hash,
362 const void *data) EINA_ARG_NONNULL(1, 2, 3);
365 * @brief Modify the entry pointer at the specified key and return the
366 * old entry or add the entry if not found.
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
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
380 EAPI void *eina_hash_set(Eina_Hash *hash,
382 const void *data) EINA_ARG_NONNULL(1, 2);
385 * @brief Change the key associated with a data without triggering the
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.
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.
397 EAPI Eina_Bool eina_hash_move(Eina_Hash *hash,
399 const void *new_key) EINA_ARG_NONNULL(1, 2, 3);
402 * Free the given hash table resources.
404 * @param hash The hash table to be freed.
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.
417 * extern Eina_Hash *hash;
419 * eina_hash_free(hash);
423 EAPI void eina_hash_free(Eina_Hash *hash) EINA_ARG_NONNULL(1);
426 * Free the given hash table buckets resources.
428 * @param hash The hash table whose buckets have to be freed.
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.
437 EAPI void eina_hash_free_buckets(Eina_Hash *hash) EINA_ARG_NONNULL(1);
440 * @brief Returns the number of entries in the given hash table.
442 * @param hash The given hash table.
443 * @return The number of entries in the hash table.
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.
448 EAPI int eina_hash_population(const Eina_Hash *hash) EINA_ARG_NONNULL(1);
451 * @brief Add an entry to the given hash table.
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.
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.
473 EAPI Eina_Bool eina_hash_add_by_hash(Eina_Hash *hash,
477 const void *data) EINA_ARG_NONNULL(1, 2, 5);
480 * @brief Add an entry to the given hash table and do not duplicate the string
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
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.
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.
506 EAPI Eina_Bool eina_hash_direct_add_by_hash(Eina_Hash *hash,
510 const void *data) EINA_ARG_NONNULL(1, 2, 5);
513 * @brief Remove the entry identified by a key and a key hash from the given
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.
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.
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.
533 EAPI Eina_Bool eina_hash_del_by_key_hash(Eina_Hash *hash,
536 int key_hash) EINA_ARG_NONNULL(1, 2);
539 * @brief Remove the entry identified by a key from the given hash table.
541 * This version will calculate key length and hash by using functions
542 * provided to hash creation function.
544 * @param hash The given hash table.
545 * @param key The key.
546 * @return #EINA_FALSE if an error occurred, #EINA_TRUE otherwise.
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.
556 * @note if you already have the key_hash, use eina_hash_del_by_key_hash()
558 * @note if you don't have the key, use eina_hash_del_by_data() instead.
560 EAPI Eina_Bool eina_hash_del_by_key(Eina_Hash *hash,
561 const void *key) EINA_ARG_NONNULL(1, 2);
564 * @brief Remove the entry identified by a data from the given hash table.
566 * This version is slow since there is no quick access to nodes based on data.
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.
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
580 * @note if you already have the key, use eina_hash_del_by_key() or
581 * eina_hash_del_by_key_hash() instead.
583 EAPI Eina_Bool eina_hash_del_by_data(Eina_Hash *hash,
584 const void *data) EINA_ARG_NONNULL(1, 2);
587 * @brief Remove the entry identified by a key and a key hash or a
588 * data from the given hash table.
590 * If @p key is @c NULL, then @p data is used to find a match to
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.
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.
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()
615 EAPI Eina_Bool eina_hash_del_by_hash(Eina_Hash *hash,
619 const void *data) EINA_ARG_NONNULL(1);
622 * @brief Retrieve a specific entry in the given hash table.
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
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.
638 EAPI void *eina_hash_find_by_hash(const Eina_Hash *hash,
641 int key_hash) EINA_ARG_NONNULL(1, 2);
644 * @brief Modify the entry pointer at the specified key and returns
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
651 * @param key_hash The hash that always match the key. Ignored if @p key is
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
658 EAPI void *eina_hash_modify_by_hash(Eina_Hash *hash,
662 const void *data) EINA_ARG_NONNULL(1, 2, 5);
665 * @brief Returned a new iterator associated to hash keys.
667 * @param hash The hash.
668 * @return A new iterator.
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.
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
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!
683 EAPI Eina_Iterator *eina_hash_iterator_key_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
686 * @brief Returned a new iterator associated to hash data.
688 * @param hash The hash.
689 * @return A new iterator.
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.
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
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.
704 EAPI Eina_Iterator *eina_hash_iterator_data_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
707 * @brief Returned a new iterator associated to hash keys and data.
709 * @param hash The hash.
710 * @return A new iterator.
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.
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
721 * @note iterator data will provide values as Eina_Hash_Tuple that should not
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!
728 EAPI Eina_Iterator *eina_hash_iterator_tuple_new(const Eina_Hash *hash) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
731 * @brief Call a function on every member stored in the hash table
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
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.
745 * extern Eina_Hash *hash;
747 * Eina_Bool hash_fn(const Eina_Hash *hash, const void *key,
748 * void *data, void *fdata)
750 * printf("Func data: %s, Hash entry: %s / %p\n",
751 * fdata, (const char *)key, data);
755 * int main(int argc, char **argv)
757 * char *hash_fn_data;
759 * hash_fn_data = strdup("Hello World");
760 * eina_hash_foreach(hash, hash_fn, hash_fn_data);
761 * free(hash_fn_data);
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);
782 #include "eina_inline_hash.x"
800 #endif /*EINA_HASH_H_*/