Skip to content

Commit fe189fb

Browse files
authored
Merge pull request #469 from JuliaIO/bja/array
support AbstractArrays
2 parents 90d011a + 4e9190c commit fe189fb

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

src/HDF5.jl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,7 @@ for (privatesym, fsym, ptype) in
16141614
end
16151615
# Scalar types
16161616
($fsym)(parent::$ptype, name::String, data::Union{T, AbstractArray{T}}, plists...) where {T<:ScalarOrString} =
1617-
($privatesym)(parent, name, data, plists...)
1617+
($privatesym)(parent, name, isa(data,AbstractArray) ? collect(data) : data, plists...)
16181618
# VLEN types
16191619
($fsym)(parent::$ptype, name::String, data::HDF5Vlen{T}, plists...) where {T<:Union{HDF5Scalar,CharType}} =
16201620
($privatesym)(parent, name, data, plists...)
@@ -1637,7 +1637,7 @@ for (privatesym, fsym, ptype, crsym) in
16371637
end
16381638
# Scalar types
16391639
($fsym)(parent::$ptype, name::String, data::Union{T, AbstractArray{T}}, plists...) where {T<:ScalarOrString} =
1640-
($privatesym)(parent, name, data, plists...)
1640+
($privatesym)(parent, name, isa(data,AbstractArray) ? collect(data) : data, plists...)
16411641
# VLEN types
16421642
($fsym)(parent::$ptype, name::String, data::HDF5Vlen{T}, plists...) where {T<:Union{HDF5Scalar,CharType}} =
16431643
($privatesym)(parent, name, data, plists...)
@@ -1664,9 +1664,10 @@ function write(obj::DatasetOrAttribute, data::HDF5Vlen{T}) where {T<:Union{HDF5S
16641664
end
16651665
# For plain files and groups, let "write(obj, name, val)" mean "d_write"
16661666
write(parent::Union{HDF5File, HDF5Group}, name::String, data::Union{T, AbstractArray{T}}, plists...) where {T<:ScalarOrString} =
1667-
d_write(parent, name, data, plists...)
1667+
d_write(parent, name, isa(data,AbstractArray) ? collect(data) : data, plists...)
16681668
# For datasets, "write(dset, name, val)" means "a_write"
1669-
write(parent::HDF5Dataset, name::String, data::Union{T, AbstractArray{T}}, plists...) where {T<:ScalarOrString} = a_write(parent, name, data, plists...)
1669+
write(parent::HDF5Dataset, name::String, data::Union{T, AbstractArray{T}}, plists...) where {T<:ScalarOrString} =
1670+
a_write(parent, name, data, plists...)
16701671

16711672
# Reading arrays using getindex: data = dset[:,:,10]
16721673
function getindex(dset::HDF5Dataset, indices::Union{AbstractRange{Int},Int}...)
@@ -1928,12 +1929,16 @@ function h5a_write(attr_id::Hid, memtype_id::Hid, v::HDF5Vlen{T}) where {T<:Unio
19281929
vp = vlenpack(v)
19291930
h5a_write(attr_id, memtype_id, vp)
19301931
end
1931-
h5a_create(loc_id::Hid, name::String, type_id::Hid, space_id::Hid) = h5a_create(loc_id, name, type_id, space_id, _attr_properties(name), H5P_DEFAULT)
1932+
h5a_create(loc_id::Hid, name::String, type_id::Hid, space_id::Hid) =
1933+
h5a_create(loc_id, name, type_id, space_id, _attr_properties(name), H5P_DEFAULT)
19321934
h5a_open(obj_id::Hid, name::String) = h5a_open(obj_id, name, H5P_DEFAULT)
1933-
h5d_create(loc_id::Hid, name::String, type_id::Hid, space_id::Hid) = h5d_create(loc_id, name, type_id, space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)
1935+
h5d_create(loc_id::Hid, name::String, type_id::Hid, space_id::Hid) =
1936+
h5d_create(loc_id, name, type_id, space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)
19341937
h5d_open(obj_id::Hid, name::String) = h5d_open(obj_id, name, H5P_DEFAULT)
1935-
h5d_read(dataset_id::Hid, memtype_id::Hid, buf::AbstractArray) = h5d_read(dataset_id, memtype_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf)
1936-
h5d_write(dataset_id::Hid, memtype_id::Hid, buf::AbstractArray) = h5d_write(dataset_id, memtype_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf)
1938+
h5d_read(dataset_id::Hid, memtype_id::Hid, buf::AbstractArray) =
1939+
h5d_read(dataset_id, memtype_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf)
1940+
h5d_write(dataset_id::Hid, memtype_id::Hid, buf::AbstractArray) =
1941+
h5d_write(dataset_id, memtype_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf)
19371942
function h5d_write(dataset_id::Hid, memtype_id::Hid, str::String)
19381943
ccall((:H5Dwrite, libhdf5), Herr, (Hid, Hid, Hid, Hid, Hid, Cstring), dataset_id, memtype_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, str)
19391944
end

test/plain.jl

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,6 @@ end
378378
end == collect(1:16)
379379

380380
# issue 463
381-
fn = tempname()
382381
f = h5open(fn, "w")
383382
s = SharedArray{UInt16}(3,4,5)
384383
s[1:end] = 1:length(s)
@@ -388,6 +387,48 @@ s2 = read(f,"/data")
388387
close(f)
389388
rm(fn)
390389

390+
# issue 466
391+
f = h5open(fn, "w")
392+
data = rand(UInt16,2,3);
393+
pda_data = PermutedDimsArray(data,(2,1));
394+
write(f,"/data",pda_data)
395+
pda_data_readback = read(f,"/data")
396+
@test pda_data == pda_data_readback
397+
close(f)
398+
rm(fn)
399+
400+
f = h5open(fn, "w")
401+
d=reshape(1:6,2,3)
402+
write(f,"/data",d)
403+
d2 = read(f,"/data")
404+
@test d == d2
405+
close(f)
406+
rm(fn)
407+
408+
f = h5open(fn, "w")
409+
d=view(rand(2,3),:,1)
410+
write(f,"/data",d)
411+
d2 = read(f,"/data")
412+
@test d == d2
413+
close(f)
414+
rm(fn)
415+
416+
f = h5open(fn, "w")
417+
d=Diagonal(1:10)
418+
write(f,"/data",d)
419+
d2 = read(f,"/data")
420+
@test d == d2
421+
close(f)
422+
rm(fn)
423+
424+
f = h5open(fn, "w")
425+
r=1:10
426+
write(f,"/data",r)
427+
r2 = read(f,"/data")
428+
@test collect(r) == r2
429+
close(f)
430+
rm(fn)
431+
391432
end # testset plain
392433

393434
# test strings with null and undefined references

0 commit comments

Comments
 (0)