Skip to content

Commit 30ffdf4

Browse files
committedAug 20, 2018
[clangd] Implement TRUE Iterator
This patch introduces TRUE Iterator which efficiently handles posting lists containing all items within `[0, Size)` range. Reviewed by: ioeric Differential Revision: https://reviews.llvm.org/D50955 llvm-svn: 340155
1 parent 5f26a64 commit 30ffdf4

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed
 

‎clang-tools-extra/clangd/index/dex/Iterator.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,41 @@ class OrIterator : public Iterator {
225225
std::vector<std::unique_ptr<Iterator>> Children;
226226
};
227227

228+
/// TrueIterator handles PostingLists which contain all items of the index. It
229+
/// stores size of the virtual posting list, and all operations are performed
230+
/// in O(1).
231+
class TrueIterator : public Iterator {
232+
public:
233+
TrueIterator(DocID Size) : Size(Size) {}
234+
235+
bool reachedEnd() const override { return Index >= Size; }
236+
237+
void advance() override {
238+
assert(!reachedEnd() && "Can't advance iterator after it reached the end.");
239+
++Index;
240+
}
241+
242+
void advanceTo(DocID ID) override {
243+
assert(!reachedEnd() && "Can't advance iterator after it reached the end.");
244+
Index = std::min(ID, Size);
245+
}
246+
247+
DocID peek() const override {
248+
assert(!reachedEnd() && "TrueIterator can't call peek() at the end.");
249+
return Index;
250+
}
251+
252+
private:
253+
llvm::raw_ostream &dump(llvm::raw_ostream &OS) const override {
254+
OS << "(TRUE {" << Index << "} out of " << Size << ")";
255+
return OS;
256+
}
257+
258+
DocID Index = 0;
259+
/// Size of the underlying virtual PostingList.
260+
DocID Size;
261+
};
262+
228263
} // end namespace
229264

230265
std::vector<DocID> consume(Iterator &It, size_t Limit) {
@@ -249,6 +284,10 @@ createOr(std::vector<std::unique_ptr<Iterator>> Children) {
249284
return llvm::make_unique<OrIterator>(move(Children));
250285
}
251286

287+
std::unique_ptr<Iterator> createTrue(DocID Size) {
288+
return llvm::make_unique<TrueIterator>(Size);
289+
}
290+
252291
} // namespace dex
253292
} // namespace clangd
254293
} // namespace clang

‎clang-tools-extra/clangd/index/dex/Iterator.h

+4
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ createAnd(std::vector<std::unique_ptr<Iterator>> Children);
129129
std::unique_ptr<Iterator>
130130
createOr(std::vector<std::unique_ptr<Iterator>> Children);
131131

132+
/// Returns TRUE Iterator which iterates over "virtual" PostingList containing
133+
/// all items in range [0, Size) in an efficient manner.
134+
std::unique_ptr<Iterator> createTrue(DocID Size);
135+
132136
/// This allows createAnd(create(...), create(...)) syntax.
133137
template <typename... Args> std::unique_ptr<Iterator> createAnd(Args... args) {
134138
std::vector<std::unique_ptr<Iterator>> Children;

‎clang-tools-extra/unittests/clangd/DexIndexTests.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,19 @@ TEST(DexIndexIterators, Limit) {
262262
EXPECT_THAT(consume(*DocIterator, 0), ElementsAre());
263263
}
264264

265+
TEST(DexIndexIterators, True) {
266+
auto TrueIterator = createTrue(0U);
267+
EXPECT_THAT(TrueIterator->reachedEnd(), true);
268+
EXPECT_THAT(consume(*TrueIterator), ElementsAre());
269+
270+
PostingList L0 = {1, 2, 5, 7};
271+
TrueIterator = createTrue(7U);
272+
EXPECT_THAT(TrueIterator->peek(), 0);
273+
auto AndIterator = createAnd(create(L0), move(TrueIterator));
274+
EXPECT_THAT(AndIterator->reachedEnd(), false);
275+
EXPECT_THAT(consume(*AndIterator), ElementsAre(1, 2, 5));
276+
}
277+
265278
testing::Matcher<std::vector<Token>>
266279
trigramsAre(std::initializer_list<std::string> Trigrams) {
267280
std::vector<Token> Tokens;

0 commit comments

Comments
 (0)
Please sign in to comment.