1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___RANGES_COUNTED_H
11 #define _LIBCPP___RANGES_COUNTED_H
12 
13 #include <__concepts/convertible_to.h>
14 #include <__config>
15 #include <__iterator/concepts.h>
16 #include <__iterator/counted_iterator.h>
17 #include <__iterator/default_sentinel.h>
18 #include <__iterator/incrementable_traits.h>
19 #include <__iterator/iterator_traits.h>
20 #include <__memory/pointer_traits.h>
21 #include <__ranges/subrange.h>
22 #include <__utility/forward.h>
23 #include <__utility/move.h>
24 #include <span>
25 #include <type_traits>
26 
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 #  pragma GCC system_header
29 #endif
30 
31 _LIBCPP_BEGIN_NAMESPACE_STD
32 
33 #if _LIBCPP_STD_VER > 17
34 
35 namespace ranges::views {
36 
37 namespace __counted {
38 
39   struct __fn {
40     template<contiguous_iterator _It>
41     _LIBCPP_HIDE_FROM_ABI
42     static constexpr auto __go(_It __it, iter_difference_t<_It> __count)
43       noexcept(noexcept(span(std::to_address(__it), static_cast<size_t>(__count))))
44       // Deliberately omit return-type SFINAE, because to_address is not SFINAE-friendly
45       { return          span(std::to_address(__it), static_cast<size_t>(__count)); }
46 
47     template<random_access_iterator _It>
48     _LIBCPP_HIDE_FROM_ABI
49     static constexpr auto __go(_It __it, iter_difference_t<_It> __count)
50       noexcept(noexcept(subrange(__it, __it + __count)))
51       -> decltype(      subrange(__it, __it + __count))
52       { return          subrange(__it, __it + __count); }
53 
54     template<class _It>
55     _LIBCPP_HIDE_FROM_ABI
56     static constexpr auto __go(_It __it, iter_difference_t<_It> __count)
57       noexcept(noexcept(subrange(counted_iterator(std::move(__it), __count), default_sentinel)))
58       -> decltype(      subrange(counted_iterator(std::move(__it), __count), default_sentinel))
59       { return          subrange(counted_iterator(std::move(__it), __count), default_sentinel); }
60 
61     template<class _It, convertible_to<iter_difference_t<_It>> _Diff>
62       requires input_or_output_iterator<decay_t<_It>>
63     [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
64     constexpr auto operator()(_It&& __it, _Diff&& __count) const
65       noexcept(noexcept(__go(std::forward<_It>(__it), std::forward<_Diff>(__count))))
66       -> decltype(      __go(std::forward<_It>(__it), std::forward<_Diff>(__count)))
67       { return          __go(std::forward<_It>(__it), std::forward<_Diff>(__count)); }
68   };
69 
70 } // namespace __counted
71 
72 inline namespace __cpo {
73   inline constexpr auto counted = __counted::__fn{};
74 } // namespace __cpo
75 
76 } // namespace ranges::views
77 
78 #endif // _LIBCPP_STD_VER > 17
79 
80 _LIBCPP_END_NAMESPACE_STD
81 
82 #endif // _LIBCPP___RANGES_COUNTED_H
83