One-element vector (w/o allocations)

Is there a package for a non-allocating representation of a single-element vector? I am basically thinking of the equivalent of

struct OneElementVector{T} <: AbstractVector{T}
    x::T
end

Base.size(v::OneElementVector) = (1, )

Base.getindex(v::OneElementVector, i::Int) = i == 1 ? v.x : throw(BoundsError(v, i))

Base.RefValue is not the same because it requires no index,while StaticArrays.SVector(x) is overkill.

1 Like
2 Likes

SVector{1,T} from StaticArrays.jl would also work for this, but might be overkill for your needs.

1 Like