Skip to content

Commit

Permalink
Use const when possible (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tortar committed Apr 18, 2024
1 parent 12af11e commit 14011ab
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
35 changes: 19 additions & 16 deletions src/UnweightedSamplingMulti.jl
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@

mutable struct SampleMultiAlgR{T,R} <: AbstractWorReservoirSampleMulti
seen_k::Int
rng::R
value::Vector{T}
const rng::R
const value::Vector{T}
end

mutable struct SampleMultiOrdAlgR{T,R} <: AbstractOrdWorReservoirSampleMulti
seen_k::Int
rng::R
value::Vector{T}
ord::Vector{Int}
const rng::R
const value::Vector{T}
const ord::Vector{Int}
end

mutable struct SampleMultiAlgL{T,R} <: AbstractWorReservoirSampleMulti
state::Float64
skip_k::Int
seen_k::Int
rng::R
value::Vector{T}
const rng::R
const value::Vector{T}
end

mutable struct SampleMultiOrdAlgL{T,R} <: AbstractOrdWorReservoirSampleMulti
state::Float64
skip_k::Int
seen_k::Int
rng::R
value::Vector{T}
ord::Vector{Int}
const rng::R
const value::Vector{T}
const ord::Vector{Int}
end

mutable struct SampleMultiAlgRSWRSKIP{T,R} <: AbstractWrReservoirSampleMulti
skip_k::Int
seen_k::Int
rng::R
value::Vector{T}
const rng::R
const value::Vector{T}
end

mutable struct SampleMultiOrdAlgRSWRSKIP{T,R} <: AbstractOrdWrReservoirSampleMulti
skip_k::Int
seen_k::Int
rng::R
value::Vector{T}
ord::Vector{Int}
const rng::R
const value::Vector{T}
const ord::Vector{Int}
end

function ReservoirSample(T, n::Integer, method::ReservoirAlgorithm=algL; ordered = false)
Expand Down Expand Up @@ -109,7 +109,10 @@ end
@inbounds s.value[s.seen_k] = el
if s.seen_k == n
recompute_skip!(s, n)
s.value = sample(s.rng, s.value, n, ordered=is_ordered(s))
new_values = sample(s.rng, s.value, n, ordered=is_ordered(s))
@inbounds for i in 1:n
s.value[i] = new_values[i]
end
end
elseif s.skip_k < 0
p = 1/s.seen_k
Expand Down
5 changes: 2 additions & 3 deletions src/UnweightedSamplingSingle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
mutable struct SampleSingleAlgL{T,R} <: AbstractReservoirSample
state::Float64
skip_k::Int
rng::R
const rng::R
value::T
SampleSingleAlgL{T,R}(state, skip_k, rng) where {T,R} = new{T,R}(state, skip_k, rng)
end

mutable struct SampleSingleAlgR{T,R} <: AbstractReservoirSample
state::Int
rng::R
const rng::R
value::T
SampleSingleAlgR{T,R}(state, rng) where {T,R} = new{T,R}(state, rng)
end
Expand Down Expand Up @@ -54,7 +54,6 @@ end
function itsample(iter, method::ReservoirAlgorithm = algL)
return itsample(Random.default_rng(), iter, method)
end

function itsample(rng::AbstractRNG, iter, method::ReservoirAlgorithm = algL)
if Base.IteratorSize(iter) isa Base.SizeUnknown
return reservoir_sample(rng, iter, method)
Expand Down
29 changes: 16 additions & 13 deletions src/WeightedSamplingMulti.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,36 @@
mutable struct SampleMultiAlgARes{BH,R} <: AbstractWeightedWorReservoirSampleMulti
seen_k::Int
n::Int
rng::R
value::BH
const rng::R
const value::BH
end

mutable struct SampleMultiAlgAExpJ{BH,R} <: AbstractWeightedWorReservoirSampleMulti
state::Float64
min_priority::Float64
seen_k::Int
n::Int
rng::R
value::BH
const n::Int
const rng::R
const value::BH
end

mutable struct SampleMultiAlgWRSWRSKIP{T,R} <: AbstractWeightedWrReservoirSampleMulti
state::Float64
skip_w::Float64
seen_k::Int
rng::R
weights::Vector{Float64}
value::Vector{T}
const rng::R
const weights::Vector{Float64}
const value::Vector{T}
end

mutable struct SampleMultiOrdAlgWRSWRSKIP{T,R} <: AbstractWeightedWrReservoirSampleMulti
state::Float64
skip_w::Float64
seen_k::Int
rng::R
weights::Vector{Float64}
value::Vector{T}
ord::Vector{Int}
const rng::R
const weights::Vector{Float64}
const value::Vector{T}
const ord::Vector{Int}
end

function ReservoirSample(rng::AbstractRNG, T, n::Integer, method::AlgAExpJ; ordered = false)
Expand Down Expand Up @@ -102,7 +102,10 @@ end
@inbounds s.value[s.seen_k] = el
@inbounds s.weights[s.seen_k] = w
if s.seen_k == n
s.value = sample(s.rng, s.value, weights(s.weights), n; ordered = is_ordered(s))
new_values = sample(s.rng, s.value, weights(s.weights), n; ordered = is_ordered(s))
@inbounds for i in 1:n
s.value[i] = new_values[i]
end
@inline recompute_skip!(s, n)
empty!(s.weights)
end
Expand Down
2 changes: 1 addition & 1 deletion src/WeightedSamplingSingle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
mutable struct SampleSingleAlgAExpJ{T,R} <: AbstractReservoirSample
state::Float64
skip_w::Float64
rng::R
const rng::R
value::T
SampleSingleAlgAExpJ{T,R}(state, skip_w, rng) where {T,R} = new{T,R}(state, skip_w, rng)
end
Expand Down

0 comments on commit 14011ab

Please sign in to comment.