Added spec:commit task to commit changes to spec/ruby sources.
[rbx.git] / shotgun / lib / config_hash.c
blob933a7ba13cab4abad5a0416ba0349352e172f9c6
1 #include <stdlib.h>
2 #include <stdint.h>
3 #include "shotgun/lib/config_hash.h"
6 DEFINE_HASHTABLE_INSERT(ht_config_insert, struct tagbstring, struct tagbstring);
7 DEFINE_HASHTABLE_SEARCH(ht_config_search, struct tagbstring, struct tagbstring);
8 DEFINE_HASHTABLE_REMOVE(ht_config_remove, struct tagbstring, struct tagbstring);
9 unsigned int bstring_hash(const void * value)
11 unsigned int retval = 0;
12 int i = 0;
13 for (; i < blength((const_bstring)value); ++i)
15 retval += 5*bchar((const_bstring)value, i);
17 return retval;
20 int bstring_eq(const void * value1, const void * value2)
22 return biseq((const_bstring)value1, (const_bstring)value2);
25 struct hashtable * ht_config_create(unsigned int minsize)
27 return create_hashtable(minsize, bstring_hash, bstring_eq);
30 void ht_config_destroy(struct hashtable *ht_config)
32 struct hashtable_itr iter;
33 hashtable_iterator_init(&iter, ht_config);
35 do
37 bdestroy((bstring)hashtable_iterator_key(&iter));
38 bdestroy((bstring)hashtable_iterator_value(&iter));
40 while (hashtable_iterator_remove(&iter));
41 hashtable_destroy(ht_config, 0);
44 DEFINE_HASHTABLE_INSERT(ht_vconfig_insert, int, void);
45 DEFINE_HASHTABLE_SEARCH(ht_vconfig_search, int, void);
46 DEFINE_HASHTABLE_REMOVE(ht_vconfig_remove, int, void);
48 static unsigned int int_hash(const void *value)
50 unsigned int retval = 0x64a3b9ac; /* totally made up */
52 retval ^= *(int*)value;
54 return retval;
57 static int int_eq(const void *value1, const void *value2)
59 return *(int*)value1 == *(int*)value2;
62 struct hashtable * ht_vconfig_create(unsigned int minsize)
64 return create_hashtable(minsize, int_hash, int_eq);
67 void ht_vconfig_destroy(struct hashtable *ht_config)
69 hashtable_destroy(ht_config, 0);
72 void ht_vconfig_each(struct hashtable *ht, void (*cb)(int key, void *val))
74 struct hashtable_itr itr;
76 hashtable_iterator_init(&itr, ht);
77 do {
78 if (itr.e) {
79 cb((uintptr_t)hashtable_iterator_key(&itr), hashtable_iterator_value(&itr));
81 } while (hashtable_iterator_advance(&itr));