-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkbitset.c
163 lines (145 loc) · 3.19 KB
/
mkbitset.c
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* simple program to create the bitset file for a gaddag */
#include <sys/mman.h>
#include <sys/types.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/stat.h>
typedef unsigned int gn_t;
typedef unsigned int bs_t;
gn_t *gaddag;
bs_t *bitset;
int dfd;
int bsfd;
int g_cnt;
#if __BYTE_ORDER__ == 12345
#define gs(n) ((n)&0x80000000)
#define gf(n) ((n)&0x40000000)
#define gl(n) (((n)>>24)&0x3F)
#define gc(n) (__builtin_bswap32(n)>>8)
#else /* "NORMAL" */
#define gc(n) ((n)>>8) // first child index
#define gs(n) ((n)&0x80) // have more sibs
#define gl(n) ((n)&0x3F) // node letter value
#define gf(n) ((n)&0x40) // final = end of word
#endif
#define SEP 0x1e
#ifdef OLDGADDAG
#define gl2c(l) (((l)-4)|0x40)
#else
#define gl2c(l) ((l)|0x40)
#endif
#define l2b(l) (0x01 << ((l)-1))
int
writebitset(char *f)
{
char *fullname = "ENABLE.bitset";
ssize_t wrv;
size_t bss;
if (f != NULL)
bsfd = open(f, O_WRONLY|O_CREAT|O_TRUNC, 00644);
else
bsfd = open(fullname, O_WRONLY|O_CREAT|O_TRUNC, 00644);
if (bsfd < 0) {
printf( "bitset file %s failed to open\n", fullname);
perror("open");
return -1;
}
bss = g_cnt * sizeof(bs_t);
wrv = write(bsfd, bitset, bss);
if (wrv != bss) {
printf( "bitset file only wrote %d bytes of %d\n", wrv, bss);
}
if (wrv < 0) {
printf("write to bitset file failed\n");
perror("write");
}
return wrv;
}
int
getdict(char *f)
{
char *fullname = "ENABLE.gaddag";
int rv;
struct stat st;
size_t len;
if (f != NULL)
dfd = open(f, O_RDONLY);
else
dfd = open(fullname, O_RDONLY);
if (dfd < 0) {
printf( "gaddag file %s failed to open\n", fullname);
perror("open");
return -1;
}
rv = fstat(dfd, &st);
if (rv < 0) {
printf( "cannot fstat open file %s\n", fullname);
perror("fstat");
return -2;
}
len = st.st_size;
if (len % sizeof(gn_t)) {
printf("gaddag data not aligned properly\n");
return -3;
}
g_cnt = len / sizeof(gn_t);
printf("opened %d len %d for %d entries\n", dfd, len, g_cnt);
gaddag = (gn_t *)mmap(0, len, PROT_READ, MAP_SHARED, dfd, 0);
if (gaddag == MAP_FAILED) {
printf("failed to mmap %d bytes of gaddag\n", len);
perror("mmap");
return -4;
}
return g_cnt;
}
int
main(int argc, char **argv)
{
int ncnt;
long nodeid;
char sc; // waydown.
int scnt = 0;;
bs_t bit;
bs_t bits;
int bsbytes;
if (argc > 1)
ncnt = getdict(argv[1]);
else
ncnt = getdict(NULL);
if (ncnt <= 0) {
printf("gaddag damage.\n");
return 1;
}
printf("sucked up gaddag file\n");
/* allocate a (big) buffer for the bitset. */
bitset = (bs_t *)malloc( ncnt * sizeof(bs_t));
if (bitset == NULL) {
perror("bitset malloc");
return 2;
}
bzero(bitset, ncnt*sizeof(bs_t));
printf("bitset buffer allocated, converting...\n");
for (nodeid = 0; nodeid < ncnt; nodeid++) {
scnt = -1;
bits = 0;
do {
scnt++;
sc = gl(gaddag[nodeid+scnt]);
bit = l2b(sc);
bits |= bit;
} while (!gs(gaddag[nodeid+scnt]));
bitset[nodeid] = bits;
}
printf("writing bitset..\n");
/* save the file. */
if (argc > 2)
bsbytes = writebitset(argv[2]);
else
bsbytes = writebitset(NULL);
printf("done\n");
return 0;
}