Skip to content

Commit

Permalink
Refactor code to simplify case statement
Browse files Browse the repository at this point in the history
  • Loading branch information
kojix2 committed Sep 12, 2024
1 parent b7ecd55 commit 94cbc16
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
16 changes: 5 additions & 11 deletions lib/hdf5/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@ def list_entries
end

case FFI::MiV
when 10
status = HDF5::FFI.H5Literate(@file_id, :H5_INDEX_NAME, :H5_ITER_NATIVE, nil, callback, nil)
when 14
status = HDF5::FFI.H5Literate2(@file_id, :H5_INDEX_NAME, :H5_ITER_NATIVE, nil, callback, nil)
end

raise 'Failed to iterate over file entries' if status < 0
when 10 then HDF5::FFI.H5Literate(@file_id, :H5_INDEX_NAME, :H5_ITER_NATIVE, nil, callback, nil)
when 14 then HDF5::FFI.H5Literate2(@file_id, :H5_INDEX_NAME, :H5_ITER_NATIVE, nil, callback, nil)
end < 0 && raise('Failed to iterate over file entries')

list
end
Expand All @@ -54,29 +50,27 @@ def group?(name)
when 10
info = HDF5::FFI::H5OInfoT.new
HDF5::FFI.H5Oget_info_by_name(@file_id, name, info, 0)
info[:type] == :H5O_TYPE_GROUP
when 14
info = HDF5::FFI::H5OInfo1T.new
HDF5::FFI.H5Oget_info_by_name1(@file_id, name, info, 0)
info[:type] == :H5O_TYPE_GROUP
else
raise 'This should not happen'
end
info[:type] == :H5O_TYPE_GROUP
end

def dataset?(name)
case FFI::MiV
when 10
info = HDF5::FFI::H5OInfoT.new
HDF5::FFI.H5Oget_info_by_name(@file_id, name, info, 0)
info[:type] == :H5O_TYPE_DATASET
when 14
info = HDF5::FFI::H5OInfo1T.new
HDF5::FFI.H5Oget_info_by_name1(@file_id, name, info, 0)
info[:type] == :H5O_TYPE_DATASET
else
raise 'This should not happen'
end
info[:type] == :H5O_TYPE_DATASET
end
end
end
15 changes: 5 additions & 10 deletions lib/hdf5/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ def list_datasets
end

case HDF5::FFI::MiV
when 10
status = HDF5::FFI.H5Literate(@group_id, :H5_INDEX_NAME, :H5_ITER_NATIVE, nil, callback, nil)
when 14
status = HDF5::FFI.H5Literate2(@group_id, :H5_INDEX_NAME, :H5_ITER_NATIVE, nil, callback, nil)
end
raise 'Failed to list datasets' if status < 0
when 10 then HDF5::FFI.H5Literate(@group_id, :H5_INDEX_NAME, :H5_ITER_NATIVE, nil, callback, nil)
when 14 then HDF5::FFI.H5Literate2(@group_id, :H5_INDEX_NAME, :H5_ITER_NATIVE, nil, callback, nil)
end.negative? && raise('Failed to list datasets')

datasets
end
Expand All @@ -44,29 +41,27 @@ def group?(name)
when 10
info = HDF5::FFI::H5OInfoT.new
HDF5::FFI.H5Oget_info_by_name(@group_id, name, info, 0)
info[:type] == :H5O_TYPE_GROUP
when 14
info = HDF5::FFI::H5OInfo1T.new
HDF5::FFI.H5Oget_info_by_name1(@group_id, name, info, 0)
info[:type] == :H5O_TYPE_GROUP
else
raise 'This should not happen'
end
info[:type] == :H5O_TYPE_GROUP
end

def dataset?(name)
case HDF5::FFI::MiV
when 10
info = HDF5::FFI::H5OInfoT.new
HDF5::FFI.H5Oget_info_by_name(@group_id, name, info, 0)
info[:type] == :H5O_TYPE_DATASET
when 14
info = HDF5::FFI::H5OInfo1T.new
HDF5::FFI.H5Oget_info_by_name1(@group_id, name, info, 0)
info[:type] == :H5O_TYPE_DATASET
else
raise 'This should not happen'
end
info[:type] == :H5O_TYPE_DATASET
end
end
end

0 comments on commit 94cbc16

Please sign in to comment.