Skip to content

Conversation

DanielVandH
Copy link
Contributor

Reverts #181

@dlfivefifty dlfivefifty merged commit 7d35a78 into JuliaArrays:master Jul 1, 2024
@cscherrer
Copy link

Curious why this was reverted. I haven't looked at your implementation, but this has been working great for me:

struct InfiniteSampleVector{S,M} <: AbstractInfiniteVector{S}
    rng::Xoshiro
    measures::M
    prefix::Vector{S}

    function InfiniteSampleVector(
        rng::R,
        ::Type{T},
        measures::M,
        sample = nothing,
    ) where {R<:AbstractRNG,T,M<:AbstractInfiniteVector}
        rng = sample_xoshiro(rng)
        if isnothing(sample)
            sample = rand(rng, T, measures[1])
        end
        S = typeof(sample)
        prefix = S[sample]
        new{S,M}(rng, measures, prefix)
    end
end

prefix(s::InfiniteSampleVector) = s.prefix

function default_element(s::InfiniteSampleVector, index::Int)
    rand(s.rng, s.measures[index])
end

This is my main use case, so I have an interface that works for it (haven't looked at yours)

abstract type AbstractInfiniteVector{T} <: AbstractVector{T} end

function extend!(v::AbstractInfiniteVector)
    push!(prefix(v), default_element(v, length(prefix(v)) + 1))
end

"""
    ensure_samples!(v::AbstractInfiniteVector, n::Int)

Ensure that at least n elements are explicitly stored in the vector.
"""
function ensure_samples!(v::AbstractInfiniteVector, n::Int)
    p = prefix(v)
    @assert isfinite(length(p))
    while length(p) < n
        extend!(v)
    end
end

function Base.getindex(v::AbstractInfiniteVector, n::Int)
    ensure_samples!(v, n)
    @inbounds prefix(v)[n]
end

function Base.getindex(v::AbstractInfiniteVector, r::AbstractUnitRange{<:Int})
    n = last(r)
    ensure_samples!(v, n)
    @inbounds prefix(v)[r]
end

function Base.setindex!(v::AbstractInfiniteVector, x, n::Int)
    ensure_samples!(v, n)
    @inbounds prefix(v)[n] = x
end

@DanielVandH
Copy link
Contributor Author

We moved it into infiniterandomarrays.jl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants