// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP___ITERATOR_WRAP_ITER_H
#define _LIBCPP___ITERATOR_WRAP_ITER_H

#include <__compare/ordering.h>
#include <__compare/three_way_comparable.h>
#include <__config>
#include <__cstddef/size_t.h>
#include <__iterator/iterator_traits.h>
#include <__memory/pointer_traits.h>
#include <__type_traits/conjunction.h>
#include <__type_traits/disjunction.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/integral_constant.h>
#include <__type_traits/is_convertible.h>
#include <__type_traits/is_same.h>
#include <__type_traits/make_const_lvalue_ref.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#  pragma GCC system_header
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

template <class _Iter>
class __wrap_iter {
public:
  typedef typename iterator_traits<_Iter>::value_type value_type;
  typedef typename iterator_traits<_Iter>::difference_type difference_type;
  typedef typename iterator_traits<_Iter>::pointer pointer;
  typedef typename iterator_traits<_Iter>::reference reference;
  typedef typename iterator_traits<_Iter>::iterator_category iterator_category;
#if _LIBCPP_STD_VER >= 20
  typedef contiguous_iterator_tag iterator_concept;
#endif

private:
  _Iter __i_;

public:
  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter() _NOEXCEPT : __i_() {}
  template <class _OtherIter,
            __enable_if_t<
                _And<is_convertible<const _OtherIter&, _Iter>,
                     _Or<is_same<reference, __iterator_reference<_OtherIter> >,
                         is_same<reference, __make_const_lvalue_ref<__iterator_reference<_OtherIter> > > > >::value,
                int> = 0>
  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter(const __wrap_iter<_OtherIter>& __u) _NOEXCEPT
      : __i_(__u.__i_) {}
  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT {
    return *__i_;
  }
  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pointer operator->() const _NOEXCEPT {
    return std::__to_address(__i_);
  }
  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator++() _NOEXCEPT {
    ++__i_;
    return *this;
  }
  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator++(int) _NOEXCEPT {
    __wrap_iter __tmp(*this);
    ++(*this);
    return __tmp;
  }

  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator--() _NOEXCEPT {
    --__i_;
    return *this;
  }
  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator--(int) _NOEXCEPT {
    __wrap_iter __tmp(*this);
    --(*this);
    return __tmp;
  }
  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter
  operator+(difference_type __n) const _NOEXCEPT {
    __wrap_iter __w(*this);
    __w += __n;
    return __w;
  }
  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator+=(difference_type __n) _NOEXCEPT {
    __i_ += __n;
    return *this;
  }
  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter
  operator-(difference_type __n) const _NOEXCEPT {
    return *this + (-__n);
  }
  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator-=(difference_type __n) _NOEXCEPT {
    *this += -__n;
    return *this;
  }
  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference
  operator[](difference_type __n) const _NOEXCEPT {
    return __i_[__n];
  }

private:
  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __wrap_iter(_Iter __x) _NOEXCEPT : __i_(__x) {}

  template <class _Up>
  friend class __wrap_iter;
  template <class _CharT, class _Traits, class _Alloc>
  friend class basic_string;
  template <class _CharT, class _Traits>
  friend class basic_string_view;
  template <class _Tp, class _Alloc>
  friend class vector;
  template <class _Tp, size_t>
  friend class span;
  template <class _Tp, size_t _Size>
  friend struct array;

  _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
  operator==(const __wrap_iter& __x, const __wrap_iter& __y) _NOEXCEPT {
    return __x.__i_ == __y.__i_;
  }

  template <class _Iter2>
  _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
  operator==(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
    return __x.__i_ == __y.__i_;
  }

  _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
  operator<(const __wrap_iter& __x, const __wrap_iter& __y) _NOEXCEPT {
    return __x.__i_ < __y.__i_;
  }

  template <class _Iter2>
  _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
  operator<(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
    return __x.__i_ < __y.__i_;
  }

#if _LIBCPP_STD_VER <= 17
  _LIBCPP_HIDE_FROM_ABI friend
      _LIBCPP_CONSTEXPR bool operator!=(const __wrap_iter& __x, const __wrap_iter& __y) _NOEXCEPT {
    return !(__x == __y);
  }

  template <class _Iter2>
  _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
  operator!=(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
    return !(__x == __y);
  }

  _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
  operator>(const __wrap_iter& __x, const __wrap_iter& __y) _NOEXCEPT {
    return __y < __x;
  }

  template <class _Iter2>
  _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
  operator>(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
    return __y < __x;
  }

  _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
  operator>=(const __wrap_iter& __x, const __wrap_iter& __y) _NOEXCEPT {
    return !(__x < __y);
  }

  template <class _Iter2>
  _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
  operator>=(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
    return !(__x < __y);
  }

  _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
  operator<=(const __wrap_iter& __x, const __wrap_iter& __y) _NOEXCEPT {
    return !(__y < __x);
  }

  template <class _Iter2>
  _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
  operator<=(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
    return !(__y < __x);
  }

#else
  template <class _Iter2>
  _LIBCPP_HIDE_FROM_ABI friend constexpr strong_ordering
  operator<=>(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) noexcept {
    if constexpr (three_way_comparable_with<_Iter, _Iter2, strong_ordering>) {
      return __x.__i_ <=> __y.__i_;
    } else {
      if (__x.__i_ < __y.__i_)
        return strong_ordering::less;

      if (__x.__i_ == __y.__i_)
        return strong_ordering::equal;

      return strong_ordering::greater;
    }
  }
#endif // _LIBCPP_STD_VER >= 20

#ifndef _LIBCPP_CXX03_LANG
  template <class _Iter2>
  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR_SINCE_CXX14 auto
  operator-(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT->decltype(__x.__i_ - __y.__i_)
#else
  template <class _Iter2>
  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR_SINCE_CXX14
  typename __wrap_iter::difference_type operator-(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
#endif // C++03
  {
    return __x.__i_ - __y.__i_;
  }

  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter
  operator+(typename __wrap_iter::difference_type __n, __wrap_iter __x) _NOEXCEPT {
    __x += __n;
    return __x;
  }
};

#if _LIBCPP_STD_VER <= 17
template <class _It>
struct __libcpp_is_contiguous_iterator<__wrap_iter<_It> > : true_type {};
#endif

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___ITERATOR_WRAP_ITER_H
