Skip to content

Commit bd7644f

Browse files
eschnettjw3126
andauthored
Implement Base.setindex with CartesianIndex (#1034)
* Implement Base.setindex with CartesianIndex * Update src/deque.jl Co-authored-by: Jan Weidner <jw3126@gmail.com> * Implement setindex indexing via to_indices * White space change to trigger CI * Simplify code Co-authored-by: Jan Weidner <jw3126@gmail.com>
1 parent a2cca42 commit bd7644f

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ using LinearAlgebra
6464
using StaticArrays
6565

6666
# Use the convenience constructor type `SA` to create vectors and matrices
67-
SA[1, 2, 3] isa SVector{3,Int}
68-
SA_F64[1, 2, 3] isa SVector{3,Float64}
69-
SA_F32[1, 2, 3] isa SVector{3,Float32}
67+
SA[1, 2, 3] isa SVector{3,Int}
68+
SA_F64[1, 2, 3] isa SVector{3,Float64}
69+
SA_F32[1, 2, 3] isa SVector{3,Float32}
7070
SA[1 2; 3 4] isa SMatrix{2,2,Int}
7171
SA_F64[1 2; 3 4] isa SMatrix{2,2,Float64}
7272

src/deque.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,4 @@ julia> setindex(@SMatrix[2 4; 6 8], 1, 2)
195195
end
196196

197197
# TODO proper multidimension boundscheck
198-
@propagate_inbounds setindex(a::StaticArray, x, inds::Int...) = setindex(a, x, LinearIndices(a)[inds...])
198+
@propagate_inbounds setindex(a::StaticArray, x, inds...) = setindex(a, x, LinearIndices(a)[inds...])

test/deque.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ end
3434
@test @inferred(setindex(v1, 5.0, 2)) == @SVector [1., 5., 3.]
3535
@test_throws BoundsError setindex(v1, 5.0, 0)
3636
@test_throws BoundsError setindex(v1, 5.0, 4)
37+
@test @inferred(setindex(v1, 5, CartesianIndex(2))) == setindex(v1, 5, 2)
38+
@test @inferred(setindex(v1, 5.0, CartesianIndex(2))) == setindex(v1, 5.0, 2)
39+
@test_throws BoundsError setindex(v1, 5.0, CartesianIndex(0))
40+
@test_throws BoundsError setindex(v1, 5.0, CartesianIndex(4))
3741

3842
v2 = @SMatrix [1 2; 3 4]
3943
@test @inferred(setindex(v2, 7, 1)) == @SMatrix [7 2; 3 4]
@@ -46,11 +50,16 @@ end
4650
@test @inferred(setindex(v2, 7, 2, 2)) == @SMatrix [1 2; 3 7]
4751
@test_throws BoundsError setindex(v2, 7, 0)
4852
@test_throws BoundsError setindex(v2, 7, 5)
53+
@test @inferred(setindex(v2, 7, CartesianIndex(1, 1))) == setindex(v2, 7, 1, 1)
54+
@test @inferred(setindex(v2, 7, CartesianIndex(2, 1))) == setindex(v2, 7, 2, 1)
55+
@test @inferred(setindex(v2, 7, CartesianIndex(1, 2))) == setindex(v2, 7, 1, 2)
56+
@test @inferred(setindex(v2, 7, CartesianIndex(2, 2))) == setindex(v2, 7, 2, 2)
4957

5058
v3 = @SArray ones(2, 2, 2)
5159
@test @inferred(setindex(v3, 7, 2, 1, 2)) == reshape([1, 1, 1, 1, 1, 7, 1, 1], (2, 2, 2))
5260
@test_throws BoundsError setindex(v3, 7, 0)
5361
@test_throws BoundsError setindex(v3, 7, 9)
62+
@test @inferred(setindex(v3, 7, CartesianIndex(2, 1, 2))) == setindex(v3, 7, 2, 1, 2)
5463

5564
# TODO: still missing proper multidimensional bounds checking
5665
# These should throw BoundsError, but don't

0 commit comments

Comments
 (0)