1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/.
25  *
26  * Adapted for FluidSynth use by Josh Green <jgreen@users.sourceforge.net>
27  * September 8, 2009 from glib 2.18.4
28  */
29 
30 /*
31  * MT safe
32  */
33 
34 #include "fluid_sys.h"
35 #include "fluid_hash.h"
36 #include "fluid_list.h"
37 
38 
39 #define HASH_TABLE_MIN_SIZE 11
40 #define HASH_TABLE_MAX_SIZE 13845163
41 
42 
43 typedef struct
44 {
45     fluid_hashtable_t *hashtable;
46     fluid_hashnode_t *prev_node;
47     fluid_hashnode_t *node;
48     int position;
49     int pre_advanced;	// Boolean
50     int version;
51 } RealIter;
52 
53 
54 /* Excerpt from glib gprimes.c */
55 
56 static const unsigned int primes[] =
57 {
58     11,
59     19,
60     37,
61     73,
62     109,
63     163,
64     251,
65     367,
66     557,
67     823,
68     1237,
69     1861,
70     2777,
71     4177,
72     6247,
73     9371,
74     14057,
75     21089,
76     31627,
77     47431,
78     71143,
79     106721,
80     160073,
81     240101,
82     360163,
83     540217,
84     810343,
85     1215497,
86     1823231,
87     2734867,
88     4102283,
89     6153409,
90     9230113,
91     13845163,
92 };
93 
94 static const unsigned int nprimes = FLUID_N_ELEMENTS(primes);
95 
96 static unsigned int
spaced_primes_closest(unsigned int num)97 spaced_primes_closest(unsigned int num)
98 {
99     unsigned int i;
100 
101     for(i = 0; i < nprimes; i++)
102     {
103         if(primes[i] > num)
104         {
105             return primes[i];
106         }
107     }
108 
109     return primes[nprimes - 1];
110 }
111 
112 /* End excerpt from glib gprimes.c */
113 
114 
115 /*
116  * @hashtable: our #fluid_hashtable_t
117  * @key: the key to lookup against
118  * @hash_return: optional key hash return location
119  * Return value: a pointer to the described #fluid_hashnode_t pointer
120  *
121  * Performs a lookup in the hash table.  Virtually all hash operations
122  * will use this function internally.
123  *
124  * This function first computes the hash value of the key using the
125  * user's hash function.
126  *
127  * If an entry in the table matching @key is found then this function
128  * returns a pointer to the pointer to that entry in the table.  In
129  * the case that the entry is at the head of a chain, this pointer
130  * will be an item in the nodes[] array.  In the case that the entry
131  * is not at the head of a chain, this pointer will be the ->next
132  * pointer on the node that precedes it.
133  *
134  * In the case that no matching entry exists in the table, a pointer
135  * to a %NULL pointer will be returned.  To insert a item, this %NULL
136  * pointer should be updated to point to the new #fluid_hashnode_t.
137  *
138  * If @hash_return is a pass-by-reference parameter.  If it is
139  * non-%NULL then the computed hash value is returned.  This is to
140  * save insertions from having to compute the hash record again for
141  * the new record.
142  */
143 static FLUID_INLINE fluid_hashnode_t **
fluid_hashtable_lookup_node(fluid_hashtable_t * hashtable,const void * key,unsigned int * hash_return)144 fluid_hashtable_lookup_node(fluid_hashtable_t *hashtable, const void *key,
145                             unsigned int *hash_return)
146 {
147     fluid_hashnode_t **node_ptr, *node;
148     unsigned int hash_value;
149 
150     hash_value = (* hashtable->hash_func)(key);
151     node_ptr = &hashtable->nodes[hash_value % hashtable->size];
152 
153     if(hash_return)
154     {
155         *hash_return = hash_value;
156     }
157 
158     /* Hash table lookup needs to be fast.
159      *  We therefore remove the extra conditional of testing
160      *  whether to call the key_equal_func or not from
161      *  the inner loop.
162      *
163      *  Additional optimisation: first check if our full hash
164      *  values are equal so we can avoid calling the full-blown
165      *  key equality function in most cases.
166      */
167     if(hashtable->key_equal_func)
168     {
169         while((node = *node_ptr))
170         {
171             if(node->key_hash == hash_value &&
172                     hashtable->key_equal_func(node->key, key))
173             {
174                 break;
175             }
176 
177             node_ptr = &(*node_ptr)->next;
178         }
179     }
180     else
181     {
182         while((node = *node_ptr))
183         {
184             if(node->key == key)
185             {
186                 break;
187             }
188 
189             node_ptr = &(*node_ptr)->next;
190         }
191     }
192 
193     return node_ptr;
194 }
195 
196 /*
197  * @hashtable: our #fluid_hashtable_t
198  * @node_ptr_ptr: a pointer to the return value from
199  *   fluid_hashtable_lookup_node()
200  * @notify: %TRUE if the destroy notify handlers are to be called
201  *
202  * Removes a node from the hash table and updates the node count.  The
203  * node is freed.  No table resize is performed.
204  *
205  * If @notify is %TRUE then the destroy notify functions are called
206  * for the key and value of the hash node.
207  *
208  * @node_ptr_ptr is a pass-by-reference in/out parameter.  When the
209  * function is called, it should point to the pointer to the node to
210  * remove.  This level of indirection is required so that the pointer
211  * may be updated appropriately once the node has been removed.
212  *
213  * Before the function returns, the pointer at @node_ptr_ptr will be
214  * updated to point to the position in the table that contains the
215  * pointer to the "next" node in the chain.  This makes this function
216  * convenient to use from functions that iterate over the entire
217  * table.  If there is no further item in the chain then the
218  * #fluid_hashnode_t pointer will be %NULL (ie: **node_ptr_ptr == %NULL).
219  *
220  * Since the pointer in the table to the removed node is replaced with
221  * either a pointer to the next node or a %NULL pointer as
222  * appropriate, the pointer at the end of @node_ptr_ptr will never be
223  * modified at all.  Stay tuned. :)
224  */
225 static void
fluid_hashtable_remove_node(fluid_hashtable_t * hashtable,fluid_hashnode_t *** node_ptr_ptr,int notify)226 fluid_hashtable_remove_node(fluid_hashtable_t *hashtable,
227                             fluid_hashnode_t  ***node_ptr_ptr, int notify)
228 {
229     fluid_hashnode_t **node_ptr, *node;
230 
231     node_ptr = *node_ptr_ptr;
232     node = *node_ptr;
233 
234     *node_ptr = node->next;
235 
236     if(notify && hashtable->key_destroy_func)
237     {
238         hashtable->key_destroy_func(node->key);
239     }
240 
241     if(notify && hashtable->value_destroy_func)
242     {
243         hashtable->value_destroy_func(node->value);
244     }
245 
246     FLUID_FREE(node);
247 
248     hashtable->nnodes--;
249 }
250 
251 /*
252  * fluid_hashtable_remove_all_nodes:
253  * @hashtable: our #fluid_hashtable_t
254  * @notify: %TRUE if the destroy notify handlers are to be called
255  *
256  * Removes all nodes from the table.  Since this may be a precursor to
257  * freeing the table entirely, no resize is performed.
258  *
259  * If @notify is %TRUE then the destroy notify functions are called
260  * for the key and value of the hash node.
261  */
262 static void
fluid_hashtable_remove_all_nodes(fluid_hashtable_t * hashtable,int notify)263 fluid_hashtable_remove_all_nodes(fluid_hashtable_t *hashtable, int notify)
264 {
265     fluid_hashnode_t **node_ptr;
266     int i;
267 
268     for(i = 0; i < hashtable->size; i++)
269     {
270         for(node_ptr = &hashtable->nodes[i]; *node_ptr != NULL;)
271         {
272             fluid_hashtable_remove_node(hashtable, &node_ptr, notify);
273         }
274     }
275 
276     hashtable->nnodes = 0;
277 }
278 
279 /*
280  * fluid_hashtable_resize:
281  * @hashtable: our #fluid_hashtable_t
282  *
283  * Resizes the hash table to the optimal size based on the number of
284  * nodes currently held.  If you call this function then a resize will
285  * occur, even if one does not need to occur.  Use
286  * fluid_hashtable_maybe_resize() instead.
287  */
288 static void
fluid_hashtable_resize(fluid_hashtable_t * hashtable)289 fluid_hashtable_resize(fluid_hashtable_t *hashtable)
290 {
291     fluid_hashnode_t **new_nodes;
292     fluid_hashnode_t *node;
293     fluid_hashnode_t *next;
294     unsigned int hash_val;
295     int new_size;
296     int i;
297 
298     new_size = spaced_primes_closest(hashtable->nnodes);
299     new_size = (new_size < HASH_TABLE_MIN_SIZE) ? HASH_TABLE_MIN_SIZE :
300                ((new_size > HASH_TABLE_MAX_SIZE) ? HASH_TABLE_MAX_SIZE : new_size);
301 
302     new_nodes = FLUID_ARRAY(fluid_hashnode_t *, new_size);
303 
304     if(!new_nodes)
305     {
306         FLUID_LOG(FLUID_ERR, "Out of memory");
307         return;
308     }
309 
310     FLUID_MEMSET(new_nodes, 0, new_size * sizeof(fluid_hashnode_t *));
311 
312     for(i = 0; i < hashtable->size; i++)
313     {
314         for(node = hashtable->nodes[i]; node; node = next)
315         {
316             next = node->next;
317 
318             hash_val = node->key_hash % new_size;
319 
320             node->next = new_nodes[hash_val];
321             new_nodes[hash_val] = node;
322         }
323     }
324 
325     FLUID_FREE(hashtable->nodes);
326     hashtable->nodes = new_nodes;
327     hashtable->size = new_size;
328 }
329 
330 /*
331  * fluid_hashtable_maybe_resize:
332  * @hashtable: our #fluid_hashtable_t
333  *
334  * Resizes the hash table, if needed.
335  *
336  * Essentially, calls fluid_hashtable_resize() if the table has strayed
337  * too far from its ideal size for its number of nodes.
338  */
339 static FLUID_INLINE void
fluid_hashtable_maybe_resize(fluid_hashtable_t * hashtable)340 fluid_hashtable_maybe_resize(fluid_hashtable_t *hashtable)
341 {
342     int nnodes = hashtable->nnodes;
343     int size = hashtable->size;
344 
345     if((size >= 3 * nnodes && size > HASH_TABLE_MIN_SIZE) ||
346             (3 * size <= nnodes && size < HASH_TABLE_MAX_SIZE))
347     {
348         fluid_hashtable_resize(hashtable);
349     }
350 }
351 
352 /**
353  * new_fluid_hashtable:
354  * @hash_func: a function to create a hash value from a key.
355  *   Hash values are used to determine where keys are stored within the
356  *   #fluid_hashtable_t data structure. The fluid_direct_hash(), fluid_int_hash() and
357  *   fluid_str_hash() functions are provided for some common types of keys.
358  *   If hash_func is %NULL, fluid_direct_hash() is used.
359  * @key_equal_func: a function to check two keys for equality.  This is
360  *   used when looking up keys in the #fluid_hashtable_t.  The fluid_direct_equal(),
361  *   fluid_int_equal() and fluid_str_equal() functions are provided for the most
362  *   common types of keys. If @key_equal_func is %NULL, keys are compared
363  *   directly in a similar fashion to fluid_direct_equal(), but without the
364  *   overhead of a function call.
365  *
366  * Creates a new #fluid_hashtable_t with a reference count of 1.
367  *
368  * Return value: a new #fluid_hashtable_t.
369  **/
370 fluid_hashtable_t *
new_fluid_hashtable(fluid_hash_func_t hash_func,fluid_equal_func_t key_equal_func)371 new_fluid_hashtable(fluid_hash_func_t hash_func, fluid_equal_func_t key_equal_func)
372 {
373     return new_fluid_hashtable_full(hash_func, key_equal_func, NULL, NULL);
374 }
375 
376 
377 /**
378  * new_fluid_hashtable_full:
379  * @hash_func: a function to create a hash value from a key.
380  * @key_equal_func: a function to check two keys for equality.
381  * @key_destroy_func: a function to free the memory allocated for the key
382  *   used when removing the entry from the #fluid_hashtable_t or %NULL if you
383  *   don't want to supply such a function.
384  * @value_destroy_func: a function to free the memory allocated for the
385  *   value used when removing the entry from the #fluid_hashtable_t or %NULL if
386  *   you don't want to supply such a function.
387  *
388  * Creates a new #fluid_hashtable_t like fluid_hashtable_new() with a reference count
389  * of 1 and allows to specify functions to free the memory allocated for the
390  * key and value that get called when removing the entry from the #fluid_hashtable_t.
391  *
392  * Return value: a new #fluid_hashtable_t.
393  **/
394 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)395 new_fluid_hashtable_full(fluid_hash_func_t hash_func,
396                          fluid_equal_func_t key_equal_func,
397                          fluid_destroy_notify_t key_destroy_func,
398                          fluid_destroy_notify_t value_destroy_func)
399 {
400     fluid_hashtable_t *hashtable;
401 
402     hashtable = FLUID_NEW(fluid_hashtable_t);
403 
404     if(!hashtable)
405     {
406         FLUID_LOG(FLUID_ERR, "Out of memory");
407         return NULL;
408     }
409 
410     hashtable->size               = HASH_TABLE_MIN_SIZE;
411     hashtable->nnodes             = 0;
412     hashtable->hash_func          = hash_func ? hash_func : fluid_direct_hash;
413     hashtable->key_equal_func     = key_equal_func;
414     fluid_atomic_int_set(&hashtable->ref_count, 1);
415     hashtable->key_destroy_func   = key_destroy_func;
416     hashtable->value_destroy_func = value_destroy_func;
417     hashtable->nodes              = FLUID_ARRAY(fluid_hashnode_t *, hashtable->size);
418     if(hashtable->nodes == NULL)
419     {
420         delete_fluid_hashtable(hashtable);
421         FLUID_LOG(FLUID_ERR, "Out of memory");
422         return NULL;
423     }
424     FLUID_MEMSET(hashtable->nodes, 0, hashtable->size * sizeof(*hashtable->nodes));
425 
426     return hashtable;
427 }
428 
429 /**
430  * fluid_hashtable_iter_init:
431  * @iter: an uninitialized #fluid_hashtable_iter_t.
432  * @hashtable: a #fluid_hashtable_t.
433  *
434  * Initializes a key/value pair iterator and associates it with
435  * @hashtable. Modifying the hash table after calling this function
436  * invalidates the returned iterator.
437  * |[
438  * fluid_hashtable_iter_t iter;
439  * gpointer key, value;
440  *
441  * fluid_hashtable_iter_init (&iter, hashtable);
442  * while (fluid_hashtable_iter_next (&iter, &key, &value))
443  *   {
444  *     /&ast; do something with key and value &ast;/
445  *   }
446  * ]|
447  *
448  * Since: 2.16
449  **/
450 void
fluid_hashtable_iter_init(fluid_hashtable_iter_t * iter,fluid_hashtable_t * hashtable)451 fluid_hashtable_iter_init(fluid_hashtable_iter_t *iter,
452                           fluid_hashtable_t *hashtable)
453 {
454     RealIter *ri = (RealIter *) iter;
455 
456     fluid_return_if_fail(iter != NULL);
457     fluid_return_if_fail(hashtable != NULL);
458 
459     ri->hashtable = hashtable;
460     ri->prev_node = NULL;
461     ri->node = NULL;
462     ri->position = -1;
463     ri->pre_advanced = FALSE;
464 }
465 
466 /**
467  * fluid_hashtable_iter_next:
468  * @iter: an initialized #fluid_hashtable_iter_t.
469  * @key: a location to store the key, or %NULL.
470  * @value: a location to store the value, or %NULL.
471  *
472  * Advances @iter and retrieves the key and/or value that are now
473  * pointed to as a result of this advancement. If %FALSE is returned,
474  * @key and @value are not set, and the iterator becomes invalid.
475  *
476  * Return value: %FALSE if the end of the #fluid_hashtable_t has been reached.
477  *
478  * Since: 2.16
479  **/
480 int
fluid_hashtable_iter_next(fluid_hashtable_iter_t * iter,void ** key,void ** value)481 fluid_hashtable_iter_next(fluid_hashtable_iter_t *iter, void **key,
482                           void **value)
483 {
484     RealIter *ri = (RealIter *) iter;
485 
486     fluid_return_val_if_fail(iter != NULL, FALSE);
487 
488     if(ri->pre_advanced)
489     {
490         ri->pre_advanced = FALSE;
491 
492         if(ri->node == NULL)
493         {
494             return FALSE;
495         }
496     }
497     else
498     {
499         if(ri->node != NULL)
500         {
501             ri->prev_node = ri->node;
502             ri->node = ri->node->next;
503         }
504 
505         while(ri->node == NULL)
506         {
507             ri->position++;
508 
509             if(ri->position >= ri->hashtable->size)
510             {
511                 return FALSE;
512             }
513 
514             ri->prev_node = NULL;
515             ri->node = ri->hashtable->nodes[ri->position];
516         }
517     }
518 
519     if(key != NULL)
520     {
521         *key = ri->node->key;
522     }
523 
524     if(value != NULL)
525     {
526         *value = ri->node->value;
527     }
528 
529     return TRUE;
530 }
531 
532 /**
533  * fluid_hashtable_iter_get_hash_table:
534  * @iter: an initialized #fluid_hashtable_iter_t.
535  *
536  * Returns the #fluid_hashtable_t associated with @iter.
537  *
538  * Return value: the #fluid_hashtable_t associated with @iter.
539  *
540  * Since: 2.16
541  **/
542 fluid_hashtable_t *
fluid_hashtable_iter_get_hash_table(fluid_hashtable_iter_t * iter)543 fluid_hashtable_iter_get_hash_table(fluid_hashtable_iter_t *iter)
544 {
545     fluid_return_val_if_fail(iter != NULL, NULL);
546 
547     return ((RealIter *) iter)->hashtable;
548 }
549 
550 static void
iter_remove_or_steal(RealIter * ri,int notify)551 iter_remove_or_steal(RealIter *ri, int notify)
552 {
553     fluid_hashnode_t *prev;
554     fluid_hashnode_t *node;
555     int position;
556 
557     fluid_return_if_fail(ri != NULL);
558     fluid_return_if_fail(ri->node != NULL);
559 
560     prev = ri->prev_node;
561     node = ri->node;
562     position = ri->position;
563 
564     /* pre-advance the iterator since we will remove the node */
565 
566     ri->node = ri->node->next;
567     /* ri->prev_node is still the correct previous node */
568 
569     while(ri->node == NULL)
570     {
571         ri->position++;
572 
573         if(ri->position >= ri->hashtable->size)
574         {
575             break;
576         }
577 
578         ri->prev_node = NULL;
579         ri->node = ri->hashtable->nodes[ri->position];
580     }
581 
582     ri->pre_advanced = TRUE;
583 
584     /* remove the node */
585 
586     if(prev != NULL)
587     {
588         prev->next = node->next;
589     }
590     else
591     {
592         ri->hashtable->nodes[position] = node->next;
593     }
594 
595     if(notify)
596     {
597         if(ri->hashtable->key_destroy_func)
598         {
599             ri->hashtable->key_destroy_func(node->key);
600         }
601 
602         if(ri->hashtable->value_destroy_func)
603         {
604             ri->hashtable->value_destroy_func(node->value);
605         }
606     }
607 
608     FLUID_FREE(node);
609 
610     ri->hashtable->nnodes--;
611 }
612 
613 /**
614  * fluid_hashtable_iter_remove():
615  * @iter: an initialized #fluid_hashtable_iter_t.
616  *
617  * Removes the key/value pair currently pointed to by the iterator
618  * from its associated #fluid_hashtable_t. Can only be called after
619  * fluid_hashtable_iter_next() returned %TRUE, and cannot be called more
620  * than once for the same key/value pair.
621  *
622  * If the #fluid_hashtable_t was created using fluid_hashtable_new_full(), the
623  * key and value are freed using the supplied destroy functions, otherwise
624  * you have to make sure that any dynamically allocated values are freed
625  * yourself.
626  *
627  * Since: 2.16
628  **/
629 void
fluid_hashtable_iter_remove(fluid_hashtable_iter_t * iter)630 fluid_hashtable_iter_remove(fluid_hashtable_iter_t *iter)
631 {
632     iter_remove_or_steal((RealIter *) iter, TRUE);
633 }
634 
635 /**
636  * fluid_hashtable_iter_steal():
637  * @iter: an initialized #fluid_hashtable_iter_t.
638  *
639  * Removes the key/value pair currently pointed to by the iterator
640  * from its associated #fluid_hashtable_t, without calling the key and value
641  * destroy functions. Can only be called after
642  * fluid_hashtable_iter_next() returned %TRUE, and cannot be called more
643  * than once for the same key/value pair.
644  *
645  * Since: 2.16
646  **/
647 void
fluid_hashtable_iter_steal(fluid_hashtable_iter_t * iter)648 fluid_hashtable_iter_steal(fluid_hashtable_iter_t *iter)
649 {
650     iter_remove_or_steal((RealIter *) iter, FALSE);
651 }
652 
653 
654 /**
655  * fluid_hashtable_ref:
656  * @hashtable: a valid #fluid_hashtable_t.
657  *
658  * Atomically increments the reference count of @hashtable by one.
659  * This function is MT-safe and may be called from any thread.
660  *
661  * Return value: the passed in #fluid_hashtable_t.
662  *
663  * Since: 2.10
664  **/
665 fluid_hashtable_t *
fluid_hashtable_ref(fluid_hashtable_t * hashtable)666 fluid_hashtable_ref(fluid_hashtable_t *hashtable)
667 {
668     fluid_return_val_if_fail(hashtable != NULL, NULL);
669     fluid_return_val_if_fail(fluid_atomic_int_get(&hashtable->ref_count) > 0, hashtable);
670 
671     fluid_atomic_int_add(&hashtable->ref_count, 1);
672     return hashtable;
673 }
674 
675 /**
676  * fluid_hashtable_unref:
677  * @hashtable: a valid #fluid_hashtable_t.
678  *
679  * Atomically decrements the reference count of @hashtable by one.
680  * If the reference count drops to 0, all keys and values will be
681  * destroyed, and all memory allocated by the hash table is released.
682  * This function is MT-safe and may be called from any thread.
683  *
684  * Since: 2.10
685  **/
686 void
fluid_hashtable_unref(fluid_hashtable_t * hashtable)687 fluid_hashtable_unref(fluid_hashtable_t *hashtable)
688 {
689     fluid_return_if_fail(hashtable != NULL);
690     fluid_return_if_fail(fluid_atomic_int_get(&hashtable->ref_count) > 0);
691 
692     if(fluid_atomic_int_exchange_and_add(&hashtable->ref_count, -1) - 1 == 0)
693     {
694         fluid_hashtable_remove_all_nodes(hashtable, TRUE);
695         FLUID_FREE(hashtable->nodes);
696         FLUID_FREE(hashtable);
697     }
698 }
699 
700 /**
701  * delete_fluid_hashtable:
702  * @hashtable: a #fluid_hashtable_t.
703  *
704  * Destroys all keys and values in the #fluid_hashtable_t and decrements its
705  * reference count by 1. If keys and/or values are dynamically allocated,
706  * you should either free them first or create the #fluid_hashtable_t with destroy
707  * notifiers using fluid_hashtable_new_full(). In the latter case the destroy
708  * functions you supplied will be called on all keys and values during the
709  * destruction phase.
710  **/
711 void
delete_fluid_hashtable(fluid_hashtable_t * hashtable)712 delete_fluid_hashtable(fluid_hashtable_t *hashtable)
713 {
714     fluid_return_if_fail(hashtable != NULL);
715     fluid_return_if_fail(fluid_atomic_int_get(&hashtable->ref_count) > 0);
716 
717     fluid_hashtable_remove_all(hashtable);
718     fluid_hashtable_unref(hashtable);
719 }
720 
721 /**
722  * fluid_hashtable_lookup:
723  * @hashtable: a #fluid_hashtable_t.
724  * @key: the key to look up.
725  *
726  * Looks up a key in a #fluid_hashtable_t. Note that this function cannot
727  * distinguish between a key that is not present and one which is present
728  * and has the value %NULL. If you need this distinction, use
729  * fluid_hashtable_lookup_extended().
730  *
731  * Return value: the associated value, or %NULL if the key is not found.
732  **/
733 void *
fluid_hashtable_lookup(fluid_hashtable_t * hashtable,const void * key)734 fluid_hashtable_lookup(fluid_hashtable_t *hashtable, const void *key)
735 {
736     fluid_hashnode_t *node;
737 
738     fluid_return_val_if_fail(hashtable != NULL, NULL);
739 
740     node = *fluid_hashtable_lookup_node(hashtable, key, NULL);
741 
742     return node ? node->value : NULL;
743 }
744 
745 /**
746  * fluid_hashtable_lookup_extended:
747  * @hashtable: a #fluid_hashtable_t.
748  * @lookup_key: the key to look up.
749  * @orig_key: returns the original key.
750  * @value: returns the value associated with the key.
751  *
752  * Looks up a key in the #fluid_hashtable_t, returning the original key and the
753  * associated value and a #gboolean which is %TRUE if the key was found. This
754  * is useful if you need to free the memory allocated for the original key,
755  * for example before calling fluid_hashtable_remove().
756  *
757  * Return value: %TRUE if the key was found in the #fluid_hashtable_t.
758  **/
759 int
fluid_hashtable_lookup_extended(fluid_hashtable_t * hashtable,const void * lookup_key,void ** orig_key,void ** value)760 fluid_hashtable_lookup_extended(fluid_hashtable_t *hashtable,
761                                 const void *lookup_key,
762                                 void **orig_key, void **value)
763 {
764     fluid_hashnode_t *node;
765 
766     fluid_return_val_if_fail(hashtable != NULL, FALSE);
767 
768     node = *fluid_hashtable_lookup_node(hashtable, lookup_key, NULL);
769 
770     if(node == NULL)
771     {
772         return FALSE;
773     }
774 
775     if(orig_key)
776     {
777         *orig_key = node->key;
778     }
779 
780     if(value)
781     {
782         *value = node->value;
783     }
784 
785     return TRUE;
786 }
787 
788 /*
789  * fluid_hashtable_insert_internal:
790  * @hashtable: our #fluid_hashtable_t
791  * @key: the key to insert
792  * @value: the value to insert
793  * @keep_new_key: if %TRUE and this key already exists in the table
794  *   then call the destroy notify function on the old key.  If %FALSE
795  *   then call the destroy notify function on the new key.
796  *
797  * Implements the common logic for the fluid_hashtable_insert() and
798  * fluid_hashtable_replace() functions.
799  *
800  * Do a lookup of @key.  If it is found, replace it with the new
801  * @value (and perhaps the new @key).  If it is not found, create a
802  * new node.
803  */
804 static void
fluid_hashtable_insert_internal(fluid_hashtable_t * hashtable,void * key,void * value,int keep_new_key)805 fluid_hashtable_insert_internal(fluid_hashtable_t *hashtable, void *key,
806                                 void *value, int keep_new_key)
807 {
808     fluid_hashnode_t **node_ptr, *node;
809     unsigned int key_hash;
810 
811     fluid_return_if_fail(hashtable != NULL);
812     fluid_return_if_fail(fluid_atomic_int_get(&hashtable->ref_count) > 0);
813 
814     node_ptr = fluid_hashtable_lookup_node(hashtable, key, &key_hash);
815 
816     if((node = *node_ptr))
817     {
818         if(keep_new_key)
819         {
820             if(hashtable->key_destroy_func)
821             {
822                 hashtable->key_destroy_func(node->key);
823             }
824 
825             node->key = key;
826         }
827         else
828         {
829             if(hashtable->key_destroy_func)
830             {
831                 hashtable->key_destroy_func(key);
832             }
833         }
834 
835         if(hashtable->value_destroy_func)
836         {
837             hashtable->value_destroy_func(node->value);
838         }
839 
840         node->value = value;
841     }
842     else
843     {
844         node = FLUID_NEW(fluid_hashnode_t);
845 
846         if(!node)
847         {
848             FLUID_LOG(FLUID_ERR, "Out of memory");
849             return;
850         }
851 
852         node->key = key;
853         node->value = value;
854         node->key_hash = key_hash;
855         node->next = NULL;
856 
857         *node_ptr = node;
858         hashtable->nnodes++;
859         fluid_hashtable_maybe_resize(hashtable);
860     }
861 }
862 
863 /**
864  * fluid_hashtable_insert:
865  * @hashtable: a #fluid_hashtable_t.
866  * @key: a key to insert.
867  * @value: the value to associate with the key.
868  *
869  * Inserts a new key and value into a #fluid_hashtable_t.
870  *
871  * If the key already exists in the #fluid_hashtable_t its current value is replaced
872  * with the new value. If you supplied a @value_destroy_func when creating the
873  * #fluid_hashtable_t, the old value is freed using that function. If you supplied
874  * a @key_destroy_func when creating the #fluid_hashtable_t, the passed key is freed
875  * using that function.
876  **/
877 void
fluid_hashtable_insert(fluid_hashtable_t * hashtable,void * key,void * value)878 fluid_hashtable_insert(fluid_hashtable_t *hashtable, void *key, void *value)
879 {
880     fluid_hashtable_insert_internal(hashtable, key, value, FALSE);
881 }
882 
883 /**
884  * fluid_hashtable_replace:
885  * @hashtable: a #fluid_hashtable_t.
886  * @key: a key to insert.
887  * @value: the value to associate with the key.
888  *
889  * Inserts a new key and value into a #fluid_hashtable_t similar to
890  * fluid_hashtable_insert(). The difference is that if the key already exists
891  * in the #fluid_hashtable_t, it gets replaced by the new key. If you supplied a
892  * @value_destroy_func when creating the #fluid_hashtable_t, the old value is freed
893  * using that function. If you supplied a @key_destroy_func when creating the
894  * #fluid_hashtable_t, the old key is freed using that function.
895  **/
896 void
fluid_hashtable_replace(fluid_hashtable_t * hashtable,void * key,void * value)897 fluid_hashtable_replace(fluid_hashtable_t *hashtable, void *key, void *value)
898 {
899     fluid_hashtable_insert_internal(hashtable, key, value, TRUE);
900 }
901 
902 /*
903  * fluid_hashtable_remove_internal:
904  * @hashtable: our #fluid_hashtable_t
905  * @key: the key to remove
906  * @notify: %TRUE if the destroy notify handlers are to be called
907  * Return value: %TRUE if a node was found and removed, else %FALSE
908  *
909  * Implements the common logic for the fluid_hashtable_remove() and
910  * fluid_hashtable_steal() functions.
911  *
912  * Do a lookup of @key and remove it if it is found, calling the
913  * destroy notify handlers only if @notify is %TRUE.
914  */
915 static int
fluid_hashtable_remove_internal(fluid_hashtable_t * hashtable,const void * key,int notify)916 fluid_hashtable_remove_internal(fluid_hashtable_t *hashtable, const void *key,
917                                 int notify)
918 {
919     fluid_hashnode_t **node_ptr;
920 
921     fluid_return_val_if_fail(hashtable != NULL, FALSE);
922 
923     node_ptr = fluid_hashtable_lookup_node(hashtable, key, NULL);
924 
925     if(*node_ptr == NULL)
926     {
927         return FALSE;
928     }
929 
930     fluid_hashtable_remove_node(hashtable, &node_ptr, notify);
931     fluid_hashtable_maybe_resize(hashtable);
932 
933     return TRUE;
934 }
935 
936 /**
937  * fluid_hashtable_remove:
938  * @hashtable: a #fluid_hashtable_t.
939  * @key: the key to remove.
940  *
941  * Removes a key and its associated value from a #fluid_hashtable_t.
942  *
943  * If the #fluid_hashtable_t was created using fluid_hashtable_new_full(), the
944  * key and value are freed using the supplied destroy functions, otherwise
945  * you have to make sure that any dynamically allocated values are freed
946  * yourself.
947  *
948  * Return value: %TRUE if the key was found and removed from the #fluid_hashtable_t.
949  **/
950 int
fluid_hashtable_remove(fluid_hashtable_t * hashtable,const void * key)951 fluid_hashtable_remove(fluid_hashtable_t *hashtable, const void *key)
952 {
953     return fluid_hashtable_remove_internal(hashtable, key, TRUE);
954 }
955 
956 /**
957  * fluid_hashtable_steal:
958  * @hashtable: a #fluid_hashtable_t.
959  * @key: the key to remove.
960  *
961  * Removes a key and its associated value from a #fluid_hashtable_t without
962  * calling the key and value destroy functions.
963  *
964  * Return value: %TRUE if the key was found and removed from the #fluid_hashtable_t.
965  **/
966 int
fluid_hashtable_steal(fluid_hashtable_t * hashtable,const void * key)967 fluid_hashtable_steal(fluid_hashtable_t *hashtable, const void *key)
968 {
969     return fluid_hashtable_remove_internal(hashtable, key, FALSE);
970 }
971 
972 /**
973  * fluid_hashtable_remove_all:
974  * @hashtable: a #fluid_hashtable_t
975  *
976  * Removes all keys and their associated values from a #fluid_hashtable_t.
977  *
978  * If the #fluid_hashtable_t was created using fluid_hashtable_new_full(), the keys
979  * and values are freed using the supplied destroy functions, otherwise you
980  * have to make sure that any dynamically allocated values are freed
981  * yourself.
982  *
983  * Since: 2.12
984  **/
985 void
fluid_hashtable_remove_all(fluid_hashtable_t * hashtable)986 fluid_hashtable_remove_all(fluid_hashtable_t *hashtable)
987 {
988     fluid_return_if_fail(hashtable != NULL);
989 
990     fluid_hashtable_remove_all_nodes(hashtable, TRUE);
991     fluid_hashtable_maybe_resize(hashtable);
992 }
993 
994 /**
995  * fluid_hashtable_steal_all:
996  * @hashtable: a #fluid_hashtable_t.
997  *
998  * Removes all keys and their associated values from a #fluid_hashtable_t
999  * without calling the key and value destroy functions.
1000  *
1001  * Since: 2.12
1002  **/
1003 void
fluid_hashtable_steal_all(fluid_hashtable_t * hashtable)1004 fluid_hashtable_steal_all(fluid_hashtable_t *hashtable)
1005 {
1006     fluid_return_if_fail(hashtable != NULL);
1007 
1008     fluid_hashtable_remove_all_nodes(hashtable, FALSE);
1009     fluid_hashtable_maybe_resize(hashtable);
1010 }
1011 
1012 /*
1013  * fluid_hashtable_foreach_remove_or_steal:
1014  * @hashtable: our #fluid_hashtable_t
1015  * @func: the user's callback function
1016  * @user_data: data for @func
1017  * @notify: %TRUE if the destroy notify handlers are to be called
1018  *
1019  * Implements the common logic for fluid_hashtable_foreach_remove() and
1020  * fluid_hashtable_foreach_steal().
1021  *
1022  * Iterates over every node in the table, calling @func with the key
1023  * and value of the node (and @user_data).  If @func returns %TRUE the
1024  * node is removed from the table.
1025  *
1026  * If @notify is true then the destroy notify handlers will be called
1027  * for each removed node.
1028  */
1029 static unsigned int
fluid_hashtable_foreach_remove_or_steal(fluid_hashtable_t * hashtable,fluid_hr_func_t func,void * user_data,int notify)1030 fluid_hashtable_foreach_remove_or_steal(fluid_hashtable_t *hashtable,
1031                                         fluid_hr_func_t func, void *user_data,
1032                                         int notify)
1033 {
1034     fluid_hashnode_t *node, **node_ptr;
1035     unsigned int deleted = 0;
1036     int i;
1037 
1038     for(i = 0; i < hashtable->size; i++)
1039     {
1040         for(node_ptr = &hashtable->nodes[i]; (node = *node_ptr) != NULL;)
1041         {
1042             if((* func)(node->key, node->value, user_data))
1043             {
1044                 fluid_hashtable_remove_node(hashtable, &node_ptr, notify);
1045                 deleted++;
1046             }
1047             else
1048             {
1049                 node_ptr = &node->next;
1050             }
1051         }
1052     }
1053 
1054     fluid_hashtable_maybe_resize(hashtable);
1055 
1056     return deleted;
1057 }
1058 
1059 #if 0
1060 /**
1061  * fluid_hashtable_foreach_remove:
1062  * @hashtable: a #fluid_hashtable_t.
1063  * @func: the function to call for each key/value pair.
1064  * @user_data: user data to pass to the function.
1065  *
1066  * Calls the given function for each key/value pair in the #fluid_hashtable_t.
1067  * If the function returns %TRUE, then the key/value pair is removed from the
1068  * #fluid_hashtable_t. If you supplied key or value destroy functions when creating
1069  * the #fluid_hashtable_t, they are used to free the memory allocated for the removed
1070  * keys and values.
1071  *
1072  * See #fluid_hashtable_iter_t for an alternative way to loop over the
1073  * key/value pairs in the hash table.
1074  *
1075  * Return value: the number of key/value pairs removed.
1076  **/
1077 static unsigned int
1078 fluid_hashtable_foreach_remove(fluid_hashtable_t *hashtable,
1079                                fluid_hr_func_t func, void *user_data)
1080 {
1081     fluid_return_val_if_fail(hashtable != NULL, 0);
1082     fluid_return_val_if_fail(func != NULL, 0);
1083 
1084     return fluid_hashtable_foreach_remove_or_steal(hashtable, func, user_data, TRUE);
1085 }
1086 #endif
1087 
1088 /**
1089  * fluid_hashtable_foreach_steal:
1090  * @hashtable: a #fluid_hashtable_t.
1091  * @func: the function to call for each key/value pair.
1092  * @user_data: user data to pass to the function.
1093  *
1094  * Calls the given function for each key/value pair in the #fluid_hashtable_t.
1095  * If the function returns %TRUE, then the key/value pair is removed from the
1096  * #fluid_hashtable_t, but no key or value destroy functions are called.
1097  *
1098  * See #fluid_hashtable_iter_t for an alternative way to loop over the
1099  * key/value pairs in the hash table.
1100  *
1101  * Return value: the number of key/value pairs removed.
1102  **/
1103 unsigned int
fluid_hashtable_foreach_steal(fluid_hashtable_t * hashtable,fluid_hr_func_t func,void * user_data)1104 fluid_hashtable_foreach_steal(fluid_hashtable_t *hashtable,
1105                               fluid_hr_func_t func, void *user_data)
1106 {
1107     fluid_return_val_if_fail(hashtable != NULL, 0);
1108     fluid_return_val_if_fail(func != NULL, 0);
1109 
1110     return fluid_hashtable_foreach_remove_or_steal(hashtable, func, user_data, FALSE);
1111 }
1112 
1113 /**
1114  * fluid_hashtable_foreach:
1115  * @hashtable: a #fluid_hashtable_t.
1116  * @func: the function to call for each key/value pair.
1117  * @user_data: user data to pass to the function.
1118  *
1119  * Calls the given function for each of the key/value pairs in the
1120  * #fluid_hashtable_t.  The function is passed the key and value of each
1121  * pair, and the given @user_data parameter.  The hash table may not
1122  * be modified while iterating over it (you can't add/remove
1123  * items). To remove all items matching a predicate, use
1124  * fluid_hashtable_foreach_remove().
1125  *
1126  * See fluid_hashtable_find() for performance caveats for linear
1127  * order searches in contrast to fluid_hashtable_lookup().
1128  **/
1129 void
fluid_hashtable_foreach(fluid_hashtable_t * hashtable,fluid_hr_func_t func,void * user_data)1130 fluid_hashtable_foreach(fluid_hashtable_t *hashtable, fluid_hr_func_t func,
1131                         void *user_data)
1132 {
1133     fluid_hashnode_t *node;
1134     int i;
1135 
1136     fluid_return_if_fail(hashtable != NULL);
1137     fluid_return_if_fail(func != NULL);
1138 
1139     for(i = 0; i < hashtable->size; i++)
1140     {
1141         for(node = hashtable->nodes[i]; node; node = node->next)
1142         {
1143             (* func)(node->key, node->value, user_data);
1144         }
1145     }
1146 }
1147 
1148 /**
1149  * fluid_hashtable_find:
1150  * @hashtable: a #fluid_hashtable_t.
1151  * @predicate:  function to test the key/value pairs for a certain property.
1152  * @user_data:  user data to pass to the function.
1153  *
1154  * Calls the given function for key/value pairs in the #fluid_hashtable_t until
1155  * @predicate returns %TRUE.  The function is passed the key and value of
1156  * each pair, and the given @user_data parameter. The hash table may not
1157  * be modified while iterating over it (you can't add/remove items).
1158  *
1159  * Note, that hash tables are really only optimized for forward lookups,
1160  * i.e. fluid_hashtable_lookup().
1161  * So code that frequently issues fluid_hashtable_find() or
1162  * fluid_hashtable_foreach() (e.g. in the order of once per every entry in a
1163  * hash table) should probably be reworked to use additional or different
1164  * data structures for reverse lookups (keep in mind that an O(n) find/foreach
1165  * operation issued for all n values in a hash table ends up needing O(n*n)
1166  * operations).
1167  *
1168  * Return value: The value of the first key/value pair is returned, for which
1169  * func evaluates to %TRUE. If no pair with the requested property is found,
1170  * %NULL is returned.
1171  *
1172  * Since: 2.4
1173  **/
1174 void *
fluid_hashtable_find(fluid_hashtable_t * hashtable,fluid_hr_func_t predicate,void * user_data)1175 fluid_hashtable_find(fluid_hashtable_t *hashtable, fluid_hr_func_t predicate,
1176                      void *user_data)
1177 {
1178     fluid_hashnode_t *node;
1179     int i;
1180 
1181     fluid_return_val_if_fail(hashtable != NULL, NULL);
1182     fluid_return_val_if_fail(predicate != NULL, NULL);
1183 
1184     for(i = 0; i < hashtable->size; i++)
1185     {
1186         for(node = hashtable->nodes[i]; node; node = node->next)
1187         {
1188             if(predicate(node->key, node->value, user_data))
1189             {
1190                 return node->value;
1191             }
1192         }
1193     }
1194 
1195     return NULL;
1196 }
1197 
1198 /**
1199  * fluid_hashtable_size:
1200  * @hashtable: a #fluid_hashtable_t.
1201  *
1202  * Returns the number of elements contained in the #fluid_hashtable_t.
1203  *
1204  * Return value: the number of key/value pairs in the #fluid_hashtable_t.
1205  **/
1206 unsigned int
fluid_hashtable_size(fluid_hashtable_t * hashtable)1207 fluid_hashtable_size(fluid_hashtable_t *hashtable)
1208 {
1209     fluid_return_val_if_fail(hashtable != NULL, 0);
1210 
1211     return hashtable->nnodes;
1212 }
1213 
1214 /**
1215  * fluid_hashtable_get_keys:
1216  * @hashtable: a #fluid_hashtable_t
1217  *
1218  * Retrieves every key inside @hashtable. The returned data is valid
1219  * until @hashtable is modified.
1220  *
1221  * Return value: a #GList containing all the keys inside the hash
1222  *   table. The content of the list is owned by the hash table and
1223  *   should not be modified or freed. Use delete_fluid_list() when done
1224  *   using the list.
1225  *
1226  * Since: 2.14
1227  */
1228 fluid_list_t *
fluid_hashtable_get_keys(fluid_hashtable_t * hashtable)1229 fluid_hashtable_get_keys(fluid_hashtable_t *hashtable)
1230 {
1231     fluid_hashnode_t *node;
1232     int i;
1233     fluid_list_t *retval;
1234 
1235     fluid_return_val_if_fail(hashtable != NULL, NULL);
1236 
1237     retval = NULL;
1238 
1239     for(i = 0; i < hashtable->size; i++)
1240     {
1241         for(node = hashtable->nodes[i]; node; node = node->next)
1242         {
1243             retval = fluid_list_prepend(retval, node->key);
1244         }
1245     }
1246 
1247     return retval;
1248 }
1249 
1250 /**
1251  * fluid_hashtable_get_values:
1252  * @hashtable: a #fluid_hashtable_t
1253  *
1254  * Retrieves every value inside @hashtable. The returned data is
1255  * valid until @hashtable is modified.
1256  *
1257  * Return value: a #GList containing all the values inside the hash
1258  *   table. The content of the list is owned by the hash table and
1259  *   should not be modified or freed. Use delete_fluid_list() when done
1260  *   using the list.
1261  *
1262  * Since: 2.14
1263  */
1264 fluid_list_t *
fluid_hashtable_get_values(fluid_hashtable_t * hashtable)1265 fluid_hashtable_get_values(fluid_hashtable_t *hashtable)
1266 {
1267     fluid_hashnode_t *node;
1268     int i;
1269     fluid_list_t *retval;
1270 
1271     fluid_return_val_if_fail(hashtable != NULL, NULL);
1272 
1273     retval = NULL;
1274 
1275     for(i = 0; i < hashtable->size; i++)
1276     {
1277         for(node = hashtable->nodes[i]; node; node = node->next)
1278         {
1279             retval = fluid_list_prepend(retval, node->value);
1280         }
1281     }
1282 
1283     return retval;
1284 }
1285 
1286 
1287 /* Extracted from glib/gstring.c */
1288 
1289 
1290 /**
1291  * fluid_str_equal:
1292  * @v1: a key
1293  * @v2: a key to compare with @v1
1294  *
1295  * Compares two strings for byte-by-byte equality and returns %TRUE
1296  * if they are equal. It can be passed to new_fluid_hashtable() as the
1297  * @key_equal_func parameter, when using strings as keys in a #Ghashtable.
1298  *
1299  * Returns: %TRUE if the two keys match
1300  */
1301 int
fluid_str_equal(const void * v1,const void * v2)1302 fluid_str_equal(const void *v1, const void *v2)
1303 {
1304     const char *string1 = v1;
1305     const char *string2 = v2;
1306 
1307     return FLUID_STRCMP(string1, string2) == 0;
1308 }
1309 
1310 /**
1311  * fluid_str_hash:
1312  * @v: a string key
1313  *
1314  * Converts a string to a hash value.
1315  * It can be passed to new_fluid_hashtable() as the @hash_func
1316  * parameter, when using strings as keys in a #fluid_hashtable_t.
1317  *
1318  * Returns: a hash value corresponding to the key
1319  */
1320 unsigned int
fluid_str_hash(const void * v)1321 fluid_str_hash(const void *v)
1322 {
1323     /* 31 bit hash function */
1324     const signed char *p = v;
1325     uint32_t h = *p;
1326 
1327     if(h)
1328     {
1329         for(p += 1; *p != '\0'; p++)
1330         {
1331             h = (h << 5) - h + *p;
1332         }
1333     }
1334 
1335     return h;
1336 }
1337 
1338 
1339 /* Extracted from glib/gutils.c */
1340 
1341 
1342 /**
1343  * fluid_direct_equal:
1344  * @v1: a key.
1345  * @v2: a key to compare with @v1.
1346  *
1347  * Compares two #gpointer arguments and returns %TRUE if they are equal.
1348  * It can be passed to new_fluid_hashtable() as the @key_equal_func
1349  * parameter, when using pointers as keys in a #fluid_hashtable_t.
1350  *
1351  * Returns: %TRUE if the two keys match.
1352  */
1353 int
fluid_direct_equal(const void * v1,const void * v2)1354 fluid_direct_equal(const void *v1, const void *v2)
1355 {
1356     return v1 == v2;
1357 }
1358 
1359 /**
1360  * fluid_direct_hash:
1361  * @v: a void * key
1362  *
1363  * Converts a gpointer to a hash value.
1364  * It can be passed to g_hashtable_new() as the @hash_func parameter,
1365  * when using pointers as keys in a #fluid_hashtable_t.
1366  *
1367  * Returns: a hash value corresponding to the key.
1368  */
1369 unsigned int
fluid_direct_hash(const void * v)1370 fluid_direct_hash(const void *v)
1371 {
1372     return FLUID_POINTER_TO_UINT(v);
1373 }
1374 
1375 /**
1376  * fluid_int_equal:
1377  * @v1: a pointer to a int key.
1378  * @v2: a pointer to a int key to compare with @v1.
1379  *
1380  * Compares the two #gint values being pointed to and returns
1381  * %TRUE if they are equal.
1382  * It can be passed to g_hashtable_new() as the @key_equal_func
1383  * parameter, when using pointers to integers as keys in a #fluid_hashtable_t.
1384  *
1385  * Returns: %TRUE if the two keys match.
1386  */
1387 int
fluid_int_equal(const void * v1,const void * v2)1388 fluid_int_equal(const void *v1, const void *v2)
1389 {
1390     return *((const int *) v1) == *((const int *) v2);
1391 }
1392 
1393 /**
1394  * fluid_int_hash:
1395  * @v: a pointer to a int key
1396  *
1397  * Converts a pointer to a #gint to a hash value.
1398  * It can be passed to g_hashtable_new() as the @hash_func parameter,
1399  * when using pointers to integers values as keys in a #fluid_hashtable_t.
1400  *
1401  * Returns: a hash value corresponding to the key.
1402  */
1403 unsigned int
fluid_int_hash(const void * v)1404 fluid_int_hash(const void *v)
1405 {
1406     return *(const int *) v;
1407 }
1408