Skip to content

Commit 8214b8f

Browse files
committed
Refactor eachstoredindex implementation
1 parent d316b9f commit 8214b8f

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/indexing.jl

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -308,18 +308,25 @@ end
308308
end
309309
end
310310

311+
@noinline function error_if_canonical_eachstoredindex(style::IndexStyle, A::AbstractArray)
312+
style === IndexStyle(A) && throw(Base.CanonicalIndexError("eachstoredindex", typeof(A)))
313+
return nothing
314+
end
315+
311316
# required: one implementation for canonical index style
312317
@interface ::AbstractSparseArrayInterface function eachstoredindex(
313318
style::IndexStyle, A::AbstractArray
314319
)
315-
if style == IndexStyle(A)
316-
throw(MethodError(eachstoredindex, Tuple{typeof(style),typeof(A)}))
317-
elseif style == IndexCartesian()
318-
return map(Base.Fix1(Base.getindex, CartesianIndices(A)), eachindex(A))
319-
elseif style == IndexLinear()
320-
return map(Base.Fix1(Base.getindex, LinearIndices(A)), eachindex(A))
320+
error_if_canonical_eachstoredindex(style, A)
321+
inds = eachstoredindex(A)
322+
if style === IndexCartesian()
323+
eltype(inds) === CartesianIndex{ndims(A)} && return inds
324+
return map(Base.Fix1(Base.getindex, CartesianIndices(A)), inds)
325+
elseif style === IndexLinear()
326+
eltype(inds) === Int && return inds
327+
return map(Base.Fix1(Base.getindex, LinearIndices(A)), inds)
321328
else
322-
throw(ArgumentError(lazy"unknown index style $style"))
329+
error(lazy"unkown index style $style")
323330
end
324331
end
325332

0 commit comments

Comments
 (0)