@@ -25,7 +25,7 @@ struct Slices{P,SM,AX,S,N} <: AbstractSlices{S,N}
2525 """
2626 parent:: P
2727 """
28- A tuple of length `ndims(parent)`, denoting how each dimension should be handled:
28+ A tuple of length at least `ndims(parent)`, denoting how each dimension should be handled:
2929 - an integer `i`: this is the `i`th dimension of the outer `Slices` object.
3030 - `:`: an "inner" dimension
3131 """
3939unitaxis (:: AbstractArray ) = Base. OneTo (1 )
4040
4141function Slices (A:: P , slicemap:: SM , ax:: AX ) where {P,SM,AX}
42+ length (slicemap) >= ndims (A) ||
43+ throw (ArgumentError (" Slices cannot be constructed with a slicemap of fewer elements than the parent has dimensions" ))
4244 N = length (ax)
43- argT = map ((a,l) -> l === (:) ? Colon : eltype (a), axes (A), slicemap)
45+ parent_axes = ntuple (d -> axes (A, d), length (slicemap))
46+ argT = map ((a,l) -> l === (:) ? Colon : eltype (a), parent_axes, slicemap)
4447 S = Base. promote_op (view, P, argT... )
4548 Slices {P,SM,AX,S,N} (A, slicemap, ax)
4649end
4750
4851_slice_check_dims (N) = nothing
4952function _slice_check_dims (N, dim, dims... )
50- 1 <= dim <= N || throw (DimensionMismatch (" Invalid dimension $dim " ))
53+ 1 <= dim || throw (DimensionMismatch (" Invalid dimension $dim " ))
5154 dim in dims && throw (DimensionMismatch (" Dimensions $dims are not unique" ))
5255 _slice_check_dims (N,dims... )
5356end
5457
5558@constprop :aggressive function _eachslice (A:: AbstractArray{T,N} , dims:: NTuple{M,Integer} , drop:: Bool ) where {T,N,M}
5659 _slice_check_dims (N,dims... )
60+ N_ = foldl (max, dims; init= N)
61+
5762 if drop
5863 # if N = 4, dims = (3,1) then
5964 # axes = (axes(A,3), axes(A,1))
6065 # slicemap = (2, :, 1, :)
6166 ax = map (dim -> axes (A,dim), dims)
62- slicemap = ntuple (dim -> something (findfirst (isequal (dim), dims), (:)), N )
67+ slicemap = ntuple (dim -> something (findfirst (isequal (dim), dims), (:)), N_ )
6368 return Slices (A, slicemap, ax)
6469 else
6570 # if N = 4, dims = (3,1) then
6671 # axes = (axes(A,1), OneTo(1), axes(A,3), OneTo(1))
6772 # slicemap = (1, :, 3, :)
68- ax = ntuple (dim -> dim in dims ? axes (A,dim) : unitaxis (A), N )
69- slicemap = ntuple (dim -> dim in dims ? dim : (:), N )
73+ ax = ntuple (dim -> dim in dims ? axes (A,dim) : unitaxis (A), N_ )
74+ slicemap = ntuple (dim -> dim in dims ? dim : (:), N_ )
7075 return Slices (A, slicemap, ax)
7176 end
7277end
0 commit comments