24 class DocumentIterator :
public Iterator {
27 : Documents(Documents), Index(std::begin(Documents)) {}
29 bool reachedEnd()
const override {
return Index == std::end(Documents); }
32 void advance()
override {
33 assert(!reachedEnd() &&
"DocumentIterator can't advance at the end.");
39 void advanceTo(
DocID ID)
override {
40 assert(!reachedEnd() &&
"DocumentIterator can't advance at the end.");
41 Index = std::lower_bound(Index, std::end(Documents), ID);
44 DocID peek()
const override {
45 assert(!reachedEnd() &&
"DocumentIterator can't call peek() at the end.");
49 llvm::raw_ostream &dump(llvm::raw_ostream &OS)
const override {
52 for (
const auto &ID : Documents) {
62 PostingListRef::const_iterator Index;
70 class AndIterator :
public Iterator {
72 AndIterator(std::vector<std::unique_ptr<Iterator>> AllChildren)
73 : Children(std::move(AllChildren)) {
74 assert(!Children.empty() &&
"AndIterator should have at least one child.");
79 bool reachedEnd()
const override {
return ReachedEnd; }
82 void advance()
override {
83 assert(!reachedEnd() &&
"AndIterator can't call advance() at the end.");
84 Children.front()->advance();
89 void advanceTo(
DocID ID)
override {
90 assert(!reachedEnd() &&
"AndIterator can't call advanceTo() at the end.");
91 Children.front()->advanceTo(ID);
95 DocID peek()
const override {
return Children.front()->peek(); }
97 llvm::raw_ostream &dump(llvm::raw_ostream &OS)
const override {
100 for (
const auto &Child : Children) {
112 ReachedEnd |= Children.front()->reachedEnd();
115 auto SyncID = Children.front()->peek();
117 bool NeedsAdvance =
false;
119 NeedsAdvance =
false;
120 for (
auto &Child : Children) {
121 Child->advanceTo(SyncID);
122 ReachedEnd |= Child->reachedEnd();
132 if (Child->peek() > SyncID) {
133 SyncID = Child->peek();
137 }
while (NeedsAdvance);
143 std::vector<std::unique_ptr<Iterator>> Children;
147 bool ReachedEnd =
false;
156 class OrIterator :
public Iterator {
158 OrIterator(std::vector<std::unique_ptr<Iterator>> AllChildren)
159 : Children(std::move(AllChildren)) {
160 assert(Children.size() > 0 &&
"Or Iterator must have at least one child.");
164 bool reachedEnd()
const override {
165 return std::all_of(begin(Children), end(Children),
166 [](
const std::unique_ptr<Iterator> &Child) {
167 return Child->reachedEnd();
172 void advance()
override {
173 assert(!reachedEnd() &&
174 "OrIterator must have at least one child to advance().");
175 const auto SmallestID = peek();
176 for (
const auto &Child : Children)
177 if (!Child->reachedEnd() && Child->peek() == SmallestID)
182 void advanceTo(
DocID ID)
override {
183 assert(!reachedEnd() &&
"Can't advance iterator after it reached the end.");
184 for (
const auto &Child : Children)
185 if (!Child->reachedEnd())
186 Child->advanceTo(ID);
191 DocID peek()
const override {
192 assert(!reachedEnd() &&
193 "OrIterator must have at least one child to peek().");
194 DocID Result = std::numeric_limits<DocID>::max();
196 for (
const auto &Child : Children)
197 if (!Child->reachedEnd())
198 Result = std::min(Result, Child->peek());
203 llvm::raw_ostream &dump(llvm::raw_ostream &OS)
const override {
206 for (
const auto &Child : Children) {
216 std::vector<std::unique_ptr<Iterator>> Children;
222 std::vector<DocID> Result;
224 Result.push_back(It.
peek());
229 return llvm::make_unique<DocumentIterator>(Documents);
232 std::unique_ptr<Iterator>
233 createAnd(std::vector<std::unique_ptr<Iterator>> Children) {
234 return llvm::make_unique<AndIterator>(move(Children));
237 std::unique_ptr<Iterator>
238 createOr(std::vector<std::unique_ptr<Iterator>> Children) {
239 return llvm::make_unique<OrIterator>(move(Children));
std::vector< DocID > consume(Iterator &It)
Exhausts given iterator and returns all processed DocIDs.
llvm::ArrayRef< DocID > PostingListRef
Immutable reference to PostingList object.
Iterator is the interface for Query Tree node.
virtual DocID peek() const =0
Returns the current element this iterator points to.
std::unique_ptr< Iterator > create(PostingListRef Documents)
Returns a document iterator over given PostingList.
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::unique_ptr< Iterator > createOr(std::vector< std::unique_ptr< Iterator >> Children)
Returns OR Iterator which performs the union of the PostingLists of its children. ...
std::unique_ptr< Iterator > createAnd(std::vector< std::unique_ptr< Iterator >> Children)
Returns AND Iterator which performs the intersection of the PostingLists of its 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.