Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
25 changes: 12 additions & 13 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3355,9 +3355,9 @@ concatenate_setindex!(R, X::AbstractArray, I...) = (R[I...] = X)
## 1 argument

function map!(f::F, dest::AbstractArray, A::AbstractArray) where F
for (i,j) in zip(eachindex(dest),eachindex(A))
val = f(@inbounds A[j])
@inbounds dest[i] = val
inds = LinearIndices(A)
for i = inds
dest[i] = f(A[i])
end
return dest
end
Expand Down Expand Up @@ -3400,10 +3400,11 @@ map(f, ::AbstractSet) = error("map is not defined on sets")

## 2 argument
function map!(f::F, dest::AbstractArray, A::AbstractArray, B::AbstractArray) where F
for (i, j, k) in zip(eachindex(dest), eachindex(A), eachindex(B))
@inbounds a, b = A[j], B[k]
val = f(a, b)
@inbounds dest[i] = val
IL = IndexLinear()
inds = intersect(eachindex(IL, A), eachindex(IL, B))
@boundscheck checkbounds(dest, inds)
for i = inds
dest[i] = f(A[i], B[i])
end
return dest
end
Expand All @@ -3417,12 +3418,10 @@ function ith_all(i, as)
end

function map_n!(f::F, dest::AbstractArray, As) where F
idxs1 = LinearIndices(As[1])
@boundscheck LinearIndices(dest) == idxs1 && all(x -> LinearIndices(x) == idxs1, As)
for i = idxs1
@inbounds I = ith_all(i, As)
val = f(I...)
@inbounds dest[i] = val
inds = mapreduce(Fix1(eachindex, IndexLinear()), intersect, As)
@boundscheck checkbounds(dest, inds)
for i = inds
dest[i] = f(ith_all(i, As)...)
end
return dest
end
Expand Down
2 changes: 1 addition & 1 deletion base/reducedim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ copyfirst!(R::AbstractArray, A::AbstractArray) = mapfirst!(identity, R, A)
function mapfirst!(f::F, R::AbstractArray, A::AbstractArray{<:Any,N}) where {N, F}
lsiz = check_reducedims(R, A)
t = _firstreducedslice(axes(R), axes(A))
map!(f, R, view(A, t...))
map!(f, view(R, firstindex(R):lastindex(R)), view(A, t...))
end
# We know that the axes of R and A are compatible, but R might have a different number of
# dimensions than A, which is trickier than it seems due to offset arrays and type stability
Expand Down
19 changes: 19 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,25 @@ include("generic_map_tests.jl")
generic_map_tests(map, map!)
@test map!(-, [1]) == [-1]

@testset "#30624" begin
### unstructured
@test map!(+, ones(3), ones(3), ones(3), [1]) == [3, 1, 1]
@test map!(+, ones(3), [1], ones(3), ones(3)) == [3, 1, 1]
@test map!(+, [1], [1], [], []) == [1]
@test map!(+, [[1]], [1], [], []) == [[1]]

@test_throws BoundsError map!(+, ones(1), ones(2))
@test_throws BoundsError map!(+, ones(1), ones(2, 2))

@test map!(+, ones(3), view(ones(2, 3), 1:2, 2:3), ones(3)) == [2, 2, 2]
@test map!(+, ones(3), ones(2, 2), ones(3)) == [2, 2, 2]

### structured (all mapped arguments are <:AbstractArray equal ndims > 1)
@test map!(+, ones(4), ones(2, 2), ones(2, 2)) == [2, 2, 2, 2]
@test map!(+, ones(4), ones(2, 2), ones(1, 2)) == [2, 2, 1, 1]
@test_throws BoundsError map!(+, ones(3), ones(2, 2), ones(2, 2))
end

test_UInt_indexing(TestAbstractArray)
test_13315(TestAbstractArray)
test_checksquare()
Expand Down
2 changes: 2 additions & 0 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ map!(+, dest, am, am)
am = map(identity, a)
@test isa(am, OffsetArray)
@test am == a
@test_throws BoundsError map!(identity, fill(0, 1:3), fill(1, 2:4))


# https://github.com/JuliaArrays/OffsetArrays.jl/issues/106
@test isequal(map(!, OffsetArray([true,missing],2)), OffsetArray([false, missing], 2))
Expand Down
Loading