shuffle.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_SHUFFLE_H
  9. #define _LIBCPP___ALGORITHM_SHUFFLE_H
  10. #include <__algorithm/iterator_operations.h>
  11. #include <__config>
  12. #include <__iterator/iterator_traits.h>
  13. #include <__random/uniform_int_distribution.h>
  14. #include <__utility/forward.h>
  15. #include <__utility/move.h>
  16. #include <__utility/swap.h>
  17. #include <cstddef>
  18. #include <cstdint>
  19. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  20. # pragma GCC system_header
  21. #endif
  22. _LIBCPP_PUSH_MACROS
  23. #include <__undef_macros>
  24. _LIBCPP_BEGIN_NAMESPACE_STD
  25. class _LIBCPP_EXPORTED_FROM_ABI __libcpp_debug_randomizer {
  26. public:
  27. _LIBCPP_HIDE_FROM_ABI __libcpp_debug_randomizer() {
  28. __state_ = __seed();
  29. __inc_ = __state_ + 0xda3e39cb94b95bdbULL;
  30. __inc_ = (__inc_ << 1) | 1;
  31. }
  32. typedef uint_fast32_t result_type;
  33. static const result_type _Min = 0;
  34. static const result_type _Max = 0xFFFFFFFF;
  35. _LIBCPP_HIDE_FROM_ABI result_type operator()() {
  36. uint_fast64_t __oldstate = __state_;
  37. __state_ = __oldstate * 6364136223846793005ULL + __inc_;
  38. return __oldstate >> 32;
  39. }
  40. static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR result_type min() { return _Min; }
  41. static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR result_type max() { return _Max; }
  42. private:
  43. uint_fast64_t __state_;
  44. uint_fast64_t __inc_;
  45. _LIBCPP_HIDE_FROM_ABI static uint_fast64_t __seed() {
  46. #ifdef _LIBCPP_DEBUG_RANDOMIZE_UNSPECIFIED_STABILITY_SEED
  47. return _LIBCPP_DEBUG_RANDOMIZE_UNSPECIFIED_STABILITY_SEED;
  48. #else
  49. static char __x;
  50. return reinterpret_cast<uintptr_t>(&__x);
  51. #endif
  52. }
  53. };
  54. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_RANDOM_SHUFFLE) \
  55. || defined(_LIBCPP_BUILDING_LIBRARY)
  56. class _LIBCPP_EXPORTED_FROM_ABI __rs_default;
  57. _LIBCPP_EXPORTED_FROM_ABI __rs_default __rs_get();
  58. class _LIBCPP_EXPORTED_FROM_ABI __rs_default
  59. {
  60. static unsigned __c_;
  61. __rs_default();
  62. public:
  63. typedef uint_fast32_t result_type;
  64. static const result_type _Min = 0;
  65. static const result_type _Max = 0xFFFFFFFF;
  66. __rs_default(const __rs_default&);
  67. ~__rs_default();
  68. result_type operator()();
  69. static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR result_type min() {return _Min;}
  70. static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR result_type max() {return _Max;}
  71. friend _LIBCPP_EXPORTED_FROM_ABI __rs_default __rs_get();
  72. };
  73. _LIBCPP_EXPORTED_FROM_ABI __rs_default __rs_get();
  74. template <class _RandomAccessIterator>
  75. _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_IN_CXX14 void
  76. random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
  77. {
  78. typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
  79. typedef uniform_int_distribution<ptrdiff_t> _Dp;
  80. typedef typename _Dp::param_type _Pp;
  81. difference_type __d = __last - __first;
  82. if (__d > 1)
  83. {
  84. _Dp __uid;
  85. __rs_default __g = __rs_get();
  86. for (--__last, (void) --__d; __first < __last; ++__first, (void) --__d)
  87. {
  88. difference_type __i = __uid(__g, _Pp(0, __d));
  89. if (__i != difference_type(0))
  90. swap(*__first, *(__first + __i));
  91. }
  92. }
  93. }
  94. template <class _RandomAccessIterator, class _RandomNumberGenerator>
  95. _LIBCPP_HIDE_FROM_ABI _LIBCPP_DEPRECATED_IN_CXX14 void
  96. random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
  97. #ifndef _LIBCPP_CXX03_LANG
  98. _RandomNumberGenerator&& __rand)
  99. #else
  100. _RandomNumberGenerator& __rand)
  101. #endif
  102. {
  103. typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
  104. difference_type __d = __last - __first;
  105. if (__d > 1)
  106. {
  107. for (--__last; __first < __last; ++__first, (void) --__d)
  108. {
  109. difference_type __i = __rand(__d);
  110. if (__i != difference_type(0))
  111. swap(*__first, *(__first + __i));
  112. }
  113. }
  114. }
  115. #endif
  116. template <class _AlgPolicy, class _RandomAccessIterator, class _Sentinel, class _UniformRandomNumberGenerator>
  117. _LIBCPP_HIDE_FROM_ABI _RandomAccessIterator __shuffle(
  118. _RandomAccessIterator __first, _Sentinel __last_sentinel, _UniformRandomNumberGenerator&& __g) {
  119. typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
  120. typedef uniform_int_distribution<ptrdiff_t> _Dp;
  121. typedef typename _Dp::param_type _Pp;
  122. auto __original_last = _IterOps<_AlgPolicy>::next(__first, __last_sentinel);
  123. auto __last = __original_last;
  124. difference_type __d = __last - __first;
  125. if (__d > 1)
  126. {
  127. _Dp __uid;
  128. for (--__last, (void) --__d; __first < __last; ++__first, (void) --__d)
  129. {
  130. difference_type __i = __uid(__g, _Pp(0, __d));
  131. if (__i != difference_type(0))
  132. _IterOps<_AlgPolicy>::iter_swap(__first, __first + __i);
  133. }
  134. }
  135. return __original_last;
  136. }
  137. template <class _RandomAccessIterator, class _UniformRandomNumberGenerator>
  138. _LIBCPP_HIDE_FROM_ABI void
  139. shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last, _UniformRandomNumberGenerator&& __g) {
  140. (void)std::__shuffle<_ClassicAlgPolicy>(
  141. std::move(__first), std::move(__last), std::forward<_UniformRandomNumberGenerator>(__g));
  142. }
  143. _LIBCPP_END_NAMESPACE_STD
  144. _LIBCPP_POP_MACROS
  145. #endif // _LIBCPP___ALGORITHM_SHUFFLE_H