shuffle.h 5.4 KB

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