Skip to content

Commit a0fbd02

Browse files
committed
Simplify and fix return type of get and set index.
1 parent 16dc24d commit a0fbd02

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

src/host/indexing.jl

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,26 +82,18 @@ end
8282

8383
Base.IndexStyle(::Type{<:AbstractGPUArray}) = Base.IndexLinear()
8484

85-
function _getindex(xs::AbstractGPUArray{T}, i::Integer) where T
85+
function Base.getindex(xs::AbstractGPUArray{T}, i::Integer) where T
86+
ndims(xs) > 0 && assertscalar("scalar getindex")
8687
x = Array{T}(undef, 1)
8788
copyto!(x, 1, xs, i, 1)
8889
return x[1]
8990
end
9091

91-
function Base.getindex(xs::AbstractGPUArray{T}, i::Integer) where T
92-
ndims(xs) > 0 && assertscalar("scalar getindex")
93-
_getindex(xs, i)
94-
end
95-
96-
function _setindex!(xs::AbstractGPUArray{T}, v::T, i::Integer) where T
97-
x = T[v]
98-
copyto!(xs, i, x, 1, 1)
99-
return v
100-
end
101-
10292
function Base.setindex!(xs::AbstractGPUArray{T}, v::T, i::Integer) where T
10393
assertscalar("scalar setindex!")
104-
_setindex!(xs, v, i)
94+
x = T[v]
95+
copyto!(xs, i, x, 1, 1)
96+
return xs
10597
end
10698

10799
Base.setindex!(xs::AbstractGPUArray, v, i::Integer) = xs[i] = convert(eltype(xs), v)

test/testsuite/indexing.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,11 @@ function test_indexing(AT)
6262
@test Array(A) == Ac
6363
end
6464
end
65+
66+
@testset "get/setindex!" begin
67+
# literal calls to get/setindex! have differen return types
68+
@test compare(x->getindex(x,1), AT, zeros(Int, 2))
69+
@test compare(x->setindex!(x,1,1), AT, zeros(Int, 2))
70+
end
6571
end
6672
end

0 commit comments

Comments
 (0)