146 throw std::invalid_argument{
"Data is empty."};
149 const auto sumOfWeights =
150 std::accumulate(data.begin(), data.end(), 0u,
151 [](
unsigned sum,
const WeightedElement<T>& element) { return sum + element.weight; });
153 if (sumOfWeights == 0u)
155 throw std::invalid_argument{
"Sum of weights is zero."};
158 const std::integral
auto targetWeightValue = number::integer<unsigned>(1, sumOfWeights);
160 unsigned currentSum = 0;
161 for (
const auto& elem : data)
163 currentSum += elem.weight;
164 if (currentSum >= targetWeightValue)
170 return data.back().value;