Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
weighted_selection.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <random>
5#include <stdexcept>
6#include <vector>
7
8template <typename T, size_t N> class WeightedSelectionConfig {
9 private:
11 size_t total_weight = 0;
12
13 public:
16 {
17 for (const auto& [option, weight] : options_with_weights) {
18 total_weight += weight;
19 }
20 }
21
24 {
25 size_t i = 0;
26 for (const auto& [option, weight] : options_with_weights) {
27 this->options_with_weights[i] = { option, weight };
28 total_weight += weight;
29 ++i;
30 }
31 }
32
33 T select(std::mt19937_64& rng) const
34 {
36 size_t selector = dist(rng);
37
38 for (const auto& [option, weight] : options_with_weights) {
39 if (selector < weight) {
40 return option;
41 }
42 selector -= weight;
43 }
44
45 throw std::runtime_error("Should have returned by now");
46 }
47};
constexpr WeightedSelectionConfig(const std::array< std::pair< T, size_t >, N > &options_with_weights)
T select(std::mt19937_64 &rng) const
constexpr WeightedSelectionConfig(std::initializer_list< std::pair< T, size_t > > options_with_weights)
std::array< std::pair< T, size_t >, N > options_with_weights
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13