From 86a4ebc001c5747f7bcae4f585fb7c2b03b687af Mon Sep 17 00:00:00 2001 From: Tortar Date: Sun, 31 Dec 2023 17:29:20 +0100 Subject: [PATCH] always valid for stateful iterators --- src/IteratorSampling.jl | 41 ++++++++++++--------------------- src/UnweightedSamplingMulti.jl | 18 +++++---------- src/UnweightedSamplingSingle.jl | 12 ++++------ 3 files changed, 25 insertions(+), 46 deletions(-) diff --git a/src/IteratorSampling.jl b/src/IteratorSampling.jl index 4f35b4f..8fe4737 100644 --- a/src/IteratorSampling.jl +++ b/src/IteratorSampling.jl @@ -11,37 +11,26 @@ include("UnweightedSamplingSingle.jl") include("UnweightedSamplingMulti.jl") """ - itsample([rng], iter, [condition::Function]; [alloc]) + itsample([rng], iter) Return a random element of the iterator, optionally specifying a `rng` -(which defaults to `Random.GLOBAL_RNG`) and a condition to restrict the -sampling on only those elements for which the function returns `true`. -If the iterator is empty or no random element satisfies the condition, -it returns `nothing`. - -## Keywords -* `alloc = false`: this keyword chooses the algorithm to perform, if - `alloc = false` the algorithm doesn't allocate a new collection to - perform the sampling, which should be better when the number of elements is - large. +(which defaults to `Random.default_rng()`). If the iterator is empty, it +returns `nothing`. ----- - itsample([rng], iter, [condition::Function], n::Int; [alloc, iter_type]) - -Return a vector of `n` random elements of the iterator without replacement, -optionally specifying a `rng` (which defaults to `Random.GLOBAL_RNG`) and -a condition to restrict the sampling on only those elements for which the -function returns `true`. If the iterator has less than `n` elements or less -than `n` elements satisfy the condition, it returns a vector of these elements. - -## Keywords -* `alloc = true`: when the function returns a vector, it happens to be much - better to use the allocating version for small iterators. -* `iter_type = Any`: the iterator type of the given iterator, if not given - it defaults to `Any`, which means that the returned vector will be also of - `Any` type. For performance reasons, if you can infer the type of the iterator, - it is better to pass it. + itsample([rng], iter, n::Int; replace = true, ordered = false) + +Return a vector of `n` random elements of the iterator, +optionally specifying a `rng` (which defaults to `Random.default_rng()`). + +`replace` dictates whether sampling is performed with replacement. +`ordered` dictates whether an ordered sample (also called a sequential +sample, i.e. a sample where items appear in the same order as in `iter`). + +If the iterator has less than `n` elements, it returns a vector of +these elements. + """ function itsample end diff --git a/src/UnweightedSamplingMulti.jl b/src/UnweightedSamplingMulti.jl index dadcd87..6896af4 100644 --- a/src/UnweightedSamplingMulti.jl +++ b/src/UnweightedSamplingMulti.jl @@ -1,22 +1,16 @@ -function itsample(iter, n::Int; - replace = false, ordered = false, is_stateful = false) - return itsample(Random.GLOBAL_RNG, iter, n; - replace=replace, ordered=ordered, is_stateful=is_stateful) +function itsample(iter, n::Int; replace = false, ordered = false) + return itsample(Random.default_rng(), iter, n; replace=replace, ordered=ordered) end function itsample(rng::AbstractRNG, iter, n::Int; - replace = false, ordered = false, is_stateful = false) + replace = false, ordered = false) IterHasKnownSize = Base.IteratorSize(iter) if IterHasKnownSize isa NonIndexable - if is_stateful - if replace - error("Not implemented yet") - else - unweighted_resorvoir_sampling(rng, iter, n, Val(ordered)) - end + if replace + error("Not implemented yet") else - double_scan_sampling(rng, iter, n, replace, ordered) + unweighted_resorvoir_sampling(rng, iter, n, Val(ordered)) end else single_scan_sampling(rng, iter, n, replace, ordered) diff --git a/src/UnweightedSamplingSingle.jl b/src/UnweightedSamplingSingle.jl index 2a7c8fe..fadb45b 100644 --- a/src/UnweightedSamplingSingle.jl +++ b/src/UnweightedSamplingSingle.jl @@ -1,16 +1,12 @@ -function itsample(iter; is_stateful = false) - return itsample(Random.GLOBAL_RNG, iter; is_stateful = false) +function itsample(iter) + return itsample(Random.default_rng(), iter) end -function itsample(rng::AbstractRNG, iter; is_stateful = false) +function itsample(rng::AbstractRNG, iter) IterHasKnownSize = Base.IteratorSize(iter) if IterHasKnownSize isa NonIndexable - if is_stateful - unweighted_resorvoir_sampling(rng, iter) - else - double_scan_sampling(rng, iter) - end + return unweighted_resorvoir_sampling(rng, iter) else return single_scan_sampling(rng, iter) end