// -*- 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) 2024 NVIDIA CORPORATION & AFFILIATES. // //===----------------------------------------------------------------------===// #ifndef _LIBCUDACXX___RANGES_VIEW_INTERFACE_H #define _LIBCUDACXX___RANGES_VIEW_INTERFACE_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 #include #include #include #include #include #include #include #include _LIBCUDACXX_BEGIN_NAMESPACE_RANGES #if _CCCL_STD_VER >= 2017 && !defined(_CCCL_COMPILER_MSVC_2017) # if _CCCL_STD_VER >= 2020 template concept __can_empty = requires(_Tp& __t) { _CUDA_VRANGES::empty(__t); }; # else // ^^^ C++20 ^^^ / vvv C++17 vvv template _LIBCUDACXX_CONCEPT_FRAGMENT(__can_empty_, requires(_Tp& __t)(typename(decltype(_CUDA_VRANGES::empty(__t))))); template _LIBCUDACXX_CONCEPT __can_empty = _LIBCUDACXX_FRAGMENT(__can_empty_, _Tp); # endif // _CCCL_STD_VER <= 2017 _LIBCUDACXX_BEGIN_NAMESPACE_RANGES_ABI # if _CCCL_STD_VER >= 2020 template requires is_class_v<_Derived> && same_as<_Derived, remove_cv_t<_Derived>> # else // ^^^ C++20 ^^^ / vvv C++17 vvv template && same_as<_Derived, remove_cv_t<_Derived>>, int>> # endif // _CCCL_STD_VER <= 2017 class view_interface { _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr _Derived& __derived() noexcept { static_assert(sizeof(_Derived) && derived_from<_Derived, view_interface> && view<_Derived>, ""); return static_cast<_Derived&>(*this); } _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr _Derived const& __derived() const noexcept { static_assert(sizeof(_Derived) && derived_from<_Derived, view_interface> && view<_Derived>, ""); return static_cast<_Derived const&>(*this); } public: _LIBCUDACXX_TEMPLATE(class _D2 = _Derived) _LIBCUDACXX_REQUIRES(forward_range<_D2>) _CCCL_NODISCARD _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr bool empty() { return _CUDA_VRANGES::begin(__derived()) == _CUDA_VRANGES::end(__derived()); } _LIBCUDACXX_TEMPLATE(class _D2 = _Derived) _LIBCUDACXX_REQUIRES(forward_range) _CCCL_NODISCARD _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr bool empty() const { return _CUDA_VRANGES::begin(__derived()) == _CUDA_VRANGES::end(__derived()); } _LIBCUDACXX_TEMPLATE(class _D2 = _Derived) _LIBCUDACXX_REQUIRES(__can_empty<_D2>) _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr explicit operator bool() { return !_CUDA_VRANGES::empty(__derived()); } _LIBCUDACXX_TEMPLATE(class _D2 = _Derived) _LIBCUDACXX_REQUIRES(__can_empty) _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr explicit operator bool() const { return !_CUDA_VRANGES::empty(__derived()); } _LIBCUDACXX_TEMPLATE(class _D2 = _Derived) _LIBCUDACXX_REQUIRES(contiguous_iterator>) _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr auto data() { return _CUDA_VSTD::to_address(_CUDA_VRANGES::begin(__derived())); } _LIBCUDACXX_TEMPLATE(class _D2 = _Derived) _LIBCUDACXX_REQUIRES(range _LIBCUDACXX_AND contiguous_iterator>) _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr auto data() const { return _CUDA_VSTD::to_address(_CUDA_VRANGES::begin(__derived())); } _LIBCUDACXX_TEMPLATE(class _D2 = _Derived) _LIBCUDACXX_REQUIRES(forward_range<_D2> _LIBCUDACXX_AND sized_sentinel_for, iterator_t<_D2>>) _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr auto size() { return _CUDA_VSTD::__to_unsigned_like(_CUDA_VRANGES::end(__derived()) - _CUDA_VRANGES::begin(__derived())); } _LIBCUDACXX_TEMPLATE(class _D2 = _Derived) _LIBCUDACXX_REQUIRES( forward_range _LIBCUDACXX_AND sized_sentinel_for, iterator_t>) _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr auto size() const { return _CUDA_VSTD::__to_unsigned_like(_CUDA_VRANGES::end(__derived()) - _CUDA_VRANGES::begin(__derived())); } _LIBCUDACXX_TEMPLATE(class _D2 = _Derived) _LIBCUDACXX_REQUIRES(forward_range<_D2>) _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr decltype(auto) front() { _LIBCUDACXX_ASSERT(!empty(), "Precondition `!empty()` not satisfied. `.front()` called on an empty view."); return *_CUDA_VRANGES::begin(__derived()); } _LIBCUDACXX_TEMPLATE(class _D2 = _Derived) _LIBCUDACXX_REQUIRES(forward_range) _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr decltype(auto) front() const { _LIBCUDACXX_ASSERT(!empty(), "Precondition `!empty()` not satisfied. `.front()` called on an empty view."); return *_CUDA_VRANGES::begin(__derived()); } _LIBCUDACXX_TEMPLATE(class _D2 = _Derived) _LIBCUDACXX_REQUIRES(bidirectional_range<_D2> _LIBCUDACXX_AND common_range<_D2>) _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr decltype(auto) back() { _LIBCUDACXX_ASSERT(!empty(), "Precondition `!empty()` not satisfied. `.back()` called on an empty view."); return *_CUDA_VRANGES::prev(_CUDA_VRANGES::end(__derived())); } _LIBCUDACXX_TEMPLATE(class _D2 = _Derived) _LIBCUDACXX_REQUIRES(bidirectional_range _LIBCUDACXX_AND common_range) _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr decltype(auto) back() const { _LIBCUDACXX_ASSERT(!empty(), "Precondition `!empty()` not satisfied. `.back()` called on an empty view."); return *_CUDA_VRANGES::prev(_CUDA_VRANGES::end(__derived())); } _LIBCUDACXX_TEMPLATE(class _RARange = _Derived) _LIBCUDACXX_REQUIRES(random_access_range<_RARange>) _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr decltype(auto) operator[](range_difference_t<_RARange> __index) { return _CUDA_VRANGES::begin(__derived())[__index]; } _LIBCUDACXX_TEMPLATE(class _RARange = const _Derived) _LIBCUDACXX_REQUIRES(random_access_range<_RARange>) _LIBCUDACXX_HIDE_FROM_ABI _LIBCUDACXX_INLINE_VISIBILITY constexpr decltype(auto) operator[](range_difference_t<_RARange> __index) const { return _CUDA_VRANGES::begin(__derived())[__index]; } }; _LIBCUDACXX_END_NAMESPACE_RANGES_ABI #endif // _CCCL_STD_VER >= 2017 && !_CCCL_COMPILER_MSVC_2017 _LIBCUDACXX_END_NAMESPACE_RANGES #endif // _LIBCUDACXX___RANGES_VIEW_INTERFACE_H