/
sure-bench.cpp
154 lines (134 loc) · 3.6 KB
/
sure-bench.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
#include <cstdio>
#include <iostream>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include "regex/RegEx.hpp"
#include "SuccinctFile.hpp"
// Debug
void display(RegEx *re) {
switch(re->getType()) {
case RegExType::Blank:
{
fprintf(stderr, "<blank>");
break;
}
case RegExType::Primitive:
{
RegExPrimitive *p = ((RegExPrimitive *)re);
std::string p_type;
switch(p->getPrimitiveType()) {
case RegExPrimitiveType::Mgram:
{
p_type = "mgram";
break;
}
case RegExPrimitiveType::Dot:
{
p_type = "dot";
break;
}
case RegExPrimitiveType::Range:
{
p_type = "range";
break;
}
default:
p_type = "unknown";
}
fprintf(stderr, "\"%s\":%s", p->getPrimitive().c_str(), p_type.c_str());
break;
}
case RegExType::Repeat:
{
fprintf(stderr, "repeat(");
display(((RegExRepeat *)re)->getInternal());
fprintf(stderr, ")");
break;
}
case RegExType::Concat:
{
fprintf(stderr, "concat(");
display(((RegExConcat *)re)->getLeft());
fprintf(stderr, ",");
display(((RegExConcat *)re)->getRight());
fprintf(stderr, ")");
break;
}
case RegExType::Union:
{
fprintf(stderr, "union(");
display(((RegExUnion *)re)->getFirst());
fprintf(stderr, ",");
display(((RegExUnion *)re)->getSecond());
fprintf(stderr, ")");
break;
}
}
}
void print_usage(char *exec) {
fprintf(stderr, "Usage: %s [-m mode] [-o] [file]\n", exec);
}
typedef unsigned long long int timestamp_t;
static timestamp_t get_timestamp() {
struct timeval now;
gettimeofday (&now, NULL);
return (now.tv_usec + (time_t)now.tv_sec * 1000000) / 1000;
}
int main(int argc, char **argv) {
if(argc < 2 || argc > 5) {
print_usage(argv[0]);
return -1;
}
int c;
uint32_t mode = 0;
bool opt = false;
while((c = getopt(argc, argv, "m:o")) != -1) {
switch(c) {
case 'm':
mode = atoi(optarg);
break;
case 'o':
opt = true;
break;
default:
fprintf(stderr, "Invalid option %c\n", c);
exit(0);
}
}
if(optind == argc) {
print_usage(argv[0]);
return -1;
}
fprintf(stderr, "opt = %d\n", opt);
std::string filename = std::string(argv[optind]);
SuccinctFile *s_file = NULL;
if(mode == 0) {
std::cout << "Constructing Succinct data structures...\n";
s_file = new SuccinctFile(filename);
std::cout << "Serializing Succinct data structures...\n";
std::ofstream s_out(filename + ".succinct");
s_file->serialize();
s_out.close();
} else {
std::cout << "De-serializing Succinct data structures...\n";
s_file = new SuccinctFile(filename, SuccinctMode::LOAD_IN_MEMORY);
}
std::cout << "Done.\n";
while(true) {
char exp[100];
std::cout << "sure> ";
std::cin.getline(exp, sizeof(exp));
std::string query = std::string(exp);
timestamp_t start = get_timestamp();
SRegEx re(query, s_file, opt);
fprintf(stderr, "Regex: [%s]\nExplanation: [", exp);
re.explain();
fprintf(stderr, "]\n");
re.execute();
timestamp_t tot_time = get_timestamp() - start;
re.show_results(10);
std::cout << "Query took " << tot_time << " ms\n";
}
return 0;
}