1 #include "common.h"
2 #include <time.h>
3 #include "mba/hashmap1.h"
4 #include "mba/suba.h"
5 
6 int
HashmapExercise1(int verbose,struct cfg * cfg,char * args[])7 HashmapExercise1(int verbose, struct cfg *cfg, char *args[])
8 {
9 	struct hashmap1 *h;
10     int rate = 0, i, n = 0;
11 	int count = atoi(args[0]);
12 	int interval = atoi(args[1]);
13 	iter_t riter;
14 	iter_t iter;
15 	char *str;
16 	struct allocator *al;
17 	char *mem;
18 	clock_t t0;
19 	int chkpnt = 0;
20 
21 	if ((mem = malloc(0xFFFFFFF)) == NULL ||
22 			(al = suba_init(mem, 0xFFFFFFF, 1, 0)) == NULL ||
23 			(h = hashmap1_new(hash_str, (compare_fn)strcmp, al)) == NULL) {
24 		AMSG("");
25 		return -1;
26 	}
27 srand(1);
28 
29 fputc('\n', stderr);
30 
31 	t0 = clock();
32 fprintf(stderr, "       time    count     size      mem\n");
33 
34 	hashmap1_iterate(h, &iter);
35 	rate_iterate(&riter);
36 	for (i = 0; i < count; i++) {
37 		if ((i % interval) == 0) {
38 			int level = 0;
39 			while (h->tables[level + 1]) {
40 				level++;
41 			}
42 fprintf(stderr, "%2d %8ld %8d %8d %8d %d\n", chkpnt++, (clock() - t0) / 1000, hashmap1_size(h), total_sizes[level], h->al->size_total - h->al->free_total, rate);
43 			rate = rate_next(&riter);
44 		}
45 
46 		if (rand() % 10 < rate) {
47 	        str = allocator_alloc(NULL, 16, 0);
48 	        sprintf(str, "%015d", n++);
49 	        if (hashmap1_put(h, str, str) == -1) {
50 				AMSG("");
51 				return -1;
52 			} else {
53 /*fputc('+', stderr);
54 */
55 	 	      	tcase_printf(verbose, "put %s %d\r", str, hashmap1_size(h));
56 			}
57 		} else {
58 			if (hashmap1_is_empty(h)) {
59 /*fputc('0', stderr);
60 */
61 				tcase_printf(verbose, "hashmap1 empty\r");
62 			} else {
63 		        str = hashmap1_next(h, &iter);
64 				if (str) {
65 	    	    	tcase_printf(verbose, "get %s %d\r", str, hashmap1_size(h));
66 					if ((str = hashmap1_remove(h, str)) == NULL) {
67 						errno = EFAULT;
68 						PMNO(errno);
69 						return -1;
70 					}
71 /*fputc('-', stderr);
72 */
73 					tcase_printf(verbose, "rem %s %d\r", str, hashmap1_size(h));
74 					allocator_free(NULL, str);
75 				} else {
76 /*fputc('x', stderr);
77 */
78 					tcase_printf(verbose, "nothing left to iterate over\r");
79 					hashmap1_iterate(h, &iter);
80 				}
81 			}
82 		}
83     }
84 {
85 	int level = 0;
86 	while (h->tables[level + 1]) {
87 		level++;
88 	}
89 fprintf(stderr, "%2d %8ld %8d %8d %8d\n", chkpnt++, (clock() - t0) / 1000, hashmap1_size(h), total_sizes[level], h->al->size_total);
90 }
91 
92 	hashmap1_del(h, allocator_free, NULL, al);
93 	tcase_printf(verbose, "\nhashmap1 done\n");
94 
95 	cfg = NULL;
96     return 0;
97 }
98