From 7be849100f10ac8e5760a082d0656580ebf16557 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Tue, 22 Jul 2014 12:30:04 +0200 Subject: [PATCH] Data: Matlab: static parser design, refactoring --- src/Data/Matlab/Matlab.csproj | 1 - src/Data/Matlab/MatlabFile.cs | 79 -------------------------- src/Data/Matlab/MatlabMatrix.cs | 2 +- src/Data/Matlab/MatlabReader.cs | 15 +++-- src/Data/Matlab/Parser.cs | 85 ++-------------------------- src/Data/Matlab/SparseArrayReader.cs | 4 +- 6 files changed, 16 insertions(+), 170 deletions(-) delete mode 100644 src/Data/Matlab/MatlabFile.cs diff --git a/src/Data/Matlab/Matlab.csproj b/src/Data/Matlab/Matlab.csproj index edbce808..08710462 100644 --- a/src/Data/Matlab/Matlab.csproj +++ b/src/Data/Matlab/Matlab.csproj @@ -55,7 +55,6 @@ - diff --git a/src/Data/Matlab/MatlabFile.cs b/src/Data/Matlab/MatlabFile.cs deleted file mode 100644 index bce0f85c..00000000 --- a/src/Data/Matlab/MatlabFile.cs +++ /dev/null @@ -1,79 +0,0 @@ -// -// Math.NET Numerics, part of the Math.NET Project -// http://numerics.mathdotnet.com -// http://github.com/mathnet/mathnet-numerics -// http://mathnetnumerics.codeplex.com -// -// Copyright (c) 2009-2014 Math.NET -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -// - -using System; -using System.Collections.Generic; -using MathNet.Numerics.LinearAlgebra; - -namespace MathNet.Numerics.Data.Matlab -{ - /// - /// Represents a MATLAB file - /// - internal class MatlabFile - { - /// - /// Matrices in a MATLAB file stored as 1-D arrays - /// - readonly IDictionary _matrices = new SortedList(); - - /// - /// Gets or sets the header text. - /// - /// The header text. - internal string HeaderText { get; set; } - - /// - /// Gets or sets the first name of the matrix. - /// - /// The first name of the matrix. - internal string FirstMatrixName { get; private set; } - - internal ICollection MatrixNames - { - get { return _matrices.Keys; } - } - - internal void Add(string name, MatlabMatrix data) - { - if (FirstMatrixName == null) - { - FirstMatrixName = name; - } - - _matrices.Add(name, data); - } - - internal Matrix ReadMatrix(string name) where TDataType : struct, IEquatable, IFormattable - { - return _matrices[name].Read(); - } - } -} diff --git a/src/Data/Matlab/MatlabMatrix.cs b/src/Data/Matlab/MatlabMatrix.cs index d10d9b12..ad63cd49 100644 --- a/src/Data/Matlab/MatlabMatrix.cs +++ b/src/Data/Matlab/MatlabMatrix.cs @@ -53,7 +53,7 @@ namespace MathNet.Numerics.Data.Matlab internal Matrix Read() where TDataType : struct, IEquatable, IFormattable { - return Parser.ReadMatrixBlock(_data); + return Parser.ReadMatrixBlock(_data); } } } diff --git a/src/Data/Matlab/MatlabReader.cs b/src/Data/Matlab/MatlabReader.cs index bca107d3..f236c3b3 100644 --- a/src/Data/Matlab/MatlabReader.cs +++ b/src/Data/Matlab/MatlabReader.cs @@ -45,16 +45,14 @@ namespace MathNet.Numerics.Data.Matlab public static Matrix ReadMatrix(Stream stream, string matrixName = null) where TDataType : struct, IEquatable, IFormattable { - var names = string.IsNullOrEmpty(matrixName) ? new string[] { } : new[] { matrixName }; - var parser = new Parser(stream, names); - var file = parser.ParseAll(); + var matrices = Parser.ParseAll(stream); if (string.IsNullOrEmpty(matrixName)) { - return file.First().Read(); + return matrices.First().Read(); } - var matrix = file.Find(m => m.Name == matrixName); + var matrix = matrices.Find(m => m.Name == matrixName); if (matrix == null) { throw new KeyNotFoundException("Matrix with the provided name was not found."); @@ -78,9 +76,10 @@ namespace MathNet.Numerics.Data.Matlab where TDataType : struct, IEquatable, IFormattable { var names = new HashSet(matrixNames); - var parser = new Parser(stream, matrixNames); - var file = parser.ParseAll(); - return file.Where(m => names.Count == 0 || names.Contains(m.Name)).ToDictionary(m => m.Name, m => m.Read()); + + return Parser.ParseAll(stream) + .Where(m => names.Count == 0 || names.Contains(m.Name)) + .ToDictionary(m => m.Name, m => m.Read()); } /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. diff --git a/src/Data/Matlab/Parser.cs b/src/Data/Matlab/Parser.cs index 7414481a..8bf503e4 100644 --- a/src/Data/Matlab/Parser.cs +++ b/src/Data/Matlab/Parser.cs @@ -41,9 +41,7 @@ namespace MathNet.Numerics.Data.Matlab /// /// Parse a MATLAB file /// - /// The data type of the matrix. - internal class Parser - where TDataType : struct, IEquatable, IFormattable + internal static class Parser { /// /// Large Block Size @@ -60,86 +58,14 @@ namespace MathNet.Numerics.Data.Matlab /// const int SmallBlockSize = 4; - /// - /// Holds the names of the matrices in the file. - /// - readonly IList _names = new List(); - - /// - /// The stream to read the MATLAB file from. - /// - readonly Stream _stream; - - /// - /// Initializes a new instance of the class. - /// - /// Name of the file. - internal Parser(string fileName) - : this(fileName, new string[0]) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The stream to read from. - internal Parser(Stream stream) - : this(stream, new string[0]) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The stream to read from. - /// The name of the objects to retrieve. - internal Parser(Stream stream, IEnumerable objectNames) - { - if (stream == null) - { - throw new ArgumentNullException("stream"); - } - - _stream = stream; - SetNames(objectNames); - } - - /// - /// Initializes a new instance of the class. - /// - /// Name of the file. - /// The name of the objects to retrieve. - Parser(string fileName, IEnumerable objectNames) - { - if (string.IsNullOrEmpty(fileName)) - { - throw new ArgumentException(Resources.StringNullOrEmpty, "fileName"); - } - - _stream = File.OpenRead(fileName); - SetNames(objectNames); - } - - /// - /// Copies the names of the objects to retrieve to a local field. - /// - /// The name of the objects to retrieve. - void SetNames(IEnumerable objectNames) - { - foreach (var name in objectNames) - { - _names.Add(name); - } - } - /// /// Extracts all matrix blocks in a format we support. /// - internal List ParseAll() + internal static List ParseAll(Stream stream) { var matrices = new List(); - using (var reader = new BinaryReader(_stream)) + using (var reader = new BinaryReader(stream)) { reader.BaseStream.Position = 126; if (reader.ReadByte() != LittleEndianIndicator) @@ -149,7 +75,7 @@ namespace MathNet.Numerics.Data.Matlab // skip version since it is always 0x0100. reader.BaseStream.Position = 128; - var length = _stream.Length; + var length = stream.Length; // for each data block add a MATLAB object to the file. while (reader.BaseStream.Position < length) @@ -251,7 +177,8 @@ namespace MathNet.Numerics.Data.Matlab return data; } - internal static Matrix ReadMatrixBlock(byte[] data) + internal static Matrix ReadMatrixBlock(byte[] data) + where TDataType : struct, IEquatable, IFormattable { using (var stream = new MemoryStream(data)) using (var reader = new BinaryReader(stream)) diff --git a/src/Data/Matlab/SparseArrayReader.cs b/src/Data/Matlab/SparseArrayReader.cs index 0faa596e..33e07067 100644 --- a/src/Data/Matlab/SparseArrayReader.cs +++ b/src/Data/Matlab/SparseArrayReader.cs @@ -63,7 +63,7 @@ namespace MathNet.Numerics.Data.Matlab ir[i] = reader.ReadInt32(); } - Parser.AlignData(reader.BaseStream, size, false); + Parser.AlignData(reader.BaseStream, size, false); // skip data type since it will always be int32 reader.BaseStream.Seek(4, SeekOrigin.Current); @@ -81,7 +81,7 @@ namespace MathNet.Numerics.Data.Matlab jc[j] = reader.ReadInt32(); } - Parser.AlignData(reader.BaseStream, jcsize, false); + Parser.AlignData(reader.BaseStream, jcsize, false); var type = (DataType)reader.ReadInt32(); var dataSize = reader.ReadInt32();