-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpivot.cpp
558 lines (459 loc) · 14 KB
/
pivot.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
/* Copyright (C) 2012,2013 Krzysztof Stachowiak */
/*
* This file is part of stat-toolkit.
*
* stat-toolkit 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 2 of the License, or
* (at your option) any later version.
*
* stat-toolkit 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.
*
* You should have received a copy of the GNU General Public License
* along with stat-toolkit; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <algorithm>
using std::find;
#include <map>
using std::map;
#include <set>
using std::set;
#include <string>
using std::string;
#include <iostream>
using std::istream;
using std::ostream;
using std::cin;
using std::cout;
using std::endl;
#include <boost/lexical_cast.hpp>
using boost::lexical_cast;
using boost::bad_lexical_cast;
#include <boost/xpressive/xpressive.hpp>
using boost::xpressive::sregex;
using boost::xpressive::smatch;
using boost::xpressive::s1;
using boost::xpressive::s2;
using boost::xpressive::_d;
using boost::xpressive::_s;
using boost::xpressive::_;
using boost::xpressive::eos;
#include <unistd.h>
#include "util.h"
#include "groupby.h"
// Handle the command line arguments.
// ----------------------------------
struct arguments {
char delim; // The input/output field separator.
bool hide_domain; // Print "dim = value" or juzt "value"?
bool print_headers; // Print row/column captions?
bool expect_data_header; // Expect column captions in 1st row?
vector<vector<uint32_t>> dimensions; // Pivot dimension definitions.
vector<string> aggr_strs; // Aggregators' construction strings.
};
// Peals out a single dimension definition which is expected to be a
// list of whitespace separated numbers indicating the indices of the
// columns to define a given dimension.
vector<uint32_t> parse_dim_arg(string const& arg) {
vector<uint32_t> result;
uint32_t index;
char base_str[arg.size() + 1];
strcpy(base_str, arg.c_str());
char* pch = strtok(base_str, " ");
while(pch) {
stringstream converter;
converter << pch;
converter >> index;
if(converter.fail()) {
stringstream errorss;
errorss << "Failed parsing dimension index \"" << pch << "\".";
throw errorss.str();
}
result.push_back(index);
pch = strtok(0, " ");
}
return result;
}
// Prepare the structure storing semantically defined input arguments.
arguments parse_args(int argc, char** argv) {
arguments args;
args.delim = '\t';
args.hide_domain = false;
args.print_headers = false;
args.expect_data_header = false;
int c;
while((c = getopt(argc, argv, "a:d:D:RhHn")) != -1) {
switch(c) {
case 'a':
args.aggr_strs.emplace_back(optarg);
break;
case 'd':
if(string(optarg).size() != 1)
throw string("The delimiter is expected to be"
"a single character.");
args.delim = optarg[0];
if(!isprint(args.delim) && args.delim != '\t')
throw string("Cannot use the given character"
"as a delimiter.");
break;
case 'D':
args.dimensions.push_back(parse_dim_arg(optarg));
break;
case 'n':
args.hide_domain = true;
break;
case 'h':
args.print_headers = true;
break;
case 'H':
args.expect_data_header = true;
break;
case '?':
if(optopt == 'a')
throw string("Option -a requires an aggregator"
"argument.");
if(optopt == 'd')
throw string("Option -d requires a delimiter"
"argument.");
if(optopt == 'D')
throw string("Option -D requires a dimension"
"argument.");
// Notice a fallthrough. It is here although it should
// not happen unless someone changes the getopt options
// definition.
default:
throw string("Illegal option -") + (char)optopt + ".";
}
}
return args;
}
// Reading additional data from the input header.
map<uint32_t, string> process_header(istream& in, arguments const& args) {
map<uint32_t, string> result;
string line;
getline(in, line);
vector<string> row = split(line, args.delim);
for(uint32_t i = 0; i < row.size(); ++i)
result[i] = row[i];
return result;
}
// The groupping phase.
// --------------------
groupby::groupper perform_groupping(
istream& in,
arguments const& args) {
// Flatten the dimension definitions.
vector<uint32_t> groupbys;
for(auto const& v : args.dimensions)
for(auto dim : v)
if(find(begin(groupbys), end(groupbys), dim) == end(groupbys))
groupbys.push_back(dim);
else
throw string("Column index repeated in the dimension "
"definition.");
groupby::groupper g(groupbys, args.aggr_strs);
string line;
while(true) {
getline(in, line);
if(!in.good())
break;
vector<string> row = split(line, args.delim);
g.consume_row(row);
}
return g;
}
// The arrangement and printing phase.
// -----------------------------------
// This will attemt at a numeric comparison, and resort to the
// lexicographical ordering upon failure.
bool smart_less(string const& l, string const& r) {
// Try casting to a number.
try {
double num_l = lexical_cast<double>(l);
double num_r = lexical_cast<double>(r);
return num_l < num_r;
} catch(bad_lexical_cast&) {
// Carry on...
}
// If we've got here, the semantic comparison
// must have failed. Perform lexicographical comparison.
return l < r;
}
vector<groupby::group_result> sort_group_results(
vector<groupby::group_result> const& results,
arguments const& args) {
// Copy the input collection.
vector<groupby::group_result> groups(begin(results), end(results));
// Sort the group by the dimensions.
sort(begin(groups), end(groups),
[&args](groupby::group_result const& lhs,
groupby::group_result const& rhs) {
for(auto const& dim : args.dimensions) {
// Compare the values from the columns determined by the
// dimension; return at the first that is not equal for the
// both groups.
for(auto const& i : dim) {
const string& l = lhs.get_def_at(i);
const string& r = rhs.get_def_at(i);
if(l != r)
return smart_less(l, r);
}
}
// Getting here means that the group definitions are equal, which
// is by no means legal.
throw string("Attempted comparing groups defined equally.");
});
return groups;
}
typedef vector<pair<uint32_t, string>> dim_t;
// Creates an object defining a given dimension of a given group.
inline dim_t group_dim(
groupby::group_result const& grp,
vector<uint32_t> const& dim) {
vector<pair<uint32_t, string>> result;
for(uint32_t col : dim)
result.emplace_back(col, grp.get_def_at(col));
return result;
}
// Prints a caption based on a dimension object and an optional mapping
// of the column indices to the corresponding column names.
inline string dim_caption(
dim_t d,
bool hide_domain,
bool has_map,
map<uint32_t, string> mapping) {
stringstream ss;
for(auto const& pr : d) {
if(hide_domain) {
ss << pr.second << " ";
} else {
if(has_map) ss << mapping[pr.first];
else ss << pr.first;
ss << " = " << pr.second << " ";
}
}
return ss.str();
}
// Prints a caption for a given aggregator.
inline string aggr_caption(
string aggr_str,
bool has_map,
map<uint32_t, string> mapping) {
// Parse the aggregator construction string.
smatch match;
sregex base_re = *_s >> (s1 = +_d) >> +_s >> (s2 = +_) >> eos;
if(!regex_match(aggr_str, match, base_re))
throw string("Unrecognize aggregator string \"") + aggr_str + "\".";
uint32_t field;
stringstream fieldss;
fieldss << match[1];
fieldss >> field;
if(fieldss.fail())
throw string("Failed parsing the field mapping of an ggregator.");
// Produce the result string.
stringstream ss;
if(has_map)
ss << match.str(2) << "(" << mapping[field] << ") ";
else
ss << match.str(2) << "(" << field << ") ";
return ss.str();
}
// Prints a row based on a range of the groupping results and a vector of the
// column defining dimensions upon which the results are to be stretched.
// If the given row doesn't contain a group for the given column (that may be
// present in another row) this feature allows to gracefully provide some
// bummer gap fillers which in turn allows to avoid confusion if values get
// placed in a wrong column, just because its real value was missing.
// A dimension offset is provided here as the row and column dimensions will be
// 0 and 1 or 1 and 2 depending on whether the pages dimension is present or not.
template<class It>
void print_row(It grp_begin, It grp_end,
uint32_t dim_offset,
vector<dim_t> const& sorted_columns,
ostream& out,
arguments const& args) {
// Iterate over a sorted list of the columns.
for(auto const& c : sorted_columns) {
// Determine the group to be placed in this column.
It grp;
for(grp = grp_begin; grp != grp_end; ++grp)
if(group_dim(*grp, args.dimensions[dim_offset + 1]) == c)
break;
if(grp == grp_end)
out << "x" << args.delim;
else
for(auto const& pr : grp->aggregators)
out << pr.second << args.delim;
}
out << endl;
}
// Prints a page of the pivot table based on a range of the groupping results.
// The dimension offset argument prepare the function to work with the data that
// may optionally have the page dimension defined which comes at the beginning
// of the dimensions list. Therefore its presence or absence shifts the rest
// of the dimensions (the rows and columns) one way or another.
template<class It>
void print_page(It grp_begin, It grp_end,
uint32_t dim_offset,
bool hide_domain,
bool has_map,
map<uint32_t, string> mapping,
ostream& out,
arguments const& args) {
// Determine the page's columns.
// -----------------------------
set<dim_t> col_set;
for(auto grp_it = grp_begin; grp_it != grp_end; ++grp_it)
col_set.insert(group_dim(*grp_it, args.dimensions[dim_offset + 1]));
vector<dim_t> sorted_columns(begin(col_set), end(col_set));
sort(begin(sorted_columns), end(sorted_columns),
[&args](dim_t const& lhs, dim_t const& rhs) {
uint32_t num_defs = lhs.size();
if(num_defs != rhs.size())
throw string("Attempted comparing dimensionf"
" of differend sizes");
for(uint32_t i = 0; i < num_defs; ++i) {
if(lhs.at(i).first != rhs.at(i).first)
return lhs.at(i).first < rhs.at(i).first;
if(lhs.at(i).second != rhs.at(i).second)
return lhs.at(i).second < rhs.at(i).second;
}
return false;
});
// Print the column captions.
// --------------------------
if(args.print_headers) {
// Print the row caption header.
for(uint32_t col : args.dimensions[dim_offset])
if(has_map)
out << mapping[col] << " ";
else
out << col << " ";
out << args.delim;
// Print all the column captions.
for(dim_t const& d : sorted_columns)
for(string const& a : args.aggr_strs)
out << dim_caption(d, hide_domain, has_map, mapping) << ' '
<< aggr_caption(a, has_map, mapping)
<< args.delim;
out << endl;
}
// Print all rows.
// ---------------
auto row_begin = grp_begin;
auto it = grp_begin;
dim_t current_row = group_dim(*it, args.dimensions[dim_offset]);
do {
dim_t group_row = group_dim(*it, args.dimensions[dim_offset]);
if(group_row != current_row) {
if(args.print_headers) {
for(auto const& pr : current_row)
out << pr.second << " ";
out << args.delim;
}
print_row(row_begin, it, dim_offset, sorted_columns, out, args);
row_begin = it;
current_row = group_dim(*it, args.dimensions[dim_offset]);
}
} while((++it) != grp_end);
if(args.print_headers) {
for(auto const& pr : current_row)
out << pr.second << " ";
out << args.delim;
}
print_row(row_begin, it, dim_offset, sorted_columns, out, args);
}
// Prints a pivot table based on the groupper or rather its resulting grouppings.
void print_table(groupby::groupper const& g,
bool hide_domain,
bool has_map,
map<uint32_t, string> mapping,
ostream& out,
arguments const& args) {
auto sorted_groups = sort_group_results(g.copy_result(), args);
if(args.dimensions.size() == 2) {
// Print just one page.
// --------------------
print_page(begin(sorted_groups),
end(sorted_groups),
0,
hide_domain,
has_map,
mapping,
out,
args);
} else if(args.dimensions.size() == 3) {
// Print all the pages.
// --------------------
auto page_begin = begin(sorted_groups);
auto it = begin(sorted_groups);
dim_t current_page = group_dim(*it, args.dimensions[0]);
do {
dim_t group_page = group_dim(*it, args.dimensions[0]);
// Check if we've entered another page.
if(group_page != current_page) {
// Print current page.
if(args.print_headers)
out << "Page: "
<< dim_caption(current_page, hide_domain, has_map, mapping)
<< endl;
print_page(page_begin,
it,
1,
hide_domain,
has_map,
mapping,
out,
args);
// Begin another page.
page_begin = it;
current_page = group_dim(*it, args.dimensions[0]);
}
} while((++it) != end(sorted_groups));
// Finalize the remaining page.
if(args.print_headers)
out << "Page: "
<< dim_caption(current_page, hide_domain, has_map, mapping)
<< endl;
print_page(page_begin,
it,
1,
hide_domain,
has_map,
mapping,
out,
args);
} else {
throw string("Only 2 or 3 dimensions supported.");
}
}
int main(int argc, char** argv) {
try {
// Parse and validate the arguments.
arguments args = parse_args(argc, argv);
if(args.aggr_strs.empty())
throw string("At leas one aggregator must be defined.");
if(args.dimensions.size() != 2 && args.dimensions.size() != 3)
throw string("Only 2 or 3 dimensions are supported.");
// Headers variant.
map<uint32_t, string> mapping;
if(args.expect_data_header)
mapping = process_header(cin, args);
// Perform the processing.
groupby::groupper g = perform_groupping(cin, args);
print_table(g,
args.hide_domain,
args.expect_data_header,
mapping,
cout,
args);
return 0;
} catch(string& ex) {
cout << "Error : " << ex << endl;
return 1;
}
}