Skip to content

Commit

Permalink
Better JLD support for arrays-of-arrays of basic types
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Aug 8, 2014
1 parent d949ce3 commit 3a95493
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/jld.jl
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,16 @@ end
read{T<:ByteString}(obj::JldDataset, ::Type{Array{T}}) = read(obj.plain, Array{T})
read{T<:BitsKindOrByteString,N}(obj::JldDataset, ::Type{Array{T,N}}) = read(obj, Array{T})

# Arrays-of-arrays of basic types
function read{T<:HDF5BitsKind,M,N}(obj::JldDataset, ::Type{Array{Array{T,N},M}})
A = read(obj.plain, HDF5.HDF5Vlen{T})
if isempty(A) && exists(obj, "dims")
dims = a_read(obj.plain, "dims")
A = reshape(A, dims...)
end
convert(Array{Array{T,N},M}, A)
end

# Nothing
read(obj::JldDataset, ::Type{Nothing}) = nothing
read(obj::JldDataset, ::Type{Bool}) = bool(read(obj, Uint8))
Expand Down Expand Up @@ -515,6 +525,30 @@ end
write{T<:Union(HDF5BitsKind, ByteString)}(parent::Union(JldFile, JldGroup), name::ByteString, data::Union(T, Array{T})) =
write(parent, name, data, full_typename(typeof(data)))

# Arrays-of-arrays of basic types
write{T<:Union(HDF5BitsKind, ByteString),N}(parent::Union(JldFile, JldGroup), name::ByteString,
data::Array{Array{T,N}}, astype::ByteString) =
write(parent, name, HDF5.HDF5Vlen(data), astype)
write{T<:Union(HDF5BitsKind, ByteString),N}(parent::Union(JldFile, JldGroup), name::ByteString,
data::Array{Array{T,N}}) =
write(parent, name, data, full_typename(typeof(data)))
function write{T}(parent::Union(JldFile, JldGroup), name::ByteString,
data::HDF5.HDF5Vlen{T}, astype::ByteString)
# Create the dataset
dset, dtype = d_create(parent.plain, name, data)
try
# Write the attribute
a_write(dset, name_type_attr, astype)
isa(data, Array) && isempty(data) && a_write(dset, "dims", [size(data)...])
# Write the data
HDF5.writearray(dset, dtype.id, data)
finally
close(dset)
close(dtype)
end
end


# Write nothing
function write(parent::Union(JldFile, JldGroup), name::ByteString, n::Nothing, astype::ASCIIString)
local dspace, dset
Expand Down
1 change: 1 addition & 0 deletions src/plain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ type HDF5Vlen{T}
end
HDF5Vlen{S<:ByteString}(strs::Array{S}) = HDF5Vlen{chartype(S)}(strs)
HDF5Vlen{T<:HDF5BitsKind}(A::Array{Array{T}}) = HDF5Vlen{T}(A)
HDF5Vlen{T<:HDF5BitsKind,N}(A::Array{Array{T,N}}) = HDF5Vlen{T}(A)

## Types that correspond to C structs and get used for ccall
# For VLEN
Expand Down
3 changes: 3 additions & 0 deletions test/jld.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using Base.Test
# Define variables of different types
x = 3.7
A = rand(1:20, 3, 5)
Aarray = Vector{Float64}[[1.2,1.3],[2.2,2.3,2.4]]
str = "Hello"
stringsA = ASCIIString["It", "was", "a", "dark", "and", "stormy", "night"]
stringsU = UTF8String["It", "was", "a", "dark", "and", "stormy", "night"]
Expand Down Expand Up @@ -141,6 +142,7 @@ fn = joinpath(tempdir(),"test.jld")
fid = jldopen(fn, "w")
@write fid x
@write fid A
@write fid Aarray
@write fid str
@write fid stringsA
@write fid stringsU
Expand Down Expand Up @@ -196,6 +198,7 @@ for mmap = (true, false)
fidr = jldopen(fn, "r", mmaparrays=mmap)
@check fidr x
@check fidr A
@check fidr Aarray
@check fidr str
@check fidr stringsA
@check fidr stringsU
Expand Down

0 comments on commit 3a95493

Please sign in to comment.