From 193eaa7fe2b690035a85b365a6ff263659a43b86 Mon Sep 17 00:00:00 2001 From: Tom Lin Date: Sun, 24 Jul 2022 23:30:24 +0100 Subject: [PATCH] Fix index iterator on large problem sizes --- src/std-indices/STDIndicesStream.h | 58 ++++++++++++++++++------------ 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/src/std-indices/STDIndicesStream.h b/src/std-indices/STDIndicesStream.h index 6810888..3fd88f3 100644 --- a/src/std-indices/STDIndicesStream.h +++ b/src/std-indices/STDIndicesStream.h @@ -23,35 +23,47 @@ // implementation doesn't target template class ranged { - N from, to; public: - ranged(N from, N to ): from(from), to(to) {} - class iterator { - N num; + class iterator { + friend class ranged; public: - using difference_type = N; - using value_type = N; - using pointer = const N*; - using reference = const N&; - using iterator_category = std::random_access_iterator_tag; - explicit iterator(N _num = 0) : num(_num) {} + using difference_type = N; + using value_type = N; + using pointer = const N*; + using reference = const N&; + using iterator_category = std::random_access_iterator_tag; - iterator& operator++() { num++; return *this; } - iterator operator++(int) { iterator retval = *this; ++(*this); return retval; } - iterator operator+(const value_type v) const { return iterator(num + v); } - iterator operator+=(int x) { iterator retval = *this; this->num+=x; return retval; } + reference operator *() const { return i_; } + const iterator &operator ++() { ++i_; return *this; } + iterator operator ++(int) { iterator copy(*this); ++i_; return copy; } - bool operator==(iterator other) const { return num == other.num; } - bool operator!=(iterator other) const { return *this != other; } - bool operator<(iterator other) const { return num < other.num; } + const iterator &operator --() { --i_; return *this; } + iterator operator --(int) { iterator copy(*this); --i_; return copy; } - reference operator*() const { return num;} - difference_type operator-(const iterator &it) const { return num - it.num; } - value_type operator[](const difference_type &i) const { return num + i; } + const iterator &operator +=(N by) { i_+=by; return *this; } - }; - iterator begin() { return iterator(from); } - iterator end() { return iterator(to >= from? to+1 : to-1); } + value_type operator[](const difference_type &i) const { return i_ + i; } + + difference_type operator-(const iterator &it) const { return i_ - it.i_; } + iterator operator+(const value_type v) const { return iterator(i_ + v); } + + bool operator ==(const iterator &other) const { return i_ == other.i_; } + bool operator !=(const iterator &other) const { return i_ != other.i_; } + bool operator < (const iterator &other) const { return i_ < other.i_; } + + protected: + explicit iterator(N start) : i_ (start) {} + + private: + N i_; + }; + + [[nodiscard]] iterator begin() const { return begin_; } + [[nodiscard]] iterator end() const { return end_; } + ranged(N begin, N end) : begin_(begin), end_(end) {} +private: + iterator begin_; + iterator end_; }; template