From ab76c57ec81049282242af4abcb3f585cbee5a8f Mon Sep 17 00:00:00 2001 From: krupnov Date: Fri, 16 Dec 2016 20:37:04 +0400 Subject: [PATCH] random access iterator for vector added (#4119) * random access iterator for vector added * Style changes --- include/flatbuffers/flatbuffers.h | 41 ++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index e830c4404..a69f8a337 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -252,9 +252,9 @@ template struct IndirectHelper { // calling Get() for every element. template struct VectorIterator - : public std::iterator { + : public std::iterator { - typedef std::iterator super_type; + typedef std::iterator super_type; public: VectorIterator(const uint8_t *data, uoffset_t i) : @@ -274,15 +274,15 @@ public: return *this; } - bool operator==(const VectorIterator& other) const { + bool operator==(const VectorIterator &other) const { return data_ == other.data_; } - bool operator!=(const VectorIterator& other) const { + bool operator!=(const VectorIterator &other) const { return data_ != other.data_; } - ptrdiff_t operator-(const VectorIterator& other) const { + ptrdiff_t operator-(const VectorIterator &other) const { return (data_ - other.data_) / IndirectHelper::element_stride; } @@ -300,11 +300,40 @@ public: } VectorIterator operator++(int) { - VectorIterator temp(data_,0); + VectorIterator temp(data_, 0); data_ += IndirectHelper::element_stride; return temp; } + VectorIterator operator+(const uoffset_t &offset) { + return VectorIterator(data_ + offset * IndirectHelper::element_stride, 0); + } + + VectorIterator& operator+=(const uoffset_t &offset) { + data_ += offset * IndirectHelper::element_stride; + return *this; + } + + VectorIterator &operator--() { + data_ -= IndirectHelper::element_stride; + return *this; + } + + VectorIterator operator--(int) { + VectorIterator temp(data_, 0); + data_ -= IndirectHelper::element_stride; + return temp; + } + + VectorIterator operator-(const uoffset_t &offset) { + return VectorIterator(data_ - offset * IndirectHelper::element_stride, 0); + } + + VectorIterator& operator-=(const uoffset_t &offset) { + data_ -= offset * IndirectHelper::element_stride; + return *this; + } + private: const uint8_t *data_; };