Skip to content

Extending setindex for vector indexing #764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Micket opened this issue Apr 13, 2020 · 2 comments
Open

Extending setindex for vector indexing #764

Micket opened this issue Apr 13, 2020 · 2 comments
Labels
arrays AbstractArray interface and array operations

Comments

@Micket
Copy link

Micket commented Apr 13, 2020

Learning from my experiments on #745 i looked back at the missing support for vector indexing in setindex.

@inline function setindex(a::SVector{L}, v::AbstractArray, indices::AbstractArray) where L
    out = MVector(a)
    for (i, x) in zip(indices, v)
        # Do boundscheck here to allow Julia to fully elide the dynamic allocation.
        @boundscheck if (i < 1 || i > L)
            throw(BoundsError(a, i))
        end
        @inbounds out[i] = x
    end
    return SVector(out)
end

while we're at it, looks like setindex for scalars can easily be written this way as well.

@inline function setindex2(a::SVector{L}, x, index::Integer) where L
    out = MVector(a)
    # Do boundscheck here to allow Julia to fully elide the dynamic allocation.
    @boundscheck if (index < 1 || index > L)
        throw(BoundsError(a, index))
    end
    @inbounds out[index] = x
    return SVector(out)
end

Looking at assembly, it looks nicer for this implementation. I have had some mixed results trying to benchmark this; peaking at @code_native looked very efficient, but benchmark times still fell a bit short. Perhaps my benchmarks were lacking, or i missed something.

So is this interesting?

@c42f c42f added the arrays AbstractArray interface and array operations label Apr 14, 2020
@andyferris
Copy link
Member

It could be interesting. I had been waiting until the dust settles on non-scalar indexing in Julia where we may prefer a distinct generic function for non-scalar indexing (perhaps with syntax like a.[b] .= c) but there's no real reason to hold off too long...

Off topic... but do we really have an "arrays" label for StaticArrays? I'm amused. :)

@eschnett
Copy link
Contributor

setindex should also support range indexing (via SUnitRange).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays AbstractArray interface and array operations
Projects
None yet
Development

No branches or pull requests

4 participants