-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.cpp
169 lines (143 loc) · 4.59 KB
/
driver.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
#include <vector>
#include <string>
#include <cassert>
#include <utility>
#include <fstream>
#include <iostream>
#include <cstring>
#include <iterator>
#include <sstream>
#include <chrono>
#include "index.hpp"
#include "read_mat.hpp"
#include "naive.hpp"
using namespace std;
int parse_formula(vector<string>& vec, string line, const char * delim) {
char cstr[line.size()+1];
strcpy(cstr, line.c_str());
char *token = strtok(cstr, delim);
short count = 0;
short size = 8;
vec.resize(size);
while (token != NULL) {
vec[count] = token;
token = strtok(NULL, delim);
count++;
if (count == size) {
size = size*2;
vec.resize(size);
}
}
return count;
}
int parse_clause(vector<pair<bool,string> >& vec, string line, const char * delim) {
char cstr[line.size()+1];
strcpy(cstr, line.c_str());
char *token = strtok(cstr, delim);
short count = 0;
short size = 8;
vec.resize(size);
while (token != NULL) {
if (token[0] == '!') {
vec[count].first = false;
vec[count].second = &token[1];
} else {
vec[count].second = token;
vec[count].first = true;
}
token = strtok(NULL, delim);
count++;
if (count == size) {
size *= 2;
vec.resize(size);
}
}
return count;
}
vector<vector<pair<bool,string> > > build_formula(const string line) {
vector<string> clauses;
vector<vector<pair<bool,string> > > formula;
short num_clauses = parse_formula(clauses, line, "^");
formula.resize(num_clauses);
// parse each clause into literals
short num_literals;
for (int i=0; i < num_clauses; i++) {
num_literals = parse_clause(formula[i], clauses[i], "|");
formula[i].resize(num_literals);
}
return formula;
}
int main(int argc, char** argv)
{
vector<vector<bool> > matrix; // genes are rows, cells are columns
vector<string> cellNames, geneNames;
if (argc < 4) {
matrix.resize(7);
for (int i=0; i<7; i++) {
for (int j=0; j<5; j++) {
matrix[i].resize(5);
}
}
matrix[0][0] = 1;
matrix[2][0] = 1;
matrix[3][0] = 1;
matrix[5][0] = 1;
matrix[0][1] = 1;
matrix[2][1] = 1;
matrix[3][1] = 1;
matrix[0][2] = 1;
cellNames.push_back("a");
cellNames.push_back("b");
cellNames.push_back("c");
cellNames.push_back("d");
cellNames.push_back("e");
geneNames.push_back("a");
geneNames.push_back("b");
geneNames.push_back("c");
geneNames.push_back("d");
geneNames.push_back("e");
geneNames.push_back("f");
geneNames.push_back("gg");
} else {
ifstream file(argv[3]);
auto ret = parseMatFile(file);
if (std::get<0>(ret).size())
std::tie(cellNames, geneNames, matrix) = ret;
else
std::exit(1);
}
if (argc >= 2) {
if (string("query").compare(argv[1]) == 0) {
ifstream input(argv[2]);
string line;
vector<string> clauses;
vector<vector<pair<bool,string> > > formula;
chrono::high_resolution_clock::duration sum1(0);
chrono::high_resolution_clock::duration sum2(0);
Index ind(matrix, geneNames, cellNames);
Naive naive(matrix, geneNames, cellNames);
while (getline(input, line)) {
vector<vector<pair<bool,string> > > formula = build_formula(line);
auto exp1 = chrono::high_resolution_clock::now();
vector<int> answer1 = ind.first_clause(formula);
sum1 += chrono::high_resolution_clock::now() - exp1;
exp1 = chrono::high_resolution_clock::now();
vector<int> answer2 = naive.query(formula);
sum2 += chrono::high_resolution_clock::now() - exp1;
std::sort(answer1.begin(), answer1.end());
std::sort(answer2.begin(), answer2.end());
cerr << "num set ours: " << answer1.size() << '\n';
cerr << "num set theirs: " << answer2.size() << '\n';
if (answer1 != answer2) {
cerr << "answers didn't match for formula:" << line << endl;
exit(1);
} else
cout << "both had: " << answer1.size() << " satisfied\n";
}
cout << "time ours: " << chrono::duration_cast<chrono::microseconds>(sum1).count() << "μs\n";
cout << "time naive: " << chrono::duration_cast<chrono::microseconds>(sum2).count() << "μs\n";
} else {
cout << "Unknown command. Please use query.";
}
}
}