-
Notifications
You must be signed in to change notification settings - Fork 20
/
percolator.cpp
137 lines (100 loc) · 5.01 KB
/
percolator.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
#include "percolator.h"
using namespace Trinity;
bool percolator_query::match(percolator_document_proxy &src) const {
return exec(root, src);
}
bool percolator_query::exec(const exec_node n, percolator_document_proxy &src) const {
switch (n.fp) {
case ENT::matchterm:
return src.match_term(n.u16);
case ENT::constfalse:
return false;
case ENT::consttrue:
return true;
case ENT::matchallterms: {
const auto run = static_cast<const compilation_ctx::termsrun *>(n.ptr);
for (decltype(run->size) i{0}; i != run->size; ++i) {
if (!src.match_term(run->terms[i]))
return false;
}
return true;
} break;
case ENT::matchanyterms: {
const auto run = static_cast<const compilation_ctx::termsrun *>(n.ptr);
for (decltype(run->size) i{0}; i != run->size; ++i) {
if (src.match_term(run->terms[i]))
return true;
}
return false;
} break;
case ENT::unaryand:
return exec(static_cast<const compilation_ctx::unaryop_ctx *>(n.ptr)->expr, src);
case ENT::unarynot:
return !exec(static_cast<const compilation_ctx::unaryop_ctx *>(n.ptr)->expr, src);
case ENT::matchanyphrases: {
const auto run = static_cast<const compilation_ctx::phrasesrun *>(n.ptr);
for (decltype(run->size) i{0}; i != run->size; ++i) {
const auto p = run->phrases[i];
if (src.match_phrase(p->termIDs, p->size))
return true;
}
return false;
}
case ENT::matchallphrases: {
const auto run = static_cast<const compilation_ctx::phrasesrun *>(n.ptr);
for (decltype(run->size) i{0}; i != run->size; ++i) {
const auto p = run->phrases[i];
if (!src.match_phrase(p->termIDs, p->size))
return false;
}
return true;
}
case ENT::matchphrase: {
const auto p = static_cast<const compilation_ctx::phrase *>(n.ptr);
return src.match_phrase(p->termIDs, p->size);
}
case ENT::logicaland: {
const auto b = static_cast<const compilation_ctx::binop_ctx *>(n.ptr);
return exec(b->lhs, src) && exec(b->rhs, src);
}
case ENT::logicalnot: {
const auto b = static_cast<const compilation_ctx::binop_ctx *>(n.ptr);
return exec(b->lhs, src) && !exec(b->rhs, src);
}
case ENT::logicalor: {
const auto b = static_cast<const compilation_ctx::binop_ctx *>(n.ptr);
return exec(b->lhs, src) || exec(b->rhs, src);
}
case ENT::matchsome: {
const auto pm = static_cast<compilation_ctx::partial_match_ctx *>(n.ptr);
uint16_t matched{0};
for (decltype(pm->size) i{0}; i != pm->size; ++i) {
if (exec(pm->nodes[i], src) && ++matched == pm->min)
return true;
}
return false;
}
case ENT::matchallnodes: {
const auto g = static_cast<compilation_ctx::nodes_group *>(n.ptr);
for (decltype(g->size) i{0}; i != g->size; ++i) {
if (!exec(g->nodes[i], src))
return false;
}
return true;
}
case ENT::matchanynodes: {
const auto g = static_cast<compilation_ctx::nodes_group *>(n.ptr);
for (decltype(g->size) i{0}; i != g->size; ++i) {
if (exec(g->nodes[i], src))
return true;
}
return false;
}
case ENT::consttrueexpr:
return true;
case ENT::dummyop:
case ENT::SPECIALIMPL_COLLECTION_LOGICALOR:
case ENT::SPECIALIMPL_COLLECTION_LOGICALAND:
std::abort();
}
}