| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#ifndef _LIBCPP___CXX03___ALGORITHM_NEXT_PERMUTATION_H |
| 10 |
#define _LIBCPP___CXX03___ALGORITHM_NEXT_PERMUTATION_H |
| 11 |
|
| 12 |
#include <__cxx03/__algorithm/comp.h> |
| 13 |
#include <__cxx03/__algorithm/comp_ref_type.h> |
| 14 |
#include <__cxx03/__algorithm/iterator_operations.h> |
| 15 |
#include <__cxx03/__algorithm/reverse.h> |
| 16 |
#include <__cxx03/__config> |
| 17 |
#include <__cxx03/__iterator/iterator_traits.h> |
| 18 |
#include <__cxx03/__utility/move.h> |
| 19 |
#include <__cxx03/__utility/pair.h> |
| 20 |
|
| 21 |
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 22 |
# pragma GCC system_header |
| 23 |
#endif |
| 24 |
|
| 25 |
_LIBCPP_PUSH_MACROS |
| 26 |
#include <__cxx03/__undef_macros> |
| 27 |
|
| 28 |
_LIBCPP_BEGIN_NAMESPACE_STD |
| 29 |
|
| 30 |
template <class _AlgPolicy, class _Compare, class _BidirectionalIterator, class _Sentinel> |
| 31 |
_LIBCPP_HIDE_FROM_ABI pair<_BidirectionalIterator, bool> |
| 32 |
__next_permutation(_BidirectionalIterator __first, _Sentinel __last, _Compare&& __comp) { |
| 33 |
using _Result = pair<_BidirectionalIterator, bool>; |
| 34 |
|
| 35 |
_BidirectionalIterator __last_iter = _IterOps<_AlgPolicy>::next(__first, __last); |
| 36 |
_BidirectionalIterator __i = __last_iter; |
| 37 |
if (__first == __last || __first == --__i) |
| 38 |
return _Result(std::move(__last_iter), false); |
| 39 |
|
| 40 |
while (true) { |
| 41 |
_BidirectionalIterator __ip1 = __i; |
| 42 |
if (__comp(*--__i, *__ip1)) { |
| 43 |
_BidirectionalIterator __j = __last_iter; |
| 44 |
while (!__comp(*__i, *--__j)) |
| 45 |
; |
| 46 |
_IterOps<_AlgPolicy>::iter_swap(__i, __j); |
| 47 |
std::__reverse<_AlgPolicy>(__ip1, __last_iter); |
| 48 |
return _Result(std::move(__last_iter), true); |
| 49 |
} |
| 50 |
if (__i == __first) { |
| 51 |
std::__reverse<_AlgPolicy>(__first, __last_iter); |
| 52 |
return _Result(std::move(__last_iter), false); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
template <class _BidirectionalIterator, class _Compare> |
| 58 |
inline _LIBCPP_HIDE_FROM_ABI bool |
| 59 |
next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp) { |
| 60 |
return std::__next_permutation<_ClassicAlgPolicy>( |
| 61 |
std::move(__first), std::move(__last), static_cast<__comp_ref_type<_Compare> >(__comp)) |
| 62 |
.second; |
| 63 |
} |
| 64 |
|
| 65 |
template <class _BidirectionalIterator> |
| 66 |
inline _LIBCPP_HIDE_FROM_ABI bool next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last) { |
| 67 |
return std::next_permutation(__first, __last, __less<>()); |
| 68 |
} |
| 69 |
|
| 70 |
_LIBCPP_END_NAMESPACE_STD |
| 71 |
|
| 72 |
_LIBCPP_POP_MACROS |
| 73 |
|
| 74 |
#endif // _LIBCPP___CXX03___ALGORITHM_NEXT_PERMUTATION_H |