xref: /freebsd-src/contrib/llvm-project/libcxx/include/__algorithm/ranges_next_permutation.h (revision b3edf4467982447620505a28fc82e38a414c07dc)
161cfbce3SDimitry Andric //===----------------------------------------------------------------------===//
261cfbce3SDimitry Andric //
361cfbce3SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
461cfbce3SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
561cfbce3SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
661cfbce3SDimitry Andric //
761cfbce3SDimitry Andric //===----------------------------------------------------------------------===//
861cfbce3SDimitry Andric 
961cfbce3SDimitry Andric #ifndef _LIBCPP___ALGORITHM_RANGES_NEXT_PERMUTATION_H
1061cfbce3SDimitry Andric #define _LIBCPP___ALGORITHM_RANGES_NEXT_PERMUTATION_H
1161cfbce3SDimitry Andric 
1261cfbce3SDimitry Andric #include <__algorithm/in_found_result.h>
1361cfbce3SDimitry Andric #include <__algorithm/iterator_operations.h>
1461cfbce3SDimitry Andric #include <__algorithm/make_projected.h>
1561cfbce3SDimitry Andric #include <__algorithm/next_permutation.h>
1661cfbce3SDimitry Andric #include <__config>
1761cfbce3SDimitry Andric #include <__functional/identity.h>
1861cfbce3SDimitry Andric #include <__functional/ranges_operations.h>
1961cfbce3SDimitry Andric #include <__iterator/concepts.h>
2061cfbce3SDimitry Andric #include <__iterator/sortable.h>
2161cfbce3SDimitry Andric #include <__ranges/access.h>
2261cfbce3SDimitry Andric #include <__ranges/concepts.h>
2361cfbce3SDimitry Andric #include <__ranges/dangling.h>
2461cfbce3SDimitry Andric #include <__utility/move.h>
25bdd1243dSDimitry Andric #include <__utility/pair.h>
2661cfbce3SDimitry Andric 
2761cfbce3SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2861cfbce3SDimitry Andric #  pragma GCC system_header
2961cfbce3SDimitry Andric #endif
3061cfbce3SDimitry Andric 
31*b3edf446SDimitry Andric _LIBCPP_PUSH_MACROS
32*b3edf446SDimitry Andric #include <__undef_macros>
33*b3edf446SDimitry Andric 
3406c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
3561cfbce3SDimitry Andric 
3661cfbce3SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
3761cfbce3SDimitry Andric 
3861cfbce3SDimitry Andric namespace ranges {
3961cfbce3SDimitry Andric 
4061cfbce3SDimitry Andric template <class _InIter>
4161cfbce3SDimitry Andric using next_permutation_result = in_found_result<_InIter>;
4261cfbce3SDimitry Andric 
4361cfbce3SDimitry Andric namespace __next_permutation {
4461cfbce3SDimitry Andric 
4561cfbce3SDimitry Andric struct __fn {
4661cfbce3SDimitry Andric   template <bidirectional_iterator _Iter, sentinel_for<_Iter> _Sent, class _Comp = ranges::less, class _Proj = identity>
4761cfbce3SDimitry Andric     requires sortable<_Iter, _Comp, _Proj>
4861cfbce3SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr next_permutation_result<_Iter>
operator__fn4961cfbce3SDimitry Andric   operator()(_Iter __first, _Sent __last, _Comp __comp = {}, _Proj __proj = {}) const {
5061cfbce3SDimitry Andric     auto __result = std::__next_permutation<_RangeAlgPolicy>(
5161cfbce3SDimitry Andric         std::move(__first), std::move(__last), std::__make_projected(__comp, __proj));
5261cfbce3SDimitry Andric     return {std::move(__result.first), std::move(__result.second)};
5361cfbce3SDimitry Andric   }
5461cfbce3SDimitry Andric 
5561cfbce3SDimitry Andric   template <bidirectional_range _Range, class _Comp = ranges::less, class _Proj = identity>
5661cfbce3SDimitry Andric     requires sortable<iterator_t<_Range>, _Comp, _Proj>
5761cfbce3SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr next_permutation_result<borrowed_iterator_t<_Range>>
operator__fn5861cfbce3SDimitry Andric   operator()(_Range&& __range, _Comp __comp = {}, _Proj __proj = {}) const {
5961cfbce3SDimitry Andric     auto __result = std::__next_permutation<_RangeAlgPolicy>(
6061cfbce3SDimitry Andric         ranges::begin(__range), ranges::end(__range), std::__make_projected(__comp, __proj));
6161cfbce3SDimitry Andric     return {std::move(__result.first), std::move(__result.second)};
6261cfbce3SDimitry Andric   }
6361cfbce3SDimitry Andric };
6461cfbce3SDimitry Andric 
6561cfbce3SDimitry Andric } // namespace __next_permutation
6661cfbce3SDimitry Andric 
6761cfbce3SDimitry Andric inline namespace __cpo {
6861cfbce3SDimitry Andric constexpr inline auto next_permutation = __next_permutation::__fn{};
6961cfbce3SDimitry Andric } // namespace __cpo
7061cfbce3SDimitry Andric } // namespace ranges
7161cfbce3SDimitry Andric 
7261cfbce3SDimitry Andric _LIBCPP_END_NAMESPACE_STD
7361cfbce3SDimitry Andric 
7406c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
7561cfbce3SDimitry Andric 
76*b3edf446SDimitry Andric _LIBCPP_POP_MACROS
77*b3edf446SDimitry Andric 
7861cfbce3SDimitry Andric #endif // _LIBCPP___ALGORITHM_RANGES_NEXT_PERMUTATION_H
79