Skip to content

Commit

Permalink
improve type stability of cat (#258)
Browse files Browse the repository at this point in the history
* improve type stability

* bump version

Co-authored-by: Alexander Plavin <alexander@plav.in>
  • Loading branch information
piever and aplavin authored Dec 10, 2022
1 parent 7446973 commit 4ea69d5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "StructArrays"
uuid = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
version = "0.6.13"
version = "0.6.14"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
9 changes: 5 additions & 4 deletions src/structarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,13 @@ function Base.sizehint!(s::StructArray, i::Integer)
end

for op in [:cat, :hcat, :vcat]
curried_op = Symbol(:curried, op)
@eval begin
function Base.$op(arg1::StructArray, argsrest::StructArray...; kwargs...)
args = (arg1, argsrest...)
f = key -> $op((getproperty(t, key) for t in args)...; kwargs...)
function Base.$op(arg::StructArray, others::StructArray...; kwargs...)
$curried_op(A...) = $op(A...; kwargs...)
args = (arg, others...)
T = mapreduce(eltype, promote_type, args)
StructArray{T}(map(f, propertynames(arg1)))
StructArray{T}(map($curried_op, map(components, args)...))
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,11 @@ end
horizontal_concat = StructArray{Pair{Int, String}}(([3 1; 5 6], ["a" "a"; "b" "b"]))
@test cat(t, t2; dims=2)::StructArray == horizontal_concat == hcat(t, t2)
@test hcat(t, t2) isa StructArray

# check that cat(dims=1) doesn't commit type piracy (#254)
t3 = StructArray(x=view([1], 1:1:1), y=view([:a], 1:1:1))
@test @inferred(vcat(t3)) == t3
@inferred vcat(t3, t3)
@inferred vcat(t3, collect(t3))
# Check that `cat(dims=1)` doesn't commit type piracy (#254)
# We only test that this works, the return value is immaterial
@test cat(dims=1) == vcat()
end
Expand Down

2 comments on commit 4ea69d5

@piever
Copy link
Collaborator Author

@piever piever commented on 4ea69d5 Dec 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/73855

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.14 -m "<description of version>" 4ea69d566f7b0ab447778a244832acb4436893f8
git push origin v0.6.14

Please sign in to comment.