Celeritas 0.7.0-dev.127+develop.e63889793
|
On-the-fly selection of a weighted discrete distribution. More...
#include <Selector.hh>
Public Types | |
Type aliases | |
using | arg_type = T |
using | real_type = typename std::invoke_result< F, arg_type >::type |
Public Member Functions | |
CELER_FUNCTION | Selector (F &&eval, arg_type size, real_type total, SelectorNormalization) |
Construct with function, size, and accumulated value. | |
template<class Engine > | |
CELER_FUNCTION T | operator() (Engine &rng) const |
Sample from the distribution. | |
On-the-fly selection of a weighted discrete distribution.
This algorithm encapsulates the loop for sampling from distributions of a function f(index) -> real
(usually with the function prototype real_type(*)
(size_type) ). The index can be an integer, an enumeration, or an OpaqueId . The Selector is constructed with the size of the distribution (but using the indexing type).
Edge cases are thoroughly tested (it will never iterate off the end if normalized, even for incorrect values of the "total" probability/xs), and it uses one fewer register than the typical accumulation algorithm. When building with debug checking and normalization, the constructor asserts that the provided "total" value is consistent.
The given function must return a consistent value for the same given argument.
or
Create a normalized selector from a function and total accumulated value.
F | function type to sample from |
T | index type passed to the function |
|
inline |
Construct with function, size, and accumulated value.
The SelectorNormalization
argument determines whether to allow an "off
the end" value. If unnormalized, the sum of all evaluated arguments can be nontrivially less than the given total.