|
|
| weighted_table ()=default |
| | An empty table.
|
| |
| | weighted_table (std::initializer_list< entry > entries) |
| | A table from entries.
|
| |
| weighted_table & | add (T value, double weight) |
| | Append an entry.
|
| |
| std::size_t | size () const noexcept |
| | Number of entries.
|
| |
| bool | empty () const noexcept |
| | Whether there are no entries.
|
| |
| std::span< const entry > | entries () const noexcept |
| | The entries, in order.
|
| |
| double | total_weight () const noexcept |
| | Sum of the base weights.
|
| |
| std::size_t | pick_index (rng &r) const |
| | Pick an index by base weight. Consumes one engine value.
|
| |
| const T & | pick (rng &r) const |
| | Pick a value by base weight. Consumes one engine value.
|
| |
| template<std::invocable< const T &, double > F> |
| std::optional< std::size_t > | try_pick_index (rng &r, const F &adjust) const |
| | Pick an index with adjusted weights, or nothing when every adjusted weight is 0. Always consumes one engine value.
|
| |
| template<std::invocable< const T &, double > F> |
| const T * | try_pick (rng &r, const F &adjust) const |
| | Pick a value with adjusted weights, or nothing when every adjusted weight is 0. Always consumes one engine value.
|
| |
| template<std::invocable< const T &, double > F> |
| const T & | pick (rng &r, const F &adjust) const |
| | Pick a value with adjusted weights. Consumes one engine value.
|
| |
| template<std::invocable< const T &, double > F> |
| std::vector< std::size_t > | pick_distinct_indices (rng &r, std::size_t k, const F &adjust) const |
| | Pick up to k distinct indices, each draw by adjusted weight among the entries not yet chosen.
|
| |
| std::vector< std::size_t > | pick_distinct_indices (rng &r, std::size_t k) const |
| | Pick up to k distinct indices by base weight.
|
| |
template<typename T>
class dasmig::core::weighted_table< T >
Values with weights, picked with probability proportional to the weight.
A pick uses the same algorithm as discrete (cumulative sums in entry order, one engine value), so entry order is part of the output.
Weights can be adjusted per call by a function of the entry and its base weight, which is how context conditions a choice: return the base weight to keep it, a multiple to favour it, or 0 to exclude it.
const auto& job = jobs.
pick(r);
const auto& adult_job = jobs.pick(r, [&](const std::string& j, double w) {
return (j == "pilot" && age < 21) ? 0.0 : w;
});
Values with weights, picked with probability proportional to the weight.
const T & pick(rng &r) const
Pick a value by base weight. Consumes one engine value.
- Template Parameters
-
Definition at line 50 of file weighted_table.hpp.
template<typename T >
template<std::invocable< const T &, double > F>
Pick up to k distinct indices, each draw by adjusted weight among the entries not yet chosen.
A draw where no remaining entry has a positive weight chooses nothing, so the result can be shorter than k. Consumes exactly k engine values either way.
- Template Parameters
-
- Parameters
-
| r | Engine. |
| k | Number of entries wanted. |
| adjust | Returns the weight to use (0 excludes the entry). |
- Returns
- The chosen indices, in the order drawn.
- Exceptions
-
Definition at line 218 of file weighted_table.hpp.