30#ifndef vtkReservoirSampler_h
31#define vtkReservoirSampler_h
34#include "vtkCommonMathModule.h"
45 using SeedType =
typename std::random_device::result_type;
50template <
typename Integer,
bool Monotonic = true>
57 const std::vector<Integer>&
operator()(Integer kk, Integer nn)
const
59 VTK_THREAD_LOCAL
static std::vector<Integer> data;
69 VTK_THREAD_LOCAL
static std::vector<Integer> data;
72 throw std::invalid_argument(
"Null arrays are disallowed.");
76 throw std::invalid_argument(
"Array size would overflow integer type.");
83 void GenerateSample(Integer kk, Integer nn, std::vector<Integer>& data)
const
91 throw std::invalid_argument(
92 "You must choose a non-negative number of values from a proper sequence.");
101 for (ii = 0; ii < kk; ++ii)
111 std::uniform_real_distribution<> unitUniform(0., 1.);
112 std::uniform_int_distribution<Integer> randomIndex(0, kk - 1);
113 double w = exp(log(unitUniform(generator)) / kk);
117 Integer delta =
static_cast<Integer
>(floor(log(unitUniform(generator)) / log(1.0 - w)) + 1);
127 Integer jj = randomIndex(generator);
129 std::cout <<
" i " << ii <<
" δ " << delta <<
" w " << w <<
" → j " << jj <<
"\n";
133 w *= exp(log(unitUniform(generator)) / kk);
144 std::sort(data.begin(), data.end());
Abstract superclass for all arrays.
vtkIdType GetNumberOfTuples() const
Get the number of complete tuples (a component group) in the array.
static SeedType RandomSeed()
typename std::random_device::result_type SeedType
void GenerateSample(Integer kk, Integer nn, std::vector< Integer > &data) const
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).