forked from Gogis0/ShapedSlp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlpEncBuild.cpp
209 lines (182 loc) · 7.29 KB
/
SlpEncBuild.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
#include <iostream>
#include <string>
#include <queue>
#include "cmdline.h"
#include "Common.hpp"
#include "PlainSlp.hpp"
#include "PoSlp.hpp"
#include "ShapedSlp_Status.hpp"
#include "ShapedSlp.hpp"
#include "ShapedSlpV2.hpp"
#include "SelfShapedSlp.hpp"
#include "SelfShapedSlpV2.hpp"
#include "DirectAccessibleGammaCode.hpp"
#include "IncBitLenCode.hpp"
#include "FixedBitLenCode.hpp"
#include "SelectType.hpp"
#include "VlcVec.hpp"
using namespace std;
using namespace std::chrono;
using timer = std::chrono::high_resolution_clock;
using var_t = uint32_t;
template
<
class SlpT
>
void measure
(
const NaiveSlp<var_t> & slp,
std::string out
) {
auto start = timer::now();
SlpT sslp(slp);
auto stop = timer::now();
cout << "time to encode (ms): " << duration_cast<milliseconds>(stop-start).count() << endl;
sslp.printStatus();
start = timer::now();
ofstream fs(out);
sslp.serialize(fs);
stop = timer::now();
cout << "time to serialize (ms): " << duration_cast<milliseconds>(stop-start).count() << endl;
}
template
<
class SlpT
>
void measure_PoSlp
(
const NaiveSlp<var_t> & slp,
std::string out
) {
NaiveSlp<var_t> temp(slp);
auto start = timer::now();
temp.makeBinaryTree();
SlpT poslp;
poslp.makePoSlpWithLen(temp);
auto stop = timer::now();
cout << "time to encode (ms): " << duration_cast<milliseconds>(stop-start).count() << endl;
poslp.printStatus();
start = timer::now();
ofstream fs(out);
poslp.serialize(fs);
stop = timer::now();
cout << "time to serialize (ms): " << duration_cast<milliseconds>(stop-start).count() << endl;
}
template
<
class SlpT
>
void measure_PlainSlp
(
const NaiveSlp<var_t> & slp,
std::string out
) {
NaiveSlp<var_t> temp(slp);
auto start = timer::now();
temp.makeBinaryTree();
SlpT pslp;
pslp.init(temp);
auto stop = timer::now();
cout << "time to encode (ms): " << duration_cast<milliseconds>(stop-start).count() << endl;
pslp.printStatus();
start = timer::now();
ofstream fs(out);
pslp.serialize(fs);
stop = timer::now();
cout << "time to serialize (ms): " << duration_cast<milliseconds>(stop-start).count() << endl;
}
int main(int argc, char* argv[])
{
using Fblc = FixedBitLenCode<>;
using SelSd = SelectSdvec<>;
using SelMcl = SelectMcl<>;
using DagcSd = DirectAccessibleGammaCode<SelSd>;
using DagcMcl = DirectAccessibleGammaCode<SelMcl>;
using Vlc64 = VlcVec<sdsl::coder::elias_delta, 64>;
using Vlc128 = VlcVec<sdsl::coder::elias_delta, 128>;
using funcs_type = map<string,
void(*)
(
const NaiveSlp<var_t> & slp,
std::string out
)>;
funcs_type funcs;
//// PlainSlp
funcs.insert(make_pair("PlainSlp_FblcFblc", measure_PlainSlp<PlainSlp<var_t, Fblc, Fblc>>));
funcs.insert(make_pair("PlainSlp_IblcFblc", measure_PlainSlp<PlainSlp<var_t, IncBitLenCode, Fblc>>));
funcs.insert(make_pair("PlainSlp_32Fblc", measure_PlainSlp<PlainSlp<var_t, FixedBitLenCode<32>, Fblc>>));
//// PoSlp: Post-order SLP
//// Sometimes PoSlp_Sd is better than PoSlp_Iblc
funcs.insert(make_pair("PoSlp_Iblc", measure_PoSlp<PoSlp<var_t, IncBitLenCode>>));
funcs.insert(make_pair("PoSlp_Sd", measure_PoSlp<PoSlp<var_t, DagcSd>>));
// funcs.insert(make_pair("PoSlp_Mcl", measure_PoSlp<PoSlp<var_t, DagcMcl>>));
//// ShapedSlp: plain implementation of slp encoding that utilizes shape-tree grammar
//// Since bit length to represent slp element is small, SelMcl is good for them.
//// For stg and bal element, SelSd is better
funcs.insert(make_pair("ShapedSlp_SdMclSd_SdMcl", measure<ShapedSlp<var_t, DagcSd, DagcMcl, DagcSd, SelSd, SelMcl>>));
funcs.insert(make_pair("ShapedSlp_SdSdSd_SdMcl", measure<ShapedSlp<var_t, DagcSd, DagcSd, DagcSd, SelSd, SelMcl>>));
//// ShapedSlpV2: all vlc vectors are merged into one.
//// Generally encoding size gets worse than ShapedSlp_SdMclSd_SdMcl because
//// - bit length to represnet stg and bal element is large and DagcSd is a good choice, whereas
//// - bit size to represent slp element is significantly small and DagcMcl should be used
funcs.insert(make_pair("ShapedSlpV2_Sd_SdMcl", measure<ShapedSlpV2<var_t, DagcSd, SelSd, SelMcl>>));
// funcs.insert(make_pair("ShapedSlpV2_SdSdSd", measure<ShapedSlp<var_t, DagcSd, SelSd, SelSd>>));
// funcs.insert(make_pair("ShapedSlpV2_SdMclMcl", measure<ShapedSlp<var_t, DagcSd, SelMcl, SelMcl>>));
// funcs.insert(make_pair("ShapedSlpV2_Vlc128SdSd", measure<ShapedSlp<var_t, Vlc128, SelSd, SelSd>>));
//// SelfShapedSlp: ShapedSlp that does not use shape-tree grammar
funcs.insert(make_pair("SelfShapedSlp_SdSd_Sd", measure<SelfShapedSlp<var_t, DagcSd, DagcSd, SelSd>>));
funcs.insert(make_pair("SelfShapedSlp_SdSd_Mcl", measure<SelfShapedSlp<var_t, DagcSd, DagcSd, SelMcl>>));
// funcs.insert(make_pair("SelfShapedSlp_MclMcl_Sd", measure<SelfShapedSlp<var_t, DagcMcl, DagcMcl, SelSd>>));
// funcs.insert(make_pair("SelfShapedSlp_SdMcl_Sd", measure<SelfShapedSlp<var_t, DagcSd, DagcMcl, SelSd>>));
//// SelfShapedSlpV2:
//// attempted to asign smaller offsets to frequent variables by giving special seats for hi-frequent ones
funcs.insert(make_pair("SelfShapedSlpV2_SdSd_Sd", measure<SelfShapedSlpV2<var_t, DagcSd, DagcSd, SelSd>>));
// funcs.insert(make_pair("SelfShapedSlpV2_SdSd_Mcl", measure<SelfShapedSlpV2<var_t, DagcSd, DagcSd, SelMcl>>));
//// ShapedSlp_Status_SdMclSd_SdMcl:
funcs.insert(make_pair("ShapedSlp_Status_SdMclSd_SdMcl", measure<ShapedSlp_Status<var_t, DagcSd, DagcMcl, DagcSd, SelSd, SelMcl>>));
string methodList;
for (auto itr = funcs.begin(); itr != funcs.end(); ++itr) {
methodList += itr->first + ". ";
}
cmdline::parser parser;
parser.add<string>("input", 'i', "input file name. <input>.C and <input>.R in Navarro's RePair format", true);
parser.add<string>("output", 'o', "output file to which data structure is written", true);
parser.add<string>("format", 'f', "format of input: NavarroRepair | Bigrepair | rrepair", true);
parser.add<string>("encoding", 'e', "encoding: " + methodList, true);
parser.parse_check(argc, argv);
const string in = parser.get<string>("input");
const string out = parser.get<string>("output");
const string format = parser.get<string>("format");
const string encoding = parser.get<string>("encoding");
NaiveSlp<var_t> slp;
if (format.compare("NavarroRepair") == 0) {
slp.load_NavarroRepair(in.data());
} else if (format.compare("Bigrepair") == 0) {
slp.load_Bigrepair(in.data(),false);
} else if (format.compare("rrepair") == 0) {
slp.load_Bigrepair(in.data(),true);
} else if (format.compare("solca") == 0) {
slp.load_solca(in.data());
} else {
cerr << "error: specify a valid format name: NavarroRepair. Bigrepair" << endl;
exit(1);
}
if (encoding.compare("All") == 0) {
for (auto itr = funcs.begin(); itr != funcs.end(); ++itr) {
if ((itr->first).compare("ShapedSlp_Status_SdMclSd_SdMcl") != 0) {
cout << itr->first << ": BEGIN" << std::endl;
itr->second(slp, out + itr->first);
cout << itr->first << ": END" << std::endl;
}
}
} else {
auto itr = funcs.find(encoding);
if (itr != funcs.end()) {
itr->second(slp, out);
} else {
cerr << "error: specify a valid encoding name: " + methodList << endl;
exit(1);
}
}
return 0;
}