Last active
April 29, 2017 11:40
-
-
Save deque-blog/b93b876eb2e57f67e412ff02af19f5cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename Finalizer, typename Generator, typename... Generators> | |
auto weighted_one_of_gen( | |
Finalizer finalizer, Weighted<Generator> head, Weighted<Generators>... tail) | |
{ | |
using Out = decltype(finalizer(head.m_value(std::declval<std::mt19937&>()))); | |
using OutGen = std::function<Out(std::mt19937&)>; | |
auto map_first = [&](auto&& wg) -> Weighted<OutGen> | |
{ | |
return Weighted<OutGen>{transform_gen(finalizer, wg.m_value), wg.m_weight}; | |
}; | |
std::vector<Weighted<OutGen>> weighted_gens{ map_first(head), map_first(tail)... }; | |
auto generator_picker = weighted_choice_gen(weighted_gens); | |
return [=](std::mt19937& bit_gen) | |
{ | |
return generator_picker(bit_gen)(bit_gen); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment