Skip to content

Commit

Permalink
invoke instead of duplicating getindex
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed Jun 27, 2022
1 parent 6f5c862 commit 9dd183d
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/SDiagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ SDiagonal(a::StaticMatrix{N,N,T}) where {N,T} = Diagonal(diag(a))
size(::Type{<:SDiagonal{N}}) where {N} = (N,N)
size(::Type{<:SDiagonal{N}}, d::Int) where {N} = d > 2 ? 1 : N

Base.axes(D::SDiagonal) = (ax = axes(diag(D), 1); (ax, ax))
Base.axes(D::SDiagonal, d) = d <= 2 ? axes(D)[d] : SOneTo(1)

Base.reshape(a::SDiagonal, s::Tuple{SOneTo,Vararg{SOneTo}}) = reshape(a, homogenize_shape(s))
Expand Down
12 changes: 2 additions & 10 deletions src/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -385,18 +385,10 @@ Base.SubArray(A::AbstractArray, indices::Tuple{StaticIndexing, Vararg{StaticInde
# SDiagonal uses Cartesian indexing, and the canonical indexing methods shadow getindex for Diagonal
# these are needed for ambiguity resolution
@inline function getindex(D::SDiagonal, i::Int, j::Int)
@boundscheck checkbounds(D, i, j)
if i == j
@inbounds r = diag(D)[i]
else
r = LinearAlgebra.diagzero(D, i, j)
end
r
invoke(getindex, Tuple{Diagonal, Int, Int}, D, i, j)
end
@inline function getindex(D::SDiagonal, i::Int...)
@boundscheck checkbounds(D, i...)
@inbounds r = D[eachindex(D)[i...]]
r
invoke(getindex, Tuple{Diagonal, Vararg{Int}}, D, i...)
end
# Ensure that vector indexing with static types lead to SArrays
@propagate_inbounds function getindex(a::SDiagonal, inds::Union{Int, StaticArray{<:Tuple, Int}, SOneTo, Colon}...)
Expand Down

0 comments on commit 9dd183d

Please sign in to comment.