From e472b14f50888ed03729685027b9817b5fbda2ab Mon Sep 17 00:00:00 2001 From: Matt Bauman Date: Thu, 31 Oct 2019 15:05:10 -0500 Subject: [PATCH] NFC: style changes per review shorter lines, more spaces, and comments (because I had already forgotten how this worked myself) --- base/multidimensional.jl | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/base/multidimensional.jl b/base/multidimensional.jl index 1978f45da44e3..3745186f5cc4f 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -487,13 +487,21 @@ module IteratorsMD return I, (I, n+1) end - simd_outer_range(iter::CartesianPartition{CartesianIndex{2}}) = @inbounds CartesianIndices((iter[1][2]:iter[end][2],)) @inline function simd_outer_range(iter::CartesianPartition) + # In general, the Cartesian Partition might start and stop in the middle of the outer + # dimensions — thus the outer range of a CartesianPartition is itself a + # CartesianPartition. t = tail(iter.parent.parent.indices) ci = CartesianIndices(t) li = LinearIndices(t) return @inbounds view(ci, li[tail(iter[1].I)...]:li[tail(iter[end].I)...]) end + function simd_outer_range(iter::CartesianPartition{CartesianIndex{2}}) + # But for two-dimensional Partitions the above is just a simple one-dimensional range + # over the second dimension; we don't need to worry about non-rectangular staggers in + # higher dimensions. + return @inbounds CartesianIndices((iter[1][2]:iter[end][2],)) + end @inline function simd_inner_length(iter::CartesianPartition, I::CartesianIndex) inner = iter.parent.parent.indices[1] @inbounds fi = iter[1].I @@ -503,8 +511,12 @@ module IteratorsMD return inner_end - inner_start + 1 end @inline function simd_index(iter::CartesianPartition, Ilast::CartesianIndex, I1::Int) + # I1 is the 0-based distance from the first dimension's offest + offset = first(iter.parent.parent.indices[1]) # (this is 1 for 1-based arrays) + # In the first column we need to also add in the iter's starting point (branchlessly) f = @inbounds iter[1] - CartesianIndex((I1+first(iter.parent.parent.indices[1])+(Ilast.I==tail(f.I))*(f[1]-1), Ilast.I...)) + startoffset = (Ilast.I == tail(f.I))*(f[1] - 1) + CartesianIndex((I1 + offset + startoffset, Ilast.I...)) end end # IteratorsMD