Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix copyto! overload for arraypartitions of scalars #386

Merged
merged 1 commit into from
Jun 6, 2024
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
6 changes: 5 additions & 1 deletion src/array_partition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ for type in [AbstractArray, SparseArrays.AbstractCompressedVector, PermutedDimsA
@assert length(dest) == length(A)
cur = 1
@inbounds for i in 1:length(A.x)
dest[cur:(cur + length(A.x[i]) - 1)] .= vec(A.x[i])
if A.x[i] isa Number
dest[cur:(cur + length(A.x[i]) - 1)] .= A.x[i]
else
dest[cur:(cur + length(A.x[i]) - 1)] .= vec(A.x[i])
end
cur += length(A.x[i])
end
dest
Expand Down
8 changes: 7 additions & 1 deletion test/partitions_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ begin
end

@testset "Copy and zero with type changing array" begin
# Motivating use case for this is ArrayPartitions of Arrow arrays which are mmap:ed and change type when copied
# Motivating use case for this is ArrayPartitions of Arrow arrays which are mmap:ed and change type when copied
struct TypeChangingArray{T, N} <: AbstractArray{T, N} end
Base.copy(::TypeChangingArray{T, N}) where {T, N} = Array{T, N}(undef,
ntuple(_ -> 0, N))
Expand All @@ -281,3 +281,9 @@ end
@testset "Cartesian indexing" begin
@test ArrayPartition([1, 2], [3])[1:3, 1] == [1, 2, 3]
end

@testset "Scalar copyto!" begin
u = [2.0,1.0]
copyto!(u, ArrayPartition(1.0,-1.2))
@test u == [1.0,-1.2]
end
Loading