xref: /freebsd/contrib/llvm-project/libcxx/include/__ranges/counted.h (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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>
22*06c3fb27SDimitry Andric #include <__type_traits/decay.h>
23349cc55cSDimitry Andric #include <__utility/forward.h>
24349cc55cSDimitry Andric #include <__utility/move.h>
25*06c3fb27SDimitry 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 
32349cc55cSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
33349cc55cSDimitry Andric 
34*06c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
35349cc55cSDimitry Andric 
36349cc55cSDimitry Andric namespace ranges::views {
37349cc55cSDimitry Andric 
38349cc55cSDimitry Andric namespace __counted {
39349cc55cSDimitry Andric 
40349cc55cSDimitry Andric   struct __fn {
410eae32dcSDimitry Andric     template<contiguous_iterator _It>
42349cc55cSDimitry Andric     _LIBCPP_HIDE_FROM_ABI
430eae32dcSDimitry Andric     static constexpr auto __go(_It __it, iter_difference_t<_It> __count)
4481ad6265SDimitry Andric       noexcept(noexcept(span(std::to_address(__it), static_cast<size_t>(__count))))
450eae32dcSDimitry Andric       // Deliberately omit return-type SFINAE, because to_address is not SFINAE-friendly
4681ad6265SDimitry Andric       { return          span(std::to_address(__it), static_cast<size_t>(__count)); }
47349cc55cSDimitry Andric 
480eae32dcSDimitry Andric     template<random_access_iterator _It>
49349cc55cSDimitry Andric     _LIBCPP_HIDE_FROM_ABI
500eae32dcSDimitry Andric     static constexpr auto __go(_It __it, iter_difference_t<_It> __count)
510eae32dcSDimitry Andric       noexcept(noexcept(subrange(__it, __it + __count)))
520eae32dcSDimitry Andric       -> decltype(      subrange(__it, __it + __count))
530eae32dcSDimitry Andric       { return          subrange(__it, __it + __count); }
54349cc55cSDimitry Andric 
550eae32dcSDimitry Andric     template<class _It>
56349cc55cSDimitry Andric     _LIBCPP_HIDE_FROM_ABI
570eae32dcSDimitry Andric     static constexpr auto __go(_It __it, iter_difference_t<_It> __count)
5881ad6265SDimitry Andric       noexcept(noexcept(subrange(counted_iterator(std::move(__it), __count), default_sentinel)))
5981ad6265SDimitry Andric       -> decltype(      subrange(counted_iterator(std::move(__it), __count), default_sentinel))
6081ad6265SDimitry Andric       { return          subrange(counted_iterator(std::move(__it), __count), default_sentinel); }
610eae32dcSDimitry Andric 
620eae32dcSDimitry Andric     template<class _It, convertible_to<iter_difference_t<_It>> _Diff>
630eae32dcSDimitry Andric       requires input_or_output_iterator<decay_t<_It>>
640eae32dcSDimitry Andric     [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
650eae32dcSDimitry Andric     constexpr auto operator()(_It&& __it, _Diff&& __count) const
6681ad6265SDimitry Andric       noexcept(noexcept(__go(std::forward<_It>(__it), std::forward<_Diff>(__count))))
6781ad6265SDimitry Andric       -> decltype(      __go(std::forward<_It>(__it), std::forward<_Diff>(__count)))
6881ad6265SDimitry Andric       { return          __go(std::forward<_It>(__it), std::forward<_Diff>(__count)); }
69349cc55cSDimitry Andric   };
700eae32dcSDimitry Andric 
710eae32dcSDimitry Andric } // namespace __counted
72349cc55cSDimitry Andric 
73349cc55cSDimitry Andric inline namespace __cpo {
74349cc55cSDimitry Andric   inline constexpr auto counted = __counted::__fn{};
75349cc55cSDimitry Andric } // namespace __cpo
76349cc55cSDimitry Andric 
77349cc55cSDimitry Andric } // namespace ranges::views
78349cc55cSDimitry Andric 
79*06c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
80349cc55cSDimitry Andric 
81349cc55cSDimitry Andric _LIBCPP_END_NAMESPACE_STD
82349cc55cSDimitry Andric 
83349cc55cSDimitry Andric #endif // _LIBCPP___RANGES_COUNTED_H
84