Commit ab76c57e authored by krupnov's avatar krupnov Committed by Wouter van Oortmerssen

random access iterator for vector added (#4119)

* random access iterator for vector added

* Style changes
parent d1e88993
...@@ -252,9 +252,9 @@ template<typename T> struct IndirectHelper<const T *> { ...@@ -252,9 +252,9 @@ template<typename T> struct IndirectHelper<const T *> {
// calling Get() for every element. // calling Get() for every element.
template<typename T, typename IT> template<typename T, typename IT>
struct VectorIterator struct VectorIterator
: public std::iterator<std::input_iterator_tag, IT, uoffset_t> { : public std::iterator<std::random_access_iterator_tag, IT, uoffset_t> {
typedef std::iterator<std::input_iterator_tag, IT, uoffset_t> super_type; typedef std::iterator<std::random_access_iterator_tag, IT, uoffset_t> super_type;
public: public:
VectorIterator(const uint8_t *data, uoffset_t i) : VectorIterator(const uint8_t *data, uoffset_t i) :
...@@ -274,15 +274,15 @@ public: ...@@ -274,15 +274,15 @@ public:
return *this; return *this;
} }
bool operator==(const VectorIterator& other) const { bool operator==(const VectorIterator &other) const {
return data_ == other.data_; return data_ == other.data_;
} }
bool operator!=(const VectorIterator& other) const { bool operator!=(const VectorIterator &other) const {
return data_ != other.data_; return data_ != other.data_;
} }
ptrdiff_t operator-(const VectorIterator& other) const { ptrdiff_t operator-(const VectorIterator &other) const {
return (data_ - other.data_) / IndirectHelper<T>::element_stride; return (data_ - other.data_) / IndirectHelper<T>::element_stride;
} }
...@@ -300,11 +300,40 @@ public: ...@@ -300,11 +300,40 @@ public:
} }
VectorIterator operator++(int) { VectorIterator operator++(int) {
VectorIterator temp(data_,0); VectorIterator temp(data_, 0);
data_ += IndirectHelper<T>::element_stride; data_ += IndirectHelper<T>::element_stride;
return temp; return temp;
} }
VectorIterator operator+(const uoffset_t &offset) {
return VectorIterator(data_ + offset * IndirectHelper<T>::element_stride, 0);
}
VectorIterator& operator+=(const uoffset_t &offset) {
data_ += offset * IndirectHelper<T>::element_stride;
return *this;
}
VectorIterator &operator--() {
data_ -= IndirectHelper<T>::element_stride;
return *this;
}
VectorIterator operator--(int) {
VectorIterator temp(data_, 0);
data_ -= IndirectHelper<T>::element_stride;
return temp;
}
VectorIterator operator-(const uoffset_t &offset) {
return VectorIterator(data_ - offset * IndirectHelper<T>::element_stride, 0);
}
VectorIterator& operator-=(const uoffset_t &offset) {
data_ -= offset * IndirectHelper<T>::element_stride;
return *this;
}
private: private:
const uint8_t *data_; const uint8_t *data_;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment