10 #include "llvm/Support/Casting.h" 25 class AndIterator :
public Iterator {
27 explicit AndIterator(std::vector<std::unique_ptr<Iterator>> AllChildren)
28 : Iterator(
Kind::And),
Children(std::move(AllChildren)) {
29 assert(!
Children.empty() &&
"AND iterator should have at least one child.");
31 for (
const auto &Child : Children)
32 ReachedEnd |= Child->reachedEnd();
41 llvm::sort(Children, [](
const std::unique_ptr<Iterator> &LHS,
42 const std::unique_ptr<Iterator> &RHS) {
43 return LHS->estimateSize() < RHS->estimateSize();
47 bool reachedEnd()
const override {
return ReachedEnd; }
50 void advance()
override {
51 assert(!reachedEnd() &&
"AND iterator can't advance() at the end.");
52 Children.front()->advance();
57 void advanceTo(
DocID ID)
override {
58 assert(!reachedEnd() &&
"AND iterator can't advanceTo() at the end.");
59 Children.front()->advanceTo(ID);
63 DocID peek()
const override {
return Children.front()->peek(); }
66 assert(!reachedEnd() &&
"AND iterator can't consume() at the end.");
68 for (
const auto &Child : Children)
69 Boost *= Child->consume();
73 size_t estimateSize()
const override {
74 return Children.front()->estimateSize();
78 llvm::raw_ostream &dump(llvm::raw_ostream &OS)
const override {
81 for (
const auto &Child : Children) {
92 ReachedEnd |= Children.front()->reachedEnd();
95 auto SyncID = Children.front()->peek();
97 bool NeedsAdvance =
false;
100 for (
auto &Child : Children) {
101 Child->advanceTo(SyncID);
102 ReachedEnd |= Child->reachedEnd();
109 if (Child->peek() > SyncID) {
110 SyncID = Child->peek();
114 }
while (NeedsAdvance);
120 std::vector<std::unique_ptr<Iterator>> Children;
124 bool ReachedEnd =
false;
134 class OrIterator :
public Iterator {
136 explicit OrIterator(std::vector<std::unique_ptr<Iterator>> AllChildren)
137 : Iterator(
Kind::Or), Children(std::move(AllChildren)) {
138 assert(!Children.empty() &&
"OR iterator should have at least one child.");
142 bool reachedEnd()
const override {
143 for (
const auto &Child : Children)
144 if (!Child->reachedEnd())
150 void advance()
override {
151 assert(!reachedEnd() &&
"OR iterator can't advance() at the end.");
152 const auto SmallestID = peek();
153 for (
const auto &Child : Children)
154 if (!Child->reachedEnd() && Child->peek() == SmallestID)
159 void advanceTo(
DocID ID)
override {
160 assert(!reachedEnd() &&
"OR iterator can't advanceTo() at the end.");
161 for (
const auto &Child : Children)
162 if (!Child->reachedEnd())
163 Child->advanceTo(ID);
168 DocID peek()
const override {
169 assert(!reachedEnd() &&
"OR iterator can't peek() at the end.");
170 DocID Result = std::numeric_limits<DocID>::max();
172 for (
const auto &Child : Children)
173 if (!Child->reachedEnd())
174 Result = std::min(Result, Child->peek());
182 assert(!reachedEnd() &&
"OR iterator can't consume() at the end.");
183 const DocID ID = peek();
185 for (
const auto &Child : Children)
186 if (!Child->reachedEnd() && Child->peek() == ID)
187 Boost = std::max(Boost, Child->consume());
191 size_t estimateSize()
const override {
193 for (
const auto &Child : Children)
194 Size = std::max(Size, Child->estimateSize());
199 llvm::raw_ostream &dump(llvm::raw_ostream &OS)
const override {
202 for (
const auto &Child : Children) {
211 std::vector<std::unique_ptr<Iterator>> Children;
218 class TrueIterator :
public Iterator {
220 explicit TrueIterator(
DocID Size) : Iterator(
Kind::True), Size(Size) {}
222 bool reachedEnd()
const override {
return Index >= Size; }
224 void advance()
override {
225 assert(!reachedEnd() &&
"TRUE iterator can't advance() at the end.");
229 void advanceTo(
DocID ID)
override {
230 assert(!reachedEnd() &&
"TRUE iterator can't advanceTo() at the end.");
231 Index = std::min(ID, Size);
234 DocID peek()
const override {
235 assert(!reachedEnd() &&
"TRUE iterator can't peek() at the end.");
240 assert(!reachedEnd() &&
"TRUE iterator can't consume() at the end.");
244 size_t estimateSize()
const override {
return Size; }
247 llvm::raw_ostream &dump(llvm::raw_ostream &OS)
const override {
257 class FalseIterator :
public Iterator {
259 FalseIterator() : Iterator(
Kind::False) {}
260 bool reachedEnd()
const override {
return true; }
261 void advance()
override { assert(
false); }
262 void advanceTo(
DocID ID)
override { assert(
false); }
263 DocID peek()
const override {
271 size_t estimateSize()
const override {
return 0; }
274 llvm::raw_ostream &dump(llvm::raw_ostream &OS)
const override {
275 return OS <<
"false";
281 class BoostIterator :
public Iterator {
283 BoostIterator(std::unique_ptr<Iterator> Child,
float Factor)
284 : Child(std::move(Child)), Factor(Factor) {}
286 bool reachedEnd()
const override {
return Child->reachedEnd(); }
288 void advance()
override { Child->advance(); }
290 void advanceTo(
DocID ID)
override { Child->advanceTo(ID); }
292 DocID peek()
const override {
return Child->peek(); }
294 float consume()
override {
return Child->consume() * Factor; }
296 size_t estimateSize()
const override {
return Child->estimateSize(); }
299 llvm::raw_ostream &dump(llvm::raw_ostream &OS)
const override {
300 return OS <<
"(* " << Factor <<
' ' << *Child <<
')';
303 std::unique_ptr<Iterator> Child;
311 class LimitIterator :
public Iterator {
313 LimitIterator(std::unique_ptr<Iterator> Child,
size_t Limit)
314 : Child(std::move(Child)), Limit(Limit), ItemsLeft(Limit) {}
316 bool reachedEnd()
const override {
317 return ItemsLeft == 0 || Child->reachedEnd();
320 void advance()
override { Child->advance(); }
322 void advanceTo(
DocID ID)
override { Child->advanceTo(ID); }
324 DocID peek()
const override {
return Child->peek(); }
329 assert(!reachedEnd() &&
"LimitIterator can't consume() at the end.");
331 return Child->consume();
334 size_t estimateSize()
const override {
335 return std::min(Child->estimateSize(),
Limit);
339 llvm::raw_ostream &dump(llvm::raw_ostream &OS)
const override {
340 return OS <<
"(LIMIT " <<
Limit <<
" " << *Child <<
')';
343 std::unique_ptr<Iterator> Child;
351 std::vector<std::pair<DocID, float>> Result;
357 std::unique_ptr<Iterator>
359 std::vector<std::unique_ptr<Iterator>> RealChildren;
360 for (
auto &Child : Children) {
361 switch (Child->kind()) {
365 return std::move(Child);
368 auto &NewChildren =
static_cast<AndIterator *
>(Child.get())->Children;
369 std::move(NewChildren.begin(), NewChildren.end(),
370 std::back_inserter(RealChildren));
374 RealChildren.push_back(std::move(Child));
377 switch (RealChildren.size()) {
381 return std::move(RealChildren.front());
383 return std::make_unique<AndIterator>(std::move(RealChildren));
387 std::unique_ptr<Iterator>
389 std::vector<std::unique_ptr<Iterator>> RealChildren;
390 for (
auto &Child : Children) {
391 switch (Child->kind()) {
396 auto &NewChildren =
static_cast<OrIterator *
>(Child.get())->Children;
397 std::move(NewChildren.begin(), NewChildren.end(),
398 std::back_inserter(RealChildren));
404 RealChildren.push_back(std::move(Child));
407 switch (RealChildren.size()) {
411 return std::move(RealChildren.front());
413 return std::make_unique<OrIterator>(std::move(RealChildren));
418 return std::make_unique<TrueIterator>(Size);
422 return std::make_unique<FalseIterator>();
426 float Factor)
const {
431 return std::make_unique<BoostIterator>(std::move(Child), Factor);
435 size_t Limit)
const {
438 return std::make_unique<LimitIterator>(std::move(Child),
Limit);
std::unique_ptr< Iterator > intersect(std::vector< std::unique_ptr< Iterator >> Children) const
Returns AND Iterator which performs the intersection of the PostingLists of its children.
virtual float consume()=0
Informs the iterator that the current document was consumed, and returns its boost.
Iterator is the interface for Query Tree node.
virtual DocID peek() const =0
Returns the current element this iterator points to.
std::vector< std::pair< DocID, float > > consume(Iterator &It)
Advances the iterator until it is exhausted.
std::unique_ptr< Iterator > unionOf(std::vector< std::unique_ptr< Iterator >> Children) const
Returns OR Iterator which performs the union of the PostingLists of its children. ...
std::unique_ptr< Iterator > limit(std::unique_ptr< Iterator > Child, size_t Limit) const
Returns LIMIT iterator, which yields up to N elements of its child iterator.
std::unique_ptr< Iterator > none() const
Returns FALSE Iterator which iterates over no documents.
uint32_t DocID
Symbol position in the list of all index symbols sorted by a pre-computed symbol quality.
virtual void advance()=0
Moves to next valid DocID.
std::vector< std::unique_ptr< HTMLNode > > Children
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
virtual bool reachedEnd() const =0
Returns true if all valid DocIDs were processed and hence the iterator is exhausted.
Symbol index queries consist of specific requirements for the requested symbol, such as high fuzzy ma...
std::unique_ptr< Iterator > boost(std::unique_ptr< Iterator > Child, float Factor) const
Returns BOOST iterator which multiplies the score of each item by given factor.
std::unique_ptr< Iterator > all() const
Returns TRUE Iterator which iterates over "virtual" PostingList containing all items in range [0...
const SymbolIndex * Index