-
Notifications
You must be signed in to change notification settings - Fork 14
/
Query.cpp
57 lines (49 loc) · 1.7 KB
/
Query.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
#include<string>
#include<memory>
#include<set>
#include<algorithm>
#include<iterator>
#include "TextQuery.h"
#include "Query.h"
Query::Query(const std::string &s) : q(new WordQuery(s)) { }
Query operator~(const Query &operand)
{
return std::shared_ptr<Query_base>(new NotQuery(operand));
}
Query operator&(const Query &lhs, const Query &rhs)
{
return std::shared_ptr<Query_base>(new AndQuery(lhs, rhs));
}
Query operator|(const Query &lhs, const Query &rhs)
{
return std::shared_ptr<Query_base>(new OrQuery(lhs, rhs));
}
QueryResult OrQuery::eval(const TextQuery &text) const
{
QueryResult left = lhs.eval(text), right = rhs.eval(text);
std::shared_ptr<std::set<line_no>> ret_lines = std::make_shared<std::set<line_no>>(left.begin(), left.end());
ret_lines->insert(right.begin(), right.end());
return QueryResult(rep(), ret_lines, left.get_file());
}
QueryResult AndQuery::eval(const TextQuery &text) const
{
QueryResult left = lhs.eval(text), right = rhs.eval(text);
std::shared_ptr<std::set<line_no>> ret_lines = std::make_shared<std::set<line_no>>();
std::set_intersection(left.begin(), left.end(), right.begin(), right.end(), std::inserter(*ret_lines, ret_lines->begin()));
return QueryResult(rep(), ret_lines, left.get_file());
}
QueryResult NotQuery::eval(const TextQuery &text) const
{
QueryResult result = query.eval(text);
std::shared_ptr<std::set<line_no>> ret_lines = std::make_shared<std::set<line_no>>();
std::set<line_no>::iterator beg = result.begin(), end = result.end();
line_no sz = result.get_file()->size();
for(line_no n = 0; n != sz; ++n)
{
if(beg == end || *beg != n)
ret_lines->insert(n);
else if(beg != end)
++beg;
}
return QueryResult(rep(), ret_lines, result.get_file());
}