| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- //
- // end_aware_iterator.hpp
- // iterator
- //
- // Created by Sam Jaffe on 2/7/17.
- //
- #pragma once
- #include <iterator/detail/traits.h>
- #include <iterator/facade.h>
- #include <iterator/iterator_fwd.hpp>
- #include <iterator/sentinel.h>
- namespace iterator {
- /**
- * @class end_aware_iterator
- * @brief An iterator that keeps track of the relative end of the range.
- *
- * @tparam It The underlying iterator type
- */
- template <typename It>
- class end_aware_iterator : public facade<end_aware_iterator<It>> {
- public:
- using sentinel_type = sentinel_t;
- public:
- end_aware_iterator() = default;
- end_aware_iterator(It it, It end) : curr_(it), end_(end) {}
- template <typename C, typename = std::enable_if_t<detail::is_container_v<C>>>
- end_aware_iterator(C && container)
- : curr_(std::begin(container)), end_(std::end(container)) {
- static_assert(std::is_reference_v<C>,
- "Cannot access iterator of a temporary");
- }
- template <typename Ot>
- end_aware_iterator(end_aware_iterator<Ot> const & other)
- : curr_(other.curr_), end_(other.end_) {}
- end_aware_iterator(end_aware_iterator<It> other, end_aware_iterator<It>)
- : curr_(other.curr_), end_(other.end_) {}
- decltype(auto) dereference() const { return *curr_; }
- void increment() { ++curr_; }
- bool at_end() const { return curr_ == end_; }
- bool equal_to(end_aware_iterator const & other) const {
- // TODO: Fix this clause
- return (at_end() && other.at_end()) || curr_ == other.curr_;
- }
- private:
- template <typename O> friend class end_aware_iterator;
- It curr_, end_;
- };
- template <typename C> end_aware_iterator(C &&) -> end_aware_iterator<iter<C>>;
- template <typename It> end_aware_iterator(It, It) -> end_aware_iterator<It>;
- template <typename It>
- end_aware_iterator(end_aware_iterator<It>, end_aware_iterator<It>)
- -> end_aware_iterator<It>;
- }
- MAKE_ITERATOR_FACADE_TYPEDEFS_T(::iterator::end_aware_iterator);
|