xref: /freebsd/contrib/llvm-project/libcxx/include/__ranges/counted.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1349cc55cSDimitry Andric // -*- C++ -*-
2349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
3349cc55cSDimitry Andric //
4349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
6349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7349cc55cSDimitry Andric //
8349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
9bdd1243dSDimitry Andric 
10349cc55cSDimitry Andric #ifndef _LIBCPP___RANGES_COUNTED_H
11349cc55cSDimitry Andric #define _LIBCPP___RANGES_COUNTED_H
12349cc55cSDimitry Andric 
130eae32dcSDimitry Andric #include <__concepts/convertible_to.h>
14349cc55cSDimitry Andric #include <__config>
15349cc55cSDimitry Andric #include <__iterator/concepts.h>
16349cc55cSDimitry Andric #include <__iterator/counted_iterator.h>
17349cc55cSDimitry Andric #include <__iterator/default_sentinel.h>
18349cc55cSDimitry Andric #include <__iterator/incrementable_traits.h>
19349cc55cSDimitry Andric #include <__iterator/iterator_traits.h>
20349cc55cSDimitry Andric #include <__memory/pointer_traits.h>
21349cc55cSDimitry Andric #include <__ranges/subrange.h>
2206c3fb27SDimitry Andric #include <__type_traits/decay.h>
23349cc55cSDimitry Andric #include <__utility/forward.h>
24349cc55cSDimitry Andric #include <__utility/move.h>
2506c3fb27SDimitry Andric #include <cstddef>
26349cc55cSDimitry Andric #include <span>
27349cc55cSDimitry Andric 
28349cc55cSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
29349cc55cSDimitry Andric #  pragma GCC system_header
30349cc55cSDimitry Andric #endif
31349cc55cSDimitry Andric 
32b3edf446SDimitry Andric _LIBCPP_PUSH_MACROS
33b3edf446SDimitry Andric #include <__undef_macros>
34b3edf446SDimitry Andric 
35349cc55cSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
36349cc55cSDimitry Andric 
3706c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
38349cc55cSDimitry Andric 
39349cc55cSDimitry Andric namespace ranges::views {
40349cc55cSDimitry Andric 
41349cc55cSDimitry Andric namespace __counted {
42349cc55cSDimitry Andric 
43349cc55cSDimitry Andric struct __fn {
440eae32dcSDimitry Andric   template <contiguous_iterator _It>
45cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI static constexpr auto
__go__fn46cb14a3feSDimitry Andric   __go(_It __it,
47cb14a3feSDimitry Andric        iter_difference_t<_It> __count) noexcept(noexcept(span(std::to_address(__it), static_cast<size_t>(__count))))
480eae32dcSDimitry Andric   // Deliberately omit return-type SFINAE, because to_address is not SFINAE-friendly
49cb14a3feSDimitry Andric   {
50cb14a3feSDimitry Andric     return span(std::to_address(__it), static_cast<size_t>(__count));
51cb14a3feSDimitry Andric   }
52349cc55cSDimitry Andric 
530eae32dcSDimitry Andric   template <random_access_iterator _It>
54*0fca6ea1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_It __it, iter_difference_t<_It> __count) noexcept(
55*0fca6ea1SDimitry Andric       noexcept(subrange(__it, __it + __count))) -> decltype(subrange(__it, __it + __count)) {
56cb14a3feSDimitry Andric     return subrange(__it, __it + __count);
57cb14a3feSDimitry Andric   }
58349cc55cSDimitry Andric 
590eae32dcSDimitry Andric   template <class _It>
60cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_It __it, iter_difference_t<_It> __count) noexcept(
61cb14a3feSDimitry Andric       noexcept(subrange(counted_iterator(std::move(__it), __count), default_sentinel)))
62cb14a3feSDimitry Andric       -> decltype(subrange(counted_iterator(std::move(__it), __count), default_sentinel)) {
63cb14a3feSDimitry Andric     return subrange(counted_iterator(std::move(__it), __count), default_sentinel);
64cb14a3feSDimitry Andric   }
650eae32dcSDimitry Andric 
660eae32dcSDimitry Andric   template <class _It, convertible_to<iter_difference_t<_It>> _Diff>
670eae32dcSDimitry Andric     requires input_or_output_iterator<decay_t<_It>>
68cb14a3feSDimitry Andric   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_It&& __it, _Diff&& __count) const
6981ad6265SDimitry Andric       noexcept(noexcept(__go(std::forward<_It>(__it), std::forward<_Diff>(__count))))
70cb14a3feSDimitry Andric           -> decltype(__go(std::forward<_It>(__it), std::forward<_Diff>(__count))) {
71cb14a3feSDimitry Andric     return __go(std::forward<_It>(__it), std::forward<_Diff>(__count));
72cb14a3feSDimitry Andric   }
73349cc55cSDimitry Andric };
740eae32dcSDimitry Andric 
750eae32dcSDimitry Andric } // namespace __counted
76349cc55cSDimitry Andric 
77349cc55cSDimitry Andric inline namespace __cpo {
78349cc55cSDimitry Andric inline constexpr auto counted = __counted::__fn{};
79349cc55cSDimitry Andric } // namespace __cpo
80349cc55cSDimitry Andric 
81349cc55cSDimitry Andric } // namespace ranges::views
82349cc55cSDimitry Andric 
8306c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
84349cc55cSDimitry Andric 
85349cc55cSDimitry Andric _LIBCPP_END_NAMESPACE_STD
86349cc55cSDimitry Andric 
87b3edf446SDimitry Andric _LIBCPP_POP_MACROS
88b3edf446SDimitry Andric 
89349cc55cSDimitry Andric #endif // _LIBCPP___RANGES_COUNTED_H
90