// // Copyright (c) 2022 Klemens Morgenstern (klemens.morgenstern@gmx.net) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #ifndef BOOST_COBALT_RACE_HPP #define BOOST_COBALT_RACE_HPP #include #include #include #include namespace boost::cobalt { namespace detail { inline std::default_random_engine &prng() { thread_local static std::default_random_engine g(std::random_device{}()); return g; } } // tag::concept[] template concept uniform_random_bit_generator = requires ( G & g) { {typename std::decay_t::result_type() } -> std::unsigned_integral; // is an unsigned integer type // T Returns the smallest value that G's operator() may return. The value is strictly less than G::max(). The function must be constexpr. {std::decay_t::min()} -> std::same_as::result_type>; // T Returns the largest value that G's operator() may return. The value is strictly greater than G::min(). The function must be constexpr. {std::decay_t::max()} -> std::same_as::result_type>; {g()} -> std::same_as::result_type>; } && (std::decay_t::max() > std::decay_t::min()); // end::concept[] template ... Promise> auto race(URBG && g, Promise && ... p) -> detail::race_variadic_impl { return detail::race_variadic_impl(std::forward(g), static_cast(p)...); } template requires awaitable().begin())>, detail::fork::promise_type> auto race(URBG && g, PromiseRange && p) -> detail::race_ranged_impl { if (std::empty(p)) throw_exception(std::invalid_argument("empty range raceed")); return detail::race_ranged_impl{std::forward(g), static_cast(p)}; } template ... Promise> auto race(Promise && ... p) -> detail::race_variadic_impl { return race(detail::prng(), static_cast(p)...); } template requires awaitable().begin())>, detail::fork::promise_type> auto race(PromiseRange && p) -> detail::race_ranged_impl { if (std::empty(p)) throw_exception(std::invalid_argument("empty range raceed")); return race(detail::prng(), static_cast(p)); } template ... Promise> auto left_race(Promise && ... p) -> detail::race_variadic_impl { return detail::race_variadic_impl( detail::left_race_tag{}, static_cast(p)...); } template requires awaitable().begin())>, detail::fork::promise_type> auto left_race(PromiseRange && p) -> detail::race_ranged_impl { if (std::empty(p)) throw_exception(std::invalid_argument("empty range left_raceed")); return detail::race_ranged_impl{ detail::left_race_tag{}, static_cast(p)}; } } #endif //BOOST_COBALT_RACE_HPP