// // end_aware_iterator.hpp // iterator // // Created by Sam Jaffe on 2/7/17. // #pragma once #include template class end_aware_iterator { public: using iter_type = Iterator; using value_type = typename std::iterator_traits::value_type; using reference = typename std::iterator_traits::reference; using pointer = typename std::iterator_traits::pointer; using difference_type = typename std::iterator_traits::difference_type; using iterator_category = std::forward_iterator_tag; public: end_aware_iterator(iter_type it, iter_type end) : curr_(it), end_(end) {} template end_aware_iterator( end_aware_iterator const & other ) : curr_(other.current()), end_(other.end()) { } end_aware_iterator & operator++() { if ( !done() ) { ++curr_; } return *this; } end_aware_iterator operator++(int) { end_aware_iterator tmp{*this}; operator++(); return tmp; } reference operator*() { return *curr_; } pointer operator->() { return std::addressof(*curr_); } bool done() const { return curr_ == end_; } bool operator==(end_aware_iterator const & other) const { return curr_ == other.curr_ && end_ == other.end_; } bool operator!=(end_aware_iterator const & other) { return !(operator==(other)); } iter_type current() const { return curr_; } iter_type end() const { return end_; } private: iter_type curr_, end_; };