Skip to content

Commit bc9ced2

Browse files
Merge pull request #18 from JuliaDiffEq/mutation
don't assume mutation for generic setindex
2 parents 1b69942 + 013fd5e commit bc9ced2

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/ArrayInterface.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,22 @@ ismutable(::Type{<:Array}) = true
1919
ismutable(::Type{<:Number}) = false
2020

2121
# Piracy
22-
function Base.setindex(x::AbstractArray,v,i::Int)
22+
function Base.setindex(x::AbstractArray,v,i::Int...)
2323
_x = copy(x)
2424
_x[i] = v
2525
_x
2626
end
2727

28+
function Base.setindex(x::AbstractVector,v,i::Int)
29+
n = length(x)
30+
x .+ v .* (i .== 1:n)
31+
end
32+
33+
function Base.setindex(x::AbstractMatrix,v,i::Int,j::Int)
34+
n,m = size(x)
35+
x .+ v .* (i .== 1:n) .* (j .== i:m)'
36+
end
37+
2838
"""
2939
can_setindex(x::DataType)
3040

0 commit comments

Comments
 (0)