-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathdict_v1.c
More file actions
44 lines (42 loc) · 1.07 KB
/
Copy pathdict_v1.c
File metadata and controls
44 lines (42 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include "khash.h"
KHASH_MAP_INIT_STR(str, int)
#define BUF_SIZE 0x10000
#define BLOCK_SIZE 0x100000
int main(int argc, char *argv[])
{
char *buf, **mem = 0;
int ret, max = 1, block_end = 0, curr = 0;
khint_t k;
khash_t(str) *h;
buf = malloc(BUF_SIZE); // string buffer
h = kh_init(str);
mem = malloc(sizeof(void*));
mem[0] = malloc(BLOCK_SIZE); // memory buffer to avoid memory fragments
curr = block_end = 0;
while (!feof(stdin)) {
fgets(buf, BUF_SIZE, stdin);
k = kh_put(str, h, buf, &ret);
if (ret) { // absent
int l = strlen(buf) + 1;
if (block_end + l > BLOCK_SIZE) {
++curr; block_end = 0;
mem = realloc(mem, (curr + 1) * sizeof(void*));
mem[curr] = malloc(BLOCK_SIZE);
}
memcpy(mem[curr] + block_end, buf, l);
kh_key(h, k) = mem[curr] + block_end;
block_end += l;
kh_val(h, k) = 1;
} else {
++kh_val(h, k);
if (kh_val(h, k) > max) max = kh_val(h, k);
}
}
printf("%u\t%d\n", kh_size(h), max);
for (ret = 0; ret <= curr; ++ret) free(mem[ret]);
free(mem);
kh_destroy(str, h);
free(buf);
return 0;
}