Skip to content

Commit 0f88f6f

Browse files
alternative get and set index
1 parent b0e4d48 commit 0f88f6f

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/ArrayInterface.jl

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ ismutable(x) = ismutable(typeof(x))
1818
ismutable(::Type{<:Array}) = true
1919
ismutable(::Type{<:Number}) = false
2020

21+
# Piracy
22+
function Base.setindex(x::Array,v,i::Int)
23+
_x = copy(x)
24+
_x[i] = v
25+
_x
26+
end
27+
2128
"""
2229
can_setindex(x::DataType)
2330
@@ -34,6 +41,20 @@ Query whether an array type has fast scalar indexing
3441
fast_scalar_indexing(x) = true
3542
fast_scalar_indexing(x::AbstractArray) = fast_scalar_indexing(typeof(x))
3643

44+
"""
45+
allowed_getindex(x,i...)
46+
47+
A scalar getindex which is always allowed
48+
"""
49+
allowed_getindex(x,i...) = x[i...]
50+
51+
"""
52+
allowed_setindex!(x,v,i...)
53+
54+
A scalar setindex! which is always allowed
55+
"""
56+
allowed_setindex!(x,v,i...) = setindex!(x,v,i...)
57+
3758
"""
3859
isstructured(x::DataType)
3960
@@ -193,12 +214,20 @@ function __init__()
193214

194215
@require CuArrays="3a865a2d-5b23-5a0f-bc46-62713ec82fae" begin
195216
fast_scalar_indexing(::Type{<:CuArrays.CuArray}) = false
217+
@inline allowed_getindex(x::CuArrays.CuArray,i...) = CuArrays._getindex(x,i...)
218+
@inline allowed_setindex!(x::CuArrays.CuArray,v,i...) = CuArrays._setindex!(x,v,i...)
219+
220+
function Base.setindex(x::CuArrays.CuArray,v,i::Int)
221+
_x = copy(x)
222+
allowed_setindex!(_x,v,i)
223+
_x
224+
end
196225
end
197226

198227
@require BandedMatrices="aae01518-5342-5314-be14-df237901396f" begin
199228
is_structured(::Type{<:BandedMatrices.BandedMatrix}) = true
200229
fast_matrix_colors(::Type{<:BandedMatrices.BandedMatrix}) = true
201-
230+
202231
function matrix_colors(A::BandedMatrices.BandedMatrix)
203232
u,l=bandwidths(A)
204233
width=u+l+1

0 commit comments

Comments
 (0)