| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- //
- // 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>
- 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 value_type = typename It::value_type;
- struct sentinel_type {};
- static constexpr sentinel_type sentinel;
- 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 I>
- end_aware_iterator(end_aware_iterator<I> const & other)
- : 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>;
- }
- MAKE_ITERATOR_FACADE_TYPEDEFS_T(::iterator::end_aware_iterator);
|