diff options
| author | Kirill Bobyrev <kbobyrev.opensource@gmail.com> | 2018-09-13 17:11:03 +0000 |
|---|---|---|
| committer | Kirill Bobyrev <kbobyrev.opensource@gmail.com> | 2018-09-13 17:11:03 +0000 |
| commit | 249c5864cf9657a320e427318a879ba809e84b1a (patch) | |
| tree | 3a612c6ebb5068f20541582a528912a71e91d7b3 /clang-tools-extra/clangd/index/dex/PostingList.cpp | |
| parent | f29b36c76d6452180957472ac62a65507d822301 (diff) | |
| download | bcm5719-llvm-249c5864cf9657a320e427318a879ba809e84b1a.tar.gz bcm5719-llvm-249c5864cf9657a320e427318a879ba809e84b1a.zip | |
[clangd] Introduce PostingList interface
This patch abstracts `PostingList` interface and reuses existing
implementation. It will be used later to test different `PostingList`
representations.
No functionality change is introduced, this patch is mostly refactoring
so that the following patches could focus on functionality while not
being too hard to review.
Reviewed By: sammccall, ioeric
Differential Revision: https://reviews.llvm.org/D51982
llvm-svn: 342155
Diffstat (limited to 'clang-tools-extra/clangd/index/dex/PostingList.cpp')
| -rw-r--r-- | clang-tools-extra/clangd/index/dex/PostingList.cpp | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/clang-tools-extra/clangd/index/dex/PostingList.cpp b/clang-tools-extra/clangd/index/dex/PostingList.cpp new file mode 100644 index 00000000000..99e534d1a7f --- /dev/null +++ b/clang-tools-extra/clangd/index/dex/PostingList.cpp @@ -0,0 +1,84 @@ +//===--- PostingList.cpp - Symbol identifiers storage interface -----------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "PostingList.h" +#include "Iterator.h" + +namespace clang { +namespace clangd { +namespace dex { + +namespace { + +/// Implements Iterator over std::vector<DocID>. This is the most basic +/// iterator and is simply a wrapper around +/// std::vector<DocID>::const_iterator. +class PlainIterator : public Iterator { +public: + explicit PlainIterator(llvm::ArrayRef<DocID> Documents) + : Documents(Documents), Index(std::begin(Documents)) {} + + bool reachedEnd() const override { return Index == std::end(Documents); } + + /// Advances cursor to the next item. + void advance() override { + assert(!reachedEnd() && + "Posting List iterator can't advance() at the end."); + ++Index; + } + + /// Applies binary search to advance cursor to the next item with DocID + /// equal or higher than the given one. + void advanceTo(DocID ID) override { + assert(!reachedEnd() && + "Posting List iterator can't advance() at the end."); + // If current ID is beyond requested one, iterator is already in the right + // state. + if (peek() < ID) + Index = std::lower_bound(Index, std::end(Documents), ID); + } + + DocID peek() const override { + assert(!reachedEnd() && + "Posting List iterator can't peek() at the end."); + return *Index; + } + + float consume() override { + assert(!reachedEnd() && + "Posting List iterator can't consume() at the end."); + return DEFAULT_BOOST_SCORE; + } + + size_t estimateSize() const override { return Documents.size(); } + +private: + llvm::raw_ostream &dump(llvm::raw_ostream &OS) const override { + OS << '['; + if (Index != std::end(Documents)) + OS << *Index; + else + OS << "END"; + OS << ']'; + return OS; + } + + llvm::ArrayRef<DocID> Documents; + llvm::ArrayRef<DocID>::const_iterator Index; +}; + +} // namespace + +std::unique_ptr<Iterator> PostingList::iterator() const { + return llvm::make_unique<PlainIterator>(Documents); +} + +} // namespace dex +} // namespace clangd +} // namespace clang |

