Skip to content

Commit

Permalink
add CartesianIndex() for Ref getindex and setindex! (#32653)
Browse files Browse the repository at this point in the history
* add CartesianIndex() for Ref getindex and setindex!

* fix typo

* add NEWS.md entry

* move definitions later in the load sequence
  • Loading branch information
mbauman authored Jul 23, 2019
2 parents 6e4acaa + 617bd1d commit 9daaed6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ New library functions
to search for that character in a string passed as the second argument ([#31664]).
* New `findall(pattern, string)` method where `pattern` is a string or regex ([#31834]).
* `istaskfailed` is now documented and exported, like its siblings `istaskdone` and `istaskstarted` ([#32300]).
* `RefArray` and `RefValue` objects now accept index `CartesianIndex()` in `getindex` and `setindex!` ([#32653])

Standard library changes
------------------------
Expand Down
3 changes: 3 additions & 0 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1743,3 +1743,6 @@ function _sortslices(A::AbstractArray, d::Val{dims}; kws...) where dims
B
end
end

getindex(b::Ref, ::CartesianIndex{0}) = getindex(b)
setindex!(b::Ref, x, ::CartesianIndex{0}) = setindex!(b, x)
8 changes: 8 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -987,3 +987,11 @@ end
@test_throws DimensionMismatch promote_shape(Dims((2, 2)), Dims((2,)))
@test_throws DimensionMismatch promote_shape(Dims((2, 3, 1)), Dims((2,2)))
end

@testset "getindex and setindex! for Ref" begin
for x in [Ref(1), Ref([1,2,3], 1)]
@test getindex(x) == getindex(x, CartesianIndex()) == 1
x[CartesianIndex()] = 10
@test getindex(x) == getindex(x, CartesianIndex()) == 10
end
end

2 comments on commit 9daaed6

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @ararslan

Please sign in to comment.