Skip to content
Open
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
18 changes: 4 additions & 14 deletions src/chainedvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -761,26 +761,16 @@ Base.map(f::F, x::ChainedVector) where {F} = ChainedVector([map(f, y) for y in x

function Base.map!(f::F, A::AbstractVector, x::ChainedVector) where {F}
length(A) >= length(x) || throw(ArgumentError("destination must be at least as long as map! source"))
idx = eachindex(A)
st = iterate(idx)
for array in x.arrays
for y in array
@inbounds A[st[1]] = f(y)
st = iterate(idx, st[2])
end
for (i, j) in zip(eachindex(A), eachindex(x))
@inbounds A[i] = f(x[j])
end
return A
end

function Base.map!(f::F, x::ChainedVector, A::AbstractVector) where {F}
length(x) >= length(A) || throw(ArgumentError("destination must be at least as long as map! source"))
idx = eachindex(A)
st = iterate(idx)
for array in x.arrays
for j in eachindex(array)
@inbounds array[j] = f(A[st[1]])
st = iterate(idx, st[2])
end
for (i, j) in zip(eachindex(x), eachindex(A))
@inbounds x[i] = f(A[j])
end
return x
end
Expand Down
3 changes: 3 additions & 0 deletions test/chainedvector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@
y = ChainedVector([[1,2,3], [4,5,6,7], [8,9,10]])
map!(x -> x + 1, x, y)
@test all(x -> x[1] + 1 == x[2], zip(y, x))
x = ChainedVector([[1,2,3], [4,5,6]])
y = [1,2]
@test map!(x -> x + 10, x, y)== [11,12,3,4,5,6]

# reductions
x = ChainedVector([[1,2,3], [4,5,6], [7,8,9,10]])
Expand Down
Loading