ranges_shuffle.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #ifndef _LIBCPP___ALGORITHM_RANGES_SHUFFLE_H
  9. #define _LIBCPP___ALGORITHM_RANGES_SHUFFLE_H
  10. #include <__algorithm/iterator_operations.h>
  11. #include <__algorithm/shuffle.h>
  12. #include <__algorithm/uniform_random_bit_generator_adaptor.h>
  13. #include <__config>
  14. #include <__functional/invoke.h>
  15. #include <__functional/ranges_operations.h>
  16. #include <__iterator/concepts.h>
  17. #include <__iterator/iterator_traits.h>
  18. #include <__iterator/next.h>
  19. #include <__iterator/permutable.h>
  20. #include <__random/uniform_random_bit_generator.h>
  21. #include <__ranges/access.h>
  22. #include <__ranges/concepts.h>
  23. #include <__ranges/dangling.h>
  24. #include <__utility/forward.h>
  25. #include <__utility/move.h>
  26. #include <type_traits>
  27. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  28. # pragma GCC system_header
  29. #endif
  30. #if _LIBCPP_STD_VER > 17
  31. _LIBCPP_BEGIN_NAMESPACE_STD
  32. namespace ranges {
  33. namespace __shuffle {
  34. struct __fn {
  35. template <random_access_iterator _Iter, sentinel_for<_Iter> _Sent, class _Gen>
  36. requires permutable<_Iter> && uniform_random_bit_generator<remove_reference_t<_Gen>>
  37. _LIBCPP_HIDE_FROM_ABI
  38. _Iter operator()(_Iter __first, _Sent __last, _Gen&& __gen) const {
  39. _ClassicGenAdaptor<_Gen> __adapted_gen(__gen);
  40. return std::__shuffle<_RangeAlgPolicy>(std::move(__first), std::move(__last), __adapted_gen);
  41. }
  42. template<random_access_range _Range, class _Gen>
  43. requires permutable<iterator_t<_Range>> && uniform_random_bit_generator<remove_reference_t<_Gen>>
  44. _LIBCPP_HIDE_FROM_ABI
  45. borrowed_iterator_t<_Range> operator()(_Range&& __range, _Gen&& __gen) const {
  46. return (*this)(ranges::begin(__range), ranges::end(__range), std::forward<_Gen>(__gen));
  47. }
  48. };
  49. } // namespace __shuffle
  50. inline namespace __cpo {
  51. inline constexpr auto shuffle = __shuffle::__fn{};
  52. } // namespace __cpo
  53. } // namespace ranges
  54. _LIBCPP_END_NAMESPACE_STD
  55. #endif // _LIBCPP_STD_VER > 17
  56. #endif // _LIBCPP___ALGORITHM_RANGES_SHUFFLE_H