From f727c6d430b60942af747d0ec2a5d548bfeab84f Mon Sep 17 00:00:00 2001 From: Alexander Luzgarev Date: Fri, 15 Mar 2019 19:39:36 +0100 Subject: [PATCH] Rename HDF-related types --- MatFileHandler.Tests/MatFileReaderHdfTests.cs | 4 +- MatFileHandler/Hdf/Array.cs | 45 ++ MatFileHandler/Hdf/Attribute.cs | 24 + MatFileHandler/Hdf/CellArray.cs | 24 + MatFileHandler/Hdf/CharArray.cs | 41 ++ MatFileHandler/Hdf/Dataset.cs | 24 + MatFileHandler/Hdf/Group.cs | 24 + MatFileHandler/Hdf/MatlabClass.cs | 20 + MatFileHandler/Hdf/MemoryHandle.cs | 25 + MatFileHandler/Hdf/NumericalArrayOf.cs | 97 ++++ MatFileHandler/Hdf/StructureArray.cs | 140 +++++ MatFileHandler/HdfFileReader.cs | 547 +++--------------- 12 files changed, 534 insertions(+), 481 deletions(-) create mode 100644 MatFileHandler/Hdf/Array.cs create mode 100644 MatFileHandler/Hdf/Attribute.cs create mode 100644 MatFileHandler/Hdf/CellArray.cs create mode 100644 MatFileHandler/Hdf/CharArray.cs create mode 100644 MatFileHandler/Hdf/Dataset.cs create mode 100644 MatFileHandler/Hdf/Group.cs create mode 100644 MatFileHandler/Hdf/MatlabClass.cs create mode 100644 MatFileHandler/Hdf/MemoryHandle.cs create mode 100644 MatFileHandler/Hdf/NumericalArrayOf.cs create mode 100644 MatFileHandler/Hdf/StructureArray.cs diff --git a/MatFileHandler.Tests/MatFileReaderHdfTests.cs b/MatFileHandler.Tests/MatFileReaderHdfTests.cs index a58e88a..b192168 100644 --- a/MatFileHandler.Tests/MatFileReaderHdfTests.cs +++ b/MatFileHandler.Tests/MatFileReaderHdfTests.cs @@ -1,7 +1,7 @@ -using NUnit.Framework; -using System.IO; +using System.IO; using System.Linq; using System.Numerics; +using NUnit.Framework; namespace MatFileHandler.Tests { diff --git a/MatFileHandler/Hdf/Array.cs b/MatFileHandler/Hdf/Array.cs new file mode 100644 index 0000000..737377f --- /dev/null +++ b/MatFileHandler/Hdf/Array.cs @@ -0,0 +1,45 @@ +using System.Numerics; + +namespace MatFileHandler.Hdf +{ + internal class Array : IArray + { + /// + /// Initializes a new instance of the class. + /// + /// Dimensions of the array. + protected Array( + int[] dimensions) + { + Dimensions = dimensions; + } + + /// + public int[] Dimensions { get; } + + /// + public int Count => Dimensions.NumberOfElements(); + + /// + /// Returns a new empty array. + /// + /// Empty array. + public static Array Empty() + { + return new Array(System.Array.Empty()); + } + + public virtual double[] ConvertToDoubleArray() + { + return null; + } + + public virtual Complex[] ConvertToComplexArray() + { + return null; + } + + /// + public bool IsEmpty => Dimensions.Length == 0; + } +} \ No newline at end of file diff --git a/MatFileHandler/Hdf/Attribute.cs b/MatFileHandler/Hdf/Attribute.cs new file mode 100644 index 0000000..374fe78 --- /dev/null +++ b/MatFileHandler/Hdf/Attribute.cs @@ -0,0 +1,24 @@ +using System; +using HDF.PInvoke; + +namespace MatFileHandler.Hdf +{ + public struct Attribute : IDisposable + { + public long Id { get; private set; } + + public Attribute(long locationId, string name) + { + Id = H5A.open_by_name(locationId, ".", name); + } + + public void Dispose() + { + if (Id != -1) + { + H5A.close(Id); + Id = -1; + } + } + } +} \ No newline at end of file diff --git a/MatFileHandler/Hdf/CellArray.cs b/MatFileHandler/Hdf/CellArray.cs new file mode 100644 index 0000000..ed893c6 --- /dev/null +++ b/MatFileHandler/Hdf/CellArray.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; +using System.Linq; + +namespace MatFileHandler.Hdf +{ + internal class CellArray : Array, ICellArray + { + public CellArray(int[] dimensions, IEnumerable elements) + : base(dimensions) + { + Data = elements.ToArray(); + } + + /// + public IArray[] Data { get; } + + /// + public IArray this[params int[] indices] + { + get => Data[Dimensions.DimFlatten(indices)]; + set => Data[Dimensions.DimFlatten(indices)] = value; + } + } +} \ No newline at end of file diff --git a/MatFileHandler/Hdf/CharArray.cs b/MatFileHandler/Hdf/CharArray.cs new file mode 100644 index 0000000..1f60ca8 --- /dev/null +++ b/MatFileHandler/Hdf/CharArray.cs @@ -0,0 +1,41 @@ +using System; +using System.Linq; +using System.Numerics; + +namespace MatFileHandler.Hdf +{ + internal class CharArray : Array, ICharArray + { + public CharArray(int[] dimensions, string data) + : base(dimensions) + { + StringData = data; + } + + public override double[] ConvertToDoubleArray() + { + return Data.Select(Convert.ToDouble).ToArray(); + } + + public override Complex[] ConvertToComplexArray() + { + return ConvertToDoubleArray().Select(x => new Complex(x, 0.0)).ToArray(); + } + + public char[] Data => StringData.ToCharArray(); + + public char this[params int[] list] + { + get => StringData[Dimensions.DimFlatten(list)]; + set { + var chars = StringData.ToCharArray(); + chars[Dimensions.DimFlatten(list)] = value; + StringData = chars.ToString(); + } + } + + public string String => StringData; + + private string StringData { get; set; } + } +} \ No newline at end of file diff --git a/MatFileHandler/Hdf/Dataset.cs b/MatFileHandler/Hdf/Dataset.cs new file mode 100644 index 0000000..2da07ea --- /dev/null +++ b/MatFileHandler/Hdf/Dataset.cs @@ -0,0 +1,24 @@ +using System; +using HDF.PInvoke; + +namespace MatFileHandler.Hdf +{ + public struct Dataset : IDisposable + { + public long Id { get; private set; } + + public Dataset(long groupId, string name) + { + Id = H5D.open(groupId, name); + } + + public void Dispose() + { + if (Id != -1) + { + H5D.close(Id); + Id = -1; + } + } + } +} \ No newline at end of file diff --git a/MatFileHandler/Hdf/Group.cs b/MatFileHandler/Hdf/Group.cs new file mode 100644 index 0000000..04b0c51 --- /dev/null +++ b/MatFileHandler/Hdf/Group.cs @@ -0,0 +1,24 @@ +using System; +using HDF.PInvoke; + +namespace MatFileHandler.Hdf +{ + public struct Group : IDisposable + { + public long Id { get; private set; } + + public Group(long groupId, string name) + { + Id = H5G.open(groupId, name); + } + + public void Dispose() + { + if (Id != -1) + { + H5G.close(Id); + Id = -1; + } + } + } +} \ No newline at end of file diff --git a/MatFileHandler/Hdf/MatlabClass.cs b/MatFileHandler/Hdf/MatlabClass.cs new file mode 100644 index 0000000..b522fdd --- /dev/null +++ b/MatFileHandler/Hdf/MatlabClass.cs @@ -0,0 +1,20 @@ +namespace MatFileHandler.Hdf +{ + internal enum MatlabClass + { + MEmpty, + MChar, + MInt8, + MUInt8, + MInt16, + MUInt16, + MInt32, + MUInt32, + MInt64, + MUInt64, + MSingle, + MDouble, + MCell, + MLogical, + } +} \ No newline at end of file diff --git a/MatFileHandler/Hdf/MemoryHandle.cs b/MatFileHandler/Hdf/MemoryHandle.cs new file mode 100644 index 0000000..0aac738 --- /dev/null +++ b/MatFileHandler/Hdf/MemoryHandle.cs @@ -0,0 +1,25 @@ +using System; +using System.Runtime.InteropServices; + +namespace MatFileHandler.Hdf +{ + internal sealed class MemoryHandle : IDisposable + { + internal MemoryHandle(int sizeInBytes) + { + Handle = Marshal.AllocHGlobal(sizeInBytes); + } + + internal IntPtr Handle { get; private set; } + + /// + public void Dispose() + { + if (Handle != IntPtr.Zero) + { + Marshal.FreeHGlobal(Handle); + Handle = IntPtr.Zero; + } + } + } +} \ No newline at end of file diff --git a/MatFileHandler/Hdf/NumericalArrayOf.cs b/MatFileHandler/Hdf/NumericalArrayOf.cs new file mode 100644 index 0000000..34a2343 --- /dev/null +++ b/MatFileHandler/Hdf/NumericalArrayOf.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Numerics; + +namespace MatFileHandler.Hdf +{ + /// + /// A numerical array. + /// + /// Element type. + internal class NumericalArrayOf : Array, IArrayOf + where T : struct + { + /// + /// Initializes a new instance of the class. + /// + /// Dimensions of the array. + /// Array name. + /// Array contents. + public NumericalArrayOf(int[] dimensions, T[] data) + : base(dimensions) + { + Data = data; + } + + /// + public T[] Data { get; } + + /// + public T this[params int[] list] + { + get => Data[Dimensions.DimFlatten(list)]; + set => Data[Dimensions.DimFlatten(list)] = value; + } + + /// + /// Tries to convert the array to an array of Double values. + /// + /// Array of values of the array, converted to Double, or null if the conversion is not possible. + public override double[] ConvertToDoubleArray() + { + return Data as double[] ?? Data.Select(x => Convert.ToDouble((object)x)).ToArray(); + } + + /// + /// Tries to convert the array to an array of Complex values. + /// + /// Array of values of the array, converted to Complex, or null if the conversion is not possible. + public override Complex[] ConvertToComplexArray() + { + if (Data is Complex[]) + { + return Data as Complex[]; + } + if (Data is ComplexOf[]) + { + return ConvertToComplex(Data as ComplexOf[]); + } + if (Data is ComplexOf[]) + { + return ConvertToComplex(Data as ComplexOf[]); + } + if (Data is ComplexOf[]) + { + return ConvertToComplex(Data as ComplexOf[]); + } + if (Data is ComplexOf[]) + { + return ConvertToComplex(Data as ComplexOf[]); + } + if (Data is ComplexOf[]) + { + return ConvertToComplex(Data as ComplexOf[]); + } + if (Data is ComplexOf[]) + { + return ConvertToComplex(Data as ComplexOf[]); + } + if (Data is ComplexOf[]) + { + return ConvertToComplex(Data as ComplexOf[]); + } + if (Data is ComplexOf[]) + { + return ConvertToComplex(Data as ComplexOf[]); + } + return ConvertToDoubleArray().Select(x => new Complex(x, 0.0)).ToArray(); + } + + private static Complex[] ConvertToComplex(IEnumerable> array) + where TS : struct + { + return array.Select(x => new Complex(Convert.ToDouble(x.Real), Convert.ToDouble(x.Imaginary))).ToArray(); + } + } +} \ No newline at end of file diff --git a/MatFileHandler/Hdf/StructureArray.cs b/MatFileHandler/Hdf/StructureArray.cs new file mode 100644 index 0000000..057e0c1 --- /dev/null +++ b/MatFileHandler/Hdf/StructureArray.cs @@ -0,0 +1,140 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace MatFileHandler.Hdf +{ + internal class StructureArray : Array, IStructureArray + { + public StructureArray( + int[] dimensions, + Dictionary> fields) + : base(dimensions) + { + Fields = fields; + } + + /// + public IEnumerable FieldNames => Fields.Keys; + + /// + /// Gets null: not implemented. + /// + public IReadOnlyDictionary[] Data => null; + + /// + /// Gets a dictionary that maps field names to lists of values. + /// + internal Dictionary> Fields { get; } + + /// + public IArray this[string field, params int[] list] + { + get => Fields[field][Dimensions.DimFlatten(list)]; + set => Fields[field][Dimensions.DimFlatten(list)] = value; + } + + /// + IReadOnlyDictionary IArrayOf>.this[params int[] list] + { + get => ExtractStructure(Dimensions.DimFlatten(list)); + set => throw new NotSupportedException( + "Cannot set structure elements via this[params int[]] indexer. Use this[string, int[]] instead."); + } + + private IReadOnlyDictionary ExtractStructure(int i) + { + return new HdfStructureArrayElement(this, i); + } + + /// + /// Provides access to an element of a structure array by fields. + /// + internal class HdfStructureArrayElement : IReadOnlyDictionary + { + /// + /// Initializes a new instance of the class. + /// + /// Parent structure array. + /// Index in the structure array. + internal HdfStructureArrayElement(StructureArray parent, int index) + { + Parent = parent; + Index = index; + } + + /// + /// Gets the number of fields. + /// + public int Count => Parent.Fields.Count; + + /// + /// Gets a list of all fields. + /// + public IEnumerable Keys => Parent.Fields.Keys; + + /// + /// Gets a list of all values. + /// + public IEnumerable Values => Parent.Fields.Values.Select(array => array[Index]); + + private StructureArray Parent { get; } + + private int Index { get; } + + /// + /// Gets the value of a given field. + /// + /// Field name. + /// The corresponding value. + public IArray this[string key] => Parent.Fields[key][Index]; + + /// + /// Enumerates fieldstructure/value pairs of the dictionary. + /// + /// All field/value pairs in the structure. + public IEnumerator> GetEnumerator() + { + foreach (var field in Parent.Fields) + { + yield return new KeyValuePair(field.Key, field.Value[Index]); + } + } + + /// + /// Enumerates field/value pairs of the structure. + /// + /// All field/value pairs in the structure. + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + /// + /// Checks if the structure has a given field. + /// + /// Field name + /// True iff the structure has a given field. + public bool ContainsKey(string key) => Parent.Fields.ContainsKey(key); + + /// + /// Tries to get the value of a given field. + /// + /// Field name. + /// Value (or null if the field is not present). + /// Success status of the query. + public bool TryGetValue(string key, out IArray value) + { + var success = Parent.Fields.TryGetValue(key, out var array); + if (!success) + { + value = default(IArray); + return false; + } + value = array[Index]; + return true; + } + } + } +} \ No newline at end of file diff --git a/MatFileHandler/HdfFileReader.cs b/MatFileHandler/HdfFileReader.cs index 9fbefa5..4ac40c0 100644 --- a/MatFileHandler/HdfFileReader.cs +++ b/MatFileHandler/HdfFileReader.cs @@ -1,427 +1,16 @@ using System; -using System.Collections; using System.Collections.Generic; using System.Linq; using System.Numerics; using System.Runtime.InteropServices; using System.Text; using HDF.PInvoke; +using MatFileHandler.Hdf; +using Array = MatFileHandler.Hdf.Array; +using Attribute = MatFileHandler.Hdf.Attribute; namespace MatFileHandler { - public struct Dataset : IDisposable - { - public long Id { get; private set; } - - public Dataset(long groupId, string name) - { - Id = H5D.open(groupId, name); - } - - public void Dispose() - { - if (Id != -1) - { - H5D.close(Id); - Id = -1; - } - } - } - - public struct Group : IDisposable - { - public long Id { get; private set; } - - public Group(long groupId, string name) - { - Id = H5G.open(groupId, name); - } - - public void Dispose() - { - if (Id != -1) - { - H5G.close(Id); - Id = -1; - } - } - } - - public struct Attribute : IDisposable - { - public long Id { get; private set; } - - public Attribute(long locationId, string name) - { - Id = H5A.open_by_name(locationId, ".", name); - } - - public void Dispose() - { - if (Id != -1) - { - H5A.close(Id); - Id = -1; - } - } - } - - sealed class MemoryHandle : IDisposable - { - internal MemoryHandle(int sizeInBytes) - { - Handle = Marshal.AllocHGlobal(sizeInBytes); - } - - internal IntPtr Handle { get; private set; } - - /// - public void Dispose() - { - if (Handle != IntPtr.Zero) - { - Marshal.FreeHGlobal(Handle); - Handle = IntPtr.Zero; - } - } - } - - internal enum HdfMatlabClass - { - MEmpty, - MChar, - MInt8, - MUInt8, - MInt16, - MUInt16, - MInt32, - MUInt32, - MInt64, - MUInt64, - MSingle, - MDouble, - MCell, - MLogical, - } - - internal class HdfArray : IArray - { - /// - /// Initializes a new instance of the class. - /// - /// Dimensions of the array. - protected HdfArray( - int[] dimensions) - { - Dimensions = dimensions; - } - - /// - public int[] Dimensions { get; } - - /// - public int Count => Dimensions.NumberOfElements(); - - /// - /// Returns a new empty array. - /// - /// Empty array. - public static HdfArray Empty() - { - return new HdfArray(Array.Empty()); - } - - public virtual double[] ConvertToDoubleArray() - { - return null; - } - - public virtual Complex[] ConvertToComplexArray() - { - return null; - } - - /// - public bool IsEmpty => Dimensions.Length == 0; - } - - internal class HdfCellArray : HdfArray, ICellArray - { - public HdfCellArray(int[] dimensions, IEnumerable elements) - : base(dimensions) - { - Data = elements.ToArray(); - } - - /// - public IArray[] Data { get; } - - /// - public IArray this[params int[] indices] - { - get => Data[Dimensions.DimFlatten(indices)]; - set => Data[Dimensions.DimFlatten(indices)] = value; - } - } - - /// - /// A numerical array. - /// - /// Element type. - internal class HdfNumericalArrayOf : HdfArray, IArrayOf - where T : struct - { - /// - /// Initializes a new instance of the class. - /// - /// Dimensions of the array. - /// Array name. - /// Array contents. - public HdfNumericalArrayOf(int[] dimensions, T[] data) - : base(dimensions) - { - Data = data; - } - - /// - public T[] Data { get; } - - /// - public T this[params int[] list] - { - get => Data[Dimensions.DimFlatten(list)]; - set => Data[Dimensions.DimFlatten(list)] = value; - } - - /// - /// Tries to convert the array to an array of Double values. - /// - /// Array of values of the array, converted to Double, or null if the conversion is not possible. - public override double[] ConvertToDoubleArray() - { - return Data as double[] ?? Data.Select(x => Convert.ToDouble(x)).ToArray(); - } - - /// - /// Tries to convert the array to an array of Complex values. - /// - /// Array of values of the array, converted to Complex, or null if the conversion is not possible. - public override Complex[] ConvertToComplexArray() - { - if (Data is Complex[]) - { - return Data as Complex[]; - } - if (Data is ComplexOf[]) - { - return ConvertToComplex(Data as ComplexOf[]); - } - if (Data is ComplexOf[]) - { - return ConvertToComplex(Data as ComplexOf[]); - } - if (Data is ComplexOf[]) - { - return ConvertToComplex(Data as ComplexOf[]); - } - if (Data is ComplexOf[]) - { - return ConvertToComplex(Data as ComplexOf[]); - } - if (Data is ComplexOf[]) - { - return ConvertToComplex(Data as ComplexOf[]); - } - if (Data is ComplexOf[]) - { - return ConvertToComplex(Data as ComplexOf[]); - } - if (Data is ComplexOf[]) - { - return ConvertToComplex(Data as ComplexOf[]); - } - if (Data is ComplexOf[]) - { - return ConvertToComplex(Data as ComplexOf[]); - } - return ConvertToDoubleArray().Select(x => new Complex(x, 0.0)).ToArray(); - } - - private static Complex[] ConvertToComplex(IEnumerable> array) - where TS : struct - { - return array.Select(x => new Complex(Convert.ToDouble(x.Real), Convert.ToDouble(x.Imaginary))).ToArray(); - } - } - - internal class HdfCharArray : HdfArray, ICharArray - { - public HdfCharArray(int[] dimensions, string data) - : base(dimensions) - { - StringData = data; - } - - public override double[] ConvertToDoubleArray() - { - return Data.Select(Convert.ToDouble).ToArray(); - } - - public override Complex[] ConvertToComplexArray() - { - return ConvertToDoubleArray().Select(x => new Complex(x, 0.0)).ToArray(); - } - - public char[] Data => StringData.ToCharArray(); - - public char this[params int[] list] - { - get => StringData[Dimensions.DimFlatten(list)]; - set { - var chars = StringData.ToCharArray(); - chars[Dimensions.DimFlatten(list)] = value; - StringData = chars.ToString(); - } - } - - public string String => StringData; - - private string StringData { get; set; } - } - - internal class HdfStructureArray : HdfArray, IStructureArray - { - public HdfStructureArray( - int[] dimensions, - Dictionary> fields) - : base(dimensions) - { - Fields = fields; - } - - /// - public IEnumerable FieldNames => Fields.Keys; - - /// - /// Gets null: not implemented. - /// - public IReadOnlyDictionary[] Data => null; - - /// - /// Gets a dictionary that maps field names to lists of values. - /// - internal Dictionary> Fields { get; } - - /// - public IArray this[string field, params int[] list] - { - get => Fields[field][Dimensions.DimFlatten(list)]; - set => Fields[field][Dimensions.DimFlatten(list)] = value; - } - - /// - IReadOnlyDictionary IArrayOf>.this[params int[] list] - { - get => ExtractStructure(Dimensions.DimFlatten(list)); - set => throw new NotSupportedException( - "Cannot set structure elements via this[params int[]] indexer. Use this[string, int[]] instead."); - } - - private IReadOnlyDictionary ExtractStructure(int i) - { - return new HdfStructureArrayElement(this, i); - } - - /// - /// Provides access to an element of a structure array by fields. - /// - internal class HdfStructureArrayElement : IReadOnlyDictionary - { - /// - /// Initializes a new instance of the class. - /// - /// Parent structure array. - /// Index in the structure array. - internal HdfStructureArrayElement(HdfStructureArray parent, int index) - { - Parent = parent; - Index = index; - } - - /// - /// Gets the number of fields. - /// - public int Count => Parent.Fields.Count; - - /// - /// Gets a list of all fields. - /// - public IEnumerable Keys => Parent.Fields.Keys; - - /// - /// Gets a list of all values. - /// - public IEnumerable Values => Parent.Fields.Values.Select(array => array[Index]); - - private HdfStructureArray Parent { get; } - - private int Index { get; } - - /// - /// Gets the value of a given field. - /// - /// Field name. - /// The corresponding value. - public IArray this[string key] => Parent.Fields[key][Index]; - - /// - /// Enumerates fieldstructure/value pairs of the dictionary. - /// - /// All field/value pairs in the structure. - public IEnumerator> GetEnumerator() - { - foreach (var field in Parent.Fields) - { - yield return new KeyValuePair(field.Key, field.Value[Index]); - } - } - - /// - /// Enumerates field/value pairs of the structure. - /// - /// All field/value pairs in the structure. - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - - /// - /// Checks if the structure has a given field. - /// - /// Field name - /// True iff the structure has a given field. - public bool ContainsKey(string key) => Parent.Fields.ContainsKey(key); - - /// - /// Tries to get the value of a given field. - /// - /// Field name. - /// Value (or null if the field is not present). - /// Success status of the query. - public bool TryGetValue(string key, out IArray value) - { - var success = Parent.Fields.TryGetValue(key, out var array); - if (!success) - { - value = default(IArray); - return false; - } - value = array[Index]; - return true; - } - } - } - internal class HdfFileReader { private long fileId; @@ -515,42 +104,42 @@ namespace MatFileHandler var rank = H5S.get_simple_extent_ndims(spaceId); var dims = new ulong[rank]; H5S.get_simple_extent_dims(spaceId, dims, null); - Array.Reverse(dims); + System.Array.Reverse(dims); return dims.Select(x => (int)x).ToArray(); } - private static HdfMatlabClass ArrayTypeFromMatlabClassName(string matlabClassName) + private static MatlabClass ArrayTypeFromMatlabClassName(string matlabClassName) { switch (matlabClassName) { case "canonical empty": - return HdfMatlabClass.MEmpty; + return MatlabClass.MEmpty; case "logical": - return HdfMatlabClass.MLogical; + return MatlabClass.MLogical; case "char": - return HdfMatlabClass.MChar; + return MatlabClass.MChar; case "int8": - return HdfMatlabClass.MInt8; + return MatlabClass.MInt8; case "uint8": - return HdfMatlabClass.MUInt8; + return MatlabClass.MUInt8; case "int16": - return HdfMatlabClass.MInt16; + return MatlabClass.MInt16; case "uint16": - return HdfMatlabClass.MUInt16; + return MatlabClass.MUInt16; case "int32": - return HdfMatlabClass.MInt32; + return MatlabClass.MInt32; case "uint32": - return HdfMatlabClass.MUInt32; + return MatlabClass.MUInt32; case "int64": - return HdfMatlabClass.MInt64; + return MatlabClass.MInt64; case "uint64": - return HdfMatlabClass.MUInt64; + return MatlabClass.MUInt64; case "single": - return HdfMatlabClass.MSingle; + return MatlabClass.MSingle; case "double": - return HdfMatlabClass.MDouble; + return MatlabClass.MDouble; case "cell": - return HdfMatlabClass.MCell; + return MatlabClass.MCell; } throw new NotImplementedException(); } @@ -586,7 +175,7 @@ namespace MatFileHandler var rank = H5S.get_simple_extent_ndims(spaceId); var dims = new ulong[rank]; H5S.get_simple_extent_dims(spaceId, dims, null); - Array.Reverse(dims); + System.Array.Reverse(dims); var dimensions = dims.Select(x => (int)x).ToArray(); var numberOfFields = dimensions.NumberOfElements(); @@ -667,7 +256,7 @@ namespace MatFileHandler throw new NotImplementedException(); } } - return new HdfStructureArray(dimensions, dictionary); + return new StructureArray(dimensions, dictionary); } } else @@ -692,33 +281,33 @@ namespace MatFileHandler switch (arrayType) { - case HdfMatlabClass.MEmpty: - return HdfArray.Empty(); - case HdfMatlabClass.MLogical: + case MatlabClass.MEmpty: + return Array.Empty(); + case MatlabClass.MLogical: return ReadNumericalArray(datasetId, dims, arrayType); - case HdfMatlabClass.MChar: + case MatlabClass.MChar: return ReadCharArray(datasetId, dims); - case HdfMatlabClass.MInt8: + case MatlabClass.MInt8: return ReadNumericalArray(datasetId, dims, arrayType); - case HdfMatlabClass.MUInt8: + case MatlabClass.MUInt8: return ReadNumericalArray(datasetId, dims, arrayType); - case HdfMatlabClass.MInt16: + case MatlabClass.MInt16: return ReadNumericalArray(datasetId, dims, arrayType); - case HdfMatlabClass.MUInt16: + case MatlabClass.MUInt16: return ReadNumericalArray(datasetId, dims, arrayType); - case HdfMatlabClass.MInt32: + case MatlabClass.MInt32: return ReadNumericalArray(datasetId, dims, arrayType); - case HdfMatlabClass.MUInt32: + case MatlabClass.MUInt32: return ReadNumericalArray(datasetId, dims, arrayType); - case HdfMatlabClass.MInt64: + case MatlabClass.MInt64: return ReadNumericalArray(datasetId, dims, arrayType); - case HdfMatlabClass.MUInt64: + case MatlabClass.MUInt64: return ReadNumericalArray(datasetId, dims, arrayType); - case HdfMatlabClass.MSingle: + case MatlabClass.MSingle: return ReadNumericalArray(datasetId, dims, arrayType); - case HdfMatlabClass.MDouble: + case MatlabClass.MDouble: return ReadNumericalArray(datasetId, dims, arrayType); - case HdfMatlabClass.MCell: + case MatlabClass.MCell: return ReadCellArray(datasetId, dims); } throw new NotImplementedException($"Unknown array type: {arrayType}."); @@ -743,63 +332,63 @@ namespace MatFileHandler elements[i] = dataset; } } - return new HdfCellArray(dims, elements); + return new CellArray(dims, elements); } - private static int SizeOfArrayElement(HdfMatlabClass arrayType) + private static int SizeOfArrayElement(MatlabClass arrayType) { switch (arrayType) { - case HdfMatlabClass.MInt8: - case HdfMatlabClass.MUInt8: - case HdfMatlabClass.MLogical: + case MatlabClass.MInt8: + case MatlabClass.MUInt8: + case MatlabClass.MLogical: return 1; - case HdfMatlabClass.MInt16: - case HdfMatlabClass.MUInt16: + case MatlabClass.MInt16: + case MatlabClass.MUInt16: return 2; - case HdfMatlabClass.MInt32: - case HdfMatlabClass.MUInt32: - case HdfMatlabClass.MSingle: + case MatlabClass.MInt32: + case MatlabClass.MUInt32: + case MatlabClass.MSingle: return 4; - case HdfMatlabClass.MInt64: - case HdfMatlabClass.MUInt64: - case HdfMatlabClass.MDouble: + case MatlabClass.MInt64: + case MatlabClass.MUInt64: + case MatlabClass.MDouble: return 8; } throw new NotImplementedException(); } - private static long H5tTypeFromHdfMatlabClass(HdfMatlabClass arrayType) + private static long H5tTypeFromHdfMatlabClass(MatlabClass arrayType) { switch (arrayType) { - case HdfMatlabClass.MInt8: + case MatlabClass.MInt8: return H5T.NATIVE_INT8; - case HdfMatlabClass.MUInt8: - case HdfMatlabClass.MLogical: + case MatlabClass.MUInt8: + case MatlabClass.MLogical: return H5T.NATIVE_UINT8; - case HdfMatlabClass.MInt16: + case MatlabClass.MInt16: return H5T.NATIVE_INT16; - case HdfMatlabClass.MUInt16: + case MatlabClass.MUInt16: return H5T.NATIVE_UINT16; - case HdfMatlabClass.MInt32: + case MatlabClass.MInt32: return H5T.NATIVE_INT32; - case HdfMatlabClass.MUInt32: + case MatlabClass.MUInt32: return H5T.NATIVE_UINT32; - case HdfMatlabClass.MInt64: + case MatlabClass.MInt64: return H5T.NATIVE_INT64; - case HdfMatlabClass.MUInt64: + case MatlabClass.MUInt64: return H5T.NATIVE_UINT64; - case HdfMatlabClass.MSingle: + case MatlabClass.MSingle: return H5T.NATIVE_FLOAT; - case HdfMatlabClass.MDouble: + case MatlabClass.MDouble: return H5T.NATIVE_DOUBLE; } throw new NotImplementedException(); } - private static T[] ConvertDataToProperType(byte[] bytes, HdfMatlabClass arrayType) + private static T[] ConvertDataToProperType(byte[] bytes, MatlabClass arrayType) where T : struct { var length = bytes.Length; @@ -820,7 +409,7 @@ namespace MatFileHandler return data; } - private static IArray ReadNumericalArray(long datasetId, int[] dims, HdfMatlabClass arrayType) + private static IArray ReadNumericalArray(long datasetId, int[] dims, MatlabClass arrayType) where T : struct { var numberOfElements = dims.NumberOfElements(); @@ -841,13 +430,13 @@ namespace MatFileHandler H5T.insert(h5tComplexImaginary, "imag", IntPtr.Zero, h5Type); var imaginaryData = ReadDataset(datasetId, h5tComplexImaginary, dataSize); var convertedImaginaryData = ConvertDataToProperType(imaginaryData, arrayType); - if (arrayType == HdfMatlabClass.MDouble) + if (arrayType == MatlabClass.MDouble) { var complexData = (convertedRealData as double[]) .Zip(convertedImaginaryData as double[], (x, y) => new Complex(x, y)) .ToArray(); - return new HdfNumericalArrayOf(dims, complexData); + return new NumericalArrayOf(dims, complexData); } else { @@ -855,7 +444,7 @@ namespace MatFileHandler convertedRealData .Zip(convertedImaginaryData, (x, y) => new ComplexOf(x, y)) .ToArray(); - return new HdfNumericalArrayOf>(dims, complexData); + return new NumericalArrayOf>(dims, complexData); } } if (dataSize != storageSize) @@ -864,7 +453,7 @@ namespace MatFileHandler } var data = ReadDataset(datasetId, H5tTypeFromHdfMatlabClass(arrayType), dataSize); var convertedData = ConvertDataToProperType(data, arrayType); - return new HdfNumericalArrayOf(dims, convertedData); + return new NumericalArrayOf(dims, convertedData); } private static IArray ReadCharArray(long datasetId, int[] dims) @@ -872,7 +461,7 @@ namespace MatFileHandler var storageSize = (int)H5D.get_storage_size(datasetId); var data = ReadDataset(datasetId, H5T.NATIVE_UINT16, storageSize); var str = Encoding.Unicode.GetString(data); - return new HdfCharArray(dims, str); + return new CharArray(dims, str); } } }