forked from jrrk2/prjxray
-
Notifications
You must be signed in to change notification settings - Fork 2
/
segmatch.cc
396 lines (322 loc) · 8.5 KB
/
segmatch.cc
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* Copyright (C) 2017-2020 The Project X-Ray Authors.
*
* Use of this source code is governed by a ISC-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/ISC
*
* SPDX-License-Identifier: ISC
*/
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
#include <absl/strings/str_cat.h>
#include <gflags/gflags.h>
DEFINE_int32(
c,
4,
"threshold under which candidates are output. set to -1 to output all.");
DEFINE_bool(i, false, "add inverted tags");
DEFINE_int32(m, 0, "min number of set/cleared samples each");
DEFINE_int32(M, 0, "min number of set/cleared samples total");
DEFINE_string(o, "", "set output file");
DEFINE_string(k, "", "set output mask file");
using std::map;
using std::string;
using std::tuple;
using std::vector;
int num_bits = 0, num_tags = 0;
map<string, int> bit_ids, tag_ids;
vector<string> bit_ids_r, tag_ids_r;
#if 0
struct bool_vec
{
vector<bool> data;
bool_vec(int n = 0, bool initval = false) : data(n, initval)
{
}
void set(int n)
{
if (int(data.size()) <= n)
data.resize(n+1);
data[n] = true;
}
bool get(int n)
{
return data.at(n);
}
void resize(int n)
{
data.resize(n);
}
int count()
{
return std::accumulate(data.begin(), data.end(), 0);
}
void apply_and(const bool_vec &other)
{
assert(data.size() == other.data.size());
for (int i = 0; i < int(data.size()); i++)
data[i] = data[i] && other.data[i];
}
void apply_andc(const bool_vec &other)
{
assert(data.size() == other.data.size());
for (int i = 0; i < int(data.size()); i++)
data[i] = data[i] && !other.data[i];
}
};
#else
struct bool_vec {
vector<uint64_t> data;
bool_vec(int n = 0, bool initval = false)
: data((n + 63) / 64, initval ? ~uint64_t(0) : uint64_t(0)) {
for (int i = data.size() * 64 - 1; i >= n; i--)
data[n / 64] &= ~(uint64_t(1) << (n % 64));
}
void set(int n) {
if (int(data.size() * 64) <= n)
data.resize((n + 64) / 64);
data[n / 64] |= uint64_t(1) << (n % 64);
}
bool get(int n) { return (data[n / 64] >> (n % 64)) & 1; }
void resize(int n) { data.resize((n + 63) / 64); }
int count() {
int sum = 0;
for (int i = 0; i < 64 * int(data.size()); i++)
if (get(i))
sum++;
return sum;
}
void apply_and(const bool_vec& other) {
assert(data.size() == other.data.size());
for (int i = 0; i < int(data.size()); i++)
data[i] &= other.data[i];
}
void apply_andc(const bool_vec& other) {
assert(data.size() == other.data.size());
for (int i = 0; i < int(data.size()); i++)
data[i] &= ~other.data[i];
}
};
#endif
// segname -> bits, tags_on, tags_off
typedef tuple<bool_vec, bool_vec, bool_vec> segdata_t;
map<string, segdata_t> segdata;
map<string, int> segnamecnt;
static inline bool_vec& segdata_bits(segdata_t& sd) {
return std::get<0>(sd);
}
static inline bool_vec& segdata_tags1(segdata_t& sd) {
return std::get<1>(sd);
}
static inline bool_vec& segdata_tags0(segdata_t& sd) {
return std::get<2>(sd);
}
void read_input(std::istream& f, std::string filename) {
string token;
segdata_t* segptr = nullptr;
while (f >> token) {
if (token == "seg") {
f >> token;
token = filename + ":" + token;
while (segdata.count(token)) {
int idx = 1;
if (segnamecnt.count(token))
idx = segnamecnt.at(token);
segnamecnt[token] = idx + 1;
char buffer[64];
snprintf(buffer, 64, "-%d", idx);
token += buffer;
}
segptr = &segdata[token];
continue;
}
if (token == "bit") {
assert(segptr != nullptr);
f >> token;
if (bit_ids.count(token) == 0) {
bit_ids[token] = num_bits++;
bit_ids_r.push_back(token);
}
int bit_idx = bit_ids.at(token);
auto& bits = segdata_bits(*segptr);
bits.set(bit_idx);
continue;
}
if (token == "tag") {
assert(segptr != nullptr);
f >> token;
if (tag_ids.count(token) == 0) {
tag_ids[token] = num_tags++;
tag_ids_r.push_back(token);
}
int tag_idx = tag_ids.at(token);
f >> token;
assert(token == "0" || token == "1");
auto& tags = token == "1" ? segdata_tags1(*segptr)
: segdata_tags0(*segptr);
tags.set(tag_idx);
if (FLAGS_i) {
auto& inv_tags = token == "1"
? segdata_tags0(*segptr)
: segdata_tags1(*segptr);
token = tag_ids_r.at(tag_idx) + "__INV";
if (tag_ids.count(token) == 0) {
tag_ids[token] = num_tags++;
tag_ids_r.push_back(token);
}
int inv_tag_idx = tag_ids.at(token);
inv_tags.set(inv_tag_idx);
}
continue;
}
abort();
}
// printf("Number of segments: %d\n", int(segdata.size()));
// printf("Number of bits: %d\n", num_bits);
// printf("Number of tags: %d\n", num_tags);
for (auto& segdat : segdata) {
segdata_bits(segdat.second).resize(num_bits);
segdata_tags1(segdat.second).resize(num_tags);
segdata_tags0(segdat.second).resize(num_tags);
}
}
int main(int argc, char** argv) {
gflags::SetUsageMessage(
absl::StrCat("Usage: ", argv[0], " [options] file.."));
gflags::ParseCommandLineFlags(&argc, &argv, true);
if (argc > 1) {
for (int optind = 1; optind < argc; optind++) {
printf("Reading %s.\n", argv[optind]);
std::ifstream f;
f.open(argv[optind]);
// Check if input file exists.
if (!f.good()) {
printf("ERROR: Input file does not exist!\n");
return -1;
}
assert(!f.fail());
read_input(f, argv[optind]);
}
} else {
printf("Reading from stdin...\n");
read_input(std::cin, "stdin");
}
printf("#of segments: %d\n", int(segdata.size()));
printf("#of bits: %d\n", num_bits);
printf("#of tags: %d\n", num_tags);
FILE* f = stdout;
if (!FLAGS_o.empty()) {
f = fopen(FLAGS_o.c_str(), "w");
assert(f != nullptr);
}
int cnt_const0 = 0;
int cnt_const1 = 0;
int cnt_candidates = 0;
int min_candidates = num_bits;
int max_candidates = 0;
float avg_candidates = 0;
std::vector<std::string> out_lines;
for (int tag_idx = 0; tag_idx < num_tags; tag_idx++) {
bool_vec mask(num_bits, true);
int count1 = 0, count0 = 0;
for (auto& segdat : segdata) {
auto& sd = segdat.second;
bool tag1 = segdata_tags1(sd).get(tag_idx);
bool tag0 = segdata_tags0(sd).get(tag_idx);
assert(!tag1 || !tag0);
if (tag1) {
count1++;
mask.apply_and(segdata_bits(sd));
continue;
}
if (tag0) {
count0++;
mask.apply_andc(segdata_bits(sd));
continue;
}
}
assert(count1 || count0);
std::string out_line = tag_ids_r.at(tag_idx);
if (count1 < FLAGS_m) {
char buffer[64];
snprintf(buffer, 64, " <m1 %d>", count1);
out_line += buffer;
}
if (count0 < FLAGS_m) {
char buffer[64];
snprintf(buffer, 64, " <m0 %d>", count0);
out_line += buffer;
}
if (count1 + count0 < FLAGS_M) {
char buffer[64];
snprintf(buffer, 64, " <M %d %d>", count1, count0);
out_line += buffer;
}
if (!count1) {
out_lines.push_back(out_line + " <const0>");
cnt_const0 += 1;
continue;
}
if (!count0) {
out_line += " <const1>";
cnt_const1 += 1;
}
int num_candidates = mask.count();
if (count0) {
min_candidates =
std::min(min_candidates, num_candidates);
max_candidates =
std::max(max_candidates, num_candidates);
avg_candidates += num_candidates;
cnt_candidates += 1;
}
if (FLAGS_c < 0 ||
(0 < num_candidates && num_candidates <= FLAGS_c)) {
std::vector<std::string> out_tags;
for (int bit_idx = 0; bit_idx < num_bits; bit_idx++)
if (mask.get(bit_idx))
out_tags.push_back(
bit_ids_r.at(bit_idx));
std::sort(out_tags.begin(), out_tags.end());
for (auto& tag : out_tags)
out_line += " " + tag;
} else {
char buffer[64];
snprintf(buffer, 64, " <%d candidates>",
num_candidates);
out_line += buffer;
}
out_lines.push_back(out_line);
}
std::sort(out_lines.begin(), out_lines.end());
for (auto& line : out_lines)
fprintf(f, "%s\n", line.c_str());
if (cnt_candidates)
avg_candidates /= cnt_candidates;
printf("#of const0 tags: %d\n", cnt_const0);
printf("#of const1 tags: %d\n", cnt_const1);
if (cnt_candidates) {
printf("min #of candidates: %d\n", min_candidates);
printf("max #of candidates: %d\n", max_candidates);
printf("avg #of candidates: %.3f\n", avg_candidates);
}
if (!FLAGS_k.empty()) {
f = fopen(FLAGS_k.c_str(), "w");
assert(f != nullptr);
std::sort(bit_ids_r.begin(), bit_ids_r.end());
for (auto bit : bit_ids_r)
fprintf(f, "bit %s\n", bit.c_str());
}
return 0;
}