Skip to content

Commit

Permalink
Remove double scan sampling (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tortar authored Jan 10, 2024
1 parent 848f788 commit 5fe5d87
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 31 deletions.
21 changes: 1 addition & 20 deletions src/UnweightedSamplingMulti.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,8 @@ function choose(n, p, q, z)
return quantile(b, q)
end

function double_scan_sampling(rng, iter, n::Int, replace, ordered)
N = get_population_size(iter)
sortedindices_sample(rng, iter, n, N, replace, ordered)
end

function sortedindices_sample(rng, iter, n::Int, replace, ordered)
return sortedindices_sample(rng, iter, n, length(iter), replace, ordered)
end

function sortedindices_sample(rng, iter, n::Int, N::Int, replace, ordered)
N = length(iter)
if N <= n
reservoir = collect(iter)
if replace
Expand Down Expand Up @@ -240,17 +232,6 @@ function sortedindices_sample(rng, iter, n::Int, N::Int, replace, ordered)
end
end

function get_population_size(iter)
n = 0
it = iterate(iter)
while !isnothing(it)
n += 1
@inbounds state = it[2]
it = iterate(iter, state)
end
return n
end

function get_sorted_indices(rng, n, N, replace)
if replace == true
return sortedrandrange(rng, 1:N, n)
Expand Down
13 changes: 2 additions & 11 deletions src/UnweightedSamplingSingle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,8 @@ function reservoir_sample(rng, iter)
end
end

function double_scan_sampling(rng, iter)
N = get_population_size(iter)
sortedindices_sample(rng, iter, N)
end

function sortedindices_sample(rng, iter)
return sortedindices_sample(rng, iter, length(iter))
end

function sortedindices_sample(rng, iter, N)
k = rand(rng, 1:N)
function sortedindices_sample(rng, iter)
k = rand(rng, 1:length(iter))
for (i, x) in enumerate(iter)
i == k && return x
end
Expand Down

0 comments on commit 5fe5d87

Please sign in to comment.