357dd7f4bd5fc5a9d6a2ac69ef009bb3e8c911d7
[profile/ivi/eina.git] / src / lib / eina_hash.c
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 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #ifdef _MSC_VER
28 # include <Evil.h>
29 #else
30 # include <stdint.h>
31 #endif
32
33 #include "eina_config.h"
34 #include "eina_private.h"
35 #include "eina_rbtree.h"
36 #include "eina_error.h"
37
38 /* undefs EINA_ARG_NONULL() so NULL checks are not compiled out! */
39 #include "eina_safety_checks.h"
40 #include "eina_hash.h"
41
42 /*============================================================================*
43  *                                  Local                                     *
44  *============================================================================*/
45
46 /**
47  * @cond LOCAL
48  */
49
50 #define EINA_MAGIC_CHECK_HASH(d)                                        \
51   do {                                                                  \
52      if (!EINA_MAGIC_CHECK(d, EINA_MAGIC_HASH))                         \
53        EINA_MAGIC_FAIL(d, EINA_MAGIC_HASH);                             \
54   } while(0)
55
56 #define EINA_MAGIC_CHECK_HASH_ITERATOR(d, ...)                          \
57   do {                                                                  \
58      if (!EINA_MAGIC_CHECK(d, EINA_MAGIC_HASH_ITERATOR))                \
59      {                                                                  \
60           EINA_MAGIC_FAIL(d, EINA_MAGIC_HASH_ITERATOR);                 \
61           return __VA_ARGS__;                                                   \
62      }                                                                  \
63   } while(0)
64
65 #define EINA_HASH_BUCKET_SIZE 8
66 #define EINA_HASH_SMALL_BUCKET_SIZE 5
67
68 #define EINA_HASH_RBTREE_MASK 0xFFF
69
70 typedef struct _Eina_Hash_Head Eina_Hash_Head;
71 typedef struct _Eina_Hash_El Eina_Hash_El;
72 typedef struct _Eina_Hash_Foreach_Data Eina_Hash_Foreach_Data;
73 typedef struct _Eina_Iterator_Hash Eina_Iterator_Hash;
74 typedef struct _Eina_Hash_Each Eina_Hash_Each;
75
76 struct _Eina_Hash
77 {
78    Eina_Key_Length key_length_cb;
79    Eina_Key_Cmp key_cmp_cb;
80    Eina_Key_Hash key_hash_cb;
81    Eina_Free_Cb data_free_cb;
82
83    Eina_Rbtree **buckets;
84    int size;
85    int mask;
86
87    int population;
88
89    EINA_MAGIC
90 };
91
92 struct _Eina_Hash_Head
93 {
94    EINA_RBTREE;
95    int hash;
96
97    Eina_Rbtree *head;
98 };
99
100 struct _Eina_Hash_El
101 {
102    EINA_RBTREE;
103    Eina_Hash_Tuple tuple;
104    Eina_Bool begin : 1;
105 };
106
107 struct _Eina_Hash_Foreach_Data
108 {
109    Eina_Hash_Foreach cb;
110    const void *fdata;
111 };
112
113 typedef void *(*Eina_Iterator_Get_Content_Callback)(Eina_Iterator_Hash *it);
114 #define FUNC_ITERATOR_GET_CONTENT(Function) ((Eina_Iterator_Get_Content_Callback)Function)
115
116 struct _Eina_Iterator_Hash
117 {
118    Eina_Iterator iterator;
119
120    Eina_Iterator_Get_Content_Callback get_content;
121    const Eina_Hash *hash;
122
123    Eina_Iterator *current;
124    Eina_Iterator *list;
125    Eina_Hash_Head *eh;
126    Eina_Hash_El *el;
127    int bucket;
128
129    int index;
130
131    EINA_MAGIC
132 };
133
134 struct _Eina_Hash_Each
135 {
136    Eina_Hash_Head *eh;
137    const Eina_Hash_El *el;
138    const void *data;
139 };
140
141 #undef get16bits
142 #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
143   || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
144 # define get16bits(d) (*((const uint16_t *) (d)))
145 #endif
146
147 #if !defined (get16bits)
148 # define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
149                         +(uint32_t)(((const uint8_t *)(d))[0]) )
150 #endif
151
152 static inline int
153 _eina_hash_hash_rbtree_cmp_hash(const Eina_Hash_Head *eh, const int *hash, __UNUSED__ int key_length, __UNUSED__ void *data)
154 {
155    return eh->hash - *hash;
156 }
157
158 static Eina_Rbtree_Direction
159 _eina_hash_hash_rbtree_cmp_node(const Eina_Hash_Head *left, const Eina_Hash_Head *right, __UNUSED__ void *data)
160 {
161    if (left->hash - right->hash < 0)
162      return EINA_RBTREE_LEFT;
163    return EINA_RBTREE_RIGHT;
164 }
165
166 static inline int
167 _eina_hash_key_rbtree_cmp_key_data(const Eina_Hash_El *el, const Eina_Hash_Tuple *tuple, __UNUSED__ unsigned int key_length, Eina_Key_Cmp cmp)
168 {
169    int result;
170
171    result = cmp(el->tuple.key, el->tuple.key_length, tuple->key, tuple->key_length);
172
173    if (result == 0 && tuple->data && tuple->data != el->tuple.data)
174      return 1;
175    return result;
176 }
177
178 static Eina_Rbtree_Direction
179 _eina_hash_key_rbtree_cmp_node(const Eina_Hash_El *left, const Eina_Hash_El *right, Eina_Key_Cmp cmp)
180 {
181    int result;
182
183    result = cmp(left->tuple.key, left->tuple.key_length,
184                 right->tuple.key, right->tuple.key_length);
185
186    if (result < 0)
187      return EINA_RBTREE_LEFT;
188    return EINA_RBTREE_RIGHT;
189 }
190
191 static inline Eina_Bool
192 eina_hash_add_alloc_by_hash(Eina_Hash *hash,
193                             const void *key, int key_length, int alloc_length,
194                             int key_hash,
195                             const void *data)
196 {
197    Eina_Hash_El *el = NULL;
198    Eina_Hash_Head *eh;
199    Eina_Error error = 0;
200    int hash_num;
201
202    EINA_MAGIC_CHECK_HASH(hash);
203    EINA_SAFETY_ON_NULL_RETURN_VAL(hash, EINA_FALSE);
204    EINA_SAFETY_ON_NULL_RETURN_VAL(key, EINA_FALSE);
205    EINA_SAFETY_ON_NULL_RETURN_VAL(data, EINA_FALSE);
206    error = EINA_ERROR_OUT_OF_MEMORY;
207
208    /* Apply eina mask to hash. */
209    hash_num = key_hash & hash->mask;
210    key_hash &= EINA_HASH_RBTREE_MASK;
211
212    if (!hash->buckets)
213      {
214         hash->buckets = malloc(sizeof (Eina_Rbtree*) * hash->size);
215         memset(hash->buckets, 0, sizeof (Eina_Rbtree*) * hash->size);
216
217         eh = NULL;
218      }
219    else
220      {
221         /* Look up for head node. */
222         eh = (Eina_Hash_Head*) eina_rbtree_inline_lookup(hash->buckets[hash_num],
223                                                          &key_hash, 0,
224                                                          EINA_RBTREE_CMP_KEY_CB(_eina_hash_hash_rbtree_cmp_hash), NULL);
225      }
226
227    if (!eh)
228      {
229         /* If not found allocate it and a element. */
230         eh = malloc(sizeof (Eina_Hash_Head) + sizeof (Eina_Hash_El) + alloc_length);
231         if (!eh) goto on_error;
232         eh->hash = key_hash;
233         eh->head = NULL;
234
235         hash->buckets[hash_num] = eina_rbtree_inline_insert(hash->buckets[hash_num], EINA_RBTREE_GET(eh),
236                                                             EINA_RBTREE_CMP_NODE_CB(_eina_hash_hash_rbtree_cmp_node), NULL);
237
238         el = (Eina_Hash_El*) (eh + 1);
239         el->begin = EINA_TRUE;
240      }
241
242    if (!el)
243      {
244         /*
245           Alloc every needed things
246           (No more lookup as we expect to support more than one item for one key).
247         */
248         el = malloc(sizeof (Eina_Hash_El) + alloc_length);
249         if (!el) goto on_error;
250         el->begin = EINA_FALSE;
251      }
252
253    /* Setup the element */
254    el->tuple.key_length = key_length;
255    el->tuple.data = (void *) data;
256    if (alloc_length > 0)
257      {
258         el->tuple.key = (char *) (el + 1);
259         memcpy((char *) el->tuple.key, key, alloc_length);
260      }
261    else
262      {
263         el->tuple.key = key;
264      }
265
266    /* add the new element to the hash. */
267    eh->head = eina_rbtree_inline_insert(eh->head, EINA_RBTREE_GET(el),
268                                         EINA_RBTREE_CMP_NODE_CB(_eina_hash_key_rbtree_cmp_node), (const void *)hash->key_cmp_cb);
269    hash->population++;
270    return EINA_TRUE;
271
272  on_error:
273    eina_error_set(error);
274    return EINA_FALSE;
275 }
276
277 static Eina_Bool
278 _eina_hash_rbtree_each(__UNUSED__ const Eina_Rbtree *container, const Eina_Hash_Head *eh, Eina_Hash_Each *data)
279 {
280    Eina_Iterator *it;
281    Eina_Hash_El *el;
282    Eina_Bool found = EINA_TRUE;
283
284    it = eina_rbtree_iterator_prefix(eh->head);
285    EINA_ITERATOR_FOREACH(it, el)
286      {
287         if (el->tuple.data == data->data)
288           {
289              data->el = el;
290              data->eh = (Eina_Hash_Head*) eh;
291              found = EINA_FALSE;
292              break ;
293           }
294      }
295
296    eina_iterator_free(it);
297    return found;
298 }
299
300 static inline Eina_Hash_El *
301 _eina_hash_find_by_hash(const Eina_Hash *hash, Eina_Hash_Tuple *tuple, int key_hash, Eina_Hash_Head **eh)
302 {
303    Eina_Hash_El *el;
304    int rb_hash = key_hash & EINA_HASH_RBTREE_MASK;
305
306    key_hash &= hash->mask;
307
308    if (!hash->buckets) return NULL;
309
310    *eh = (Eina_Hash_Head*) eina_rbtree_inline_lookup(hash->buckets[key_hash],
311                                                     &rb_hash, 0,
312                                                     EINA_RBTREE_CMP_KEY_CB(_eina_hash_hash_rbtree_cmp_hash), NULL);
313    if (!*eh) return NULL;
314
315    el = (Eina_Hash_El*) eina_rbtree_inline_lookup((*eh)->head,
316                                                   tuple, 0,
317                                                   EINA_RBTREE_CMP_KEY_CB(_eina_hash_key_rbtree_cmp_key_data), (const void *)hash->key_cmp_cb);
318
319    return el;
320 }
321
322 static inline Eina_Hash_El *
323 _eina_hash_find_by_data(const Eina_Hash *hash, const void *data, int *key_hash, Eina_Hash_Head **eh)
324 {
325    Eina_Hash_Each each;
326    Eina_Iterator *it;
327    int hash_num;
328
329    if (!hash->buckets) return NULL;
330
331    each.el = NULL;
332    each.data = data;
333
334    for (hash_num = 0; hash_num < hash->size; hash_num++)
335      {
336         if (!hash->buckets[hash_num])
337           continue;
338         it = eina_rbtree_iterator_prefix(hash->buckets[hash_num]);
339         eina_iterator_foreach(it, EINA_EACH(_eina_hash_rbtree_each), &each);
340         eina_iterator_free(it);
341
342         if (each.el)
343           {
344             *key_hash = hash_num;
345             *eh = each.eh;
346             return (Eina_Hash_El*) each.el;
347           }
348      }
349
350    return NULL;
351 }
352
353 static void
354 _eina_hash_el_free(Eina_Hash_El *el, Eina_Hash *hash)
355 {
356    if (hash->data_free_cb)
357      hash->data_free_cb(el->tuple.data);
358    if (el->begin == EINA_FALSE) free(el);
359 }
360
361 static void
362 _eina_hash_head_free(Eina_Hash_Head *eh, Eina_Hash *hash)
363 {
364    eina_rbtree_delete(eh->head, EINA_RBTREE_FREE_CB(_eina_hash_el_free), hash);
365    free(eh);
366 }
367
368 static Eina_Bool
369 _eina_hash_del_by_hash_el(Eina_Hash *hash, Eina_Hash_El *el, Eina_Hash_Head *eh, int key_hash)
370 {
371   eh->head = eina_rbtree_inline_remove(eh->head, EINA_RBTREE_GET(el), EINA_RBTREE_CMP_NODE_CB(_eina_hash_key_rbtree_cmp_node), (const void *)hash->key_cmp_cb);
372    _eina_hash_el_free(el, hash);
373
374    if (!eh->head)
375      {
376         key_hash &= hash->mask;
377
378         hash->buckets[key_hash] = eina_rbtree_inline_remove(hash->buckets[key_hash], EINA_RBTREE_GET(eh), EINA_RBTREE_CMP_NODE_CB(_eina_hash_hash_rbtree_cmp_node), NULL);
379         free(eh);
380      }
381
382    hash->population--;
383    if (hash->population == 0)
384      {
385         free(hash->buckets);
386         hash->buckets = NULL;
387      }
388
389    return EINA_TRUE;
390 }
391
392 static Eina_Bool
393 _eina_hash_del_by_key_hash(Eina_Hash *hash, const void *key, int key_length, int key_hash, const void *data)
394 {
395    Eina_Hash_El *el;
396    Eina_Hash_Head *eh;
397    Eina_Hash_Tuple tuple;
398
399    EINA_MAGIC_CHECK_HASH(hash);
400    EINA_SAFETY_ON_NULL_RETURN_VAL(hash, EINA_FALSE);
401    EINA_SAFETY_ON_NULL_RETURN_VAL(key, EINA_FALSE);
402
403    if (!hash->buckets) return EINA_FALSE;
404
405    tuple.key = (void *) key;
406    tuple.key_length = key_length;
407    tuple.data = (void *) data;
408
409    el = _eina_hash_find_by_hash(hash, &tuple, key_hash, &eh);
410    if (!el) return EINA_FALSE;
411    return _eina_hash_del_by_hash_el(hash, el, eh, key_hash);
412 }
413
414 static Eina_Bool
415 _eina_hash_del_by_key(Eina_Hash *hash, const void *key, const void *data)
416 {
417    int key_length, key_hash;
418
419    EINA_MAGIC_CHECK_HASH(hash);
420    if (!hash) return EINA_FALSE;
421    if (!key) return EINA_FALSE;
422    if (!hash->buckets) return EINA_FALSE;
423
424    key_length = hash->key_length_cb ? hash->key_length_cb(key) : 0;
425    key_hash = hash->key_hash_cb(key, key_length);
426    return _eina_hash_del_by_key_hash(hash, key, key_length, key_hash, data);
427 }
428
429 static unsigned int
430 _eina_string_key_length(const char *key)
431 {
432    if (!key) return 0;
433    return (int)strlen(key) + 1;
434 }
435
436 static int
437 _eina_string_key_cmp(const char *key1, __UNUSED__ int key1_length,
438                      const char *key2, __UNUSED__ int key2_length)
439 {
440    return strcmp(key1, key2);
441 }
442
443 static int
444 _eina_stringshared_key_cmp(const char *key1, __UNUSED__ int key1_length,
445                            const char *key2, __UNUSED__ int key2_length)
446 {
447    return key1 - key2;
448 }
449
450 static unsigned int
451 _eina_int32_key_length(__UNUSED__ const uint32_t *key)
452 {
453   return 4;
454 }
455
456 static int
457 _eina_int32_key_cmp(const uint32_t *key1, __UNUSED__ int key1_length,
458                     const uint32_t *key2, __UNUSED__ int key2_length)
459 {
460    return *key1 - *key2;
461 }
462
463 static unsigned int
464 _eina_int64_key_length(__UNUSED__ const uint32_t *key)
465 {
466   return 8;
467 }
468
469 static int
470 _eina_int64_key_cmp(const uint64_t *key1, __UNUSED__ int key1_length,
471                     const uint64_t *key2, __UNUSED__ int key2_length)
472 {
473    return *key1 - *key2;
474 }
475
476 static Eina_Bool
477 _eina_foreach_cb(const Eina_Hash *hash, Eina_Hash_Tuple *data, Eina_Hash_Foreach_Data *fdata)
478 {
479    return fdata->cb((Eina_Hash *) hash, data->key, data->data, (void*) fdata->fdata);
480 }
481
482 static void *
483 _eina_hash_iterator_data_get_content(Eina_Iterator_Hash *it)
484 {
485    Eina_Hash_El *stuff;
486
487    EINA_MAGIC_CHECK_HASH_ITERATOR(it, NULL);
488
489    stuff = it->el;
490
491    if (!stuff) return NULL;
492    return stuff->tuple.data;
493 }
494
495 static void *
496 _eina_hash_iterator_key_get_content(Eina_Iterator_Hash *it)
497 {
498    Eina_Hash_El *stuff;
499
500    EINA_MAGIC_CHECK_HASH_ITERATOR(it, NULL);
501
502    stuff = it->el;
503
504    if (!stuff) return NULL;
505    return (void *) stuff->tuple.key;
506 }
507
508 static Eina_Hash_Tuple *
509 _eina_hash_iterator_tuple_get_content(Eina_Iterator_Hash *it)
510 {
511    Eina_Hash_El *stuff;
512
513    EINA_MAGIC_CHECK_HASH_ITERATOR(it, NULL);
514
515    stuff = it->el;
516
517    if (!stuff) return NULL;
518    return &stuff->tuple;
519 }
520
521 static Eina_Bool
522 _eina_hash_iterator_next(Eina_Iterator_Hash *it, void **data)
523 {
524    Eina_Bool ok;
525    int bucket;
526
527    if (!(it->index < it->hash->population)) return EINA_FALSE;
528    if (it->current == NULL)
529      {
530         ok = EINA_FALSE;
531         bucket = 0;
532         it->index = -1;
533      }
534    else
535      {
536         ok = eina_iterator_next(it->list, (void**) &it->el);
537         if (!ok)
538           {
539              eina_iterator_free(it->list);
540              it->list = NULL;
541
542              ok = eina_iterator_next(it->current, (void**) &it->eh);
543              if (!ok)
544                {
545                   eina_iterator_free(it->current);
546                   it->current = NULL;
547                   it->bucket++;
548                }
549              else
550                {
551                   it->list = eina_rbtree_iterator_prefix(it->eh->head);
552                   ok = eina_iterator_next(it->list, (void**) &it->el);
553                }
554           }
555
556         bucket = it->bucket;
557      }
558
559    if (ok == EINA_FALSE)
560      {
561         while (bucket < it->hash->size)
562           {
563              if (it->hash->buckets[bucket] != NULL)
564                {
565                   it->current = eina_rbtree_iterator_prefix(it->hash->buckets[bucket]);
566                   ok = eina_iterator_next(it->current, (void**) &it->eh);
567                   if (ok) break ;
568                   eina_iterator_free(it->current);
569                   it->current = NULL;
570                }
571              ++bucket;
572           }
573         if (it->list) eina_iterator_free(it->list);
574         it->list = eina_rbtree_iterator_prefix(it->eh->head);
575         ok = eina_iterator_next(it->list, (void**) &it->el);
576         if (bucket == it->hash->size) ok = EINA_FALSE;
577      }
578
579    it->index++;
580    it->bucket = bucket;
581
582    if (ok)
583      *data = it->get_content(it);
584
585    return ok;
586 }
587
588 static void *
589 _eina_hash_iterator_get_container(Eina_Iterator_Hash *it)
590 {
591    EINA_MAGIC_CHECK_HASH_ITERATOR(it, NULL);
592    return (void *) it->hash;
593 }
594
595 static void
596 _eina_hash_iterator_free(Eina_Iterator_Hash *it)
597 {
598    EINA_MAGIC_CHECK_HASH_ITERATOR(it);
599    if (it->current) eina_iterator_free(it->current);
600    if (it->list) eina_iterator_free(it->list);
601    free(it);
602 }
603
604 /**
605  * @endcond
606  */
607
608 /*============================================================================*
609  *                                 Global                                     *
610  *============================================================================*/
611
612 /*============================================================================*
613  *                                   API                                      *
614  *============================================================================*/
615
616 /**
617  * @addtogroup Eina_Hash_Group Hash Table
618  *
619  * @brief give a small description here : what it is for, what it does
620  * , etc...
621  *
622  * Hash API. Give some hints about the use (functions that must be
623  * used like init / shutdown), general use, etc... Give also a link to
624  * tutorial below.
625  *
626  * @section hashtable_algo Algorithm
627  *
628  * Give here the algorithm used in the implementation
629  *
630  * @section hashtable_perf Performance
631  *
632  * Give some hints about performance if it is possible, and an image !
633  *
634  * @section hashtable_tutorial Tutorial
635  *
636  * Here is a fantastic tutorial about our hash table
637  *
638  * @{
639  */
640
641 EAPI Eina_Hash *
642 eina_hash_new(Eina_Key_Length key_length_cb,
643               Eina_Key_Cmp key_cmp_cb,
644               Eina_Key_Hash key_hash_cb,
645               Eina_Free_Cb data_free_cb,
646               int buckets_power_size)
647 {
648    /* FIXME: Use mempool. */
649    Eina_Hash *new;
650
651    eina_error_set(0);
652    EINA_SAFETY_ON_NULL_RETURN_VAL(key_cmp_cb, NULL);
653    EINA_SAFETY_ON_NULL_RETURN_VAL(key_hash_cb, NULL);
654    EINA_SAFETY_ON_TRUE_RETURN_VAL(buckets_power_size < 3, NULL);
655    EINA_SAFETY_ON_TRUE_RETURN_VAL(buckets_power_size > 16, NULL);
656
657    new = malloc(sizeof (Eina_Hash));
658    if (!new) goto on_error;
659
660    new->key_length_cb = key_length_cb;
661    new->key_cmp_cb = key_cmp_cb;
662    new->key_hash_cb = key_hash_cb;
663    new->data_free_cb = data_free_cb;
664    new->buckets = NULL;
665    new->population = 0;
666
667    new->size = 1 << buckets_power_size;
668    new->mask = new->size - 1;
669
670    EINA_MAGIC_SET(new, EINA_MAGIC_HASH);
671
672    return new;
673
674  on_error:
675    eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
676    return NULL;
677 }
678
679 EAPI Eina_Hash *
680 eina_hash_string_djb2_new(Eina_Free_Cb data_free_cb)
681 {
682    return eina_hash_new(EINA_KEY_LENGTH(_eina_string_key_length),
683                         EINA_KEY_CMP(_eina_string_key_cmp),
684                         EINA_KEY_HASH(eina_hash_djb2),
685                         data_free_cb,
686                         EINA_HASH_BUCKET_SIZE);
687 }
688
689 /**
690  * @brief Create a new hash for use with strings.
691  * @param data_free_cb The function to call on values when the hash table is freed
692  * @return The @ref Eina_Hash object, or @c NULL on error
693  * Use to create a new hash for use with strings.
694  * NOTE: If your hash is created by this, you CAN look up values with pointers other
695  * than the original key pointer that was used to add a value.
696  */
697 EAPI Eina_Hash *
698 eina_hash_string_superfast_new(Eina_Free_Cb data_free_cb)
699 {
700    return eina_hash_new(EINA_KEY_LENGTH(_eina_string_key_length),
701                         EINA_KEY_CMP(_eina_string_key_cmp),
702                         EINA_KEY_HASH(eina_hash_superfast),
703                         data_free_cb,
704                         EINA_HASH_BUCKET_SIZE);
705 }
706
707 /**
708  * @brief Create a new hash for use with strings. If you are unsure of which hash creation
709  * function to use, use this one.
710  * @param data_free_cb The function to call on values when the hash table is freed
711  * @return The @ref Eina_Hash object, or @c NULL on error
712  * Use to create a new hash with small bucket size for use with strings.
713  * If you are unsure of which hash creation function to use, you should probably use this one.
714  * NOTE: If your hash is created by this, you CAN look up values with pointers other
715  * than the original key pointer that was used to add a value.
716  */
717 EAPI Eina_Hash *
718 eina_hash_string_small_new(Eina_Free_Cb data_free_cb)
719 {
720    return eina_hash_new(EINA_KEY_LENGTH(_eina_string_key_length),
721                         EINA_KEY_CMP(_eina_string_key_cmp),
722                         EINA_KEY_HASH(eina_hash_superfast),
723                         data_free_cb,
724                         EINA_HASH_SMALL_BUCKET_SIZE);
725 }
726
727 EAPI Eina_Hash *
728 eina_hash_int32_new(Eina_Free_Cb data_free_cb)
729 {
730    return eina_hash_new(EINA_KEY_LENGTH(_eina_int32_key_length),
731                         EINA_KEY_CMP(_eina_int32_key_cmp),
732                         EINA_KEY_HASH(eina_hash_int32),
733                         data_free_cb,
734                         EINA_HASH_BUCKET_SIZE);
735 }
736
737 EAPI Eina_Hash *
738 eina_hash_int64_new(Eina_Free_Cb data_free_cb)
739 {
740    return eina_hash_new(EINA_KEY_LENGTH(_eina_int64_key_length),
741                         EINA_KEY_CMP(_eina_int64_key_cmp),
742                         EINA_KEY_HASH(eina_hash_int64),
743                         data_free_cb,
744                         EINA_HASH_BUCKET_SIZE);
745 }
746
747 EAPI Eina_Hash *
748 eina_hash_pointer_new(Eina_Free_Cb data_free_cb)
749 {
750 #ifdef __LP64__
751    return eina_hash_new(EINA_KEY_LENGTH(_eina_int64_key_length),
752                         EINA_KEY_CMP(_eina_int64_key_cmp),
753                         EINA_KEY_HASH(eina_hash_int64),
754                         data_free_cb,
755                         EINA_HASH_BUCKET_SIZE);
756 #else
757    return eina_hash_new(EINA_KEY_LENGTH(_eina_int32_key_length),
758                         EINA_KEY_CMP(_eina_int32_key_cmp),
759                         EINA_KEY_HASH(eina_hash_int32),
760                         data_free_cb,
761                         EINA_HASH_BUCKET_SIZE);
762 #endif
763 }
764 /**
765  * @brief Create a new hash optimized for stringshared values.
766  * @param data_free_cb The function to call on values when the hash table is freed
767  * @return The @ref Eina_Hash object, or @c NULL on error
768  * Use to create a new hash optimized for stringshared values.
769  * NOTE: If your hash is created by this, you CANNOT look up values with pointers not
770  * equal to the original key pointer that was used to add a value.
771  * The following code will NOT work with this type of hash:
772  * @code
773  * extern Eina_Hash *hash;
774  * extern const char *value;
775  * const char *a = eina_stringshare_add("key");
776  *
777  * eina_hash_add(hash, a, value);
778  * eina_hash_find(hash, "key")
779  * @endcode
780  */
781 EAPI Eina_Hash *
782 eina_hash_stringshared_new(Eina_Free_Cb data_free_cb)
783 {
784    return eina_hash_new(NULL,
785                         EINA_KEY_CMP(_eina_stringshared_key_cmp),
786                         EINA_KEY_HASH(eina_hash_superfast),
787                         data_free_cb,
788                         EINA_HASH_BUCKET_SIZE);
789 }
790
791 /**
792  * @brief Returns the number of entires in the hash table.
793  * @param hash The given hash table.
794  * @return The number of entries in the hash table, @c 0 on error
795  * Returns the number of entires in the hash table.
796  */
797 EAPI int
798 eina_hash_population(const Eina_Hash *hash)
799 {
800    if (!hash) return 0;
801
802    EINA_MAGIC_CHECK_HASH(hash);
803    return hash->population;
804 }
805
806 /**
807  * Calls @ref Eina_Free_Cb (if one was specified at time of creation) on all hash table
808  * buckets, frees the buckets, then frees the hash table
809  * @param hash The hash table to be freed
810  *
811  * This function frees up all the memory allocated to storing the specified
812  * hash table pointed to by @p hash. If no data_free_cb has been passed to the
813  * hash at creation time, any entries in the table that the program
814  * has no more pointers for elsewhere may now be lost, so this should only be
815  * called if the program has already freed any allocated data in the hash table
816  * or has the pointers for data in the table stored elsewhere as well.
817  *
818  * Example:
819  * @code
820  * extern Eina_Hash *hash;
821  *
822  * eina_hash_free(hash);
823  * hash = NULL;
824  * @endcode
825  */
826 EAPI void
827 eina_hash_free(Eina_Hash *hash)
828 {
829    int i;
830
831    EINA_MAGIC_CHECK_HASH(hash);
832    EINA_SAFETY_ON_NULL_RETURN(hash);
833
834    if (hash->buckets)
835      {
836         for (i = 0; i < hash->size; i++)
837           eina_rbtree_delete(hash->buckets[i], EINA_RBTREE_FREE_CB(_eina_hash_head_free), hash);
838         free(hash->buckets);
839      }
840    free(hash);
841 }
842
843 /**
844  * Calls @ref Eina_Free_Cb (if one was specified at time of creation) on all hash table buckets,
845  * then frees the buckets.
846  * @param hash The hash table to free buckets on
847  *
848  * Frees all memory allocated for hash table buckets.  Note that the bucket value is not freed
849  * unless an @ref Eina_Free_Cb was specified at creation time.
850  * @see Noooo they be stealin' my bucket!
851  */
852 EAPI void
853 eina_hash_free_buckets(Eina_Hash *hash)
854 {
855    int i;
856
857    EINA_MAGIC_CHECK_HASH(hash);
858    EINA_SAFETY_ON_NULL_RETURN(hash);
859
860    if (hash->buckets)
861      {
862         for (i = 0; i < hash->size; i++)
863           eina_rbtree_delete(hash->buckets[i], EINA_RBTREE_FREE_CB(_eina_hash_head_free), hash);
864         free(hash->buckets);
865         hash->buckets = NULL;
866         hash->population = 0;
867      }
868 }
869
870 /**
871  * Adds an entry to the given hash table.
872  *
873  * @p key is expected to be a unique string within the hash table.
874  * Otherwise, you cannot be sure which inserted data pointer will be
875  * accessed with @ref eina_hash_find , and removed with
876  * @ref eina_hash_del .
877  *
878  * @p key_hash is expected to always match @p key. Otherwise, you
879  * cannot be sure to find it again with @ref eina_hash_find_by_hash.
880  *
881  * Key strings are case sensitive.
882  *
883  * @ref eina_error_get should be used to determine if an
884  * allocation error occurred during this function.
885  *
886  * @param   hash The given hash table.  Can be @c NULL.
887  * @param   key  A unique key.  Can be @c NULL.
888  * @param   key_length Should be the length of @p key (don't forget to count '\\0' for string).
889  * @param   key_hash The hash that will always match key.
890  * @param   data Data to associate with the string given by @p key.
891  * @return  Will return EINA_FALSE if an error occured, and EINA_TRUE if every
892  *          thing goes fine.
893  */
894 EAPI Eina_Bool
895 eina_hash_add_by_hash(Eina_Hash *hash,
896                       const void *key, int key_length, int key_hash,
897                       const void *data)
898 {
899    return eina_hash_add_alloc_by_hash(hash, key, key_length, key_length, key_hash, data);
900 }
901
902 /**
903  * Adds an entry to the given hash table and does not duplicate the string key.
904  *
905  * @p key is expected to be a unique string within the hash table.
906  * Otherwise, you cannot be sure which inserted data pointer will be
907  * accessed with @ref eina_hash_find , and removed with
908  * @ref eina_hash_del . This call does not make a copy of the key so it must
909  * be a string constant or stored elsewhere (in the object being added) etc.
910  *
911  * @p key_hash is expected to always match @p key. Otherwise, you
912  * cannot be sure to find it again with @ref eina_hash_find_by_hash.
913  *
914  * Key strings are case sensitive.
915  *
916  * @ref eina_error_get should be used to determine if an
917  * allocation error occurred during this function.
918  *
919  * @param   hash The given hash table.  Can be @c NULL.
920  * @param   key  A unique key.  Can be @c NULL.
921  * @param   key_length Should be the length of @p key (don't forget to count '\\0' for string).
922  * @param   key_hash The hash that will always match key.
923  * @param   data Data to associate with the string given by @p key.
924  * @return  Will return EINA_FALSE if an error occured, and EINA_TRUE if every
925  *          thing goes fine.
926  */
927 EAPI Eina_Bool
928 eina_hash_direct_add_by_hash(Eina_Hash *hash,
929                              const void *key, int key_length, int key_hash,
930                              const void *data)
931 {
932    return eina_hash_add_alloc_by_hash(hash, key, key_length, 0, key_hash, data);
933 }
934
935 /**
936  * Adds an entry to the given hash table.
937  *
938  * @p key is expected to be unique within the hash table.  Key uniqueness varies depending
939  * on the type of @p hash: a stringshared @ref Eina_Hash need only have unique pointers for
940  * keys, but the strings in the pointers may be identical.  All other hash types require
941  * the strings themselves to be unique.  Failure to use sufficient uniqueness will result in
942  * unexpected results when inserting data pointers accessed with @ref eina_hash_find ,
943  * and removed with @ref eina_hash_del .
944  *
945  * Key strings are case sensitive.
946  *
947  * @ref eina_error_get() should be used to determine if an
948  * allocation error occurred during this function.
949  *
950  * @param   hash The given hash table.  Can be @c NULL.
951  * @param   key  A unique key.  Can be @c NULL.
952  * @param   data Data to associate with the string given by @p key.
953  * @return  Will return EINA_FALSE if an error occured, and EINA_TRUE if every
954  *          thing goes fine.
955  */
956 EAPI Eina_Bool
957 eina_hash_add(Eina_Hash *hash, const void *key, const void *data)
958 {
959    unsigned int key_length;
960    int key_hash;
961
962    EINA_MAGIC_CHECK_HASH(hash);
963    EINA_SAFETY_ON_NULL_RETURN_VAL(hash, EINA_FALSE);
964    EINA_SAFETY_ON_NULL_RETURN_VAL(hash->key_hash_cb, EINA_FALSE);
965    EINA_SAFETY_ON_NULL_RETURN_VAL(key, EINA_FALSE);
966    EINA_SAFETY_ON_NULL_RETURN_VAL(data, EINA_FALSE);
967
968    key_length = hash->key_length_cb ? hash->key_length_cb(key) : 0;
969    key_hash = hash->key_hash_cb(key, key_length);
970
971    return eina_hash_add_by_hash(hash, key, key_length, key_hash, data);
972 }
973
974 /**
975  * Adds an entry to the given hash table but does not duplicate the string key.
976  *
977  * @p key is expected to be unique within the hash table.  Key uniqueness varies depending
978  * on the type of @p hash: a stringshared @ref Eina_Hash need only have unique pointers for
979  * keys, but the strings in the pointers may be identical.  All other hash types require
980  * the strings themselves to be unique.  Failure to use sufficient uniqueness will result in
981  * unexpected results when inserting data pointers accessed with @ref eina_hash_find ,
982  * and removed with @ref eina_hash_del . This call does not make a copy
983  * of the key so it must be a string constant or stored elsewhere (in the object
984  * being added) etc.
985  *
986  * Key strings are case sensitive.
987  *
988  * @ref eina_error_get() should be used to determine if an
989  * allocation error occurred during this function.
990  *
991  * @param   hash The given hash table.  Can be @c NULL.
992  * @param   key  A unique key.  Can be @c NULL.
993  * @param   data Data to associate with the string given by @p key.
994  * @return  Will return EINA_FALSE if an error occured, and EINA_TRUE if every
995  *          thing goes fine.
996  */
997 EAPI Eina_Bool
998 eina_hash_direct_add(Eina_Hash *hash, const void *key, const void *data)
999 {
1000    int key_length;
1001    int key_hash;
1002
1003    EINA_MAGIC_CHECK_HASH(hash);
1004    EINA_SAFETY_ON_NULL_RETURN_VAL(hash, EINA_FALSE);
1005    EINA_SAFETY_ON_NULL_RETURN_VAL(hash->key_hash_cb, EINA_FALSE);
1006    EINA_SAFETY_ON_NULL_RETURN_VAL(key, EINA_FALSE);
1007    EINA_SAFETY_ON_NULL_RETURN_VAL(data, EINA_FALSE);
1008
1009    key_length = hash->key_length_cb ? hash->key_length_cb(key) : 0;
1010    key_hash = hash->key_hash_cb(key, key_length);
1011
1012    return eina_hash_direct_add_by_hash(hash, key, key_length, key_hash, data);
1013 }
1014
1015 /**
1016  * Removes the entry identified by @p key and @p key_hash from the given
1017  * hash table.
1018  *
1019  * @param   hash The given hash table.
1020  * @param   key  The key.  Cannot be @c NULL.
1021  * @param   key_length Should be the length of @p key (don't forget to count '\\0' for string).
1022  * @param   key_hash The hash that always match the key.
1023  * @return  Will return EINA_FALSE if an error occured, and EINA_TRUE if every
1024  *          thing goes fine.
1025  *
1026  * @note if you don't have the key_hash, use eina_hash_del_by_key() instead.
1027  * @note if you don't have the key, use eina_hash_del_by_data() instead.
1028  */
1029 EAPI Eina_Bool
1030 eina_hash_del_by_key_hash(Eina_Hash *hash, const void *key, int key_length, int key_hash)
1031 {
1032    EINA_SAFETY_ON_NULL_RETURN_VAL(hash, EINA_FALSE);
1033    EINA_SAFETY_ON_NULL_RETURN_VAL(key, EINA_FALSE);
1034    return _eina_hash_del_by_key_hash(hash, key, key_length, key_hash, NULL);
1035 }
1036
1037 /**
1038  * Removes the entry identified by @p key from the given hash table.
1039  *
1040  * This version will calculate key length and hash by using functions
1041  * provided to hash creation function.
1042  *
1043  * @param   hash The given hash table.
1044  * @param   key  The key.  Cannot be @c NULL.
1045  * @return  Will return EINA_FALSE if an error occured, and EINA_TRUE if every
1046  *          thing goes fine.
1047  *
1048  * @note if you already have the key_hash, use eina_hash_del_by_key_hash() instead.
1049  * @note if you don't have the key, use eina_hash_del_by_data() instead.
1050  */
1051 EAPI Eina_Bool
1052 eina_hash_del_by_key(Eina_Hash *hash, const void *key)
1053 {
1054    EINA_SAFETY_ON_NULL_RETURN_VAL(hash, EINA_FALSE);
1055    EINA_SAFETY_ON_NULL_RETURN_VAL(key, EINA_FALSE);
1056    return _eina_hash_del_by_key(hash, key, NULL);
1057 }
1058
1059 /**
1060  * Removes the entry identified by @p data from the given hash table.
1061  *
1062  * This version is slow since there is no quick access to nodes based on data.
1063  *
1064  * @param   hash The given hash table.
1065  * @param   data  The data value to search and remove.
1066  * @return  Will return EINA_FALSE if an error occured, and EINA_TRUE if every
1067  *          thing goes fine.
1068  *
1069  * @note if you already have the key, use eina_hash_del_by_key() or eina_hash_del_by_key_hash() instead.
1070  */
1071 EAPI Eina_Bool
1072 eina_hash_del_by_data(Eina_Hash *hash, const void *data)
1073 {
1074    Eina_Hash_El *el;
1075    Eina_Hash_Head *eh;
1076    int key_hash;
1077
1078    EINA_MAGIC_CHECK_HASH(hash);
1079    EINA_SAFETY_ON_NULL_RETURN_VAL(hash, EINA_FALSE);
1080    EINA_SAFETY_ON_NULL_RETURN_VAL(data, EINA_FALSE);
1081
1082    el = _eina_hash_find_by_data(hash, data, &key_hash, &eh);
1083    if (!el) return EINA_FALSE;
1084    if (el->tuple.data != data) return EINA_FALSE;
1085    return _eina_hash_del_by_hash_el(hash, el, eh, key_hash);
1086 }
1087
1088 /**
1089  * Removes the entry identified by @p key and @p key_hash or @p data from the given
1090  * hash table.
1091  *
1092  * If @p key is @c NULL, then @p data is used to find a match to
1093  * remove.
1094  *
1095  * @param   hash The given hash table.
1096  * @param   key  The key.  Can be @c NULL.
1097  * @param   key_length Should be the length of @p key (don't forget to count '\\0' for string).
1098  * @param   key_hash The hash that always match the key. Ignored if @p key is @c NULL.
1099  * @param   data The data pointer to remove if @p key is @c NULL.
1100  *               Otherwise, not required and can be @c NULL.
1101  * @return  Will return EINA_FALSE if an error occured, and EINA_TRUE if every
1102  *          thing goes fine.
1103  *
1104  * @note if you know you already have the key, use eina_hash_del_by_key_hash(),
1105  *       if you know you don't have the key, use eina_hash_del_by_data()
1106  *       directly.
1107  */
1108 EAPI Eina_Bool
1109 eina_hash_del_by_hash(Eina_Hash *hash, const void *key, int key_length, int key_hash, const void *data)
1110 {
1111    EINA_MAGIC_CHECK_HASH(hash);
1112    EINA_SAFETY_ON_NULL_RETURN_VAL(hash, EINA_FALSE);
1113    if (key) return _eina_hash_del_by_key_hash(hash, key, key_length, key_hash, data);
1114    else return eina_hash_del_by_data(hash, data);
1115 }
1116
1117 /**
1118  * Removes the entry identified by @p key or @p data from the given
1119  * hash table.
1120  *
1121  * If @p key is @c NULL, then @p data is used to find a match to
1122  * remove.
1123  *
1124  * @param   hash The given hash table.
1125  * @param   key  The key.  Can be @c NULL.
1126  * @param   data The data pointer to remove if @p key is @c NULL.
1127  *               Otherwise, not required and can be @c NULL.
1128  * @return  Will return EINA_FALSE if an error occured, and EINA_TRUE if every
1129  *          thing goes fine.
1130  *
1131  * @note if you know you already have the key, use
1132  *       eina_hash_del_by_key() or eina_hash_del_by_key_hash(). If you
1133  *       know you don't have the key, use eina_hash_del_by_data()
1134  *       directly.
1135  */
1136 EAPI Eina_Bool
1137 eina_hash_del(Eina_Hash *hash, const void *key, const void *data)
1138 {
1139    EINA_MAGIC_CHECK_HASH(hash);
1140    EINA_SAFETY_ON_NULL_RETURN_VAL(hash, EINA_FALSE);
1141    if (key) return _eina_hash_del_by_key(hash, key, data);
1142    else return eina_hash_del_by_data(hash, data);
1143 }
1144
1145 /**
1146  * Retrieves a specific entry in the given hash table.
1147  * @param   hash The given hash table.
1148  * @param   key  The key of the entry to find.
1149  * @param   key_length Should be the length of @p key (don't forget to count '\\0' for string).
1150  * @param   key_hash The hash that always match the key. Ignored if @p key is @c NULL.
1151  * @return  The data pointer for the stored entry, or @c NULL if not
1152  *          found.
1153  */
1154 EAPI void *
1155 eina_hash_find_by_hash(const Eina_Hash *hash, const void *key, int key_length, int key_hash)
1156 {
1157    Eina_Hash_Head *eh;
1158    Eina_Hash_El *el;
1159    Eina_Hash_Tuple tuple;
1160
1161    if (!hash) return NULL;
1162
1163    EINA_MAGIC_CHECK_HASH(hash);
1164    EINA_SAFETY_ON_NULL_RETURN_VAL(key, NULL);
1165
1166    tuple.key = key;
1167    tuple.key_length = key_length;
1168    tuple.data = NULL;
1169
1170    el = _eina_hash_find_by_hash(hash, &tuple, key_hash, &eh);
1171    if (el) return el->tuple.data;
1172    return NULL;
1173 }
1174
1175 /**
1176  * Retrieves a specific entry in the given hash table.
1177  * @param   hash The given hash table.
1178  * @param   key  The key of the entry to find.
1179  * @return  The data pointer for the stored entry, or @c NULL if not
1180  *          found.
1181  */
1182 EAPI void *
1183 eina_hash_find(const Eina_Hash *hash, const void *key)
1184 {
1185    int key_length;
1186    int hash_num;
1187
1188    if (!hash) return NULL;
1189
1190    EINA_MAGIC_CHECK_HASH(hash);
1191    EINA_SAFETY_ON_NULL_RETURN_VAL(hash->key_hash_cb, NULL);
1192    EINA_SAFETY_ON_NULL_RETURN_VAL(key, NULL);
1193
1194    key_length = hash->key_length_cb ? hash->key_length_cb(key) : 0;
1195    hash_num = hash->key_hash_cb(key, key_length);
1196
1197    return eina_hash_find_by_hash(hash, key, key_length, hash_num);
1198 }
1199
1200 /**
1201  * Modifies the entry pointer at the specified key and returns the old entry
1202  * @param   hash The given hash table.
1203  * @param   key  The key of the entry to modify.
1204  * @param   key_length Should be the length of @p key (don't forget to count '\\0' for string).
1205  * @param   key_hash The hash that always match the key. Ignored if @p key is @c NULL.
1206  * @param   data The data to replace the old entry, if it exists.
1207  * @return  The data pointer for the old stored entry, or @c NULL if not
1208  *          found. If an existing entry is not found, nothing is added to the
1209  *          hash.
1210  */
1211 EAPI void *
1212 eina_hash_modify_by_hash(Eina_Hash *hash, const void *key, int key_length, int key_hash, const void *data)
1213 {
1214    Eina_Hash_Head *eh;
1215    Eina_Hash_El *el;
1216    void *old_data = NULL;
1217    Eina_Hash_Tuple tuple;
1218
1219    EINA_MAGIC_CHECK_HASH(hash);
1220    EINA_SAFETY_ON_NULL_RETURN_VAL(hash, NULL);
1221    EINA_SAFETY_ON_NULL_RETURN_VAL(key, NULL);
1222    EINA_SAFETY_ON_NULL_RETURN_VAL(data, NULL);
1223
1224    tuple.key = key;
1225    tuple.key_length = key_length;
1226    tuple.data = NULL;
1227
1228    el = _eina_hash_find_by_hash(hash, &tuple, key_hash, &eh);
1229    if (el)
1230      {
1231         old_data = el->tuple.data;
1232         el->tuple.data = (void *) data;
1233      }
1234
1235    return old_data;
1236 }
1237
1238 /**
1239  * Modifies the entry pointer at the specified key and returns the old entry
1240  * @param   hash The given hash table.
1241  * @param   key  The key of the entry to modify.
1242  * @param   data The data to replace the old entry, if it exists.
1243  * @return  The data pointer for the old stored entry, or @c NULL if not
1244  *          found. If an existing entry is not found, nothing is added to the
1245  *          hash.
1246  */
1247 EAPI void *
1248 eina_hash_modify(Eina_Hash *hash, const void *key, const void *data)
1249 {
1250    int key_length;
1251    int hash_num;
1252
1253    EINA_MAGIC_CHECK_HASH(hash);
1254    EINA_SAFETY_ON_NULL_RETURN_VAL(hash, NULL);
1255    EINA_SAFETY_ON_NULL_RETURN_VAL(hash->key_hash_cb, NULL);
1256    EINA_SAFETY_ON_NULL_RETURN_VAL(key, NULL);
1257    EINA_SAFETY_ON_NULL_RETURN_VAL(data, NULL);
1258
1259    key_length = hash->key_length_cb ? hash->key_length_cb(key) : 0;
1260    hash_num = hash->key_hash_cb(key, key_length);
1261
1262    return eina_hash_modify_by_hash(hash, key, key_length, hash_num, data);
1263 }
1264
1265 /*============================================================================*
1266  *                                Iterator                                    *
1267  *============================================================================*/
1268
1269 /**
1270  * Call a function on every member stored in the hash table
1271  * @param hash The hash table whose members will be walked
1272  * @param func The function to call on each parameter
1273  * @param fdata The data pointer to pass to the function being called
1274  *
1275  * This function goes through every entry in the hash table @p hash and calls
1276  * the function @p func on each member. The function should NOT modify the
1277  * hash table contents if it returns 1. IF the hash table contents are
1278  * modified by this function or the function wishes to stop processing it must
1279  * return 0, otherwise return 1 to keep processing.
1280  *
1281  * Example:
1282  * @code
1283  * extern Eina_Hash *hash;
1284  *
1285  * Eina_Bool hash_fn(const Eina_Hash *hash, const void *key, void *data, void *fdata)
1286  * {
1287  *   printf("Func data: %s, Hash entry: %s / %p\n", fdata, (const char *)key, data);
1288  *   return 1;
1289  * }
1290  *
1291  * int main(int argc, char **argv)
1292  * {
1293  *   char *hash_fn_data;
1294  *
1295  *   hash_fn_data = strdup("Hello World");
1296  *   eina_hash_foreach(hash, hash_fn, hash_fn_data);
1297  *   free(hash_fn_data);
1298  * }
1299  * @endcode
1300  */
1301 EAPI void
1302 eina_hash_foreach(const Eina_Hash *hash,
1303                   Eina_Hash_Foreach func,
1304                   const void *fdata)
1305 {
1306    Eina_Iterator *it;
1307    Eina_Hash_Foreach_Data foreach;
1308
1309    EINA_MAGIC_CHECK_HASH(hash);
1310    EINA_SAFETY_ON_NULL_RETURN(hash);
1311    EINA_SAFETY_ON_NULL_RETURN(func);
1312
1313    foreach.cb = func;
1314    foreach.fdata = fdata;
1315
1316    it = eina_hash_iterator_tuple_new(hash);
1317    if (!it) return;
1318
1319    eina_iterator_foreach(it, EINA_EACH(_eina_foreach_cb), &foreach);
1320    eina_iterator_free(it);
1321 }
1322
1323 /**
1324  * @brief Returned a new iterator asociated to hash data.
1325  *
1326  * @param hash The hash.
1327  * @return A new iterator.
1328  *
1329  * This function returns a newly allocated iterator associated to @p
1330  * hash. If @p hash is not populated, this function still returns a
1331  * valid iterator that will always return false on
1332  * eina_iterator_next(), thus keeping API sane.
1333  *
1334  * If the memory can not be allocated, NULL is returned and
1335  * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
1336  * returned.
1337  *
1338  * @warning if the hash structure changes then the iterator becomes
1339  *    invalid! That is, if you add or remove items this iterator
1340  *    behavior is undefined and your program may crash!
1341  */
1342 EAPI Eina_Iterator *
1343 eina_hash_iterator_data_new(const Eina_Hash *hash)
1344 {
1345    Eina_Iterator_Hash *it;
1346
1347    EINA_MAGIC_CHECK_HASH(hash);
1348    EINA_SAFETY_ON_NULL_RETURN_VAL(hash, NULL);
1349
1350    eina_error_set(0);
1351    it = calloc(1, sizeof (Eina_Iterator_Hash));
1352    if (!it) {
1353       eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
1354       return NULL;
1355    }
1356
1357    it->hash = hash;
1358    it->get_content = FUNC_ITERATOR_GET_CONTENT(_eina_hash_iterator_data_get_content);
1359
1360    it->iterator.next = FUNC_ITERATOR_NEXT(_eina_hash_iterator_next);
1361    it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(_eina_hash_iterator_get_container);
1362    it->iterator.free = FUNC_ITERATOR_FREE(_eina_hash_iterator_free);
1363
1364    EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);
1365    EINA_MAGIC_SET(it, EINA_MAGIC_HASH_ITERATOR);
1366
1367    return &it->iterator;
1368 }
1369
1370 /**
1371  * @brief Returned a new iterator asociated to hash keys.
1372  *
1373  * @param hash The hash.
1374  * @return A new iterator.
1375  *
1376  * This function returns a newly allocated iterator associated to @p
1377  * hash. If @p hash is not populated, this function still returns a
1378  * valid iterator that will always return false on
1379  * eina_iterator_next(), thus keeping API sane.
1380  *
1381  * If the memory can not be allocated, NULL is returned and
1382  * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
1383  * returned.
1384  *
1385  * @warning if the hash structure changes then the iterator becomes
1386  *    invalid! That is, if you add or remove items this iterator
1387  *    behavior is undefined and your program may crash!
1388  */
1389 EAPI Eina_Iterator *
1390 eina_hash_iterator_key_new(const Eina_Hash *hash)
1391 {
1392    Eina_Iterator_Hash *it;
1393
1394    EINA_MAGIC_CHECK_HASH(hash);
1395    EINA_SAFETY_ON_NULL_RETURN_VAL(hash, NULL);
1396
1397    eina_error_set(0);
1398    it = calloc(1, sizeof (Eina_Iterator_Hash));
1399    if (!it) {
1400       eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
1401       return NULL;
1402    }
1403
1404    it->hash = hash;
1405    it->get_content = FUNC_ITERATOR_GET_CONTENT(_eina_hash_iterator_key_get_content);
1406
1407    it->iterator.next = FUNC_ITERATOR_NEXT(_eina_hash_iterator_next);
1408    it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(_eina_hash_iterator_get_container);
1409    it->iterator.free = FUNC_ITERATOR_FREE(_eina_hash_iterator_free);
1410
1411    EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);
1412    EINA_MAGIC_SET(it, EINA_MAGIC_HASH_ITERATOR);
1413
1414    return &it->iterator;
1415 }
1416
1417 /**
1418  * @brief Returned a new iterator asociated to hash keys and data.
1419  *
1420  * @param hash The hash.
1421  * @return A new iterator.
1422  *
1423  * This function returns a newly allocated iterator associated to @p
1424  * hash. If @p hash is not populated, this function still returns a
1425  * valid iterator that will always return false on
1426  * eina_iterator_next(), thus keeping API sane.
1427  *
1428  * If the memory can not be allocated, NULL is returned and
1429  * #EINA_ERROR_OUT_OF_MEMORY is set. Otherwise, a valid iterator is
1430  * returned.
1431  *
1432  * @note iterator data will provide values as Eina_Hash_Tuple that should not
1433  *   be modified!
1434  *
1435  * @warning if the hash structure changes then the iterator becomes
1436  *    invalid! That is, if you add or remove items this iterator
1437  *    behavior is undefined and your program may crash!
1438  */
1439 EAPI Eina_Iterator *
1440 eina_hash_iterator_tuple_new(const Eina_Hash *hash)
1441 {
1442    Eina_Iterator_Hash *it;
1443
1444    EINA_MAGIC_CHECK_HASH(hash);
1445    EINA_SAFETY_ON_NULL_RETURN_VAL(hash, NULL);
1446
1447    eina_error_set(0);
1448    it = calloc(1, sizeof (Eina_Iterator_Hash));
1449    if (!it) {
1450       eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
1451       return NULL;
1452    }
1453
1454    it->hash = hash;
1455    it->get_content = FUNC_ITERATOR_GET_CONTENT(_eina_hash_iterator_tuple_get_content);
1456
1457    it->iterator.next = FUNC_ITERATOR_NEXT(_eina_hash_iterator_next);
1458    it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(_eina_hash_iterator_get_container);
1459    it->iterator.free = FUNC_ITERATOR_FREE(_eina_hash_iterator_free);
1460
1461    EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);
1462    EINA_MAGIC_SET(it, EINA_MAGIC_HASH_ITERATOR);
1463
1464    return &it->iterator;
1465 }
1466
1467 /* Common hash functions */
1468
1469 /* Paul Hsieh (http://www.azillionmonkeys.com/qed/hash.html)
1470    used by WebCore (http://webkit.org/blog/8/hashtables-part-2/) */
1471 EAPI int
1472 eina_hash_superfast(const char *key, int len)
1473 {
1474    int hash = len, tmp;
1475    int rem;
1476
1477    rem = len & 3;
1478    len >>= 2;
1479
1480    /* Main loop */
1481    for ( ;len > 0; len--)
1482      {
1483         hash += get16bits(key);
1484         tmp = (get16bits(key + 2) << 11) ^ hash;
1485         hash = (hash << 16) ^ tmp;
1486         key += 2 * sizeof (uint16_t);
1487         hash += hash >> 11;
1488      }
1489
1490    /* Handle end cases */
1491    switch (rem)
1492      {
1493       case 3:
1494          hash += get16bits(key);
1495          hash ^= hash << 16;
1496          hash ^= key[sizeof (uint16_t)] << 18;
1497          hash += hash >> 11;
1498          break;
1499       case 2:
1500          hash += get16bits(key);
1501          hash ^= hash << 11;
1502          hash += hash >> 17;
1503          break;
1504       case 1:
1505          hash += *key;
1506          hash ^= hash << 10;
1507          hash += hash >> 1;
1508      }
1509
1510    /* Force "avalanching" of final 127 bits */
1511    hash ^= hash << 3;
1512    hash += hash >> 5;
1513    hash ^= hash << 4;
1514    hash += hash >> 17;
1515    hash ^= hash << 25;
1516    hash += hash >> 6;
1517
1518    return hash;
1519 }
1520
1521 /**
1522  * @}
1523  */