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
8
template
<
typename
T,
size_t
N>
class
WeightedSelectionConfig
{
9
private
:
10
std::array<std::pair<T, size_t>
, N>
options_with_weights
;
11
size_t
total_weight
= 0;
12
13
public
:
14
constexpr
WeightedSelectionConfig
(
const
std::array<
std::pair<T, size_t>
, N>&
options_with_weights
)
15
:
options_with_weights
(
options_with_weights
)
16
{
17
for
(
const
auto
& [option, weight] :
options_with_weights
) {
18
total_weight
+= weight;
19
}
20
}
21
22
constexpr
WeightedSelectionConfig
(
std::initializer_list
<
std::pair<T, size_t>
>
options_with_weights
)
23
:
options_with_weights
()
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
{
35
std::uniform_int_distribution<size_t>
dist(0,
total_weight
- 1);
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
};
WeightedSelectionConfig
Definition
weighted_selection.hpp:8
WeightedSelectionConfig::WeightedSelectionConfig
constexpr WeightedSelectionConfig(const std::array< std::pair< T, size_t >, N > &options_with_weights)
Definition
weighted_selection.hpp:14
WeightedSelectionConfig::select
T select(std::mt19937_64 &rng) const
Definition
weighted_selection.hpp:33
WeightedSelectionConfig::WeightedSelectionConfig
constexpr WeightedSelectionConfig(std::initializer_list< std::pair< T, size_t > > options_with_weights)
Definition
weighted_selection.hpp:22
WeightedSelectionConfig::options_with_weights
std::array< std::pair< T, size_t >, N > options_with_weights
Definition
weighted_selection.hpp:10
WeightedSelectionConfig::total_weight
size_t total_weight
Definition
weighted_selection.hpp:11
std::get
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition
tuple.hpp:13
src
barretenberg
avm_fuzzer
common
weighted_selection.hpp
Generated by
1.9.8