| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- #pragma once
- #include <iterator>
- #include <type_traits>
- #include <iterator/detail/arrow_proxy.h>
- #include <iterator/detail/traits.h>
- #include <iterator/detail/macro.h>
- namespace iterator::detail {
- template <typename, typename = void> struct distance_to {
- using type = std::ptrdiff_t;
- };
- template <typename T>
- struct distance_to<T, EXISTS(VAL(T).distance_to(VAL(T)))> {
- using type = decltype(VAL(T).distance_to(VAL(T)));
- };
- template <typename T> using distance_to_t = typename distance_to<T>::type;
- }
- namespace iterator {
- template <typename D, typename T>
- using difference_type_arg_t =
- std::enable_if_t<std::is_convertible_v<D, detail::distance_to_t<T>>>;
- template <typename CRTP, category C> class facade {
- private:
- using self_type = CRTP;
- public:
- constexpr static auto category_enum = C;
- public:
- decltype(auto) operator*() const { return self().dereference(); }
- decltype(auto) operator->() const {
- if constexpr (std::is_reference<decltype(**this)>{}) {
- return std::addressof(**this);
- } else {
- return detail::arrow_proxy{**this};
- }
- }
- template <typename D, typename = difference_type_arg_t<D, self_type>,
- REQUIRES(DEFER(D) && C == category::random_access)>
- decltype(auto) operator[](D off) const {
- return *(self() + off);
- }
- self_type & operator++() {
- if constexpr (C == category::random_access) {
- self() += 1;
- } else {
- self().increment();
- }
- return self();
- }
- auto operator++(int) {
- if constexpr (C == category::single_pass) {
- ++*this;
- } else {
- auto tmp = self();
- ++*this;
- return tmp;
- }
- }
- SFINAE(C >= category::bidirectional)
- self_type & operator--() {
- if constexpr (C == category::random_access) {
- self() -= 1;
- } else {
- self().decrement();
- }
- return self();
- }
- SFINAE(C >= category::bidirectional)
- self_type operator--(int) {
- auto tmp = self();
- --*this;
- return tmp;
- }
- template <typename D, typename = difference_type_arg_t<D, self_type>,
- REQUIRES(DEFER(D) && C == category::random_access)>
- friend self_type & operator+=(self_type & self, D off) {
- self.advance(off);
- return self;
- }
- template <typename D, typename = difference_type_arg_t<D, self_type>,
- REQUIRES(DEFER(D) && C == category::random_access)>
- friend self_type & operator-=(self_type & self, D off) {
- self.advance(-off);
- return self;
- }
- template <typename D, typename = difference_type_arg_t<D, self_type>,
- REQUIRES(DEFER(D) && C == category::random_access)>
- friend auto operator+(self_type self, D off) {
- return self += off;
- }
- template <typename D, typename = difference_type_arg_t<D, self_type>,
- REQUIRES(DEFER(D) && C == category::random_access)>
- friend auto operator+(D off, self_type self) {
- return self += off;
- }
- template <typename D, typename = difference_type_arg_t<D, self_type>,
- REQUIRES(DEFER(D) && C == category::random_access)>
- friend auto operator-(self_type self, D off) {
- return self -= off;
- }
- SFINAE(C == category::random_access)
- friend auto operator-(self_type const & left, self_type const & right) {
- return right.distance_to(left);
- }
- friend bool operator==(self_type const & left, self_type const & right) {
- if constexpr (C == category::random_access) {
- return (left - right) == 0;
- } else {
- return left.equal_to(right);
- }
- }
- friend bool operator!=(self_type const & left, self_type const & right) {
- return !(left == right);
- }
- SFINAE(C == category::random_access)
- friend bool operator<(self_type const & left, self_type const & right) {
- return (left - right) < 0;
- }
- SFINAE(C == category::random_access)
- friend bool operator<=(self_type const & left, self_type const & right) {
- return (left - right) <= 0;
- }
- SFINAE(C == category::random_access)
- friend bool operator>(self_type const & left, self_type const & right) {
- return (left - right) > 0;
- }
- SFINAE(C == category::random_access)
- friend bool operator>=(self_type const & left, self_type const & right) {
- return (left - right) >= 0;
- }
- protected:
- self_type & self() { return *static_cast<self_type *>(this); }
- self_type const & self() const {
- return *static_cast<self_type const *>(this);
- }
- };
- }
- // In C++20, a concept/requires could be used to eschew the need for the below
- // macros.
- #define MAKE_ITERATOR_FACADE_TYPEDEFS_T(Iter) \
- template <typename... T> struct std::iterator_traits<Iter<T...>> { \
- using type = Iter<T...>; \
- using reference = decltype(*std::declval<type>()); \
- using value_type = std::decay_t<reference>; \
- using pointer = decltype(std::declval<type>().operator->()); \
- using difference_type = ::iterator::detail::distance_to_t<type>; \
- using iterator_category = ::iterator::detail::tag_for_t<type>; \
- }
- #include <iterator/detail/undef.h>
|