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

Add built-in support to write compound data types #1013

Merged
merged 25 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions src/HDF5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export @read,
open_dataset,
read_dataset,
write_dataset,
write_compound_dataset,
tamasgal marked this conversation as resolved.
Show resolved Hide resolved
create_group,
open_group,
copy_object,
Expand Down
9 changes: 9 additions & 0 deletions src/typeconversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ function datatype(str::VLen{C}) where {C<:CharType}
Datatype(API.h5t_vlen_create(type_id))
end

# Compound types
function datatype(A::AbstractArray{T}) where {T}
tamasgal marked this conversation as resolved.
Show resolved Hide resolved
dtype = API.h5t_create(API.H5T_COMPOUND, sizeof(T))
for (idx, fn) in enumerate(fieldnames(T))
API.h5t_insert(dtype, fn, fieldoffset(T, idx), datatype(fieldtype(T, idx)))
end
Datatype(dtype)
end

# Opaque types
struct Opaque
data
Expand Down
33 changes: 33 additions & 0 deletions test/compound.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,36 @@ end
@test HDF5.do_reclaim(TTTT) == false
@test HDF5.do_normalize(TTTT) == true
end

struct Bar
a::Int32
b::Float64
c::Bool
end

@testset "write_compound" begin
bars = [
[Bar(1, 1.1, true) Bar(2, 2.1, false) Bar(3, 3.1, true)]
[Bar(4, 4.1, false) Bar(5, 5.1, true) Bar(6, 6.1, false)]
]

fn = tempname()
h5open(fn, "w") do h5f
write_dataset(h5f, "the/bar", bars)
end

thebars = h5open(fn, "r") do h5f
read(h5f, "the/bar")
end

@test (2, 3) == size(thebars)
@test thebars[1, 2].b ≈ 2.1
@test thebars[2, 1].a == 4
@test thebars[1, 3].c

thebars_r = reinterpret(Bar, thebars)
@test (2, 3) == size(thebars_r)
@test thebars_r[1, 2].b ≈ 2.1
@test thebars_r[2, 1].a == 4
@test thebars_r[1, 3].c
end