summaryrefslogtreecommitdiff
path: root/src/benchmarks/eina/ecore_hash.c
blob: 76a5ad2e38139cc63b122a0f8b9cd355d4d7bb8d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "Ecore_Data.h"

#define PRIME_TABLE_MAX 21
#define PRIME_MIN 17
#define PRIME_MAX 16777213

#define ECORE_HASH_CHAIN_MAX 3

#define ECORE_COMPUTE_HASH(hash, key) hash->hash_func(key) % \
   ecore_prime_table[hash->size];

#define ECORE_HASH_INCREASE(hash) ((hash && ecore_prime_table[hash->size] < \
                                    PRIME_MAX) ? \
                                   (hash->nodes / \
                                    ecore_prime_table[hash->size]) > \
                                   ECORE_HASH_CHAIN_MAX : FALSE)
#define ECORE_HASH_REDUCE(hash) ((hash && ecore_prime_table[hash->size] > \
                                  PRIME_MIN) ? \
                                 (double)hash->nodes / \
                                 (double)ecore_prime_table[hash->size - 1] \
                                 < ((double)ECORE_HASH_CHAIN_MAX * \
                                    0.375) : FALSE)


static const unsigned int ecore_prime_table[] =
{
   17, 31, 61, 127, 257, 509, 1021,
   2053, 4093, 8191, 16381, 32771, 65537, 131071, 262147, 524287, 1048573,
   2097143, 4194301, 8388617, 16777213
};


/* Private hash manipulation functions */
static int                     _ecore_hash_node_add(Ecore_Hash *hash,
                                                    Ecore_Hash_Node *node);
static Ecore_Hash_Node *       _ecore_hash_node_get(Ecore_Hash *hash,
                                                    const void *key);
static int                     _ecore_hash_increase(Ecore_Hash *hash);
static int                     _ecore_hash_decrease(Ecore_Hash *hash);
static inline int              _ecore_hash_rehash(Ecore_Hash *hash,
                                                  Ecore_Hash_Node **old_table,
                                                  int old_size);
static int                     _ecore_hash_bucket_destroy(Ecore_Hash_Node *list,
                                                          Ecore_Free_Cb keyd,
                                                          Ecore_Free_Cb valued);
static inline Ecore_Hash_Node *_ecore_hash_bucket_get(Ecore_Hash *hash,
                                                      Ecore_Hash_Node *bucket,
                                                      const void *key);

static Ecore_Hash_Node *       _ecore_hash_node_new(void *key, void *value);
static int                     _ecore_hash_node_init(Ecore_Hash_Node *node,
                                                     void *key,
                                                     void *value);
static int                     _ecore_hash_node_destroy(Ecore_Hash_Node *node,
                                                        Ecore_Free_Cb keyd,
                                                        Ecore_Free_Cb valued);

/**
 * @defgroup Ecore_Data_Hash_ADT_Creation_Group Hash Creation Functions
 *
 * Functions that create hash tables.
 */

/**
 * Creates and initializes a new hash
 * @param hash_func The function for determining hash position.
 * @param compare   The function for comparing node keys.
 * @return @c NULL on error, a new hash on success.
 * @ingroup Ecore_Data_Hash_ADT_Creation_Group
 */
EAPI Ecore_Hash *
ecore_hash_new(Ecore_Hash_Cb hash_func, Ecore_Compare_Cb compare)
{
   Ecore_Hash *new_hash = (Ecore_Hash *)malloc(sizeof(Ecore_Hash));
   if (!new_hash)
      return NULL;

   if (!ecore_hash_init(new_hash, hash_func, compare))
     {
        FREE(new_hash);
        return NULL;
     }

   return new_hash;
}

/**
 * Initializes the given hash.
 * @param   hash       The given hash.
 * @param   hash_func  The function used for hashing node keys.
 * @param   compare    The function used for comparing node keys.
 * @return  @c TRUE on success, @c FALSE on an error.
 * @ingroup Ecore_Data_Hash_ADT_Creation_Group
 */
EAPI int
ecore_hash_init(Ecore_Hash *hash,
                Ecore_Hash_Cb hash_func,
                Ecore_Compare_Cb compare)
{
   CHECK_PARAM_POINTER_RETURN("hash", hash, FALSE);

   memset(hash, 0, sizeof(Ecore_Hash));

   hash->hash_func = hash_func;
   hash->compare = compare;

   hash->buckets = (Ecore_Hash_Node **)calloc(ecore_prime_table[0],
                                              sizeof(Ecore_Hash_Node *));

   return TRUE;
}

/**
 * @defgroup Ecore_Data_Hash_ADT_Destruction_Group Hash Destruction Functions
 *
 * Functions that destroy hash tables and their contents.
 */

/**
 * Sets the function to destroy the keys of the given hash.
 * @param   hash     The given hash.
 * @param   function The function used to free the node keys. NULL is a
 *          valid value and means that no function will be called.
 * @return  @c TRUE on success, @c FALSE on error.
 * @ingroup Ecore_Data_Hash_ADT_Destruction_Group
 */
EAPI int
ecore_hash_free_key_cb_set(Ecore_Hash *hash, Ecore_Free_Cb function)
{
   CHECK_PARAM_POINTER_RETURN("hash", hash, FALSE);

   hash->free_key = function;

   return TRUE;
}

/**
 * Sets the function to destroy the values in the given hash.
 * @param   hash     The given hash.
 * @param   function The function that will free the node values. NULL is a
 *          valid value and means that no function will be called.
 * @return  @c TRUE on success, @c FALSE on error
 * @ingroup Ecore_Data_Hash_ADT_Destruction_Group
 */
EAPI int
ecore_hash_free_value_cb_set(Ecore_Hash *hash, Ecore_Free_Cb function)
{
   CHECK_PARAM_POINTER_RETURN("hash", hash, FALSE);

   hash->free_value = function;

   return TRUE;
}

/**
 * @defgroup Ecore_Data_Hash_ADT_Data_Group Hash Data Functions
 *
 * Functions that set, access and delete values from the hash tables.
 */

/**
 * Sets a key-value pair in the given hash table.
 * @param   hash    The given hash table.
 * @param   key     The key.
 * @param   value   The value.
 * @return  @c TRUE if successful, @c FALSE if not.
 * @ingroup Ecore_Data_Hash_ADT_Data_Group
 */
EAPI int
ecore_hash_set(Ecore_Hash *hash, void *key, void *value)
{
   int ret = FALSE;
   Ecore_Hash_Node *node;

   CHECK_PARAM_POINTER_RETURN("hash", hash, FALSE);

   node = _ecore_hash_node_get(hash, key);
   if (node)
     {
        if (hash->free_key)
           hash->free_key(key);

        if (node->value && hash->free_value)
           hash->free_value(node->value);

        node->value = value;
        ret = TRUE;
     }
   else
     {
        node = _ecore_hash_node_new(key, value);
        if (node)
           ret = _ecore_hash_node_add(hash, node);
     }

   return ret;
}

/**
 * Sets all key-value pairs from set in the given hash table.
 * @param   hash    The given hash table.
 * @param   set     The hash table to import.
 * @return  @c TRUE if successful, @c FALSE if not.
 * @ingroup Ecore_Data_Hash_ADT_Data_Group
 */
EAPI int
ecore_hash_hash_set(Ecore_Hash *hash, Ecore_Hash *set)
{
   unsigned int i;
   Ecore_Hash_Node *node, *old;

   CHECK_PARAM_POINTER_RETURN("hash", hash, FALSE);
   CHECK_PARAM_POINTER_RETURN("set",  set,  FALSE);

   for (i = 0; i < ecore_prime_table[set->size]; i++)
     {
        /* Hash into a new list to avoid loops of rehashing the same nodes */
        while ((old = set->buckets[i]))
          {
             set->buckets[i] = old->next;
             old->next = NULL;
             node = _ecore_hash_node_get(hash, old->key);
             if (node)
               {
                  /* This key already exists. Delete the old and add the new
                   * value */
                  if (hash->free_key)
                     hash->free_key(node->key);

                  if (hash->free_value)
                     hash->free_value(node->value);

                  node->key = old->key;
                  node->value = old->value;
                  free(old);
               }
             else
                _ecore_hash_node_add(hash, old);
          }
     }
   FREE(set->buckets);
   ecore_hash_init(set, set->hash_func, set->compare);
   return TRUE;
}

/**
 * @brief Frees the hash table and the data contained inside it.
 * @param   hash: The hash table to destroy.
 * @ingroup Ecore_Data_Hash_ADT_Destruction_Group
 */
EAPI void
ecore_hash_destroy(Ecore_Hash *hash)
{
   unsigned int i = 0;

   CHECK_PARAM_POINTER("hash", hash);

   if (hash->buckets)
     {
        while (i < ecore_prime_table[hash->size])
          {
             if (hash->buckets[i])
               {
                  Ecore_Hash_Node *bucket;

                  /*
                   * Remove the bucket list to avoid possible recursion
                   * on the free callbacks.
                   */
                  bucket = hash->buckets[i];
                  hash->buckets[i] = NULL;
                  _ecore_hash_bucket_destroy(bucket,
                                             hash->free_key,
                                             hash->free_value);
               }

             i++;
          }

        FREE(hash->buckets);
     }

        FREE(hash);

   return;
}

/**
 * @defgroup Ecore_Data_Hash_ADT_Traverse_Group Hash Traverse Functions
 *
 * Functions that iterate through hash tables.
 */

/**
 * Counts the number of nodes in a hash table.
 * @param   hash The hash table to count current nodes.
 * @return  The number of nodes in the hash.
 * @ingroup Ecore_Data_Hash_ADT_Destruction_Group
 */
EAPI int
ecore_hash_count(Ecore_Hash *hash)
{
   CHECK_PARAM_POINTER_RETURN("hash", hash, 0);

   return hash->nodes;
}

/**
 * Runs the @p for_each_func function on each entry in the given hash.
 * @param   hash          The given hash.
 * @param   for_each_func The function that each entry is passed to.
 * @param		user_data			a pointer passed to calls of for_each_func
 * @return  TRUE on success, FALSE otherwise.
 * @ingroup Ecore_Data_Hash_ADT_Traverse_Group
 */
EAPI int
ecore_hash_for_each_node(Ecore_Hash *hash,
                         Ecore_For_Each for_each_func,
                         void *user_data)
{
   unsigned int i = 0;

   CHECK_PARAM_POINTER_RETURN("hash",          hash,          FALSE);
   CHECK_PARAM_POINTER_RETURN("for_each_func", for_each_func, FALSE);

   while (i < ecore_prime_table[hash->size])
     {
        if (hash->buckets[i])
          {
             Ecore_Hash_Node *node;

             for (node = hash->buckets[i]; node; node = node->next)
               {
                  for_each_func(node, user_data);
               }
          }

        i++;
     }

   return TRUE;
}

/**
 * Retrieves an ecore_list of all keys in the given hash.
 * @param   hash          The given hash.
 * @return  new ecore_list on success, NULL otherwise
 * @ingroup Ecore_Data_Hash_ADT_Traverse_Group
 */
EAPI Ecore_List *
ecore_hash_keys(Ecore_Hash *hash)
{
   unsigned int i = 0;
   Ecore_List *keys;

   CHECK_PARAM_POINTER_RETURN("hash", hash, NULL);

   keys = ecore_list_new();
   while (i < ecore_prime_table[hash->size])
     {
        if (hash->buckets[i])
          {
             Ecore_Hash_Node *node;

             for (node = hash->buckets[i]; node; node = node->next)
               {
                  ecore_list_append(keys, node->key);
               }
          }

        i++;
     }
   ecore_list_first_goto(keys);

   return keys;
}

/**
 * Prints the distribution of the given hash table for graphing.
 * @param hash The given hash table.
 */
EAPI void
ecore_hash_dump_graph(Ecore_Hash *hash)
{
   unsigned int i;

   for (i = 0; i < ecore_prime_table[hash->size]; i++)
      if (hash->buckets[i])
        {
           unsigned int n = 0;
           Ecore_Hash_Node *node;
           for (node = hash->buckets[i]; node; node = node->next)
              n++;
           printf("%u\t%u", i, n);
        }
      else
           printf("%u\t0",  i);

}

/**
 * Prints the distribution of the given hash table for graphing.
 * @param hash The given hash table.
 */
EAPI void
ecore_hash_dump_stats(Ecore_Hash *hash)
{
   unsigned int i;
   double variance, sum_n_2 = 0, sum_n = 0;

   for (i = 0; i < ecore_prime_table[hash->size]; i++)
     {
        if (hash->buckets[i])
          {
             int n = 0;
             Ecore_Hash_Node *node;
             for (node = hash->buckets[i]; node; node = node->next)
                n++;
             sum_n_2 += ((double)n * (double)n);
             sum_n += (double)n;
          }
     }
   variance = (sum_n_2 - ((sum_n * sum_n) / (double)i)) / (double)i;
           printf("Average length: %f\n\tvariance^2: %f", (sum_n / (double)i),
          variance);
}

static int
_ecore_hash_bucket_destroy(Ecore_Hash_Node *list,
                           Ecore_Free_Cb keyd,
                           Ecore_Free_Cb valued)
{
   Ecore_Hash_Node *node;

   CHECK_PARAM_POINTER_RETURN("list", list, FALSE);

   for (node = list; node; node = list)
     {
        list = list->next;
        _ecore_hash_node_destroy(node, keyd, valued);
     }

   return TRUE;
}

/*
 * @brief Add the node to the hash table
 * @param hash: the hash table to add the key
 * @param node: the node to add to the hash table
 * @return Returns FALSE on error, TRUE on success
 */
static int
_ecore_hash_node_add(Ecore_Hash *hash, Ecore_Hash_Node *node)
{
   size_t hash_val;

   CHECK_PARAM_POINTER_RETURN("hash", hash, FALSE);
   CHECK_PARAM_POINTER_RETURN("node", node, FALSE);

   /* Check to see if the hash needs to be resized */
   if (ECORE_HASH_INCREASE(hash))
      _ecore_hash_increase(hash);

   /* Compute the position in the table */
   if (!hash->hash_func)
      hash_val = (size_t)node->key % ecore_prime_table[hash->size];
   else
      hash_val = ECORE_COMPUTE_HASH(hash, node->key);

   /* Prepend the node to the list at the index position */
   node->next = hash->buckets[hash_val];
   hash->buckets[hash_val] = node;
   hash->nodes++;

   return TRUE;
}

/**
 * Retrieves the value associated with the given key from the given hash
 * table.
 * @param   hash The given hash table.
 * @param   key  The key to search for.
 * @return  The value corresponding to key on success, @c NULL otherwise.
 * @ingroup Ecore_Data_Hash_ADT_Data_Group
 */
EAPI void *
ecore_hash_get(Ecore_Hash *hash, const void *key)
{
   void *data;
   Ecore_Hash_Node *node;

   CHECK_PARAM_POINTER_RETURN("hash", hash, NULL);

   node = _ecore_hash_node_get(hash, key);
   if (!node)
      return NULL;

   data = node->value;

   return data;
}

/**
 * Removes the value associated with the given key in the given hash
 * table.
 * @param   hash The given hash table.
 * @param   key  The key to search for.
 * @return  The value corresponding to the key on success.  @c NULL is
 *          returned if there is an error.
 * @ingroup Ecore_Data_Hash_ADT_Data_Group
 */
EAPI void *
ecore_hash_remove(Ecore_Hash *hash, const void *key)
{
   Ecore_Hash_Node *node = NULL;
   Ecore_Hash_Node *list;
   size_t hash_val;
   void *ret = NULL;

   CHECK_PARAM_POINTER_RETURN("hash", hash, NULL);

   /* Compute the position in the table */
   if (!hash->hash_func)
      hash_val = (size_t)key % ecore_prime_table[hash->size];
   else
      hash_val = ECORE_COMPUTE_HASH(hash, key);

   /*
    * If their is a list that could possibly hold the key/value pair
    * traverse it and remove the hash node.
    */
   if (hash->buckets[hash_val])
     {
        list = hash->buckets[hash_val];

        /*
         * Traverse the list to find the specified key
         */
        node = list;
        if (hash->compare)
           while ((node) && (hash->compare(node->key, key) != 0))
             {
                list = node;
                node = node->next;
             }
        else
           while ((node) && (node->key != key))
             {
                list = node;
                node = node->next;
             }

        /*
         * Remove the node with the matching key and free it's memory
         */
        if (node)
          {
             if (list == node)
                hash->buckets[hash_val] = node->next;
             else
                list->next = node->next;

             ret = node->value;
             node->value = NULL;
             _ecore_hash_node_destroy(node, hash->free_key, NULL);
             hash->nodes--;
          }
     }

   if (ECORE_HASH_REDUCE(hash))
      _ecore_hash_decrease(hash);

   return ret;
}

/**
 * Retrieves the first value that matches
 * table.
 * @param   hash The given hash table.
 * @param   key  The key to search for.
 * @return  The value corresponding to key on success, @c NULL otherwise.
 * @ingroup Ecore_Data_Hash_ADT_Data_Group
 */
EAPI void *
ecore_hash_find(Ecore_Hash *hash, Ecore_Compare_Cb compare, const void *value)
{
   unsigned int i = 0;

   CHECK_PARAM_POINTER_RETURN("hash",    hash,    NULL);
   CHECK_PARAM_POINTER_RETURN("compare", compare, NULL);
   CHECK_PARAM_POINTER_RETURN("value",   value,   NULL);

   while (i < ecore_prime_table[hash->size])
     {
        if (hash->buckets[i])
          {
             Ecore_Hash_Node *node;

             for (node = hash->buckets[i]; node; node = node->next)
               {
                  if (!compare(node->value, value))
                     return node->value;
               }
          }

        i++;
     }

   return NULL;
}

/*
 * @brief Retrieve the node associated with key
 * @param hash: the hash table to search for the key
 * @param key: the key to search for in the hash table
 * @return Returns NULL on error, node corresponding to key on success
 */
static Ecore_Hash_Node *
_ecore_hash_node_get(Ecore_Hash *hash, const void *key)
{
   size_t hash_val;
   Ecore_Hash_Node *node = NULL;

   CHECK_PARAM_POINTER_RETURN("hash", hash, NULL);

   if (!hash->buckets)
      return NULL;

   /* Compute the position in the table */
   if (!hash->hash_func)
      hash_val = (size_t)key % ecore_prime_table[hash->size];
   else
      hash_val = ECORE_COMPUTE_HASH(hash, key);

   /* Grab the bucket at the specified position */
   if (hash->buckets[hash_val])
     {
        node = _ecore_hash_bucket_get(hash, hash->buckets[hash_val], key);
        /*
         * Move matched node to the front of the list as it's likely
         * to be searched for again soon.
         */
        if (node && node != hash->buckets[hash_val])
          {
             node->next = hash->buckets[hash_val];
             hash->buckets[hash_val] = node;
          }
     }

   return node;
}

/*
 * @brief Search the hash bucket for a specified key
 * @param hash: the hash table to retrieve the comparison function
 * @param bucket: the list to search for the key
 * @param key: the key to search for in the list
 * @return Returns NULL on error or not found, the found node on success
 */
static inline Ecore_Hash_Node *
_ecore_hash_bucket_get(Ecore_Hash *hash,
                       Ecore_Hash_Node *bucket,
                       const void *key)
{
   Ecore_Hash_Node *prev = NULL;
   Ecore_Hash_Node *node = NULL;

   /*
    * Traverse the list to find the desired node, if the node is in the
    * list, then return the node.
    */
   if (hash->compare)
      for (node = bucket; node; node = node->next)
        {
           if (hash->compare(node->key, key) == 0)
              break;

           prev = node;
        }
   else
      for (node = bucket; node; node = node->next)
        {
           if (node->key == key)
              break;

           prev = node;
        }

   /*
    * Remove node from the list to replace it at the beginning.
    */
   if (node && prev)
     {
        prev->next = node->next;
        node->next = NULL;
     }

   return node;
}

/*
 * @brief Increase the size of the hash table by approx.  2 * current size
 * @param hash: the hash table to increase the size of
 * @return Returns TRUE on success, FALSE on error
 */
static int
_ecore_hash_increase(Ecore_Hash *hash)
{
   void *old;

   CHECK_PARAM_POINTER_RETURN("hash", hash, FALSE);

   /* Max size reached so return FALSE */
   if ((ecore_prime_table[hash->size] == PRIME_MAX) ||
       (hash->size == PRIME_TABLE_MAX))
      return FALSE;

   /*
    * Increase the size of the hash and save a pointer to the old data
    */
   hash->size++;
   old = hash->buckets;

   /*
    * Allocate a new bucket area, of the new larger size
    */
   hash->buckets =
      calloc(ecore_prime_table[hash->size], sizeof(Ecore_Hash_Node *));

   /*
    * Make sure the allocation succeeded, if not replace the old data and
    * return a failure.
    */
   if (!hash->buckets)
     {
        hash->buckets = old;
        hash->size--;
        return FALSE;
     }

   hash->nodes = 0;

   /*
    * Now move all of the old data into the new bucket area
    */
   if (_ecore_hash_rehash(hash, old, hash->size - 1))
     {
        FREE(old);
        return TRUE;
     }

   /*
    * Free the old buckets regardless of success.
    */
        FREE(old);

   return FALSE;
}

/*
 * @brief Decrease the size of the hash table by < 1/2 * current size
 * @param hash: the hash table to decrease the size of
 * @return Returns TRUE on success, FALSE on error
 */
static int
_ecore_hash_decrease(Ecore_Hash *hash)
{
   Ecore_Hash_Node **old;

   CHECK_PARAM_POINTER_RETURN("hash", hash, FALSE);

   if (ecore_prime_table[hash->size] == PRIME_MIN)
      return FALSE;

   /*
    * Decrease the hash size and store a pointer to the old data
    */
   hash->size--;
   old = hash->buckets;

   /*
    * Allocate a new area to store the data
    */
   hash->buckets = (Ecore_Hash_Node **)calloc(ecore_prime_table[hash->size],
                                              sizeof(Ecore_Hash_Node *));

   /*
    * Make sure allocation succeeded otherwise rreturn to the previous
    * state
    */
   if (!hash->buckets)
     {
        hash->buckets = old;
        hash->size++;
        return FALSE;
     }

   hash->nodes = 0;

   if (_ecore_hash_rehash(hash, old, hash->size + 1))
     {
        FREE(old);
        return TRUE;
     }

   return FALSE;
}

/*
 * @brief Rehash the nodes of a table into the hash table
 * @param hash: the hash to place the nodes of the table
 * @param table: the table to remove the nodes from and place in hash
 * @return Returns TRUE on success, FALSE on error
 */
static inline int
_ecore_hash_rehash(Ecore_Hash *hash, Ecore_Hash_Node **old_table, int old_size)
{
   unsigned int i;
   Ecore_Hash_Node *old;

   CHECK_PARAM_POINTER_RETURN("hash",      hash,      FALSE);
   CHECK_PARAM_POINTER_RETURN("old_table", old_table, FALSE);

   for (i = 0; i < ecore_prime_table[old_size]; i++)
     {
        /* Hash into a new list to avoid loops of rehashing the same nodes */
        while ((old = old_table[i]))
          {
             old_table[i] = old->next;
             old->next = NULL;
             _ecore_hash_node_add(hash, old);
          }
     }

   return TRUE;
}

/*
 * @brief Create a new hash node for key and value storage
 * @param key: the key for this node
 * @param value: the value that the key references
 * @return Returns NULL on error, a new hash node on success
 */
static Ecore_Hash_Node *
_ecore_hash_node_new(void *key, void *value)
{
   Ecore_Hash_Node *node;

   node = (Ecore_Hash_Node *)malloc(sizeof(Ecore_Hash_Node));
   if (!node)
      return NULL;

   if (!_ecore_hash_node_init(node, key, value))
     {
        FREE(node);
        return NULL;
     }

   return node;
}

/*
 * @brief Initialize a hash node to some sane default values
 * @param node: the node to set the values
 * @param key: the key to reference this node
 * @param value: the value that key refers to
 * @return Returns TRUE on success, FALSE on error
 */
static int
_ecore_hash_node_init(Ecore_Hash_Node *node, void *key, void *value)
{
      CHECK_PARAM_POINTER_RETURN("node", node, FALSE);

   node->key = key;
   node->value = value;

   return TRUE;
}

/*
 * @brief Destroy a node and call the specified callbacks to free data
 * @param node: the node to be destroyed
 * @param keyd: the function to free the key
 * @param valued: the function  to free the value
 * @return Returns TRUE on success, FALSE on error
 */
static int
_ecore_hash_node_destroy(Ecore_Hash_Node *node,
                         Ecore_Free_Cb keyd,
                         Ecore_Free_Cb valued)
{
      CHECK_PARAM_POINTER_RETURN("node", node, FALSE);

   if (keyd)
      keyd(node->key);

   if (valued)
      valued(node->value);

   FREE(node);

   return TRUE;
}

int
ecore_str_compare(const void *key1, const void *key2)
{
   const char *k1, *k2;

   if (!key1 || !key2)
      return ecore_direct_compare(key1, key2);
   else if (key1 == key2)
      return 0;

   k1 = key1;
   k2 = key2;

   return strcmp(k1, k2);
}

unsigned int
ecore_str_hash(const void *key)
{
   int i;
   unsigned int mask;
   unsigned int value = 0;
   const char *k = key;

   if (!k)
      return 0;

   mask = (sizeof(unsigned int) * 8) - 1;

   for (i = 0; k[i] != '\0'; i++)
     {
        value ^= ((unsigned int)k[i] << ((i * 5) & mask));
     }

   return value;
}