-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrp.cpp
829 lines (518 loc) · 16.3 KB
/
rp.cpp
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
/*
* This file is part of Re-Pair.
* Copyright (c) by
* Nicola Prezza <[email protected]>, Philip Bille, and Inge Li Gørtz
*
* Re-Pair is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* Re-Pair is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details (<http://www.gnu.org/licenses/>).
*
*
* rp.cpp
*
* Created on: Jan 11, 2017
* Author: nico
*/
#include <chrono>
#include <string>
#include <iostream>
#include <vector>
#include <set>
#include <stack>
#include <lf_queue.hpp>
#include <ll_vec.hpp>
#include <ll_el.hpp>
#include <fstream>
#include <cmath>
#include "internal/hf_queue.hpp"
#include "internal/skippable_text.hpp"
#include "internal/text_positions.hpp"
#include "internal/packed_gamma_file3.hpp"
using namespace std;
void help(){
cout << "Compressor and decompressor based on the Re-Pair grammar. Space usage: roughly 6n Bytes of RAM, where n < 2^32 is the file size." << endl << endl;
cout << "Usage: rp <c|d> <input> [output]" << endl;
cout << " c compress <input>" << endl;
cout << " d decompress <input>" << endl;
cout << " <input> input text file (compression mode) or rp archive (decompression mode)" << endl;
cout << " [output] optional output file name. If not specified, suffix .rp is added (compression) or removed (decompression)" << endl;
exit(0);
}
//high/low frequency text type
using text_t = skippable_text32_t;
using TP_t = text_positions32_t;
using hf_q_t = hf_queue32_t;
using lf_q_t = lf_queue32_t;
using itype = uint32_t;
/*
using text_t = skippable_text64_t;
using TP_t = text_positions64_t;
using hf_q_t = hf_queue64_t;
using lf_q_t = lf_queue64_t;
using itype = uint64_t;
*/
using cpair = hf_q_t::cpair;
//next free dictionary symbol
itype X=0;
itype last_freq = 0;
itype n_distinct_freqs = 0;
vector<itype> A; //alphabet (mapping int->ascii)
vector<pair<itype, itype> > G; //grammar
vector<itype> T_vec;// compressed text
/*
* Given (empty) queue, text positions, text, and minimum frequency: insert in Q all pairs with frequency at least min_freq.
*
* assumptions: TP is sorted by character pairs, Q is void
*
*/
void new_high_frequency_queue(hf_q_t & Q, TP_t & TP, text_t & T, uint64_t min_freq){
itype j = 0; //current position on TP
itype n = TP.size();
int old_perc = 0;
int perc;
itype n_pairs = 0;
/*
* step 1: count number of high-freq pairs
*/
while(j<n){
itype k = 1; //current pair frequency
while( j<TP.size()-1 &&
T.pair_starting_at(TP[j]) != T.blank_pair() &&
T.pair_starting_at(TP[j]) == T.pair_starting_at(TP[j+1]) ){
j++;
k++;
}
if(k>=min_freq){
n_pairs++;
}
j++;
}
//largest possible dictionary symbol
itype max_d = 256+T.size()/min_freq;
//create new queue. Capacity is number of pairs / min_frequency
Q.init(max_d,min_freq);
/*
* step 2. Fill queue
*/
j = 0;
while(j<n){
itype P_ab = j; //starting position in TP of pair
itype k = 1; //current pair frequency
cpair ab = T.blank_pair();
while( j<TP.size()-1 &&
T.pair_starting_at(TP[j]) != T.blank_pair() &&
T.pair_starting_at(TP[j]) == T.pair_starting_at(TP[j+1]) ){
ab = T.pair_starting_at(TP[j]);
j++;
k++;
}
if(k>=min_freq){
Q.insert({ab, P_ab, k, k});
}
j++;
}
}
/*
* synchronize queue in range corresponding to pair AB.
*/
template<typename queue_t>
void synchronize(queue_t & Q, TP_t & TP, text_t & T, cpair AB){
//variables associated with AB
assert(Q.contains(AB));
auto q_el = Q[AB];
itype P_AB = q_el.P_ab;
itype L_AB = q_el.L_ab;
itype F_AB = q_el.F_ab;
itype freq_AB = 0;//number of pairs AB seen inside the interval. Computed inside this function
assert(P_AB+L_AB <= TP.size());
//sort sub-array corresponding to AB
TP.cluster(P_AB,P_AB+L_AB);
assert(TP.is_clustered(P_AB,P_AB+L_AB));
//scan TP[P_AB,...,P_AB+L_AB-1] and detect new pairs
itype j = P_AB;//current position in TP
while(j<P_AB+L_AB){
itype p = j; //starting position of current pair in TP
itype k = 1; //current pair frequency
cpair XY = T.pair_starting_at(TP[j]);
while( j<(P_AB+L_AB)-1 &&
XY != T.blank_pair() &&
XY == T.pair_starting_at(TP[j+1]) ){
j++;
k++;
}
freq_AB = XY == AB ? k : freq_AB;
if(k >= Q.minimum_frequency()){
//if the pair is not AB and it is a high-frequency pair, insert it in queue
if(XY != AB){
assert(XY != T.blank_pair());
assert(not Q.contains(XY));
Q.insert({XY,p,k,k});
assert(TP.contains_only(p,p+k,XY));
}else if(XY == AB){ //the pair is AB and is already in the queue: update its frequency
assert(Q.contains(AB));
Q.update({AB,p,k,k});
assert(TP.contains_only(p,p+k,AB));
}
}
j++;
}
assert(Q.contains(AB));
//it could be that now AB's frequency is too small: delete it
if(freq_AB < Q.minimum_frequency()){
Q.remove(AB);
}
assert(not Q.contains(AB) || Q[AB].F_ab == Q[AB].L_ab);
}
/*
* look at F_ab and L_ab. Cases:
*
* 1. F_ab <= L_ab/2 and F_ab >= min_freq: synchronize pair. There could be new high-freq pairs in ab's list
* 2. F_ab <= L_ab/2 and F_ab < min_freq: as above. This because there could be new high-freq pairs in ab's list.
* 3. F_ab > L_ab/2 and F_ab >= min_freq: do nothing
* 4. F_ab > L_ab/2 and F_ab < min_freq: remove ab. ab's list cannot contain high-freq pairs, so it is safe to lose references to these pairs.
*
*/
template<typename queue_t>
void synchro_or_remove_pair(queue_t & Q, TP_t & TP, text_t & T, cpair ab){
assert(Q.contains(ab));
auto q_el = Q[ab];
itype F_ab = q_el.F_ab;
itype L_ab = q_el.L_ab;
if(F_ab <= L_ab/2){
synchronize<queue_t>(Q, TP, T, ab);
}else{
if(F_ab < Q.minimum_frequency()){
Q.remove(ab);
}
}
}
/*
* bit-width of x
*/
uint64_t wd(uint64_t x){
auto w = 64 - __builtin_clzll(uint64_t(x));
return x == 0 ? 1 : w;
}
const pair<itype,itype> nullpair = {~itype(0),~itype(0)};
/*
* return frequency of replaced pair
*/
template<typename queue_t>
uint64_t substitution_round(queue_t & Q, TP_t & TP, text_t & T){
using ctype = text_t::char_type;
//compute max
cpair AB = Q.max();
G.push_back(AB);
//cout << "MAX freq = " << Q[AB].F_ab << endl;
assert(Q.contains(AB));
assert(Q[AB].F_ab >= Q.minimum_frequency());
//extract P_AB and L_AB
auto q_el = Q[AB];
itype F_AB = q_el.F_ab;
itype P_AB = q_el.P_ab;
itype L_AB = q_el.L_ab;
uint64_t f_replaced = F_AB;
n_distinct_freqs += (F_AB != last_freq);
last_freq = F_AB;
for(itype j = P_AB; j<P_AB+L_AB;++j){
itype i = TP[j];
if(T.pair_starting_at(i) == AB){
ctype A = AB.first;
ctype B = AB.second;
//the context of AB is xABy. We now extract AB's context:
cpair xA = T.pair_ending_at(i);
cpair By = T.next_pair(i);
assert(xA == T.blank_pair() or xA.second == A);
assert(By == T.blank_pair() or By.first == B);
//note: xA and By could be blank pairs if this AB was the first/last pair in the text
//perform replacement
T.replace(i,X);
assert(By == T.blank_pair() || T.pair_starting_at(i) == cpair(X,By.second));
if(Q.contains(xA) && xA != AB){
Q.decrease(xA);
}
if(Q.contains(By) && By != AB){
Q.decrease(By);
}
}
}
/*
* re-scan text positions associated to AB and synchronize if needed
*/
for(itype j = P_AB; j<P_AB+L_AB;++j){
itype i = TP[j];
assert(T.pair_starting_at(i) != AB); //we replaced all ABs ...
if(T[i] == X){
//the context of X is xXy. We now extract X's left (x) and right (y) contexts:
cpair xX = T.pair_ending_at(i);
cpair Xy = T.pair_starting_at(i);
ctype A = AB.first;
ctype B = AB.second;
//careful: x and y could be = X. in this case, before the replacements this xX was equal to ABAB -> a BA disappeared
ctype x = xX.first == X ? B : xX.first;
ctype y = Xy.second == X ? A : Xy.second;
//these are the pairs that disappeared
cpair xA = xX == T.blank_pair() ? xX : cpair {x,A};
cpair By = Xy == T.blank_pair() ? Xy : cpair {B,y};
if(Q.contains(By) && By != AB){
synchro_or_remove_pair<queue_t>(Q, TP, T, By);
}
if(Q.contains(xA) && xA != AB){
synchro_or_remove_pair<queue_t>(Q, TP, T, xA);
}
}
}
assert(Q.contains(AB));
synchronize<queue_t>(Q, TP, T, AB); //automatically removes AB since new AB's frequency is 0
assert(not Q.contains(AB));
//advance next free dictionary symbol
X++;
//cout << " current text size = " << T.number_of_non_blank_characters() << endl << endl;
return f_replaced;
}
void compute_repair(string in){
//packed_gamma_file2<> out_file(out);
//packed_gamma_file2<> out_file(out);
/*
* tradeoff between low-frequency and high-freq phase:
*
* - High-freq phase will use n^(2 - 2*alpha) words of memory and process approximately n^(1-alpha) pairs
* alpha should satisfy 0.5 < alpha < 1
*
* - Low-freq phase will use n^alpha words of memory
*
*/
double alpha = 0.66; // = 2/3
/*
* in the low-frequency pair processing phase, insert at most n/B elements in the hash
*/
uint64_t B = 50;
itype n;
itype sigma = 0; //alphabet size
/*
* Pairs with frequency greater than or equal to min_high_frequency are inserted in high-freq queue
*/
itype min_high_frequency = 0;
itype lf_queue_capacity = 0;
/*
* (1) INITIALIZE DATA STRUCTURES
*/
const itype null = ~itype(0);
ifstream ifs(in);
vector<itype> char_to_int(256,null);
//count file size
{
ifstream file(in,ios::ate);
n = file.tellg();
}
min_high_frequency = std::pow( n, alpha );// n^(alpha)
min_high_frequency = min_high_frequency <2 ? 2 : min_high_frequency;
cout << "File size = " << n << " characters" << endl;
cout << "cut-off frequency = " << min_high_frequency << endl;
itype width = 64 - __builtin_clzll(uint64_t(n));
//largest possible high-frequency dictionary symbol
//at most max_d <= n high-freq dictionary symbols can be created; given that min. freq of
//a high-freq dictionary symbol is f=min_high_frequency and that every new dictionary symbol
//introduces a blank in the text, we have the inequality max_d * f <= n <=> max_d <= n/f
itype max_d = 256+n/min_high_frequency;
//cout << "Max high-frequency dictionary symbol = " << max_d << endl << endl;
//initialize text and text positions
text_t T(n);
itype j = 0;
cout << "filling skippable text with text characters ... " << flush;
char c;
while(ifs.get(c)){
if(char_to_int[uint8_t(c)] == null){
char_to_int[uint8_t(c)] = sigma;
A.push_back(uint8_t(c));
sigma++;
}
T.set(j++,char_to_int[uint8_t(c)]);
}
cout << "done. " << endl << endl;
cout << "alphabet size is " << sigma << endl << endl;
cout << "initializing and sorting text positions vector ... " << flush;
TP_t TP(&T,min_high_frequency);
cout << "done. Number of text positions containing a high-frequency pair: " << TP.size() << endl;
//next free dictionary symbol = sigma
X = sigma;
cout << "\nSTEP 1. HIGH FREQUENCY PAIRS" << endl << endl;
cout << "inserting pairs in high-frequency queue ... " << flush;
hf_q_t HFQ;
new_high_frequency_queue(HFQ, TP, T, min_high_frequency);
cout << "done. Number of distinct high-frequency pairs = " << HFQ.size() << endl;
cout << "Replacing high-frequency pairs ... " << endl;
int last_perc = -1;
uint64_t F = 0;//largest freq
while(HFQ.max() != HFQ.nullpair()){
auto f = substitution_round<hf_q_t>(HFQ, TP, T);
if(last_perc == -1){
F = f;
last_perc = 0;
}else{
int perc = 100-(100*f)/F;
if(perc > last_perc+4){
last_perc = perc;
cout << perc << "%" << endl;
}
}
}
cout << "done. " << endl;
cout << "Peak queue size = " << HFQ.peak() << " (" << double(HFQ.peak())/double(n) << "n)" << endl;
cout << "\nSTEP 2. LOW FREQUENCY PAIRS" << endl << endl;
cout << "Re-computing TP array ... " << flush;
//T.compact(); //remove blank positions
TP.fill_with_text_positions(); //store here all remaining text positions
cout << "done." << endl;
cout << "Sorting TP array ... " << flush;
TP.cluster(); //cluster text positions by character pairs
cout << "done." << endl;
cout << "Counting low-frequency pairs ... " << flush;
/*
* scan sorted array of text positions and count frequencies
*
* in this phase, all pairs have frequency < min_high_frequency
*
* after counting, frequencies[f] is the number of pairs with frequency equal to f
*
*/
//auto frequencies = vector<uint64_t>(min_high_frequency,0);
uint64_t n_lf_pairs = 0; //number of low-frequency pairs
uint64_t f = 1;
for(uint64_t i=1;i<TP.size();++i){
if(T.pair_starting_at(TP[i]) == T.pair_starting_at(TP[i-1])){
f++;
}else{
f=1;
n_lf_pairs++;
}
}
cout << "done. Number of distict low-frequency pairs: "<< n_lf_pairs << endl;
cout << "Filling low-frequency queue ... " << flush;
lf_q_t LFQ(min_high_frequency-1);
f = 1;
using el_t = lf_q_t::el_type;
for(uint64_t i=1;i<TP.size();++i){
if(T.pair_starting_at(TP[i]) == T.pair_starting_at(TP[i-1])){
f++;
}else{
if(f>1){
cpair ab = T.pair_starting_at(TP[i-1]);
assert(i>=f);
itype P_ab = i - f;
itype L_ab = f;
itype F_ab = f;
el_t el = {ab,P_ab,L_ab,F_ab};
LFQ.insert(el);
}
f=1;
}
}
cout << "done." << endl;
cout << "Replacing low-frequency pairs ... " << endl;
pair<itype,itype> replaced = {0,0};
last_perc = -1;
uint64_t tl = T.number_of_non_blank_characters();
while(LFQ.max() != LFQ.nullpair()){
auto f = substitution_round<lf_q_t>(LFQ, TP, T);
int perc = 100-(100*T.number_of_non_blank_characters())/tl;
if(perc>last_perc+4){
last_perc = perc;
cout << perc << "%" << endl;
}
}
cout << "done. " << endl;
cout << "Peak queue size = " << LFQ.peak() << " (" << double(LFQ.peak())/double(n) << "n)" << endl;
cout << "Compressing grammar and storing it to file ... " << endl << endl;
for(itype i=0;i<T.size();++i){
if(not T.is_blank(i)) T_vec.push_back(T[i]);
}
}
void decompress(vector<itype> & A, vector<pair<itype,itype> > & G, vector<itype> & Tc, ofstream & ofs){
std::stack<itype> S;
string buffer;
int buf_size = 1000000;//1 MB buffer
/*
* decompress Tc symbols one by one
*/
for(itype i = 0;i<Tc.size();++i){
S.push(Tc[i]);
while(!S.empty()){
itype X = S.top(); //get symbol
S.pop();//remove top
if(X<A.size()){
char c = A[X];
buffer.push_back(c);
if(buffer.size()==buf_size){
ofs.write(buffer.c_str(),buffer.size());
buffer = string();
}
}else{
//expand rule: X -> ab
auto ab = G[X-A.size()];
S.push(ab.second);
S.push(ab.first);
}
}
}
if(buffer.size()>0) ofs.write(buffer.c_str(),buffer.size());
}
int main(int argc,char** argv) {
if(argc!=3 and argc != 4) help();
string mode(argv[1]);
string in(argv[2]);
string out;
if(argc == 4){
//use output name provided by user
out = string(argv[3]);
}else{
out = string(argv[2]);
if(mode.compare("c")==0){
//if compress mode, append .rp
out.append(".rp");
}else if(mode.compare("d")==0){
//if decompress mode, extract extension (if any)
size_t dot = out.find_last_of(".");
if (dot != std::string::npos){
string name = out.substr(0, dot);
string ext = out.substr(dot, out.size() - dot);
//if extension = .rp, remove it. Otherwise, add extension .decompressed
if(ext.compare(".rp") == 0){
out = name;
}else{
out.append(".decompressed");
}
}
}else{
help();
}
}
if(not ifstream(in).good()) help();
if(mode.compare("c")==0){
cout << "Compressing file " << in << endl;
cout << "Output will be saved to file " << out << endl<<endl;
compute_repair(in);
packed_gamma_file3<> out_file(out);
//compress the grammar with Elias' gamma-encoding and store it to file
out_file.compress_and_store(A,G,T_vec);
}else{
cout << "Decompressing archive " << in << endl;
cout << "Output will be saved to " << out << endl;
auto pgf = packed_gamma_file3<>(in, false);
vector<itype> A;
vector<pair<itype,itype> > G;
vector<itype> Tc;
//read and decompress grammar (the DAG)
pgf.read_and_decompress(A,G,Tc);
ofstream ofs(out);
//expand the grammar to file
decompress(A,G,Tc,ofs);
ofs.close();
cout << "done." << endl;
}
}