33 #ifndef vtkReservoirSampler_h
34 #define vtkReservoirSampler_h
37 #include "vtkCommonMathModule.h"
48 using SeedType =
typename std::random_device::result_type;
53 template <
typename Integer,
bool Monotonic = true>
60 const std::vector<Integer>&
operator()(Integer kk, Integer nn)
const
62 VTK_THREAD_LOCAL
static std::vector<Integer>
data;
72 VTK_THREAD_LOCAL
static std::vector<Integer>
data;
75 throw std::invalid_argument(
"Null arrays are disallowed.");
79 throw std::invalid_argument(
"Array size would overflow integer type.");
94 throw std::invalid_argument(
95 "You must choose a non-negative number of values from a proper sequence.");
104 for (ii = 0; ii < kk; ++ii)
114 std::uniform_real_distribution<> unitUniform(0., 1.);
115 std::uniform_int_distribution<Integer> randomIndex(0, kk - 1);
116 double w = exp(log(unitUniform(generator)) / kk);
120 Integer delta =
static_cast<Integer
>(floor(log(unitUniform(generator)) / log(1.0 - w)) + 1);
130 Integer jj = randomIndex(generator);
132 std::cout <<
" i " << ii <<
" δ " << delta <<
" w " << w <<
" → j " << jj <<
"\n";
136 w *= exp(log(unitUniform(generator)) / kk);
147 std::sort(data.begin(), data.end());
152 #endif // vtkReservoirSampler_h
Abstract superclass for all arrays.
static SeedType RandomSeed()
typename std::random_device::result_type SeedType
const std::vector< Integer > & operator()(Integer kk, Integer nn) const
Choose kk items from a sequence of (0, nn - 1).
const std::vector< Integer > & operator()(Integer kk, vtkAbstractArray *array) const
Choose kk items from a sequence of (0, array->GetNumberOfTuples() - 1).
vtkIdType GetNumberOfTuples() const
Get the number of complete tuples (a component group) in the array.
void GenerateSample(Integer kk, Integer nn, std::vector< Integer > &data) const