8 typedef const char *str_t
;
11 KBTREE_INIT(int, uint32_t, kb_generic_cmp
)
12 KBTREE_INIT(str
, str_t
, kb_str_cmp
)
14 static int data_size
= 5000000;
15 static unsigned *int_data
;
16 static char **str_data
;
22 printf("--- generating data... ");
24 int_data
= (unsigned*)calloc(data_size
, sizeof(unsigned));
25 str_data
= (char**)calloc(data_size
, sizeof(char*));
26 for (i
= 0; i
< data_size
; ++i
) {
27 int_data
[i
] = (unsigned)(data_size
* drand48() / 4) * 271828183u;
28 sprintf(buf
, "%x", int_data
[i
]);
29 str_data
[i
] = strdup(buf
);
33 void ht_destroy_data()
36 for (i
= 0; i
< data_size
; ++i
) free(str_data
[i
]);
37 free(str_data
); free(int_data
);
43 unsigned *data
= int_data
;
47 h
= kb_init(int, KB_DEFAULT_SIZE
);
48 for (i
= 0; i
< data_size
; ++i
) {
49 if (kb_get(int, h
, data
[i
]) == 0) kb_put(int, h
, data
[i
]);
50 else kb_del(int, h
, data
[i
]);
52 printf("[ht_khash_int] size: %d\n", kb_size(h
));
56 kb_interval(int, h
, 2174625464u, &l
, &u
);
57 printf("interval for 2174625464: (%u, %u)\n", l
? *l
: 0, u
? *u
: 0);
58 #define traverse_f(p) { if (cnt == 0) y = *p; ++cnt; }
59 __kb_traverse(uint32_t, h
, traverse_f
);
60 __kb_get_first(uint32_t, h
, x
);
61 printf("# of elements from traversal: %d\n", cnt
);
62 printf("first element: %d == %d\n", x
, y
);
69 char **data
= str_data
;
72 h
= kb_init(str
, KB_DEFAULT_SIZE
);
73 for (i
= 0; i
< data_size
; ++i
) {
74 if (kb_get(str
, h
, data
[i
]) == 0) kb_put(str
, h
, data
[i
]);
75 else kb_del(str
, h
, data
[i
]);
77 printf("[ht_khash_int] size: %d\n", kb_size(h
));
80 void ht_timing(void (*f
)(void))
84 printf("[ht_timing] %.3lf sec\n", (double)(clock() - t
) / CLOCKS_PER_SEC
);
86 int main(int argc
, char *argv
[])
88 if (argc
> 1) data_size
= atoi(argv
[1]);
90 ht_timing(ht_khash_int
);
91 ht_timing(ht_khash_str
);