-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcmd_bv_operate.cc
646 lines (508 loc) · 19 KB
/
cmd_bv_operate.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
// cmd_bv_operate.cc-- perform some user-specified operation on bit vectors
#include <string>
#include <cstdlib>
#include <cstdint>
#include <iostream>
#include <vector>
#include "utilities.h"
#include "bit_utilities.h"
#include "bit_vector.h"
#include "file_manager.h"
#include "support.h"
#include "commands.h"
#include "cmd_bv_operate.h"
using std::string;
using std::vector;
using std::cout;
using std::cerr;
using std::endl;
#define u8 std::uint8_t
#define u32 std::uint32_t
#define u64 std::uint64_t
void BVOperateCommand::short_description
(std::ostream& s)
{
s << commandName << "-- perform some user-specified operation on bit vectors" << endl;
}
void BVOperateCommand::usage
(std::ostream& s,
const string& message)
{
if (!message.empty())
{
s << message << endl;
s << endl;
}
short_description(s);
s << "usage: " << commandName << " <filename> [<filename>..] [options]" << endl;
// 123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789
s << " <filename> (cumulative) a bit vector file, either .bv, .rrr or .roar;" << endl;
s << " There should be as many bit vectors as the operation needs," << endl;
s << " usually 2." << endl;
s << " <filename>:<type>[:<offset>] bit vector is embedded in another file; <type>" << endl;
s << " is bv, rrr or roar; <offset> is location within the file" << endl;
s << " --out=<filename> name for the resulting bit vector file" << endl;
s << " --noout don't write the resulting bit vector to a file" << endl;
s << " --and output = a AND b" << endl;
s << " --mask output = a MASK b (i.e. a AND NOT b)" << endl;
s << " --or output = a OR b" << endl;
s << " --ornot output = a OR NOT b" << endl;
s << " --xor output = a XOR b" << endl;
s << " --eq output = a EQ b" << endl;
s << " --not output = NOT a (i.e. 1s complement)" << endl;
s << " --squeeze output = a SQUEEZE b" << endl;
s << " --unsqueeze output = a UNSQUEEZE b" << endl;
s << " --rrr output = RRR a" << endl;
s << " --unrrr output = UNRRR a" << endl;
s << " --quiet don't report information about the result" << endl;
s << " --report:counts report the number of active bits in the bit vectors (inputs" << endl;
s << " and result); only applicable for --and, --mask, --or," << endl;
s << " --ornot, --xor, --eq, or --not" << endl;
}
void BVOperateCommand::debug_help
(std::ostream& s)
{
s << "--debug= options" << endl;
s << " (none, yet)" << endl;
}
void BVOperateCommand::parse
(int _argc,
char** _argv)
{
int argc;
char** argv;
// defaults
saveToFile = true;
reportCounts = false;
beQuiet = false;
// skip command name
argv = _argv+1; argc = _argc - 1;
if (argc <= 0) chastise ();
//////////
// scan arguments
//////////
for (int argIx=0 ; argIx<argc ; argIx++)
{
string arg = argv[argIx];
string argVal;
if (arg.empty()) continue;
string::size_type argValIx = arg.find('=');
if (argValIx == string::npos) argVal = "";
else argVal = arg.substr(argValIx+1);
// --help, etc.
if ((arg == "--help")
|| (arg == "-help")
|| (arg == "--h")
|| (arg == "-h")
|| (arg == "?")
|| (arg == "-?")
|| (arg == "--?"))
{ usage (cerr); std::exit (EXIT_SUCCESS); }
if ((arg == "--help=debug")
|| (arg == "--help:debug")
|| (arg == "?debug"))
{ debug_help(cerr); std::exit (EXIT_SUCCESS); }
// --out=<filename>
if ((is_prefix_of (arg, "--out="))
|| (is_prefix_of (arg, "--output=")))
{ outputFilename = argVal; continue; }
// --noout
if ((arg == "--noout") || (arg == "--nooutput"))
{ saveToFile = false; continue; }
// operations (some unadvertised)
if ((arg == "--and") || (arg == "--AND") || (arg == "AND"))
{ operation = "and"; continue; }
if ((arg == "--mask") || (arg == "--MASK") || (arg == "MASK")
|| (arg == "--andnot") || (arg == "--ANDNOT") || (arg == "ANDNOT"))
{ operation = "mask"; continue; }
if ((arg == "--or") || (arg == "--OR") || (arg == "OR"))
{ operation = "or"; continue; }
if ((arg == "--ornot") || (arg == "--ORNOT") || (arg == "ORNOT"))
{ operation = "or not"; continue; }
if ((arg == "--xor") || (arg == "--XOR") || (arg == "XOR"))
{ operation = "xor"; continue; }
if ((arg == "--eq") || (arg == "--EQ") || (arg == "EQ") || (arg == "=="))
{ operation = "eq"; continue; }
if ((arg == "--not") || (arg == "--NOT") || (arg == "NOT") || (arg == "--complement"))
{ operation = "complement"; continue; }
if ((arg == "--squeeze") || (arg == "--SQUEEZE") || (arg == "SQUEEZE"))
{ operation = "squeeze"; continue; }
if ((arg == "--squeeze.long") || (arg == "--SQUEEZE.LONG") || (arg == "SQUEEZE.LONG"))
{ operation = "squeeze long"; continue; }
if ((arg == "--unsqueeze") || (arg == "--UNSQUEEZE") || (arg == "UNSQUEEZE"))
{ operation = "unsqueeze"; continue; }
if ((arg == "--rrr") || (arg == "--RRR") || (arg == "RRR"))
{ operation = "rrr compress"; continue; }
if ((arg == "--unrrr") || (arg == "--UNRRR") || (arg == "UNRRR"))
{ operation = "rrr decompress"; continue; }
// --report:counts
if ((arg == "--report:counts") || (arg == "--report=counts")
|| (arg == "--report:count") || (arg == "--report=count")
|| (arg == "--counts") || (arg == "--count"))
{ reportCounts = true; continue; }
// --quiet
if (arg == "--quiet")
{ beQuiet = true; continue; }
// (unadvertised) debug options
if (arg == "--debug")
{ debug.insert ("debug"); continue; }
if (is_prefix_of (arg, "--debug="))
{
for (const auto& field : parse_comma_list(argVal))
debug.insert(to_lower(field));
continue;
}
// unrecognized --option
if (is_prefix_of (arg, "--"))
chastise ("unrecognized option: \"" + arg + "\"");
// <filename>
if (BitVector::valid_filename (arg))
{ bvFilenames.emplace_back(strip_blank_ends(arg)); continue; }
// <filename>:<type>[:<offset>]
if (arg.find(':') != string::npos)
{ bvFilenames.emplace_back(strip_blank_ends(arg)); continue; }
// unrecognized argument
chastise ("unrecognized argument: \"" + arg + "\"");
}
// sanity checks
if ((saveToFile) && (outputFilename.empty()))
chastise ("an output bit vector filename is required (--out)");
if ((not saveToFile) && (not outputFilename.empty()))
chastise ("an output bit vector filename was given, inconsistent with --noout");
if (operation.empty())
chastise ("an operation is required (e.g. --AND)");
if (operation == "and")
{
if (bvFilenames.size() != 2)
chastise ("AND requires two input bit vectors");
}
if (operation == "mask")
{
if (bvFilenames.size() != 2)
chastise ("MASK requires two input bit vectors");
}
else if (operation == "or")
{
if (bvFilenames.size() != 2)
chastise ("OR requires two input bit vectors");
}
else if (operation == "xor")
{
if (bvFilenames.size() != 2)
chastise ("XOR requires two input bit vectors");
}
else if (operation == "eq")
{
if (bvFilenames.size() != 2)
chastise ("EQ requires two input bit vectors");
}
else if (operation == "complement")
{
if (bvFilenames.size() != 1)
chastise ("NOT requires one input bit vector");
}
else if ((operation == "squeeze") || (operation == "squeeze long"))
{
if (bvFilenames.size() != 2)
chastise ("SQUEEZE requires two input bit vectors");
if (reportCounts)
chastise ("--report:count is not implemented for --squeeze");
}
else if (operation == "unsqueeze")
{
if (bvFilenames.size() != 2)
chastise ("UNSQUEEZE requires two input bit vectors");
if (reportCounts)
chastise ("--report:count is not implemented for --unsqueeze");
}
else if (operation == "rrr compress")
{
if (bvFilenames.size() != 1)
chastise ("RRR requires one input bit vector");
if (reportCounts)
chastise ("--report:count is not implemented for --rrr");
}
else if (operation == "rrr decompress")
{
if (bvFilenames.size() != 1)
chastise ("UNRRR requires one input bit vector");
if (reportCounts)
chastise ("--report:count is not implemented for --unrrr");
}
// for the no output case, we need to provide a name for the bit vector,
// matching the type of the input bit vector(s); this is necessary because
// BitVector::bit_vector() derives the bit vector type from the file
// extension
if (not saveToFile)
{
string::size_type dotIx = bvFilenames[0].find_last_of(".");
if (dotIx != string::npos)
{
// e.g. outputFilename = ".bv" or ".rrr" or ".roar";
outputFilename = bvFilenames[0].substr(dotIx);
}
else
{
// this case will fail later, in BitVector::bit_vector(), but
// there's not much we can do about it
;
}
}
return;
}
int BVOperateCommand::execute()
{
if (operation == "and") op_and();
else if (operation == "mask") op_mask();
else if (operation == "or") op_or();
else if (operation == "or not") op_or_not();
else if (operation == "xor") op_xor();
else if (operation == "eq") op_eq();
else if (operation == "complement") op_complement();
else if (operation == "squeeze") op_squeeze(false);
else if (operation == "squeeze long") op_squeeze(true);
else if (operation == "unsqueeze") op_unsqueeze();
else if (operation == "rrr compress") op_rrr();
else if (operation == "rrr decompress") op_unrrr();
return EXIT_SUCCESS;
}
void BVOperateCommand::op_and()
{
BitVector* bvA = BitVector::bit_vector (bvFilenames[0]);
BitVector* bvB = BitVector::bit_vector (bvFilenames[1]);
bvA->load();
bvB->load();
u64 numBits = bvA->num_bits();
if (bvB->num_bits() != numBits)
fatal ("error: \"" + bvFilenames[0] + "\" has " + std::to_string(numBits) + " bits"
+ ", but \"" + bvFilenames[1] + "\" has " + std::to_string(bvB->num_bits()));
BitVector* dstBv = BitVector::bit_vector (outputFilename);
dstBv->new_bits (numBits);
bitwise_and (bvA->bits->data(), bvB->bits->data(), dstBv->bits->data(), numBits);
if (reportCounts)
{
// u64 numOnes = bv->rank1(bv->num_bits());
cout << bvFilenames[0] << " has " << bvA->rank1(bvA->num_bits()) << " 'active' bits" << endl;
cout << bvFilenames[1] << " has " << bvB->rank1(bvB->num_bits()) << " 'active' bits" << endl;
cout << ((saveToFile)? outputFilename : "result") << " has " << dstBv->rank1(dstBv->num_bits()) << " 'active' bits" << endl;
}
if (saveToFile) dstBv->save();
delete bvA;
delete bvB;
delete dstBv;
}
void BVOperateCommand::op_mask()
{
BitVector* bvA = BitVector::bit_vector (bvFilenames[0]);
BitVector* bvB = BitVector::bit_vector (bvFilenames[1]);
bvA->load();
bvB->load();
u64 numBits = bvA->num_bits();
if (bvB->num_bits() != numBits)
fatal ("error: \"" + bvFilenames[0] + "\" has " + std::to_string(numBits) + " bits"
+ ", but \"" + bvFilenames[1] + "\" has " + std::to_string(bvB->num_bits()));
BitVector* dstBv = BitVector::bit_vector (outputFilename);
dstBv->new_bits (numBits);
bitwise_mask (bvA->bits->data(), bvB->bits->data(), dstBv->bits->data(), numBits);
if (reportCounts)
{
// u64 numOnes = bv->rank1(bv->num_bits());
cout << bvFilenames[0] << " has " << bvA->rank1(bvA->num_bits()) << " 'active' bits" << endl;
cout << bvFilenames[1] << " has " << bvB->rank1(bvB->num_bits()) << " 'active' bits" << endl;
cout << ((saveToFile)? outputFilename : "result") << " has " << dstBv->rank1(dstBv->num_bits()) << " 'active' bits" << endl;
}
if (saveToFile) dstBv->save();
delete bvA;
delete bvB;
delete dstBv;
}
void BVOperateCommand::op_or()
{
BitVector* bvA = BitVector::bit_vector (bvFilenames[0]);
BitVector* bvB = BitVector::bit_vector (bvFilenames[1]);
bvA->load();
bvB->load();
u64 numBits = bvA->num_bits();
if (bvB->num_bits() != numBits)
fatal ("error: \"" + bvFilenames[0] + "\" has " + std::to_string(numBits) + " bits"
+ ", but \"" + bvFilenames[1] + "\" has " + std::to_string(bvB->num_bits()));
BitVector* dstBv = BitVector::bit_vector (outputFilename);
dstBv->new_bits (numBits);
bitwise_or (bvA->bits->data(), bvB->bits->data(), dstBv->bits->data(), numBits);
if (reportCounts)
{
// u64 numOnes = bv->rank1(bv->num_bits());
cout << bvFilenames[0] << " has " << bvA->rank1(bvA->num_bits()) << " 'active' bits" << endl;
cout << bvFilenames[1] << " has " << bvB->rank1(bvB->num_bits()) << " 'active' bits" << endl;
cout << ((saveToFile)? outputFilename : "result") << " has " << dstBv->rank1(dstBv->num_bits()) << " 'active' bits" << endl;
}
if (saveToFile) dstBv->save();
delete bvA;
delete bvB;
delete dstBv;
}
void BVOperateCommand::op_or_not()
{
BitVector* bvA = BitVector::bit_vector (bvFilenames[0]);
BitVector* bvB = BitVector::bit_vector (bvFilenames[1]);
bvA->load();
bvB->load();
u64 numBits = bvA->num_bits();
if (bvB->num_bits() != numBits)
fatal ("error: \"" + bvFilenames[0] + "\" has " + std::to_string(numBits) + " bits"
+ ", but \"" + bvFilenames[1] + "\" has " + std::to_string(bvB->num_bits()));
BitVector* dstBv = BitVector::bit_vector (outputFilename);
dstBv->new_bits (numBits);
bitwise_or_not (bvA->bits->data(), bvB->bits->data(), dstBv->bits->data(), numBits);
if (reportCounts)
{
// u64 numOnes = bv->rank1(bv->num_bits());
cout << bvFilenames[0] << " has " << bvA->rank1(bvA->num_bits()) << " 'active' bits" << endl;
cout << bvFilenames[1] << " has " << bvB->rank1(bvB->num_bits()) << " 'active' bits" << endl;
cout << ((saveToFile)? outputFilename : "result") << " has " << dstBv->rank1(dstBv->num_bits()) << " 'active' bits" << endl;
}
if (saveToFile) dstBv->save();
delete bvA;
delete bvB;
delete dstBv;
}
void BVOperateCommand::op_xor()
{
BitVector* bvA = BitVector::bit_vector (bvFilenames[0]);
BitVector* bvB = BitVector::bit_vector (bvFilenames[1]);
bvA->load();
bvB->load();
u64 numBits = bvA->num_bits();
if (bvB->num_bits() != numBits)
fatal ("error: \"" + bvFilenames[0] + "\" has " + std::to_string(numBits) + " bits"
+ ", but \"" + bvFilenames[1] + "\" has " + std::to_string(bvB->num_bits()));
BitVector* dstBv = BitVector::bit_vector (outputFilename);
dstBv->new_bits (numBits);
bitwise_xor (bvA->bits->data(), bvB->bits->data(), dstBv->bits->data(), numBits);
if (reportCounts)
{
// u64 numOnes = bv->rank1(bv->num_bits());
cout << bvFilenames[0] << " has " << bvA->rank1(bvA->num_bits()) << " 'active' bits" << endl;
cout << bvFilenames[1] << " has " << bvB->rank1(bvB->num_bits()) << " 'active' bits" << endl;
cout << ((saveToFile)? outputFilename : "result") << " has " << dstBv->rank1(dstBv->num_bits()) << " 'active' bits" << endl;
}
if (saveToFile) dstBv->save();
delete bvA;
delete bvB;
delete dstBv;
}
void BVOperateCommand::op_eq()
{
BitVector* bvA = BitVector::bit_vector (bvFilenames[0]);
BitVector* bvB = BitVector::bit_vector (bvFilenames[1]);
bvA->load();
bvB->load();
u64 numBits = bvA->num_bits();
if (bvB->num_bits() != numBits)
fatal ("error: \"" + bvFilenames[0] + "\" has " + std::to_string(numBits) + " bits"
+ ", but \"" + bvFilenames[1] + "\" has " + std::to_string(bvB->num_bits()));
BitVector* dstBv = BitVector::bit_vector (outputFilename);
dstBv->new_bits (numBits);
bitwise_xor (bvA->bits->data(), bvB->bits->data(), dstBv->bits->data(), numBits);
bitwise_complement (dstBv->bits->data(), numBits);
if (reportCounts)
{
// u64 numOnes = bv->rank1(bv->num_bits());
cout << bvFilenames[0] << " has " << bvA->rank1(bvA->num_bits()) << " 'active' bits" << endl;
cout << bvFilenames[1] << " has " << bvB->rank1(bvB->num_bits()) << " 'active' bits" << endl;
cout << ((saveToFile)? outputFilename : "result") << " has " << dstBv->rank1(dstBv->num_bits()) << " 'active' bits" << endl;
}
if (saveToFile) dstBv->save();
delete bvA;
delete bvB;
delete dstBv;
}
void BVOperateCommand::op_complement()
{
BitVector* bv = BitVector::bit_vector (bvFilenames[0]);
bv->load();
u64 numBits = bv->num_bits();
BitVector* dstBv = BitVector::bit_vector (outputFilename);
dstBv->new_bits (numBits);
bitwise_complement (bv->bits->data(), dstBv->bits->data(), numBits);
if (reportCounts)
{
// u64 numOnes = bv->rank1(bv->num_bits());
cout << bvFilenames[0] << " has " << bv->rank1(bv->num_bits()) << " 'active' bits" << endl;
cout << ((saveToFile)? outputFilename : "result") << " has " << dstBv->rank1(dstBv->num_bits()) << " 'active' bits" << endl;
}
if (saveToFile) dstBv->save();
delete bv;
delete dstBv;
}
void BVOperateCommand::op_squeeze
(bool fullLengthResult)
{
BitVector* srcBv = BitVector::bit_vector (bvFilenames[0]);
BitVector* specBv = BitVector::bit_vector (bvFilenames[1]);
srcBv->load();
specBv->load();
u64 numBits = srcBv->num_bits();
if (specBv->num_bits() != numBits)
fatal ("error: \"" + bvFilenames[0] + "\" has " + std::to_string(numBits) + " bits"
+ ", but \"" + bvFilenames[1] + "\" has " + std::to_string(specBv->num_bits()));
BitVector* dstBv = BitVector::bit_vector (outputFilename);
u64 dstNumBits;
if (fullLengthResult) dstNumBits = numBits;
else dstNumBits = bitwise_count(specBv->bits->data(),numBits);
dstBv->new_bits (dstNumBits);
u64 numCopied = bitwise_squeeze (srcBv->bits->data(), specBv->bits->data(), numBits,
dstBv->bits->data(), dstNumBits);
if (not beQuiet) cout << "result has " << numCopied << " bits" << endl;
if (saveToFile) dstBv->save();
delete srcBv;
delete specBv;
delete dstBv;
}
void BVOperateCommand::op_unsqueeze()
{
BitVector* srcBv = BitVector::bit_vector (bvFilenames[0]);
BitVector* specBv = BitVector::bit_vector (bvFilenames[1]);
srcBv->load();
specBv->load();
u64 numBits = srcBv->num_bits();
u64 specNumBits = specBv->num_bits();
u64 specOneBits = bitwise_count(specBv->bits->data(),specNumBits);
if (specOneBits > numBits)
fatal ("error: \"" + bvFilenames[1] + "\" has " + std::to_string(specOneBits) + " ones"
+ ", but \"" + bvFilenames[0] + "\" only has " + std::to_string(numBits) + " bits");
BitVector* dstBv = BitVector::bit_vector (outputFilename);
u64 dstNumBits = specNumBits;
dstBv->new_bits (dstNumBits);
u64 resultNumBits = bitwise_unsqueeze (srcBv->bits->data(), numBits,
specBv->bits->data(), specNumBits,
dstBv->bits->data(), dstNumBits);
if (not beQuiet) cout << "result has " << resultNumBits << " bits" << endl;
if (saveToFile) dstBv->save();
delete srcBv;
delete specBv;
delete dstBv;
}
void BVOperateCommand::op_rrr()
{
BitVector* bv = BitVector::bit_vector (bvFilenames[0]);
bv->load();
RrrBitVector* rrrBv = new RrrBitVector (bv);
rrrBv->filename = outputFilename;
if (saveToFile)
rrrBv->save();
delete bv;
delete rrrBv;
}
void BVOperateCommand::op_unrrr()
{
RrrBitVector* rrrBv = new RrrBitVector (bvFilenames[0]);
rrrBv->load();
u64 numBits = rrrBv->num_bits();
BitVector* dstBv = BitVector::bit_vector (outputFilename);
dstBv->new_bits (numBits);
decompress_rrr (rrrBv->rrrBits, dstBv->bits->data(), numBits);
if (saveToFile) dstBv->save();
delete rrrBv;
delete dstBv;
}