From e6918187568dbd01842d8d1d2c808ce16a894239 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 21 Apr 2024 13:54:28 +0200 Subject: Adding upstream version 18.2.2. Signed-off-by: Daniel Baumann --- src/common/weighted_shuffle.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/common/weighted_shuffle.h (limited to 'src/common/weighted_shuffle.h') diff --git a/src/common/weighted_shuffle.h b/src/common/weighted_shuffle.h new file mode 100644 index 000000000..10def0a01 --- /dev/null +++ b/src/common/weighted_shuffle.h @@ -0,0 +1,25 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#pragma once + +#include +#include +#include + +template +void weighted_shuffle(RandomIt first, RandomIt last, + DistIt weight_first, DistIt weight_last, + URBG &&g) +{ + if (first == last) { + return; + } else { + std::discrete_distribution d{weight_first, weight_last}; + if (auto n = d(g); n > 0) { + std::iter_swap(first, std::next(first, n)); + std::iter_swap(weight_first, std::next(weight_first, n)); + } + weighted_shuffle(++first, last, ++weight_first, weight_last, std::move(g)); + } +} -- cgit v1.2.3