Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3116,14 +3116,14 @@ end
function _ind2sub_recurse(inds, ind)
@inline
r1 = inds[1]
indnext, f, l = _div(ind, r1)
(ind-l*indnext+f, _ind2sub_recurse(tail(inds), indnext)...)
indnext, indsub = divrem(ind, _indexlength(r1))
(_lookup(indsub, r1), _ind2sub_recurse(tail(inds), indnext)...)
end

_indexlength(d::Integer) = d
_indexlength(r::AbstractUnitRange) = length(r)
_lookup(ind, d::Integer) = ind+1
_lookup(ind, r::AbstractUnitRange) = ind+first(r)
_div(ind, d::Integer) = div(ind, d), 1, d
_div(ind, r::AbstractUnitRange) = (d = length(r); (div(ind, d), first(r), d))

# Vectorized forms
function _sub2ind(inds::Indices{1}, I1::AbstractVector{T}, I::AbstractVector{T}...) where T<:Integer
Expand Down
19 changes: 11 additions & 8 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -673,21 +673,24 @@ module IteratorsMD
# CartesianPartition.
mi = iter.parent.mi
ci = iter.parent.parent
ax, ax1 = axes(ci), Base.axes1(ci)
subs = Base.ind2sub_rs(ax, mi, first(iter.indices[1]))
vl, fl = Base._sub2ind(tail(ax), tail(subs)...), subs[1]
vr, fr = divrem(last(iter.indices[1]) - 1, mi[end]) .+ (1, first(ax1))
ax1 = Base.axes1(ci)
function splitdim1(i, mi)
d, r = divrem(i - 1, mi)
d + 1, r + first(ax1)
end
vl, fl = splitdim1(first(iter.indices[1]), mi[1])
vr, fr = splitdim1(last(iter.indices[1]), mi[1])
# form the iterator for outer dimensions, equivalent to vec(oci), but mi is reused
oci = CartesianIndices(tail(ci.indices))
# A fake CartesianPartition to reuse the outer iterate fallback
outer = @inbounds view(ReshapedArray(oci, (length(oci),), mi), vl:vr)
init = @inbounds dec(oci[tail(subs)...].I, oci.indices) # real init state
roci = ReshapedArray(oci, (length(oci),), tail(mi))
outer = @inbounds view(roci, vl:vr)
# Use Generator to make inner loop branchless
@inline function skip_len_I(i::Int, I::CartesianIndex)
l = i == 1 ? fl : first(ax1)
r = i == length(outer) ? fr : last(ax1)
l - first(ax1), r - l + 1, I
end
(skip_len_I(i, I) for (i, I) in Iterators.enumerate(Iterators.rest(outer, (init, 0))))
(skip_len_I(i, I) for (i, I) in Iterators.enumerate(outer))
end
@inline function simd_outer_range(iter::CartesianPartition{CartesianIndex{2}})
# But for two-dimensional Partitions the above is just a simple one-dimensional range
Expand Down
16 changes: 8 additions & 8 deletions base/reshapedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ _reshape(R::ReshapedArray, dims::Dims) = _reshape(R.parent, dims)

function __reshape(p::Tuple{AbstractArray,IndexStyle}, dims::Dims)
parent = p[1]
strds = front(size_to_strides(map(length, axes(parent))..., 1))
strds1 = map(s->max(1,Int(s)), strds) # for resizing empty arrays
mi = map(SignedMultiplicativeInverse, strds1)
ReshapedArray(parent, dims, reverse(mi))
szs = front(size(parent))
szs1 = map(s -> max(1, Int(s)), szs) # for resizing empty arrays
mi = map(SignedMultiplicativeInverse, szs1)
ReshapedArray(parent, dims, mi)
end

function __reshape(p::Tuple{AbstractArray{<:Any,0},IndexCartesian}, dims::Dims)
Expand Down Expand Up @@ -269,11 +269,11 @@ mightalias(A::ReshapedArray, B::SubArray) = mightalias(parent(A), B)
mightalias(A::SubArray, B::ReshapedArray) = mightalias(A, parent(B))

@inline ind2sub_rs(ax, ::Tuple{}, i::Int) = (i,)
@inline ind2sub_rs(ax, strds, i) = _ind2sub_rs(ax, strds, i - 1)
@inline ind2sub_rs(ax, szs, i) = _ind2sub_rs(ax, szs, i - 1)
@inline _ind2sub_rs(ax, ::Tuple{}, ind) = (ind + first(ax[end]),)
@inline function _ind2sub_rs(ax, strds, ind)
d, r = divrem(ind, strds[1])
(_ind2sub_rs(front(ax), tail(strds), r)..., d + first(ax[end]))
@inline function _ind2sub_rs(ax, szs, ind)
d, r = divrem(ind, szs[1])
(r + first(ax[1]), _ind2sub_rs(tail(ax), tail(szs), d)...)
end
offset_if_vec(i::Integer, axs::Tuple{<:AbstractUnitRange}) = i + first(axs[1]) - 1
offset_if_vec(i::Integer, axs::Tuple) = i
Expand Down