-
Notifications
You must be signed in to change notification settings - Fork 0
/
distinct_squares.cpp
69 lines (57 loc) · 2.44 KB
/
distinct_squares.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
#include "main.hpp"
#include <gflags/gflags.h>
DEFINE_bool(quiet, false, "Do not output the found squares");
DEFINE_bool(verbose, false, "Output additionally the integer values of each output character");
int main(int argc, char* argv[])
{
google::ParseCommandLineFlags(&argc, &argv, true);
if(argc == 1 || strcmp(argv[1],"-h") == 0 || strcmp(argv[1],"--help") == 0 || strcmp(argv[1],"-help") == 0) {
// google::ShowUsageWithFlagsRestrict(argv[0], __FILE__); //shortcut
std::vector<google::CommandLineFlagInfo> info;
google::GetAllFlags(&info);
std::cout << argv[0] << " [options] {file to analyze}" << std::endl;
std::cout << "You need to provide at least a file whose distinct squares should be computed." << std::endl << std::endl;
std::cout
<< std::setw(20) << std::setiosflags(std::ios::left) << "Parameter"
<< std::setw(10) << "Type"
<< std::setw(20) << "Default"
<< "Description" << std::endl;
std::cout << std::endl;
for(auto it = info.cbegin(); it != info.cend(); ++it) {
if(it->filename != __FILE__) continue;
std::cout
<< std::setw(20) << std::setiosflags(std::ios::left)<< (std::string("--")+ it->name)
<< std::setw(10) << it->type
<< std::setw(20) << (std::string("(") + it->default_value + ")")
<< it->description << std::endl;
}
return 0;
}
std::ifstream t(argv[1]);
if(!((bool)t)) {
std::cerr << "Could not open file " << argv[1] << std::endl;
return 1;
}
std::string text((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
size_t square_counter = 0; //! counts all found squares
if(FLAGS_quiet) {
compute_distinct_squares(text, [&text,&square_counter] (len_t, len_t ) {
++square_counter;
});
} else {
compute_distinct_squares(text, [&text,&square_counter] (len_t pos, len_t period) {
std::cout << "T[" << pos << "," << (pos+period*2-1) << "] = " << text.substr(pos,period) << "," << text.substr(pos+period,period) << " | ";
++square_counter;
if(FLAGS_verbose) {
for(len_t i = pos; i < pos+period*2; ++i) {
if(i == pos+period) DVLOG(1) << ",";
std::cout << "(" << ((size_t)text[i]) << ")";
}
}
std::cout << std::endl;
});
}
std::cout << "The number of all distinct squares is " << square_counter << std::endl;
return 0;
}