11 #include "llvm/Support/Casting.h" 26 class AndIterator :
public Iterator {
28 explicit AndIterator(std::vector<std::unique_ptr<Iterator>> AllChildren)
29 : Iterator(
Kind::And), Children(std::move(AllChildren)) {
30 assert(!Children.empty() &&
"AND iterator should have at least one child.");
32 for (
const auto &Child : Children)
33 ReachedEnd |= Child->reachedEnd();
42 llvm::sort(Children, [](
const std::unique_ptr<Iterator> &LHS,
43 const std::unique_ptr<Iterator> &RHS) {
44 return LHS->estimateSize() < RHS->estimateSize();
48 bool reachedEnd()
const override {
return ReachedEnd; }
51 void advance()
override {
52 assert(!reachedEnd() &&
"AND iterator can't advance() at the end.");
53 Children.front()->advance();
58 void advanceTo(
DocID ID)
override {
59 assert(!reachedEnd() &&
"AND iterator can't advanceTo() at the end.");
60 Children.front()->advanceTo(ID);
64 DocID peek()
const override {
return Children.front()->peek(); }
67 assert(!reachedEnd() &&
"AND iterator can't consume() at the end.");
69 for (
const auto &Child : Children)
70 Boost *= Child->consume();
74 size_t estimateSize()
const override {
75 return Children.front()->estimateSize();
79 llvm::raw_ostream &dump(llvm::raw_ostream &OS)
const override {
82 for (
const auto &Child : Children) {
93 ReachedEnd |= Children.front()->reachedEnd();
96 auto SyncID = Children.front()->peek();
98 bool NeedsAdvance =
false;
100 NeedsAdvance =
false;
101 for (
auto &Child : Children) {
102 Child->advanceTo(SyncID);
103 ReachedEnd |= Child->reachedEnd();
110 if (Child->peek() > SyncID) {
111 SyncID = Child->peek();
115 }
while (NeedsAdvance);
121 std::vector<std::unique_ptr<Iterator>> Children;
125 bool ReachedEnd =
false;
135 class OrIterator :
public Iterator {
137 explicit OrIterator(std::vector<std::unique_ptr<Iterator>> AllChildren)
138 : Iterator(
Kind::Or), Children(std::move(AllChildren)) {
139 assert(!Children.empty() &&
"OR iterator should have at least one child.");
143 bool reachedEnd()
const override {
144 for (
const auto &Child : Children)
145 if (!Child->reachedEnd())
151 void advance()
override {
152 assert(!reachedEnd() &&
"OR iterator can't advance() at the end.");
153 const auto SmallestID = peek();
154 for (
const auto &Child : Children)
155 if (!Child->reachedEnd() && Child->peek() == SmallestID)
160 void advanceTo(
DocID ID)
override {
161 assert(!reachedEnd() &&
"OR iterator can't advanceTo() at the end.");
162 for (
const auto &Child : Children)
163 if (!Child->reachedEnd())
164 Child->advanceTo(ID);
169 DocID peek()
const override {
170 assert(!reachedEnd() &&
"OR iterator can't peek() at the end.");
173 for (
const auto &Child : Children)
174 if (!Child->reachedEnd())
175 Result = std::min(Result, Child->peek());
183 assert(!reachedEnd() &&
"OR iterator can't consume() at the end.");
184 const DocID ID = peek();
186 for (
const auto &Child : Children)
187 if (!Child->reachedEnd() && Child->peek() == ID)
188 Boost = std::max(Boost, Child->consume());
192 size_t estimateSize()
const override {
194 for (
const auto &Child : Children)
195 Size = std::max(Size, Child->estimateSize());
200 llvm::raw_ostream &dump(llvm::raw_ostream &OS)
const override {
203 for (
const auto &Child : Children) {
212 std::vector<std::unique_ptr<Iterator>> Children;
219 class TrueIterator :
public Iterator {
221 explicit TrueIterator(
DocID Size) : Iterator(
Kind::True), Size(Size) {}
223 bool reachedEnd()
const override {
return Index >= Size; }
225 void advance()
override {
226 assert(!reachedEnd() &&
"TRUE iterator can't advance() at the end.");
230 void advanceTo(
DocID ID)
override {
231 assert(!reachedEnd() &&
"TRUE iterator can't advanceTo() at the end.");
232 Index = std::min(ID, Size);
235 DocID peek()
const override {
236 assert(!reachedEnd() &&
"TRUE iterator can't peek() at the end.");
241 assert(!reachedEnd() &&
"TRUE iterator can't consume() at the end.");
245 size_t estimateSize()
const override {
return Size; }
248 llvm::raw_ostream &dump(llvm::raw_ostream &OS)
const override {
258 class FalseIterator :
public Iterator {
260 FalseIterator() : Iterator(
Kind::False) {}
261 bool reachedEnd()
const override {
return true; }
262 void advance()
override { assert(
false); }
263 void advanceTo(
DocID ID)
override { assert(
false); }
264 DocID peek()
const override {
272 size_t estimateSize()
const override {
return 0; }
275 llvm::raw_ostream &dump(llvm::raw_ostream &OS)
const override {
276 return OS <<
"false";
282 class BoostIterator :
public Iterator {
284 BoostIterator(std::unique_ptr<Iterator> Child,
float Factor)
285 : Child(std::move(Child)), Factor(Factor) {}
287 bool reachedEnd()
const override {
return Child->reachedEnd(); }
289 void advance()
override { Child->advance(); }
291 void advanceTo(
DocID ID)
override { Child->advanceTo(ID); }
293 DocID peek()
const override {
return Child->peek(); }
295 float consume()
override {
return Child->consume() * Factor; }
297 size_t estimateSize()
const override {
return Child->estimateSize(); }
300 llvm::raw_ostream &dump(llvm::raw_ostream &OS)
const override {
301 return OS <<
"(* " << Factor <<
' ' << *Child <<
')';
304 std::unique_ptr<Iterator> Child;
312 class LimitIterator :
public Iterator {
314 LimitIterator(std::unique_ptr<Iterator> Child,
size_t Limit)
315 : Child(std::move(Child)), Limit(Limit), ItemsLeft(Limit) {}
317 bool reachedEnd()
const override {
318 return ItemsLeft == 0 || Child->reachedEnd();
321 void advance()
override { Child->advance(); }
323 void advanceTo(
DocID ID)
override { Child->advanceTo(ID); }
325 DocID peek()
const override {
return Child->peek(); }
330 assert(!reachedEnd() &&
"LimitIterator can't consume() at the end.");
332 return Child->consume();
335 size_t estimateSize()
const override {
336 return std::min(Child->estimateSize(), Limit);
340 llvm::raw_ostream &dump(llvm::raw_ostream &OS)
const override {
341 return OS <<
"(LIMIT " << Limit <<
" " << *Child <<
')';
344 std::unique_ptr<Iterator> Child;
352 std::vector<std::pair<DocID, float>>
Result;
358 std::unique_ptr<Iterator>
360 std::vector<std::unique_ptr<Iterator>> RealChildren;
361 for (
auto &Child : Children) {
362 switch (Child->kind()) {
366 return std::move(Child);
369 auto &NewChildren =
static_cast<AndIterator *
>(Child.get())->Children;
370 std::move(NewChildren.begin(), NewChildren.end(),
371 std::back_inserter(RealChildren));
375 RealChildren.push_back(std::move(Child));
378 switch (RealChildren.size()) {
382 return std::move(RealChildren.front());
384 return llvm::make_unique<AndIterator>(std::move(RealChildren));
388 std::unique_ptr<Iterator>
390 std::vector<std::unique_ptr<Iterator>> RealChildren;
391 for (
auto &Child : Children) {
392 switch (Child->kind()) {
397 auto &NewChildren =
static_cast<OrIterator *
>(Child.get())->Children;
398 std::move(NewChildren.begin(), NewChildren.end(),
399 std::back_inserter(RealChildren));
405 RealChildren.push_back(std::move(Child));
408 switch (RealChildren.size()) {
412 return std::move(RealChildren.front());
414 return llvm::make_unique<OrIterator>(std::move(RealChildren));
419 return llvm::make_unique<TrueIterator>(Size);
423 return llvm::make_unique<FalseIterator>();
427 float Factor)
const {
432 return llvm::make_unique<BoostIterator>(std::move(Child), Factor);
436 size_t Limit)
const {
439 return llvm::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.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– 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