Skip to content

Commit

Permalink
Implement julia 0.7 iteration protocol (#494)
Browse files Browse the repository at this point in the history
also add deps/build.log in .gitignore
  • Loading branch information
carlobaldassi authored and musm committed Aug 14, 2018
1 parent 66055ad commit 76878cb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
1 change: 1 addition & 0 deletions deps/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ builds
src
deps.jl
downloads
build.log
46 changes: 31 additions & 15 deletions src/HDF5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ using Compat: findfirst
using Base: unsafe_convert, StringVector

import Base:
close, convert, done, eltype, endof, flush, getindex, ==,
isempty, isvalid, length, names, ndims, next, parent, read,
setindex!, show, size, sizeof, start, write, isopen
close, convert, eltype, endof, flush, getindex, ==,
isempty, isvalid, length, names, ndims, parent, read,
setindex!, show, size, sizeof, write, isopen

@static if VERSION < v"0.7.0-DEV.5126"
import Base: done, next, start
else
import Base: iterate
end

import Compat.Libdl

Expand Down Expand Up @@ -1144,19 +1150,29 @@ function names(x::HDF5Attributes)
end

# iteration by objects
# "next" opens new objects, "done" closes the old one. This prevents resource leaks.
start(parent::Union{HDF5File, HDF5Group}) = Any[1, nothing]
function done(parent::Union{HDF5File, HDF5Group}, iter::Array{Any})
obj = iter[2]
if !(obj === nothing)
close(obj)
@static if VERSION < v"0.7.0-DEV.5126"
# "next" opens new objects, "done" closes the old one. This prevents resource leaks.
start(parent::Union{HDF5File, HDF5Group}) = Any[1, nothing]
function done(parent::Union{HDF5File, HDF5Group}, iter::Array{Any})
obj = iter[2]
if !(obj === nothing)
close(obj)
end
iter[1] > length(parent)
end
function next(parent::Union{HDF5File, HDF5Group}, iter)
iter[2] = h5object(h5o_open_by_idx(checkvalid(parent).id, ".", H5_INDEX_NAME, H5_ITER_INC, iter[1]-1, H5P_DEFAULT), parent)
iter[1] += 1
(iter[2], iter)
end
else
function iterate(parent::Union{HDF5File, HDF5Group}, iter = (1,nothing))
n, prev_obj = iter
prev_obj nothing && close(prev_obj)
n > length(parent) && return nothing
obj = h5object(h5o_open_by_idx(checkvalid(parent).id, ".", H5_INDEX_NAME, H5_ITER_INC, n-1, H5P_DEFAULT), parent)
return (obj, (n+1,obj))
end
iter[1] > length(parent)
end
function next(parent::Union{HDF5File, HDF5Group}, iter)
iter[2] = h5object(h5o_open_by_idx(checkvalid(parent).id, ".", H5_INDEX_NAME, H5_ITER_INC, iter[1]-1, H5P_DEFAULT), parent)
iter[1] += 1
(iter[2], iter)
end

endof(dset::HDF5Dataset) = length(dset)
Expand Down

0 comments on commit 76878cb

Please sign in to comment.