Skip to content

Commit ca2c2dd

Browse files
committed
Replace global dicts with if-else chains
1 parent 6f694a2 commit ca2c2dd

File tree

1 file changed

+95
-73
lines changed

1 file changed

+95
-73
lines changed

src/HDF5.jl

Lines changed: 95 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -270,18 +270,27 @@ const HDF5Scalar = Union{HDF5BitsKind, HDF5ReferenceObj}
270270
const ScalarOrString = Union{HDF5Scalar, String}
271271

272272
# It's not safe to use particular id codes because these can change, so we use characteristics of the type.
273-
const hdf5_type_map = Dict(
274-
(H5T_INTEGER, H5T_SGN_2, Csize_t(1)) => Int8,
275-
(H5T_INTEGER, H5T_SGN_2, Csize_t(2)) => Int16,
276-
(H5T_INTEGER, H5T_SGN_2, Csize_t(4)) => Int32,
277-
(H5T_INTEGER, H5T_SGN_2, Csize_t(8)) => Int64,
278-
(H5T_INTEGER, H5T_SGN_NONE, Csize_t(1)) => UInt8,
279-
(H5T_INTEGER, H5T_SGN_NONE, Csize_t(2)) => UInt16,
280-
(H5T_INTEGER, H5T_SGN_NONE, Csize_t(4)) => UInt32,
281-
(H5T_INTEGER, H5T_SGN_NONE, Csize_t(8)) => UInt64,
282-
(H5T_FLOAT, nothing, Csize_t(4)) => Float32,
283-
(H5T_FLOAT, nothing, Csize_t(8)) => Float64,
284-
)
273+
function _hdf5_type_map(class_id, is_signed, native_size)
274+
if class_id == H5T_INTEGER
275+
if is_signed == H5T_SGN_2
276+
return native_size === Csize_t(1) ? Int8 :
277+
native_size === Csize_t(2) ? Int16 :
278+
native_size === Csize_t(4) ? Int32 :
279+
native_size === Csize_t(8) ? Int64 :
280+
throw(KeyError(class_id, is_signed, native_size))
281+
else
282+
return native_size === Csize_t(1) ? UInt8 :
283+
native_size === Csize_t(2) ? UInt16 :
284+
native_size === Csize_t(4) ? UInt32 :
285+
native_size === Csize_t(8) ? UInt64 :
286+
throw(KeyError(class_id, is_signed, native_size))
287+
end
288+
else
289+
return native_size === Csize_t(4) ? Float32 :
290+
native_size === Csize_t(8) ? Float64 :
291+
throw(KeyError(class_id, is_signed, native_size))
292+
end
293+
end
285294

286295
hdf5_type_id(::Type{S}) where {S<:AbstractString} = H5T_C_S1
287296

@@ -889,16 +898,19 @@ root(obj::Union{HDF5Group, HDF5Dataset}) = g_open(file(obj), "/")
889898
getindex(dset::HDF5Dataset, name::String) = a_open(dset, name)
890899
getindex(x::HDF5Attributes, name::String) = a_open(x.parent, name)
891900

892-
const hdf5_obj_open = Dict(
893-
H5I_DATASET => (d_open, H5P_DATASET_ACCESS, H5P_DATASET_XFER),
894-
H5I_DATATYPE => (t_open, H5P_DATATYPE_ACCESS),
895-
H5I_GROUP => (g_open, H5P_GROUP_ACCESS),
896-
)
897901
function getindex(parent::Union{HDF5File, HDF5Group}, path::String; pv...)
898902
objtype = gettype(parent, path)
899-
fun, propids = hdf5_obj_open[objtype]
900-
props = [p_create(prop; pv...) for prop in propids]
901-
obj = fun(parent, path, props...)
903+
if objtype == H5I_DATASET
904+
dapl = p_create(H5P_DATASET_ACCESS; pv...)
905+
dxpl = p_create(H5P_DATASET_XFER; pv...)
906+
return d_open(parent, path, dapl, dxpl)
907+
elseif objtype == H5I_GROUP
908+
gapl = p_create(H5P_GROUP_ACCESS; pv...)
909+
return g_open(parent, path, gapl)
910+
else#if objtype == H5I_DATATYPE # only remaining choice
911+
tapl = p_create(H5P_DATATYPE_ACCESS; pv...)
912+
return t_open(parent, path, tapl)
913+
end
902914
end
903915

904916
# Path manipulation
@@ -971,19 +983,62 @@ end
971983
t_commit(parent::Union{HDF5File, HDF5Group}, path::String, dtype::HDF5Datatype) = t_commit(parent, path, dtype, p_create(H5P_LINK_CREATE))
972984

973985
a_create(parent::Union{HDF5File, HDF5Object}, name::String, dtype::HDF5Datatype, dspace::HDF5Dataspace) = HDF5Attribute(h5a_create(checkvalid(parent).id, name, dtype.id, dspace.id), file(parent))
986+
987+
function _prop_set!(p::HDF5Properties, name::Symbol, val, check::Bool = true)
988+
class = p.class
989+
990+
if class == H5P_FILE_CREATE
991+
return name === :userblock ? h5p_set_userblock(p, val...) :
992+
name === :track_times ? h5p_set_obj_track_times(p, val...) : # H5P_OBJECT_CREATE
993+
check ? error("unknown file create property ", name) : nothing
994+
end
995+
996+
if class == H5P_FILE_ACCESS
997+
return name === :alignment ? h5p_set_alignment(p, val...) :
998+
name === :fclose_degree ? h5p_set_fclose_degree(p, val...) :
999+
name === :libver_bounds ? h5p_set_libver_bounds(p, val...) :
1000+
check ? error("unknown file access property ", name) : nothing
1001+
end
1002+
1003+
if class == H5P_GROUP_CREATE
1004+
return name === :local_heap_size_hint ? h5p_set_local_heap_size_hint(p, val...) :
1005+
name === :track_times ? h5p_set_obj_track_times(p, val...) : # H5P_OBJECT_CREATE
1006+
check ? error("unknown group create property ", name) : nothing
1007+
end
1008+
1009+
if class == H5P_LINK_CREATE
1010+
return name === :char_encoding ? h5p_set_char_encoding(p, val...) :
1011+
name === :create_intermediate_group ? h5p_set_create_intermediate_group(p, val...) :
1012+
check ? error("unknown link create property ", name) : nothing
1013+
end
1014+
1015+
if class == H5P_DATASET_CREATE
1016+
return name === :alloc_time ? h5p_set_alloc_time(p, val...) :
1017+
name === :blosc ? h5p_set_blosc(p, val...) :
1018+
name === :chunk ? set_chunk(p, val...) :
1019+
name === :compress ? h5p_set_deflate(p, val...) :
1020+
name === :deflate ? h5p_set_deflate(p, val...) :
1021+
name === :external ? h5p_set_external(p, val...) :
1022+
name === :layout ? h5p_set_layout(p, val...) :
1023+
name === :shuffle ? h5p_set_shuffle(p, val...) :
1024+
name === :track_times ? h5p_set_obj_track_times(p, val...) : # H5P_OBJECT_CREATE
1025+
check ? error("unknown dataset create property ", name) : nothing
1026+
end
1027+
1028+
if class == H5P_ATTRIBUTE_CREATE
1029+
return name === :char_encoding ? h5p_set_char_encoding(p, val...) :
1030+
check ? error("unknown attribute create property ", name) : nothing
1031+
end
1032+
1033+
check ? error("unknown property class ", class) : return nothing
1034+
end
1035+
9741036
function p_create(class; pv...)
975-
p = HDF5Properties(h5p_create(class), class)
976-
for (k,v) in pairs(pv)
977-
funcget, funcset, classprop = hdf5_prop_get_set[k]
978-
if classprop == H5P_OBJECT_CREATE && (class == H5P_DATASET_CREATE ||
979-
class == H5P_GROUP_CREATE ||
980-
class == H5P_FILE_CREATE)
981-
classprop = class
1037+
p = HDF5Properties(h5p_create(class), class)
1038+
for (k, v) in pairs(pv)
1039+
_prop_set!(p, k, v, false)
9821040
end
983-
class != classprop && continue
984-
funcset(p, v...)
985-
end
986-
return p
1041+
return p
9871042
end
9881043

9891044
# Delete objects
@@ -1002,8 +1057,7 @@ setindex!(dset::HDF5Dataset, val, name::String) = a_write(dset, name, val)
10021057
setindex!(x::HDF5Attributes, val, name::String) = a_write(x.parent, name, val)
10031058
# Getting and setting properties: p[:chunk] = dims, p[:compress] = 6
10041059
function setindex!(p::HDF5Properties, val, name::Symbol)
1005-
funcget, funcset, _ = hdf5_prop_get_set[name]
1006-
funcset(p, val...)
1060+
_prop_set!(p, name, val, true)
10071061
return p
10081062
end
10091063
# Create a dataset with properties: obj[path, prop = val, ...] = val
@@ -1850,7 +1904,7 @@ function get_mem_compatible_jl_type(objtype::HDF5Datatype)
18501904
else
18511905
is_signed = nothing
18521906
end
1853-
return hdf5_type_map[(class_id, is_signed, native_size)]
1907+
return _hdf5_type_map(class_id, is_signed, native_size)
18541908
finally
18551909
h5t_close(native_type)
18561910
end
@@ -1863,7 +1917,7 @@ function get_mem_compatible_jl_type(objtype::HDF5Datatype)
18631917
try
18641918
native_size = h5t_get_size(native_type)
18651919
is_signed = h5t_get_sign(native_type)
1866-
return hdf5_type_map[(H5T_INTEGER, is_signed, native_size)]
1920+
return _hdf5_type_map(H5T_INTEGER, is_signed, native_size)
18671921
finally
18681922
h5t_close(native_type)
18691923
end
@@ -2125,30 +2179,6 @@ function get_alignment(p::HDF5Properties)
21252179
return threshold[], alignment[]
21262180
end
21272181

2128-
# Map property names to function and attribute symbol
2129-
# Property names should follow the naming introduced by HDF5, i.e.
2130-
# keyname => (h5p_get_keyname, h5p_set_keyname, id )
2131-
const hdf5_prop_get_set = Dict(
2132-
:alignment => (get_alignment, h5p_set_alignment, H5P_FILE_ACCESS),
2133-
:alloc_time => (get_alloc_time, h5p_set_alloc_time, H5P_DATASET_CREATE),
2134-
:blosc => (nothing, h5p_set_blosc, H5P_DATASET_CREATE),
2135-
:char_encoding => (nothing, h5p_set_char_encoding, H5P_LINK_CREATE),
2136-
:chunk => (get_chunk, set_chunk, H5P_DATASET_CREATE),
2137-
:compress => (nothing, h5p_set_deflate, H5P_DATASET_CREATE),
2138-
:create_intermediate_group => (nothing, h5p_set_create_intermediate_group, H5P_LINK_CREATE),
2139-
:deflate => (nothing, h5p_set_deflate, H5P_DATASET_CREATE),
2140-
:driver => (h5p_get_driver, nothing, H5P_FILE_ACCESS),
2141-
:driver_info => (h5p_get_driver_info, nothing, H5P_FILE_ACCESS),
2142-
:external => (nothing, h5p_set_external, H5P_DATASET_CREATE),
2143-
:fclose_degree => (get_fclose_degree, h5p_set_fclose_degree, H5P_FILE_ACCESS),
2144-
:layout => (h5p_get_layout, h5p_set_layout, H5P_DATASET_CREATE),
2145-
:libver_bounds => (get_libver_bounds, h5p_set_libver_bounds, H5P_FILE_ACCESS),
2146-
:local_heap_size_hint => (nothing, h5p_set_local_heap_size_hint, H5P_GROUP_CREATE),
2147-
:shuffle => (nothing, h5p_set_shuffle, H5P_DATASET_CREATE),
2148-
:userblock => (get_userblock, h5p_set_userblock, H5P_FILE_CREATE),
2149-
:track_times => (nothing, h5p_set_obj_track_times, H5P_OBJECT_CREATE),
2150-
)
2151-
21522182
# properties that require chunks in order to work (e.g. any filter)
21532183
# values do not matter -- just needed to form a NamedTuple with the desired keys
21542184
const chunked_props = (; compress=nothing, deflate=nothing, blosc=nothing, shuffle=nothing)
@@ -2217,20 +2247,12 @@ function __init__()
22172247
# Turn off automatic error printing
22182248
# h5e_set_auto(H5E_DEFAULT, C_NULL, C_NULL)
22192249

2220-
ASCII_LINK_PROPERTIES[] = p_create(H5P_LINK_CREATE)
2221-
h5p_set_char_encoding(ASCII_LINK_PROPERTIES[].id, H5T_CSET_ASCII)
2222-
h5p_set_create_intermediate_group(ASCII_LINK_PROPERTIES[].id, 1)
2223-
UTF8_LINK_PROPERTIES[] = p_create(H5P_LINK_CREATE)
2224-
h5p_set_char_encoding(UTF8_LINK_PROPERTIES[].id, H5T_CSET_UTF8)
2225-
h5p_set_create_intermediate_group(UTF8_LINK_PROPERTIES[].id, 1)
2226-
ASCII_ATTRIBUTE_PROPERTIES[] = p_create(H5P_ATTRIBUTE_CREATE)
2227-
h5p_set_char_encoding(ASCII_ATTRIBUTE_PROPERTIES[].id, H5T_CSET_ASCII)
2228-
UTF8_ATTRIBUTE_PROPERTIES[] = p_create(H5P_ATTRIBUTE_CREATE)
2229-
h5p_set_char_encoding(UTF8_ATTRIBUTE_PROPERTIES[].id, H5T_CSET_UTF8)
2230-
2231-
rehash!(hdf5_type_map, length(hdf5_type_map.keys))
2232-
rehash!(hdf5_prop_get_set, length(hdf5_prop_get_set.keys))
2233-
rehash!(hdf5_obj_open, length(hdf5_obj_open.keys))
2250+
ASCII_LINK_PROPERTIES[] = p_create(H5P_LINK_CREATE; char_encoding = H5T_CSET_ASCII,
2251+
create_intermediate_group = 1)
2252+
UTF8_LINK_PROPERTIES[] = p_create(H5P_LINK_CREATE; char_encoding = H5T_CSET_UTF8,
2253+
create_intermediate_group = 1)
2254+
ASCII_ATTRIBUTE_PROPERTIES[] = p_create(H5P_ATTRIBUTE_CREATE; char_encoding = H5T_CSET_ASCII)
2255+
UTF8_ATTRIBUTE_PROPERTIES[] = p_create(H5P_ATTRIBUTE_CREATE; char_encoding = H5T_CSET_UTF8)
22342256

22352257
@require MPI="da04e1cc-30fd-572f-bb4f-1f8673147195" @eval include("mpio.jl")
22362258

0 commit comments

Comments
 (0)