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
19 changes: 13 additions & 6 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3417,12 +3417,19 @@ 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
idxs = LinearIndices(dest)
if all(x -> LinearIndices(x) == idxs, As)
for i in idxs
@inbounds as = ith_all(i, As)
val = f(as...)
@inbounds dest[i] = val
end
else
for (i, Is...) in zip(eachindex(dest), map(eachindex, As)...)
as = ntuple(j->getindex(As[j], Is[j]), length(As))
val = f(as...)
dest[i] = val
end
end
return dest
end
Expand Down
20 changes: 20 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,26 @@ 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]]

# TODO: decide if input axes & lengths should be validated
# @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