9 #include "ul_utmalloc.h"
10 #include "ul_hashtab.h"
11 #include "ul_hashtabcust.h"
13 typedef int ul_randser_int_t;
15 #define UL_HASHTAB_WITH_HASHVAL
17 typedef struct cust3_item {
19 #ifndef UL_HASHTAB_WITH_HASHVAL
20 ul_hashtab_node_t my_node;
21 #else /*UL_HASHTAB_WITH_HASHCACHE*/
22 ul_hashtab_node_hashval_t my_node;
23 #endif /*UL_HASHTAB_WITH_HASHCACHE*/
27 typedef struct cust3_table {
28 ul_hashtab_cust_table_field_t my_table;
34 typedef int cust3_key_t;
36 UL_HASTAB_CUST_NODE_INT_DEC(cust3, cust3_table_t, cust3_item_t, cust3_key_t,
37 my_table, my_node, my_val, cust3_cmp_fnc)
40 cust3_cmp_fnc(const cust3_key_t *a, const cust3_key_t *b)
47 inline ul_hashtab_hashval_t
48 cust3_hash_fnc(const cust3_key_t *a)
53 inline ul_hashtab_hashval_t
54 cust3_hash_get_fnc(const cust3_item_t *item)
56 #ifdef UL_HASHTAB_WITH_HASHVAL
57 return item->my_node.hashval;
58 #else /*UL_HASHTAB_WITH_HASHCACHE*/
59 return cust3_hash_fnc(&item->my_val);
60 #endif /*UL_HASHTAB_WITH_HASHCACHE*/
64 cust_hash_prep_fnc(cust3_item_t *item)
66 #ifdef UL_HASHTAB_WITH_HASHVAL
67 item->my_node.hashval=cust3_hash_fnc(&item->my_val);
68 #endif /*UL_HASHTAB_WITH_HASHCACHE*/
71 UL_HASTAB_CUST_NODE_INT_IMP(cust3, cust3_table_t, cust3_item_t, cust3_key_t,
72 my_table, my_node, my_val, cust3_cmp_fnc, cust3_hash_fnc,
73 cust3_hash_get_fnc, cust_hash_prep_fnc, ul_hashtab_sizestep_global)
76 cust3_table_t cust3_table;
78 ul_randser_int_t *ul_randser_generate(ul_randser_int_t len)
80 ul_randser_int_t *ser;
83 ser=malloc(sizeof(*ser)*len);
87 for(i=0; i<len; i++) {
92 ul_randser_int_t t, k;
94 k=(unsigned long long)k*i/RAND_MAX;
103 void timing_test_print(struct timeval *start, struct timeval *stop, char *s)
106 sec=stop->tv_sec-start->tv_sec;
107 usec=stop->tv_usec-start->tv_usec;
116 printf("%s :\t%4ld.%06ld\n",s,sec,usec);
119 int main(int argc, char *argv[])
121 cust3_table_t *table=&cust3_table;
127 cust3_item_t **item_arr;
128 struct timeval time_start, time_stop;
131 ul_randser_int_t *ser1, *ser2;
133 cust3_init_table_field(table);
139 tablepow=atol(argv[2]);
140 ul_hashtab_sizestep_global=ul_hashtab_sizestep_null;
143 cust3_resize_table(table, 1<<tablepow);
145 ser1=ul_randser_generate(count);
149 ser2=ul_randser_generate(count);
153 item_arr=malloc(sizeof(*item_arr)*count);
157 for(i=0; i<count; i++) {
158 item=malloc(sizeof(*item));
159 item->my_val=ser1[i];
163 gettimeofday(&time_start,NULL);
164 for(i=0; i<count; i++) {
165 res=cust3_insert(table, item_arr[i]);
167 printf("cust3_insert %d returned %d\n", i, res);
170 gettimeofday(&time_stop,NULL);
171 timing_test_print(&time_start,&time_stop,"HASHTAB random insert");
173 gettimeofday(&time_start,NULL);
174 for(j=0; j<10; j++) {
175 for(i=0; i<count; i++) {
177 item=cust3_find(table, &k);
179 printf("cust3_find %d failed to found item\n", k);
183 gettimeofday(&time_stop,NULL);
184 timing_test_print(&time_start,&time_stop,"HASHTAB random find 10x");
187 for(k=10; (k<count) && (k<20); k++) {
188 item=cust3_find(table, &k);
190 printf("cust3_find %d failed to found item in loop 2\n", k);
193 if(cust3_delete(table, item)<0)
194 printf("cust3_delete failed to found item %d\n", item->my_val);
199 for(k=20; k<count; k++) {
200 item=cust3_delete_key(table, &k);
202 printf("cust3_delete_key failed to found key %d\n", k);
208 gettimeofday(&time_start,NULL);
209 for(j=0; j<10; j++) {
211 ul_for_each_it(cust3, table, it){
212 item=cust3_it2item(&it);
216 printf("ul_for_each_it error\n");
219 if(i!=table->my_table.count)
220 printf("ul_for_each_it skipped some items\n");
222 gettimeofday(&time_stop,NULL);
223 timing_test_print(&time_start,&time_stop,"HASHTAB for each by iterator 10x");
225 gettimeofday(&time_start,NULL);
226 for(cust3_first_it(table,&it); !cust3_is_end_it(&it); ) {
227 item=cust3_it2item(&it);
228 cust3_delete_it(&it);
231 gettimeofday(&time_stop,NULL);
232 timing_test_print(&time_start,&time_stop,"HASHTAB delete rest by iterator");
234 cust3_delete_all(table);