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___ALGORITHM_RANGES_UNIFORM_RANDOM_BIT_GENERATOR_ADAPTOR_H
10 #define _LIBCPP___ALGORITHM_RANGES_UNIFORM_RANDOM_BIT_GENERATOR_ADAPTOR_H
11 
12 #include <__config>
13 #include <__functional/invoke.h>
14 #include <__type_traits/remove_cvref.h>
15 
16 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17 #  pragma GCC system_header
18 #endif
19 
20 #if _LIBCPP_STD_VER >= 20
21 
22 _LIBCPP_PUSH_MACROS
23 #include <__undef_macros>
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 // Range versions of random algorithms (e.g. `std::shuffle`) are less constrained than their classic counterparts.
28 // Range algorithms only require the given generator to satisfy the `std::uniform_random_bit_generator` concept.
29 // Classic algorithms require the given generator to meet the uniform random bit generator requirements; these
30 // requirements include satisfying `std::uniform_random_bit_generator` and add a requirement for the generator to
31 // provide a nested `result_type` typedef (see `[rand.req.urng]`).
32 //
33 // To be able to reuse classic implementations, make the given generator meet the classic requirements by wrapping
34 // it into an adaptor type that forwards all of its interface and adds the required typedef.
35 template <class _Gen>
36 class _ClassicGenAdaptor {
37 private:
38   // The generator is not required to be copyable or movable, so it has to be stored as a reference.
39   _Gen& __gen_;
40 
41 public:
42   using result_type = invoke_result_t<_Gen&>;
43 
44   _LIBCPP_HIDE_FROM_ABI
45   static constexpr auto min() { return __remove_cvref_t<_Gen>::min(); }
46   _LIBCPP_HIDE_FROM_ABI
47   static constexpr auto max() { return __remove_cvref_t<_Gen>::max(); }
48 
49   _LIBCPP_HIDE_FROM_ABI
50   constexpr explicit _ClassicGenAdaptor(_Gen& __g) : __gen_(__g) {}
51 
52   _LIBCPP_HIDE_FROM_ABI
53   constexpr auto operator()() const { return __gen_(); }
54 };
55 
56 _LIBCPP_END_NAMESPACE_STD
57 
58 _LIBCPP_POP_MACROS
59 
60 #endif // _LIBCPP_STD_VER >= 20
61 
62 #endif // _LIBCPP___ALGORITHM_RANGES_UNIFORM_RANDOM_BIT_GENERATOR_ADAPTOR_H
63