/
QueryResult.cpp
76 lines (66 loc) · 2.34 KB
/
QueryResult.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
#include "QueryResult.h"
#include <algorithm>
void QueryResult::do_or(const QueryResult &other, QueryCounter *counter) {
auto op = QueryOperation(counter);
if (this->is_everything() || other.is_everything()) {
has_everything = true;
results = SortedRun();
} else {
results.do_or(other.results);
}
}
void QueryResult::do_and(const QueryResult &other, QueryCounter *counter) {
auto op = QueryOperation(counter);
if (other.is_everything()) {
} else if (this->is_everything()) {
results = other.results;
has_everything = other.has_everything;
} else {
results.do_and(other.results);
}
}
QueryResult QueryResult::do_min_of_real(
int cutoff, const std::vector<const QueryResult *> &sources) {
std::vector<const SortedRun *> nontrivial_sources;
for (const auto *source : sources) {
if (source->is_everything()) {
cutoff -= 1;
} else if (!source->is_empty()) {
nontrivial_sources.push_back(&source->vector());
}
}
// '0 of (...)' should match everything.
if (cutoff <= 0) {
return QueryResult::everything();
}
// Short circuit when cutoff is too big.
// This may happen when there are empty results in sources.
if (cutoff > static_cast<int>(nontrivial_sources.size())) {
return QueryResult::empty();
}
// Special case optimisation - reduction to AND.
if (cutoff == static_cast<int>(nontrivial_sources.size())) {
QueryResult out{nontrivial_sources[0]->clone()};
for (int i = 1; i < nontrivial_sources.size(); i++) {
out.results.do_and(*nontrivial_sources[i]);
}
return out;
}
// Special case optimisation - reduction to OR.
if (cutoff == 1) {
QueryResult out{nontrivial_sources[0]->clone()};
for (int i = 1; i < nontrivial_sources.size(); i++) {
out.results.do_or(*nontrivial_sources[i]);
}
return out;
}
return QueryResult(SortedRun::pick_common(cutoff, nontrivial_sources));
}
QueryResult QueryResult::do_min_of(
int cutoff, const std::vector<const QueryResult *> &sources,
QueryCounter *counter) {
// TODO: sources can be mutable here, to save us some copies later.
QueryOperation op(counter);
QueryResult out{do_min_of_real(cutoff, sources)};
return out;
}