// -*- 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 // SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. // //===----------------------------------------------------------------------===// #ifndef _LIBCUDACXX___ITERATOR_WRAP_ITER_H #define _LIBCUDACXX___ITERATOR_WRAP_ITER_H #include #if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) # pragma GCC system_header #elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) # pragma clang system_header #elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) # pragma system_header #endif // no system header #include #include #include #include #include #include #include #ifndef _LIBCUDACXX_CONSTEXPR_IF_NODEBUG # if defined(_LIBCUDACXX_DEBUG) # define _LIBCUDACXX_CONSTEXPR_IF_NODEBUG # else # define _LIBCUDACXX_CONSTEXPR_IF_NODEBUG constexpr # endif #endif _LIBCUDACXX_BEGIN_NAMESPACE_STD template class __wrap_iter { public: typedef _Iter iterator_type; typedef typename iterator_traits::value_type value_type; typedef typename iterator_traits::difference_type difference_type; typedef typename iterator_traits::pointer pointer; typedef typename iterator_traits::reference reference; typedef typename iterator_traits::iterator_category iterator_category; #if _CCCL_STD_VER > 2011 typedef contiguous_iterator_tag iterator_concept; #endif private: iterator_type __i_; public: _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 __wrap_iter() noexcept : __i_() { _CUDA_VSTD::__debug_db_insert_i(this); } template _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 __wrap_iter(const __wrap_iter<_Up>& __u, typename enable_if::value>::type* = nullptr) noexcept : __i_(__u.base()) { #ifdef _LIBCUDACXX_ENABLE_DEBUG_MODE if (!__libcpp_is_constant_evaluated()) { __get_db()->__iterator_copy(this, _CUDA_VSTD::addressof(__u)); } #endif } #ifdef _LIBCUDACXX_ENABLE_DEBUG_MODE _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 __wrap_iter(const __wrap_iter& __x) : __i_(__x.base()) { if (!__libcpp_is_constant_evaluated()) { __get_db()->__iterator_copy(this, _CUDA_VSTD::addressof(__x)); } } _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 __wrap_iter& operator=(const __wrap_iter& __x) { if (this != _CUDA_VSTD::addressof(__x)) { if (!__libcpp_is_constant_evaluated()) { __get_db()->__iterator_copy(this, _CUDA_VSTD::addressof(__x)); } __i_ = __x.__i_; } return *this; } _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX20 ~__wrap_iter() { if (!__libcpp_is_constant_evaluated()) { __get_db()->__erase_i(this); } } #endif _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 reference operator*() const noexcept { _LIBCUDACXX_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this), "Attempted to dereference a non-dereferenceable iterator"); return *__i_; } _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 pointer operator->() const noexcept { _LIBCUDACXX_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this), "Attempted to dereference a non-dereferenceable iterator"); return _CUDA_VSTD::__to_address(__i_); } _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 __wrap_iter& operator++() noexcept { _LIBCUDACXX_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this), "Attempted to increment a non-incrementable iterator"); ++__i_; return *this; } _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 __wrap_iter operator++(int) noexcept { __wrap_iter __tmp(*this); ++(*this); return __tmp; } _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 __wrap_iter& operator--() noexcept { _LIBCUDACXX_DEBUG_ASSERT(__get_const_db()->__decrementable(this), "Attempted to decrement a non-decrementable iterator"); --__i_; return *this; } _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 __wrap_iter operator--(int) noexcept { __wrap_iter __tmp(*this); --(*this); return __tmp; } _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 __wrap_iter operator+(difference_type __n) const noexcept { __wrap_iter __w(*this); __w += __n; return __w; } _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 __wrap_iter& operator+=(difference_type __n) noexcept { _LIBCUDACXX_DEBUG_ASSERT(__get_const_db()->__addable(this, __n), "Attempted to add/subtract an iterator outside its valid range"); __i_ += __n; return *this; } _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 __wrap_iter operator-(difference_type __n) const noexcept { return *this + (-__n); } _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 __wrap_iter& operator-=(difference_type __n) noexcept { *this += -__n; return *this; } _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 reference operator[](difference_type __n) const noexcept { _LIBCUDACXX_DEBUG_ASSERT(__get_const_db()->__subscriptable(this, __n), "Attempted to subscript an iterator outside its valid range"); return __i_[__n]; } _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 iterator_type base() const noexcept { return __i_; } // private: #if _LIBCUDACXX_DEBUG_LEVEL >= 2 _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_IF_NODEBUG __wrap_iter(const void* __p, iterator_type __x) : __i_(__x) { if (!__libcpp_is_constant_evaluated()) { __get_db()->__insert_ic(this, __p); } } #else _LIBCUDACXX_INLINE_VISIBILITY _LIBCUDACXX_CONSTEXPR_IF_NODEBUG __wrap_iter(iterator_type __x) noexcept : __i_(__x) {} #endif template friend class __wrap_iter; template friend class basic_string; template friend class vector; template friend class span; }; template _LIBCUDACXX_INLINE_VISIBILITY constexpr bool operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) noexcept { return __x.base() == __y.base(); } template _LIBCUDACXX_INLINE_VISIBILITY constexpr bool operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) noexcept { return __x.base() == __y.base(); } template _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 bool operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) noexcept { _LIBCUDACXX_DEBUG_ASSERT( __get_const_db()->__less_than_comparable(_CUDA_VSTD::addressof(__x), _CUDA_VSTD::addressof(__y)), "Attempted to compare incomparable iterators"); return __x.base() < __y.base(); } template _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 bool operator<(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) noexcept { _LIBCUDACXX_DEBUG_ASSERT(__get_const_db()->__less_than_comparable(&__x, &__y), "Attempted to compare incomparable iterators"); return __x.base() < __y.base(); } template _LIBCUDACXX_INLINE_VISIBILITY constexpr bool operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) noexcept { return !(__x == __y); } template _LIBCUDACXX_INLINE_VISIBILITY constexpr bool operator!=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) noexcept { return !(__x == __y); } template _LIBCUDACXX_INLINE_VISIBILITY constexpr bool operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) noexcept { return __y < __x; } template _LIBCUDACXX_INLINE_VISIBILITY constexpr bool operator>(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) noexcept { return __y < __x; } template _LIBCUDACXX_INLINE_VISIBILITY constexpr bool operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) noexcept { return !(__x < __y); } template _LIBCUDACXX_INLINE_VISIBILITY constexpr bool operator>=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) noexcept { return !(__x < __y); } template _LIBCUDACXX_INLINE_VISIBILITY constexpr bool operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter1>& __y) noexcept { return !(__y < __x); } template _LIBCUDACXX_INLINE_VISIBILITY constexpr bool operator<=(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) noexcept { return !(__y < __x); } template _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 auto operator-(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) noexcept -> decltype(__x.base() - __y.base()) { _LIBCUDACXX_DEBUG_ASSERT( __get_const_db()->__less_than_comparable(_CUDA_VSTD::addressof(__x), _CUDA_VSTD::addressof(__y)), "Attempted to subtract incompatible iterators"); return __x.base() - __y.base(); } template _LIBCUDACXX_INLINE_VISIBILITY _CCCL_CONSTEXPR_CXX14 __wrap_iter<_Iter1> operator+(typename __wrap_iter<_Iter1>::difference_type __n, __wrap_iter<_Iter1> __x) noexcept { __x += __n; return __x; } #if _CCCL_STD_VER <= 2017 template struct __is_cpp17_contiguous_iterator<__wrap_iter<_It>> : true_type {}; #endif template struct _LIBCUDACXX_TEMPLATE_VIS pointer_traits<__wrap_iter<_It>> { typedef __wrap_iter<_It> pointer; typedef typename pointer_traits<_It>::element_type element_type; typedef typename pointer_traits<_It>::difference_type difference_type; _LIBCUDACXX_INLINE_VISIBILITY constexpr static element_type* to_address(pointer __w) noexcept { return _CUDA_VSTD::__to_address(__w.base()); } }; _LIBCUDACXX_END_NAMESPACE_STD #undef _LIBCUDACXX_CONSTEXPR_IF_NODEBUG #endif // _LIBCUDACXX___ITERATOR_WRAP_ITER_H