summaryrefslogtreecommitdiff
path: root/libs/fluidsynth/src/fluid_hash.c
blob: 9d5a92009e651c107b1697c171a5e1cfc6b3339a (plain)
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
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
/* GLIB - Library of useful routines for C programming
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02110-1301, USA.
 */

/*
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GLib Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
 *
 * Adapted for FluidSynth use by Josh Green <jgreen@users.sourceforge.net>
 * September 8, 2009 from glib 2.18.4
 */

/*
 * MT safe
 */

#include "fluidsynth_priv.h"
#include "fluid_hash.h"
#include "fluid_list.h"


#define HASH_TABLE_MIN_SIZE 11
#define HASH_TABLE_MAX_SIZE 13845163


typedef struct
{
  fluid_hashtable_t *hashtable;
  fluid_hashnode_t *prev_node;
  fluid_hashnode_t *node;
  int position;
  int pre_advanced;	// Boolean
  int version;
} RealIter;


/* Excerpt from glib gprimes.c */

static const guint primes[] =
{
  11,
  19,
  37,
  73,
  109,
  163,
  251,
  367,
  557,
  823,
  1237,
  1861,
  2777,
  4177,
  6247,
  9371,
  14057,
  21089,
  31627,
  47431,
  71143,
  106721,
  160073,
  240101,
  360163,
  540217,
  810343,
  1215497,
  1823231,
  2734867,
  4102283,
  6153409,
  9230113,
  13845163,
};

static const unsigned int nprimes = sizeof (primes) / sizeof (primes[0]);

static unsigned int
spaced_primes_closest (unsigned int num)
{
  unsigned int i;

  for (i = 0; i < nprimes; i++)
    if (primes[i] > num)
      return primes[i];

  return primes[nprimes - 1];
}

/* End excerpt from glib gprimes.c */


/*
 * @hashtable: our #fluid_hashtable_t
 * @key: the key to lookup against
 * @hash_return: optional key hash return location
 * Return value: a pointer to the described #fluid_hashnode_t pointer
 *
 * Performs a lookup in the hash table.  Virtually all hash operations
 * will use this function internally.
 *
 * This function first computes the hash value of the key using the
 * user's hash function.
 *
 * If an entry in the table matching @key is found then this function
 * returns a pointer to the pointer to that entry in the table.  In
 * the case that the entry is at the head of a chain, this pointer
 * will be an item in the nodes[] array.  In the case that the entry
 * is not at the head of a chain, this pointer will be the ->next
 * pointer on the node that preceeds it.
 *
 * In the case that no matching entry exists in the table, a pointer
 * to a %NULL pointer will be returned.  To insert a item, this %NULL
 * pointer should be updated to point to the new #fluid_hashnode_t.
 *
 * If @hash_return is a pass-by-reference parameter.  If it is
 * non-%NULL then the computed hash value is returned.  This is to
 * save insertions from having to compute the hash record again for
 * the new record.
 */
static inline fluid_hashnode_t **
fluid_hashtable_lookup_node (fluid_hashtable_t *hashtable, const void *key,
                             unsigned int *hash_return)
{
  fluid_hashnode_t **node_ptr, *node;
  unsigned int hash_value;

  hash_value = (* hashtable->hash_func)(key);
  node_ptr = &hashtable->nodes[hash_value % hashtable->size];

  if (hash_return)
    *hash_return = hash_value;

  /* Hash table lookup needs to be fast.
   *  We therefore remove the extra conditional of testing
   *  whether to call the key_equal_func or not from
   *  the inner loop.
   *
   *  Additional optimisation: first check if our full hash
   *  values are equal so we can avoid calling the full-blown
   *  key equality function in most cases.
   */
  if (hashtable->key_equal_func)
    {
      while ((node = *node_ptr))
        {
          if (node->key_hash == hash_value &&
              hashtable->key_equal_func (node->key, key))
            break;

          node_ptr = &(*node_ptr)->next;
        }
    }
  else
    {
      while ((node = *node_ptr))
        {
          if (node->key == key)
            break;

          node_ptr = &(*node_ptr)->next;
        }
    }

  return node_ptr;
}

/*
 * @hashtable: our #fluid_hashtable_t
 * @node_ptr_ptr: a pointer to the return value from
 *   fluid_hashtable_lookup_node()
 * @notify: %TRUE if the destroy notify handlers are to be called
 *
 * Removes a node from the hash table and updates the node count.  The
 * node is freed.  No table resize is performed.
 *
 * If @notify is %TRUE then the destroy notify functions are called
 * for the key and value of the hash node.
 *
 * @node_ptr_ptr is a pass-by-reference in/out parameter.  When the
 * function is called, it should point to the pointer to the node to
 * remove.  This level of indirection is required so that the pointer
 * may be updated appropriately once the node has been removed.
 *
 * Before the function returns, the pointer at @node_ptr_ptr will be
 * updated to point to the position in the table that contains the
 * pointer to the "next" node in the chain.  This makes this function
 * convenient to use from functions that iterate over the entire
 * table.  If there is no further item in the chain then the
 * #fluid_hashnode_t pointer will be %NULL (ie: **node_ptr_ptr == %NULL).
 *
 * Since the pointer in the table to the removed node is replaced with
 * either a pointer to the next node or a %NULL pointer as
 * appropriate, the pointer at the end of @node_ptr_ptr will never be
 * modified at all.  Stay tuned. :)
 */
static void
fluid_hashtable_remove_node (fluid_hashtable_t *hashtable,
                             fluid_hashnode_t  ***node_ptr_ptr, int notify)
{
  fluid_hashnode_t **node_ptr, *node;

  node_ptr = *node_ptr_ptr;
  node = *node_ptr;

  *node_ptr = node->next;

  if (notify && hashtable->key_destroy_func)
    hashtable->key_destroy_func (node->key);

  if (notify && hashtable->value_destroy_func)
    hashtable->value_destroy_func (node->value);

  FLUID_FREE (node);

  hashtable->nnodes--;
}

/*
 * fluid_hashtable_remove_all_nodes:
 * @hashtable: our #fluid_hashtable_t
 * @notify: %TRUE if the destroy notify handlers are to be called
 *
 * Removes all nodes from the table.  Since this may be a precursor to
 * freeing the table entirely, no resize is performed.
 *
 * If @notify is %TRUE then the destroy notify functions are called
 * for the key and value of the hash node.
 */
static void
fluid_hashtable_remove_all_nodes (fluid_hashtable_t *hashtable, int notify)
{
  fluid_hashnode_t **node_ptr;
  int i;

  for (i = 0; i < hashtable->size; i++)
    for (node_ptr = &hashtable->nodes[i]; *node_ptr != NULL;)
      fluid_hashtable_remove_node (hashtable, &node_ptr, notify);

  hashtable->nnodes = 0;
}

/*
 * fluid_hashtable_resize:
 * @hashtable: our #fluid_hashtable_t
 *
 * Resizes the hash table to the optimal size based on the number of
 * nodes currently held.  If you call this function then a resize will
 * occur, even if one does not need to occur.  Use
 * fluid_hashtable_maybe_resize() instead.
 */
static void
fluid_hashtable_resize (fluid_hashtable_t *hashtable)
{
  fluid_hashnode_t **new_nodes;
  fluid_hashnode_t *node;
  fluid_hashnode_t *next;
  unsigned int hash_val;
  int new_size;
  int i;

  new_size = spaced_primes_closest (hashtable->nnodes);
  new_size = (new_size < HASH_TABLE_MIN_SIZE) ? HASH_TABLE_MIN_SIZE :
    ((new_size > HASH_TABLE_MAX_SIZE) ? HASH_TABLE_MAX_SIZE : new_size);

  new_nodes = FLUID_ARRAY (fluid_hashnode_t *, new_size);

  if (!new_nodes)
  {
    FLUID_LOG (FLUID_ERR, "Out of memory");
    return;
  }

  FLUID_MEMSET (new_nodes, 0, new_size * sizeof (fluid_hashnode_t *));

  for (i = 0; i < hashtable->size; i++)
    for (node = hashtable->nodes[i]; node; node = next)
      {
	next = node->next;

	hash_val = node->key_hash % new_size;

	node->next = new_nodes[hash_val];
	new_nodes[hash_val] = node;
      }

  FLUID_FREE (hashtable->nodes);
  hashtable->nodes = new_nodes;
  hashtable->size = new_size;
}

/*
 * fluid_hashtable_maybe_resize:
 * @hashtable: our #fluid_hashtable_t
 *
 * Resizes the hash table, if needed.
 *
 * Essentially, calls fluid_hashtable_resize() if the table has strayed
 * too far from its ideal size for its number of nodes.
 */
static inline void
fluid_hashtable_maybe_resize (fluid_hashtable_t *hashtable)
{
  int nnodes = hashtable->nnodes;
  int size = hashtable->size;

  if ((size >= 3 * nnodes && size > HASH_TABLE_MIN_SIZE) ||
      (3 * size <= nnodes && size < HASH_TABLE_MAX_SIZE))
    fluid_hashtable_resize (hashtable);
}

/**
 * new_fluid_hashtable:
 * @hash_func: a function to create a hash value from a key.
 *   Hash values are used to determine where keys are stored within the
 *   #fluid_hashtable_t data structure. The fluid_direct_hash(), fluid_int_hash() and
 *   fluid_str_hash() functions are provided for some common types of keys.
 *   If hash_func is %NULL, fluid_direct_hash() is used.
 * @key_equal_func: a function to check two keys for equality.  This is
 *   used when looking up keys in the #fluid_hashtable_t.  The fluid_direct_equal(),
 *   fluid_int_equal() and fluid_str_equal() functions are provided for the most
 *   common types of keys. If @key_equal_func is %NULL, keys are compared
 *   directly in a similar fashion to fluid_direct_equal(), but without the
 *   overhead of a function call.
 *
 * Creates a new #fluid_hashtable_t with a reference count of 1.
 *
 * Return value: a new #fluid_hashtable_t.
 **/
fluid_hashtable_t*
new_fluid_hashtable (fluid_hash_func_t hash_func, fluid_equal_func_t key_equal_func)
{
  return new_fluid_hashtable_full (hash_func, key_equal_func, NULL, NULL);
}


/**
 * new_fluid_hashtable_full:
 * @hash_func: a function to create a hash value from a key.
 * @key_equal_func: a function to check two keys for equality.
 * @key_destroy_func: a function to free the memory allocated for the key
 *   used when removing the entry from the #fluid_hashtable_t or %NULL if you
 *   don't want to supply such a function.
 * @value_destroy_func: a function to free the memory allocated for the
 *   value used when removing the entry from the #fluid_hashtable_t or %NULL if
 *   you don't want to supply such a function.
 *
 * Creates a new #fluid_hashtable_t like fluid_hashtable_new() with a reference count
 * of 1 and allows to specify functions to free the memory allocated for the
 * key and value that get called when removing the entry from the #fluid_hashtable_t.
 *
 * Return value: a new #fluid_hashtable_t.
 **/
fluid_hashtable_t*
new_fluid_hashtable_full (fluid_hash_func_t hash_func,
                          fluid_equal_func_t key_equal_func,
                          fluid_destroy_notify_t key_destroy_func,
                          fluid_destroy_notify_t value_destroy_func)
{
  fluid_hashtable_t *hashtable;

  hashtable = FLUID_NEW (fluid_hashtable_t);

  if (!hashtable)
  {
    FLUID_LOG (FLUID_ERR, "Out of memory");
    return NULL;
  }

  hashtable->size               = HASH_TABLE_MIN_SIZE;
  hashtable->nnodes             = 0;
  hashtable->hash_func          = hash_func ? hash_func : fluid_direct_hash;
  hashtable->key_equal_func     = key_equal_func;
  hashtable->ref_count          = 1;
  hashtable->key_destroy_func   = key_destroy_func;
  hashtable->value_destroy_func = value_destroy_func;
  hashtable->nodes              = FLUID_ARRAY (fluid_hashnode_t*, hashtable->size);
  FLUID_MEMSET (hashtable->nodes, 0, hashtable->size * sizeof (fluid_hashnode_t *));

  return hashtable;
}

/**
 * fluid_hashtable_iter_init:
 * @iter: an uninitialized #fluid_hashtable_iter_t.
 * @hashtable: a #fluid_hashtable_t.
 *
 * Initializes a key/value pair iterator and associates it with
 * @hashtable. Modifying the hash table after calling this function
 * invalidates the returned iterator.
 * |[
 * fluid_hashtable_iter_t iter;
 * gpointer key, value;
 *
 * fluid_hashtable_iter_init (&iter, hashtable);
 * while (fluid_hashtable_iter_next (&iter, &key, &value)) 
 *   {
 *     /&ast; do something with key and value &ast;/
 *   }
 * ]|
 *
 * Since: 2.16
 **/
void
fluid_hashtable_iter_init (fluid_hashtable_iter_t *iter,
                           fluid_hashtable_t *hashtable)
{
  RealIter *ri = (RealIter *) iter;

  fluid_return_if_fail (iter != NULL);
  fluid_return_if_fail (hashtable != NULL);

  ri->hashtable = hashtable;
  ri->prev_node = NULL;
  ri->node = NULL;
  ri->position = -1;
  ri->pre_advanced = FALSE;
}

/**
 * fluid_hashtable_iter_next:
 * @iter: an initialized #fluid_hashtable_iter_t.
 * @key: a location to store the key, or %NULL.
 * @value: a location to store the value, or %NULL.
 *
 * Advances @iter and retrieves the key and/or value that are now
 * pointed to as a result of this advancement. If %FALSE is returned,
 * @key and @value are not set, and the iterator becomes invalid.
 *
 * Return value: %FALSE if the end of the #fluid_hashtable_t has been reached.
 *
 * Since: 2.16
 **/
int
fluid_hashtable_iter_next (fluid_hashtable_iter_t *iter, void **key,
                           void **value)
{
  RealIter *ri = (RealIter *) iter;

  fluid_return_val_if_fail (iter != NULL, FALSE);

  if (ri->pre_advanced)
    {
      ri->pre_advanced = FALSE;

      if (ri->node == NULL)
	return FALSE;
    }
  else
    {
      if (ri->node != NULL)
	{
	  ri->prev_node = ri->node;
	  ri->node = ri->node->next;
	}

      while (ri->node == NULL)
	{
	  ri->position++;
	  if (ri->position >= ri->hashtable->size)
	    return FALSE;

	  ri->prev_node = NULL;
	  ri->node = ri->hashtable->nodes[ri->position];
	}
    }

  if (key != NULL)
    *key = ri->node->key;
  if (value != NULL)
    *value = ri->node->value;

  return TRUE;
}

/**
 * fluid_hashtable_iter_get_hash_table:
 * @iter: an initialized #fluid_hashtable_iter_t.
 *
 * Returns the #fluid_hashtable_t associated with @iter.
 *
 * Return value: the #fluid_hashtable_t associated with @iter.
 *
 * Since: 2.16
 **/
fluid_hashtable_t *
fluid_hashtable_iter_get_hash_table (fluid_hashtable_iter_t *iter)
{
  fluid_return_val_if_fail (iter != NULL, NULL);

  return ((RealIter *) iter)->hashtable;
}

static void
iter_remove_or_steal (RealIter *ri, int notify)
{
  fluid_hashnode_t *prev;
  fluid_hashnode_t *node;
  int position;

  fluid_return_if_fail (ri != NULL);
  fluid_return_if_fail (ri->node != NULL);

  prev = ri->prev_node;
  node = ri->node;
  position = ri->position;

  /* pre-advance the iterator since we will remove the node */

  ri->node = ri->node->next;
  /* ri->prev_node is still the correct previous node */

  while (ri->node == NULL)
    {
      ri->position++;
      if (ri->position >= ri->hashtable->size)
	break;

      ri->prev_node = NULL;
      ri->node = ri->hashtable->nodes[ri->position];
    }

  ri->pre_advanced = TRUE;

  /* remove the node */

  if (prev != NULL)
    prev->next = node->next;
  else
    ri->hashtable->nodes[position] = node->next;

  if (notify)
    {
      if (ri->hashtable->key_destroy_func)
	ri->hashtable->key_destroy_func(node->key);
      if (ri->hashtable->value_destroy_func)
	ri->hashtable->value_destroy_func(node->value);
    }

  FLUID_FREE (node);

  ri->hashtable->nnodes--;
}

/**
 * fluid_hashtable_iter_remove():
 * @iter: an initialized #fluid_hashtable_iter_t.
 *
 * Removes the key/value pair currently pointed to by the iterator
 * from its associated #fluid_hashtable_t. Can only be called after
 * fluid_hashtable_iter_next() returned %TRUE, and cannot be called more
 * than once for the same key/value pair.
 *
 * If the #fluid_hashtable_t was created using fluid_hashtable_new_full(), the
 * key and value are freed using the supplied destroy functions, otherwise
 * you have to make sure that any dynamically allocated values are freed 
 * yourself.
 *
 * Since: 2.16
 **/
void
fluid_hashtable_iter_remove (fluid_hashtable_iter_t *iter)
{
  iter_remove_or_steal ((RealIter *) iter, TRUE);
}

/**
 * fluid_hashtable_iter_steal():
 * @iter: an initialized #fluid_hashtable_iter_t.
 *
 * Removes the key/value pair currently pointed to by the iterator
 * from its associated #fluid_hashtable_t, without calling the key and value
 * destroy functions. Can only be called after
 * fluid_hashtable_iter_next() returned %TRUE, and cannot be called more
 * than once for the same key/value pair.
 *
 * Since: 2.16
 **/
void
fluid_hashtable_iter_steal (fluid_hashtable_iter_t *iter)
{
  iter_remove_or_steal ((RealIter *) iter, FALSE);
}


/**
 * fluid_hashtable_ref:
 * @hashtable: a valid #fluid_hashtable_t.
 *
 * Atomically increments the reference count of @hashtable by one.
 * This function is MT-safe and may be called from any thread.
 *
 * Return value: the passed in #fluid_hashtable_t.
 *
 * Since: 2.10
 **/
fluid_hashtable_t*
fluid_hashtable_ref (fluid_hashtable_t *hashtable)
{
  fluid_return_val_if_fail (hashtable != NULL, NULL);
  fluid_return_val_if_fail (hashtable->ref_count > 0, hashtable);

  fluid_atomic_int_add (&hashtable->ref_count, 1);
  return hashtable;
}

/**
 * fluid_hashtable_unref:
 * @hashtable: a valid #fluid_hashtable_t.
 *
 * Atomically decrements the reference count of @hashtable by one.
 * If the reference count drops to 0, all keys and values will be
 * destroyed, and all memory allocated by the hash table is released.
 * This function is MT-safe and may be called from any thread.
 *
 * Since: 2.10
 **/
void
fluid_hashtable_unref (fluid_hashtable_t *hashtable)
{
  fluid_return_if_fail (hashtable != NULL);
  fluid_return_if_fail (hashtable->ref_count > 0);

  if (fluid_atomic_int_exchange_and_add (&hashtable->ref_count, -1) - 1 == 0)
    {
      fluid_hashtable_remove_all_nodes (hashtable, TRUE);
      FLUID_FREE (hashtable->nodes);
      FLUID_FREE (hashtable);
    }
}

/**
 * delete_fluid_hashtable:
 * @hashtable: a #fluid_hashtable_t.
 *
 * Destroys all keys and values in the #fluid_hashtable_t and decrements its
 * reference count by 1. If keys and/or values are dynamically allocated,
 * you should either free them first or create the #fluid_hashtable_t with destroy
 * notifiers using fluid_hashtable_new_full(). In the latter case the destroy
 * functions you supplied will be called on all keys and values during the
 * destruction phase.
 **/
void
delete_fluid_hashtable (fluid_hashtable_t *hashtable)
{
  fluid_return_if_fail (hashtable != NULL);
  fluid_return_if_fail (hashtable->ref_count > 0);

  fluid_hashtable_remove_all (hashtable);
  fluid_hashtable_unref (hashtable);
}

/**
 * fluid_hashtable_lookup:
 * @hashtable: a #fluid_hashtable_t.
 * @key: the key to look up.
 *
 * Looks up a key in a #fluid_hashtable_t. Note that this function cannot
 * distinguish between a key that is not present and one which is present
 * and has the value %NULL. If you need this distinction, use
 * fluid_hashtable_lookup_extended().
 *
 * Return value: the associated value, or %NULL if the key is not found.
 **/
void *
fluid_hashtable_lookup (fluid_hashtable_t *hashtable, const void *key)
{
  fluid_hashnode_t *node;

  fluid_return_val_if_fail (hashtable != NULL, NULL);

  node = *fluid_hashtable_lookup_node (hashtable, key, NULL);

  return node ? node->value : NULL;
}

/**
 * fluid_hashtable_lookup_extended:
 * @hashtable: a #fluid_hashtable_t.
 * @lookup_key: the key to look up.
 * @orig_key: returns the original key.
 * @value: returns the value associated with the key.
 *
 * Looks up a key in the #fluid_hashtable_t, returning the original key and the
 * associated value and a #gboolean which is %TRUE if the key was found. This
 * is useful if you need to free the memory allocated for the original key,
 * for example before calling fluid_hashtable_remove().
 *
 * Return value: %TRUE if the key was found in the #fluid_hashtable_t.
 **/
int
fluid_hashtable_lookup_extended (fluid_hashtable_t *hashtable,
                                 const void *lookup_key,
                                 void **orig_key, void **value)
{
  fluid_hashnode_t *node;

  fluid_return_val_if_fail (hashtable != NULL, FALSE);

  node = *fluid_hashtable_lookup_node (hashtable, lookup_key, NULL);

  if (node == NULL)
    return FALSE;

  if (orig_key)
    *orig_key = node->key;

  if (value)
    *value = node->value;

  return TRUE;
}

/*
 * fluid_hashtable_insert_internal:
 * @hashtable: our #fluid_hashtable_t
 * @key: the key to insert
 * @value: the value to insert
 * @keep_new_key: if %TRUE and this key already exists in the table
 *   then call the destroy notify function on the old key.  If %FALSE
 *   then call the destroy notify function on the new key.
 *
 * Implements the common logic for the fluid_hashtable_insert() and
 * fluid_hashtable_replace() functions.
 *
 * Do a lookup of @key.  If it is found, replace it with the new
 * @value (and perhaps the new @key).  If it is not found, create a
 * new node.
 */
static void
fluid_hashtable_insert_internal (fluid_hashtable_t *hashtable, void *key,
                                 void *value, int keep_new_key)
{
  fluid_hashnode_t **node_ptr, *node;
  unsigned int key_hash;

  fluid_return_if_fail (hashtable != NULL);
  fluid_return_if_fail (hashtable->ref_count > 0);

  node_ptr = fluid_hashtable_lookup_node (hashtable, key, &key_hash);

  if ((node = *node_ptr))
    {
      if (keep_new_key)
        {
          if (hashtable->key_destroy_func)
            hashtable->key_destroy_func (node->key);
          node->key = key;
        }
      else
        {
          if (hashtable->key_destroy_func)
            hashtable->key_destroy_func (key);
        }

      if (hashtable->value_destroy_func)
        hashtable->value_destroy_func (node->value);

      node->value = value;
    }
  else
    {
      node = FLUID_NEW (fluid_hashnode_t);

      if (!node)
      {
        FLUID_LOG (FLUID_ERR, "Out of memory");
        return;
      }

      node->key = key;
      node->value = value;
      node->key_hash = key_hash;
      node->next = NULL;

      *node_ptr = node;
      hashtable->nnodes++;
      fluid_hashtable_maybe_resize (hashtable);
    }
}

/**
 * fluid_hashtable_insert:
 * @hashtable: a #fluid_hashtable_t.
 * @key: a key to insert.
 * @value: the value to associate with the key.
 *
 * Inserts a new key and value into a #fluid_hashtable_t.
 *
 * If the key already exists in the #fluid_hashtable_t its current value is replaced
 * with the new value. If you supplied a @value_destroy_func when creating the
 * #fluid_hashtable_t, the old value is freed using that function. If you supplied
 * a @key_destroy_func when creating the #fluid_hashtable_t, the passed key is freed
 * using that function.
 **/
void
fluid_hashtable_insert (fluid_hashtable_t *hashtable, void *key, void *value)
{
  fluid_hashtable_insert_internal (hashtable, key, value, FALSE);
}

/**
 * fluid_hashtable_replace:
 * @hashtable: a #fluid_hashtable_t.
 * @key: a key to insert.
 * @value: the value to associate with the key.
 *
 * Inserts a new key and value into a #fluid_hashtable_t similar to
 * fluid_hashtable_insert(). The difference is that if the key already exists
 * in the #fluid_hashtable_t, it gets replaced by the new key. If you supplied a
 * @value_destroy_func when creating the #fluid_hashtable_t, the old value is freed
 * using that function. If you supplied a @key_destroy_func when creating the
 * #fluid_hashtable_t, the old key is freed using that function.
 **/
void
fluid_hashtable_replace (fluid_hashtable_t *hashtable, void *key, void *value)
{
  fluid_hashtable_insert_internal (hashtable, key, value, TRUE);
}

/*
 * fluid_hashtable_remove_internal:
 * @hashtable: our #fluid_hashtable_t
 * @key: the key to remove
 * @notify: %TRUE if the destroy notify handlers are to be called
 * Return value: %TRUE if a node was found and removed, else %FALSE
 *
 * Implements the common logic for the fluid_hashtable_remove() and
 * fluid_hashtable_steal() functions.
 *
 * Do a lookup of @key and remove it if it is found, calling the
 * destroy notify handlers only if @notify is %TRUE.
 */
static int
fluid_hashtable_remove_internal (fluid_hashtable_t *hashtable, const void *key,
                                 int notify)
{
  fluid_hashnode_t **node_ptr;

  fluid_return_val_if_fail (hashtable != NULL, FALSE);

  node_ptr = fluid_hashtable_lookup_node (hashtable, key, NULL);
  if (*node_ptr == NULL)
    return FALSE;

  fluid_hashtable_remove_node (hashtable, &node_ptr, notify);
  fluid_hashtable_maybe_resize (hashtable);

  return TRUE;
}

/**
 * fluid_hashtable_remove:
 * @hashtable: a #fluid_hashtable_t.
 * @key: the key to remove.
 *
 * Removes a key and its associated value from a #fluid_hashtable_t.
 *
 * If the #fluid_hashtable_t was created using fluid_hashtable_new_full(), the
 * key and value are freed using the supplied destroy functions, otherwise
 * you have to make sure that any dynamically allocated values are freed
 * yourself.
 *
 * Return value: %TRUE if the key was found and removed from the #fluid_hashtable_t.
 **/
int
fluid_hashtable_remove (fluid_hashtable_t *hashtable, const void *key)
{
  return fluid_hashtable_remove_internal (hashtable, key, TRUE);
}

/**
 * fluid_hashtable_steal:
 * @hashtable: a #fluid_hashtable_t.
 * @key: the key to remove.
 *
 * Removes a key and its associated value from a #fluid_hashtable_t without
 * calling the key and value destroy functions.
 *
 * Return value: %TRUE if the key was found and removed from the #fluid_hashtable_t.
 **/
int
fluid_hashtable_steal (fluid_hashtable_t *hashtable, const void *key)
{
  return fluid_hashtable_remove_internal (hashtable, key, FALSE);
}

/**
 * fluid_hashtable_remove_all:
 * @hashtable: a #fluid_hashtable_t
 *
 * Removes all keys and their associated values from a #fluid_hashtable_t.
 *
 * If the #fluid_hashtable_t was created using fluid_hashtable_new_full(), the keys
 * and values are freed using the supplied destroy functions, otherwise you
 * have to make sure that any dynamically allocated values are freed
 * yourself.
 *
 * Since: 2.12
 **/
void
fluid_hashtable_remove_all (fluid_hashtable_t *hashtable)
{
  fluid_return_if_fail (hashtable != NULL);

  fluid_hashtable_remove_all_nodes (hashtable, TRUE);
  fluid_hashtable_maybe_resize (hashtable);
}

/**
 * fluid_hashtable_steal_all:
 * @hashtable: a #fluid_hashtable_t.
 *
 * Removes all keys and their associated values from a #fluid_hashtable_t
 * without calling the key and value destroy functions.
 *
 * Since: 2.12
 **/
void
fluid_hashtable_steal_all (fluid_hashtable_t *hashtable)
{
  fluid_return_if_fail (hashtable != NULL);

  fluid_hashtable_remove_all_nodes (hashtable, FALSE);
  fluid_hashtable_maybe_resize (hashtable);
}

/*
 * fluid_hashtable_foreach_remove_or_steal:
 * @hashtable: our #fluid_hashtable_t
 * @func: the user's callback function
 * @user_data: data for @func
 * @notify: %TRUE if the destroy notify handlers are to be called
 *
 * Implements the common logic for fluid_hashtable_foreach_remove() and
 * fluid_hashtable_foreach_steal().
 *
 * Iterates over every node in the table, calling @func with the key
 * and value of the node (and @user_data).  If @func returns %TRUE the
 * node is removed from the table.
 *
 * If @notify is true then the destroy notify handlers will be called
 * for each removed node.
 */
static unsigned int
fluid_hashtable_foreach_remove_or_steal (fluid_hashtable_t *hashtable,
                                         fluid_hr_func_t func, void *user_data,
                                         int notify)
{
  fluid_hashnode_t *node, **node_ptr;
  unsigned int deleted = 0;
  int i;

  for (i = 0; i < hashtable->size; i++)
    for (node_ptr = &hashtable->nodes[i]; (node = *node_ptr) != NULL;)
      if ((* func) (node->key, node->value, user_data))
        {
          fluid_hashtable_remove_node (hashtable, &node_ptr, notify);
          deleted++;
        }
      else
        node_ptr = &node->next;

  fluid_hashtable_maybe_resize (hashtable);

  return deleted;
}

#if 0
/**
 * fluid_hashtable_foreach_remove:
 * @hashtable: a #fluid_hashtable_t.
 * @func: the function to call for each key/value pair.
 * @user_data: user data to pass to the function.
 *
 * Calls the given function for each key/value pair in the #fluid_hashtable_t.
 * If the function returns %TRUE, then the key/value pair is removed from the
 * #fluid_hashtable_t. If you supplied key or value destroy functions when creating
 * the #fluid_hashtable_t, they are used to free the memory allocated for the removed
 * keys and values.
 *
 * See #fluid_hashtable_iter_t for an alternative way to loop over the 
 * key/value pairs in the hash table.
 *
 * Return value: the number of key/value pairs removed.
 **/
static unsigned int
fluid_hashtable_foreach_remove (fluid_hashtable_t *hashtable,
                                fluid_hr_func_t func, void *user_data)
{
  fluid_return_val_if_fail (hashtable != NULL, 0);
  fluid_return_val_if_fail (func != NULL, 0);

  return fluid_hashtable_foreach_remove_or_steal (hashtable, func, user_data, TRUE);
}
#endif

/**
 * fluid_hashtable_foreach_steal:
 * @hashtable: a #fluid_hashtable_t.
 * @func: the function to call for each key/value pair.
 * @user_data: user data to pass to the function.
 *
 * Calls the given function for each key/value pair in the #fluid_hashtable_t.
 * If the function returns %TRUE, then the key/value pair is removed from the
 * #fluid_hashtable_t, but no key or value destroy functions are called.
 *
 * See #fluid_hashtable_iter_t for an alternative way to loop over the 
 * key/value pairs in the hash table.
 *
 * Return value: the number of key/value pairs removed.
 **/
unsigned int
fluid_hashtable_foreach_steal (fluid_hashtable_t *hashtable,
                               fluid_hr_func_t func, void *user_data)
{
  fluid_return_val_if_fail (hashtable != NULL, 0);
  fluid_return_val_if_fail (func != NULL, 0);

  return fluid_hashtable_foreach_remove_or_steal (hashtable, func, user_data, FALSE);
}

/**
 * fluid_hashtable_foreach:
 * @hashtable: a #fluid_hashtable_t.
 * @func: the function to call for each key/value pair.
 * @user_data: user data to pass to the function.
 *
 * Calls the given function for each of the key/value pairs in the
 * #fluid_hashtable_t.  The function is passed the key and value of each
 * pair, and the given @user_data parameter.  The hash table may not
 * be modified while iterating over it (you can't add/remove
 * items). To remove all items matching a predicate, use
 * fluid_hashtable_foreach_remove().
 *
 * See fluid_hashtable_find() for performance caveats for linear
 * order searches in contrast to fluid_hashtable_lookup().
 **/
void
fluid_hashtable_foreach (fluid_hashtable_t *hashtable, fluid_hr_func_t func,
                         void *user_data)
{
  fluid_hashnode_t *node;
  int i;

  fluid_return_if_fail (hashtable != NULL);
  fluid_return_if_fail (func != NULL);

  for (i = 0; i < hashtable->size; i++)
    for (node = hashtable->nodes[i]; node; node = node->next)
      (* func) (node->key, node->value, user_data);
}

/**
 * fluid_hashtable_find:
 * @hashtable: a #fluid_hashtable_t.
 * @predicate:  function to test the key/value pairs for a certain property.
 * @user_data:  user data to pass to the function.
 *
 * Calls the given function for key/value pairs in the #fluid_hashtable_t until
 * @predicate returns %TRUE.  The function is passed the key and value of
 * each pair, and the given @user_data parameter. The hash table may not
 * be modified while iterating over it (you can't add/remove items).
 *
 * Note, that hash tables are really only optimized for forward lookups,
 * i.e. fluid_hashtable_lookup().
 * So code that frequently issues fluid_hashtable_find() or
 * fluid_hashtable_foreach() (e.g. in the order of once per every entry in a
 * hash table) should probably be reworked to use additional or different
 * data structures for reverse lookups (keep in mind that an O(n) find/foreach
 * operation issued for all n values in a hash table ends up needing O(n*n)
 * operations).
 *
 * Return value: The value of the first key/value pair is returned, for which
 * func evaluates to %TRUE. If no pair with the requested property is found,
 * %NULL is returned.
 *
 * Since: 2.4
 **/
void *
fluid_hashtable_find (fluid_hashtable_t *hashtable, fluid_hr_func_t predicate,
                      void *user_data)
{
  fluid_hashnode_t *node;
  int i;

  fluid_return_val_if_fail (hashtable != NULL, NULL);
  fluid_return_val_if_fail (predicate != NULL, NULL);

  for (i = 0; i < hashtable->size; i++)
    for (node = hashtable->nodes[i]; node; node = node->next)
      if (predicate (node->key, node->value, user_data))
        return node->value;
  return NULL;
}

/**
 * fluid_hashtable_size:
 * @hashtable: a #fluid_hashtable_t.
 *
 * Returns the number of elements contained in the #fluid_hashtable_t.
 *
 * Return value: the number of key/value pairs in the #fluid_hashtable_t.
 **/
unsigned int
fluid_hashtable_size (fluid_hashtable_t *hashtable)
{
  fluid_return_val_if_fail (hashtable != NULL, 0);

  return hashtable->nnodes;
}

/**
 * fluid_hashtable_get_keys:
 * @hashtable: a #fluid_hashtable_t
 *
 * Retrieves every key inside @hashtable. The returned data is valid
 * until @hashtable is modified.
 *
 * Return value: a #GList containing all the keys inside the hash
 *   table. The content of the list is owned by the hash table and
 *   should not be modified or freed. Use delete_fluid_list() when done
 *   using the list.
 *
 * Since: 2.14
 */
fluid_list_t *
fluid_hashtable_get_keys (fluid_hashtable_t *hashtable)
{
  fluid_hashnode_t *node;
  int i;
  fluid_list_t *retval;

  fluid_return_val_if_fail (hashtable != NULL, NULL);

  retval = NULL;
  for (i = 0; i < hashtable->size; i++)
    for (node = hashtable->nodes[i]; node; node = node->next)
      retval = fluid_list_prepend (retval, node->key);

  return retval;
}

/**
 * fluid_hashtable_get_values:
 * @hashtable: a #fluid_hashtable_t
 *
 * Retrieves every value inside @hashtable. The returned data is
 * valid until @hashtable is modified.
 *
 * Return value: a #GList containing all the values inside the hash
 *   table. The content of the list is owned by the hash table and
 *   should not be modified or freed. Use delete_fluid_list() when done
 *   using the list.
 *
 * Since: 2.14
 */
fluid_list_t *
fluid_hashtable_get_values (fluid_hashtable_t *hashtable)
{
  fluid_hashnode_t *node;
  int i;
  fluid_list_t *retval;

  fluid_return_val_if_fail (hashtable != NULL, NULL);

  retval = NULL;
  for (i = 0; i < hashtable->size; i++)
    for (node = hashtable->nodes[i]; node; node = node->next)
      retval = fluid_list_prepend (retval, node->value);

  return retval;
}


/* Extracted from glib/gstring.c */


/**
 * fluid_str_equal:
 * @v1: a key
 * @v2: a key to compare with @v1
 * 
 * Compares two strings for byte-by-byte equality and returns %TRUE 
 * if they are equal. It can be passed to new_fluid_hashtable() as the 
 * @key_equal_func parameter, when using strings as keys in a #Ghashtable.
 *
 * Returns: %TRUE if the two keys match
 */
int
fluid_str_equal (const void *v1, const void *v2)
{
  const char *string1 = v1;
  const char *string2 = v2;
  
  return strcmp (string1, string2) == 0;
}

/**
 * fluid_str_hash:
 * @v: a string key
 *
 * Converts a string to a hash value.
 * It can be passed to new_fluid_hashtable() as the @hash_func 
 * parameter, when using strings as keys in a #fluid_hashtable_t.
 *
 * Returns: a hash value corresponding to the key
 */
unsigned int
fluid_str_hash (const void *v)
{
  /* 31 bit hash function */
  const signed char *p = v;
  uint32 h = *p;

  if (h)
    for (p += 1; *p != '\0'; p++)
      h = (h << 5) - h + *p;

  return h;
}


/* Extracted from glib/gutils.c */


/**
 * fluid_direct_equal:
 * @v1: a key.
 * @v2: a key to compare with @v1.
 *
 * Compares two #gpointer arguments and returns %TRUE if they are equal.
 * It can be passed to new_fluid_hashtable() as the @key_equal_func
 * parameter, when using pointers as keys in a #fluid_hashtable_t.
 * 
 * Returns: %TRUE if the two keys match.
 */
int
fluid_direct_equal (const void *v1, const void *v2)
{
  return v1 == v2;
}

/**
 * fluid_direct_hash:
 * @v: a void * key
 *
 * Converts a gpointer to a hash value.
 * It can be passed to g_hashtable_new() as the @hash_func parameter, 
 * when using pointers as keys in a #fluid_hashtable_t.
 *
 * Returns: a hash value corresponding to the key.
 */
unsigned int
fluid_direct_hash (const void *v)
{
  return FLUID_POINTER_TO_UINT (v);
}

/**
 * fluid_int_equal:
 * @v1: a pointer to a int key.
 * @v2: a pointer to a int key to compare with @v1.
 *
 * Compares the two #gint values being pointed to and returns 
 * %TRUE if they are equal.
 * It can be passed to g_hashtable_new() as the @key_equal_func
 * parameter, when using pointers to integers as keys in a #fluid_hashtable_t.
 * 
 * Returns: %TRUE if the two keys match.
 */
int
fluid_int_equal (const void *v1, const void *v2)
{
  return *((const int*) v1) == *((const int*) v2);
}

/**
 * fluid_int_hash:
 * @v: a pointer to a int key
 *
 * Converts a pointer to a #gint to a hash value.
 * It can be passed to g_hashtable_new() as the @hash_func parameter, 
 * when using pointers to integers values as keys in a #fluid_hashtable_t.
 *
 * Returns: a hash value corresponding to the key.
 */
unsigned int
fluid_int_hash (const void *v)
{
  return *(const int*) v;
}