From bb47accc2178c48ef7876e2b0e3f7e1472821862 Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Fri, 28 Apr 2017 23:29:33 +0000 Subject: [PATCH] [Support] Provide unsafe random access for VarStreamArray. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@301716 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/BinaryStreamArray.h | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/include/llvm/Support/BinaryStreamArray.h b/include/llvm/Support/BinaryStreamArray.h index e86dc423aee..748a62be231 100644 --- a/include/llvm/Support/BinaryStreamArray.h +++ b/include/llvm/Support/BinaryStreamArray.h @@ -93,13 +93,25 @@ public: : Stream(Other.Stream), Context(Other.Context) {} Iterator begin(bool *HadError = nullptr) const { + if (empty()) + return end(); + return Iterator(*this, Context, HadError); } Iterator end() const { return Iterator(); } + bool empty() const { return Stream.getLength() == 0; } + + /// \brief given an offset into the array's underlying stream, return an + /// iterator to the record at that offset. This is considered unsafe + /// since the behavior is undefined if \p Offset does not refer to the + /// beginning of a valid record. + Iterator at(uint32_t Offset) const { + return Iterator(*this, Context, Stream.drop_front(Offset), nullptr); + } + BinaryStreamRef getUnderlyingStream() const { return Stream; } - void setUnderlyingStream(BinaryStreamRef Stream) { this->Stream = Stream; } private: BinaryStreamRef Stream; @@ -117,9 +129,8 @@ class VarStreamArrayIterator public: VarStreamArrayIterator(const ArrayType &Array, ContextType *Context, - bool *HadError = nullptr) - : IterRef(Array.Stream), Context(Context), Array(&Array), - HadError(HadError) { + BinaryStreamRef Stream, bool *HadError = nullptr) + : IterRef(Stream), Context(Context), Array(&Array), HadError(HadError) { if (IterRef.getLength() == 0) moveToEnd(); else { @@ -130,6 +141,11 @@ public: } } } + + VarStreamArrayIterator(const ArrayType &Array, ContextType *Context, + bool *HadError = nullptr) + : VarStreamArrayIterator(Array, Context, Array.Stream, HadError) {} + VarStreamArrayIterator() = default; ~VarStreamArrayIterator() = default; -- 2.40.0