random access iterator for vector added (#4119)

* random access iterator for vector added

* Style changes
This commit is contained in:
krupnov 2016-12-16 20:37:04 +04:00 committed by Wouter van Oortmerssen
parent d1e8899310
commit ab76c57ec8
1 changed files with 35 additions and 6 deletions

View File

@ -252,9 +252,9 @@ template<typename T> struct IndirectHelper<const T *> {
// calling Get() for every element.
template<typename T, typename IT>
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:
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<T>::element_stride;
}
@ -300,11 +300,40 @@ public:
}
VectorIterator operator++(int) {
VectorIterator temp(data_,0);
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;
}
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:
const uint8_t *data_;
};