Skip to content

Commit

Permalink
apply code review
Browse files Browse the repository at this point in the history
  • Loading branch information
adienes committed Nov 28, 2024
1 parent e6acfa9 commit 592d233
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
27 changes: 11 additions & 16 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3400,9 +3400,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 @@ -3445,10 +3445,10 @@ 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
inds = intersect(eachindex(LinearIndices(A)), eachindex(LinearIndices(B)))
@boundscheck checkbounds(dest, inds)
for i = inds
dest[i] = f(A[i], B[i])
end
return dest
end
Expand All @@ -3462,15 +3462,10 @@ function ith_all(i, as)
end

function map_n!(f::F, dest::AbstractArray, As) where F
idxs1 = eachindex(LinearIndices(As[1]))
@boundscheck begin
idxs1 = intersect(map(eachindex LinearIndices, As)...)
checkbounds(dest, idxs1)
end
for i = idxs1
@inbounds I = ith_all(i, As)
val = f(I...)
@inbounds dest[i] = val
inds = mapreduce(eachindex LinearIndices, intersect, As)
@boundscheck checkbounds(dest, inds)
for i = inds
dest[i] = f(ith_all(i, As)...)
end
return dest
end
Expand Down
21 changes: 19 additions & 2 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -910,8 +910,25 @@ include("generic_map_tests.jl")
generic_map_tests(map, map!)
@test_throws ArgumentError map!(-, [1])

# Issue #30624
@test map!(+, [0,0,0], [1,2], [10,20,30], [100]) == [111,0,0]
function test_30624()
### 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_30624()

test_UInt_indexing(TestAbstractArray)
test_13315(TestAbstractArray)
Expand Down

0 comments on commit 592d233

Please sign in to comment.