| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- #pragma once
- #include <iterator/facade.h>
- #include <iterator/forwards.h>
- namespace iterator {
- template <typename It, typename Self, typename Cat>
- class Proxy : public Facade<Self> {
- public:
- static constexpr bool single_pass_iterator = true;
- private:
- It impl_;
- public:
- Proxy() = default;
- Proxy(It impl) : impl_(impl) {}
- template <typename... Args>
- Proxy(Args &&... args) : impl_(std::forward<Args>(args)...) {}
- decltype(auto) dereference() const { return *impl_; }
- void increment() { ++impl_; }
- bool equal_to(Self const & other) const { return impl_ == other.impl_; }
- bool at_end() const
- requires(has_sentinel<It>)
- {
- return impl() == typename It::sentinel_type();
- }
- protected:
- auto impl() const { return impl_; }
- };
- template <typename It, typename Self>
- class Proxy<It, Self, std::forward_iterator_tag> : public Facade<Self> {
- private:
- It impl_;
- public:
- Proxy() = default;
- Proxy(It impl) : impl_(impl) {}
- template <typename... Args>
- Proxy(Args &&... args) : impl_(std::forward<Args>(args)...) {}
- decltype(auto) dereference() const { return *impl_; }
- void increment() { ++impl_; }
- bool equal_to(Self const & other) const { return impl_ == other.impl_; }
- bool at_end() const
- requires(has_sentinel<It>)
- {
- return impl() == typename It::sentinel_type();
- }
- protected:
- auto impl() const { return impl_; }
- };
- template <typename It, typename Self>
- class Proxy<It, Self, std::bidirectional_iterator_tag> : public Facade<Self> {
- private:
- It impl_;
- public:
- Proxy() = default;
- Proxy(It impl) : impl_(impl) {}
- template <typename... Args>
- Proxy(Args &&... args) : impl_(std::forward<Args>(args)...) {}
- decltype(auto) dereference() const { return *impl_; }
- void increment() { ++impl_; }
- void decrement() { --impl_; }
- bool equal_to(Self const & other) const { return impl_ == other.impl_; }
- bool at_end() const
- requires(has_sentinel<It>)
- {
- return impl() == typename It::sentinel_type();
- }
- protected:
- auto impl() const { return impl_; }
- };
- template <typename It, typename Self>
- class Proxy<It, Self, std::random_access_iterator_tag> : public Facade<Self> {
- public:
- using difference_type = typename std::iterator_traits<It>::difference_type;
- private:
- It impl_;
- public:
- Proxy() = default;
- Proxy(It impl) : impl_(impl) {}
- template <typename... Args>
- Proxy(Args &&... args) : impl_(std::forward<Args>(args)...) {}
- decltype(auto) dereference() const { return *impl_; }
- void advance(difference_type off) { impl_ += off; }
- bool equal_to(Self const & other) const { return impl_ == other.impl_; }
- difference_type distance_to(Self const & other) const {
- return other.impl_ - impl_;
- }
- friend auto operator-(sentinel_for<It> auto sentinel, Self const & self) {
- return sentinel - self.impl();
- }
- bool at_end() const
- requires(has_sentinel<It>)
- {
- return (typename It::sentinel_type() - impl()) <= 0;
- }
- protected:
- auto impl() const { return impl_; }
- };
- }
|