-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
146 lines (136 loc) · 5.15 KB
/
main.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
/* ---------------------------------------------------------------------------
** main.cpp
** Simple program that uses suffix trees to get largest and maximal repetitions
** from strings. It has a simple interface to benchmark and debug its behaviour.
**
** Author: Miguel Jorge Galindo Ramos, NIA: 679954
** Santiago Gil Begué, NIA: 683482
** -------------------------------------------------------------------------*/
#include <compactSuffixTree.hpp>
#include <iostream>
#include <fstream>
#include <chrono>
using namespace std;
using namespace std::chrono;
using namespace std::experimental;
tuple<long int, CompactSuffixTree*> timeConstruction(string& parameter, Constructor strategy)
{
high_resolution_clock::time_point t1 = high_resolution_clock::now();
CompactSuffixTree* tmp = new CompactSuffixTree(parameter, strategy);
high_resolution_clock::time_point t2 = high_resolution_clock::now();
return make_tuple(duration_cast<microseconds>(t2-t1).count(), tmp);
}
tuple<long int, string> timeLongestSubstring(const CompactSuffixTree& tree)
{
high_resolution_clock::time_point t1 = high_resolution_clock::now();
string retVal = tree.GetLongestRepeatedSubstring();
high_resolution_clock::time_point t2 = high_resolution_clock::now();
return make_tuple(duration_cast<microseconds>(t2-t1).count(), retVal);
}
tuple<long int, vector<string_view>> timeMaximalRepetitions(const CompactSuffixTree& tree)
{
high_resolution_clock::time_point t1 = high_resolution_clock::now();
vector<string_view> retVal = tree.GetMaximalRepetitions();
high_resolution_clock::time_point t2 = high_resolution_clock::now();
return make_tuple(duration_cast<microseconds>(t2-t1).count(), retVal);
}
/**
* Prints an usage message on how to use this program.
*/
void PrintUsage()
{
cout << "Usage: ./Repeticiones [OPTION]... \n"
"\tWhen the command starts type the string you want to build the tree and get information about.\n"
"\tOptionally you can just redirect a file to stdin or use a pipe.\n"
"\n"
"Options:\n"
"\t-l : get the longest repeated substring in the input string.\n"
"\t-m : get all the maximal repetitions in the input string.\n"
"\t--nlogn : default algorithm, builds the compact suffix tree directly from the input string.\n"
"\t--n2 : builds the compact suffix tree from an auxiliary extended tree.\n"
"\t--print : print the suffix tree in a LaTeX-friendly format.\n"
"\t--time : override behaviour to get the time it takes to run each of the selected options of the program.\n"
"\tThe format for the --time option is:\n"
"\t\t tree_build_time longest_repetition_time maximal_repetitions_time\n"
"\tKeep in mind that time will always be measured for the tree build but not so for the other options, those have to be manually selected.\n"
"\tAll time is measured in microseconds.\n";
}
int main(int argc, char *argv[])
{
// Wrong number of parameters.
if (argc == 1)
{
PrintUsage();
return 1;
}
// Read parameters.
bool getLongest = false;
bool getMaximals = false;
bool time = false;
bool print = false;
Constructor strategy = NLOGN;
for (int i = 1; i < argc; i++)
{
string tmpArg = string(argv[i]);
if (tmpArg == "-l") getLongest = true;
else if (tmpArg == "-m") getMaximals = true;
else if (tmpArg == "--time") time = true;
else if (tmpArg == "--print") print = true;
else if (tmpArg == "--n2") strategy = N2;
else if (tmpArg == "--nlogn") ;
else
{
PrintUsage();
return 1;
}
}
// Input string.
string input;
getline(cin, input);
// Execute the query vs the input string.
if (time)
{
auto result = timeConstruction(input, strategy);
CompactSuffixTree* tree(get<1>(result));
long int treeBuildTime = get<0>(result);
long int longestSubstrTime = 0L;
long int maximalsTime = 0L;
if (getLongest)
{
auto longest = timeLongestSubstring(*tree);
longestSubstrTime = get<0>(longest);
}
if (getMaximals)
{
auto maximals = timeMaximalRepetitions(*tree);
maximalsTime = get<0>(maximals);
}
cout << treeBuildTime << ' ' << longestSubstrTime << ' ' << maximalsTime << endl;
delete tree;
}
else
{
CompactSuffixTree tree (input, strategy);
if (getLongest)
{
cout << "The longest repetition is: " << tree.GetLongestRepeatedSubstring() << endl;
}
if (getMaximals)
{
cout << "The maximals are: " << endl ;
vector<string_view> maximals = tree.GetMaximalRepetitions();
if (maximals.size() != 0)
{
for (int i = 0; i < maximals.size()-1; ++i)
{
cout << maximals[i] << ", ";
}
cout << maximals.back() << endl;
}
}
if (print)
{
tree.Print();
}
}
}