Skip to content

Commit 67e4c8e

Browse files
committed
Cleanups
1 parent a6cb427 commit 67e4c8e

4 files changed

Lines changed: 40 additions & 61 deletions

File tree

src/Data.Matlab/Formatter.cs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,17 @@ internal static MatlabMatrix FormatMatrix<T>(Matrix<T> matrix, string name)
105105
{
106106
if (matrix == null)
107107
{
108-
throw new ArgumentNullException("matrix");
108+
throw new ArgumentNullException(nameof(matrix));
109109
}
110110

111111
if (string.IsNullOrEmpty(name))
112112
{
113-
throw new ArgumentException("String parameter cannot be empty or null.", "name");
113+
throw new ArgumentException("String parameter cannot be empty or null.", nameof(name));
114114
}
115115

116116
if (name.IndexOf(' ') > -1)
117117
{
118-
throw new ArgumentException(string.Format("Name cannot contain a space.name: {0}", name), "name");
118+
throw new ArgumentException($"Name cannot contain a space.name: {name}", nameof(name));
119119
}
120120

121121
var dataType = typeof(T);
@@ -167,7 +167,7 @@ internal static MatlabMatrix FormatMatrix<T>(Matrix<T> matrix, string name)
167167
writer.Write((byte)(sparse ? ArrayClass.Sparse : doublePrecision ? ArrayClass.Double : ArrayClass.Single));
168168
writer.Write((byte)(complex ? ArrayFlags.Complex : 0));
169169
writer.Write((short)0);
170-
writer.Write((int)sparseNonZeroValues);
170+
writer.Write(sparseNonZeroValues);
171171

172172
// Dimensions Array tag: data type + size (8 bytes)
173173
writer.Write((int)DataType.Int32);
@@ -178,11 +178,10 @@ internal static MatlabMatrix FormatMatrix<T>(Matrix<T> matrix, string name)
178178
writer.Write(matrix.ColumnCount);
179179

180180
// Array Name:
181-
bool smallBlock;
182181
var nameBytes = Encoding.ASCII.GetBytes(name);
183-
WriteElementTag(writer, DataType.Int8, nameBytes.Length, out smallBlock);
182+
WriteElementTag(writer, DataType.Int8, nameBytes.Length, out var isSmallBlock);
184183
writer.Write(nameBytes);
185-
PadElement(writer, nameBytes.Length, smallBlock);
184+
PadElement(writer, nameBytes.Length, isSmallBlock);
186185

187186
if (sparse)
188187
{
@@ -203,9 +202,8 @@ static void WriteDenseMatrix<T>(BinaryWriter writer, Matrix<T> matrix, bool comp
203202
{
204203
int count = matrix.RowCount*matrix.ColumnCount;
205204

206-
bool smallBlock;
207205
int size = doublePrecision ? count*8 : count*4;
208-
WriteElementTag(writer, doublePrecision ? DataType.Double : DataType.Single, size, out smallBlock);
206+
WriteElementTag(writer, doublePrecision ? DataType.Double : DataType.Single, size, out var isSmallBlock);
209207

210208
var data = ((DenseColumnMajorMatrixStorage<T>)matrix.Storage).Data;
211209

@@ -219,14 +217,14 @@ static void WriteDenseMatrix<T>(BinaryWriter writer, Matrix<T> matrix, bool comp
219217
}
220218
else if (doublePrecision)
221219
{
222-
WriteComplexArray(writer, (Complex[])(object)data, data.Length, size, ref smallBlock);
220+
WriteComplexArray(writer, (Complex[])(object)data, data.Length, size, ref isSmallBlock);
223221
}
224222
else
225223
{
226-
WriteComplex32Array(writer, (Complex32[])(object)data, data.Length, size, ref smallBlock);
224+
WriteComplex32Array(writer, (Complex32[])(object)data, data.Length, size, ref isSmallBlock);
227225
}
228226

229-
PadElement(writer, size, smallBlock);
227+
PadElement(writer, size, isSmallBlock);
230228
}
231229

232230
static void WriteSparseMatrix<T>(BinaryWriter writer, Matrix<T> matrix, bool complex, bool doublePrecision)
@@ -235,32 +233,31 @@ static void WriteSparseMatrix<T>(BinaryWriter writer, Matrix<T> matrix, bool com
235233
var transposed = matrix.Transpose();
236234
var storage = (SparseCompressedRowMatrixStorage<T>)transposed.Storage;
237235

238-
bool smallBlock;
239236
int nzcount = storage.ValueCount;
240237

241238
// row data array
242239
var ir = storage.ColumnIndices;
243-
WriteElementTag(writer, DataType.Int32, nzcount*4, out smallBlock);
240+
WriteElementTag(writer, DataType.Int32, nzcount*4, out var isSmallBlock);
244241
for (var i = 0; i < nzcount; i++)
245242
{
246243
writer.Write(ir[i]);
247244
}
248245

249-
PadElement(writer, nzcount*4, smallBlock);
246+
PadElement(writer, nzcount*4, isSmallBlock);
250247

251248
// column data array
252249
var jc = storage.RowPointers;
253-
WriteElementTag(writer, DataType.Int32, jc.Length*4, out smallBlock);
250+
WriteElementTag(writer, DataType.Int32, jc.Length*4, out isSmallBlock);
254251
for (var i = 0; i < jc.Length; i++)
255252
{
256253
writer.Write(jc[i]);
257254
}
258255

259-
PadElement(writer, jc.Length*4, smallBlock);
256+
PadElement(writer, jc.Length*4, isSmallBlock);
260257

261258
// values
262259
int size = doublePrecision ? nzcount*8 : nzcount*4;
263-
WriteElementTag(writer, doublePrecision ? DataType.Double : DataType.Single, size, out smallBlock);
260+
WriteElementTag(writer, doublePrecision ? DataType.Double : DataType.Single, size, out isSmallBlock);
264261

265262
if (doublePrecision && !complex)
266263
{
@@ -272,14 +269,14 @@ static void WriteSparseMatrix<T>(BinaryWriter writer, Matrix<T> matrix, bool com
272269
}
273270
else if (doublePrecision)
274271
{
275-
WriteComplexArray(writer, (Complex[])(object)storage.Values, nzcount, size, ref smallBlock);
272+
WriteComplexArray(writer, (Complex[])(object)storage.Values, nzcount, size, ref isSmallBlock);
276273
}
277274
else
278275
{
279-
WriteComplex32Array(writer, (Complex32[])(object)storage.Values, nzcount, size, ref smallBlock);
276+
WriteComplex32Array(writer, (Complex32[])(object)storage.Values, nzcount, size, ref isSmallBlock);
280277
}
281278

282-
PadElement(writer, size, smallBlock);
279+
PadElement(writer, size, isSmallBlock);
283280
}
284281

285282
static void WriteDoubleArray(BinaryWriter writer, double[] data, int count)
@@ -348,7 +345,7 @@ static void WriteElementTag(BinaryWriter writer, DataType dataType, int size, ou
348345
}
349346
}
350347

351-
static void PadElement(BinaryWriter writer, int size, bool smallBlock, byte padValue = (byte)0)
348+
static void PadElement(BinaryWriter writer, int size, bool smallBlock, byte padValue = 0)
352349
{
353350
var blockSize = smallBlock ? SmallBlockSize : LargeBlockSize;
354351
var offset = 0;
@@ -364,7 +361,7 @@ static void PadElement(BinaryWriter writer, int size, bool smallBlock, byte padV
364361
}
365362
}
366363

367-
static void Pad(BinaryWriter writer, int count, byte padValue = (byte)0)
364+
static void Pad(BinaryWriter writer, int count, byte padValue = 0)
368365
{
369366
for (var i = 0; i < count; i++)
370367
{

src/Data.Matlab/Parser.cs

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,7 @@ internal static List<MatlabMatrix> ParseFile(Stream stream)
8989
// small format: size (2 bytes), type (2 bytes), data (4 bytes)
9090
// long format: type (4 bytes), size (4 bytes), data (size, aligned to 8 bytes)
9191

92-
DataType type;
93-
int size;
94-
bool smallBlock;
95-
ReadElementTag(reader, out type, out size, out smallBlock);
92+
ReadElementTag(reader, out var type, out var size, out var isSmallBlock);
9693

9794
// read element data of the size provided in the element header
9895
// uncompress if compressed
@@ -105,7 +102,7 @@ internal static List<MatlabMatrix> ParseFile(Stream stream)
105102
{
106103
data = new byte[size];
107104
reader.Read(data, 0, size);
108-
SkipElementPadding(reader, size, smallBlock);
105+
SkipElementPadding(reader, size, isSmallBlock);
109106
}
110107

111108
if (type == DataType.Matrix)
@@ -169,12 +166,9 @@ internal static Matrix<T> ParseMatrix<T>(byte[] data)
169166
var columns = reader.ReadInt32();
170167

171168
// Array name
172-
DataType type;
173-
int size;
174-
bool smallBlock;
175-
ReadElementTag(reader, out type, out size, out smallBlock);
169+
ReadElementTag(reader, out _, out var size, out var isSmallBlock);
176170
reader.BaseStream.Seek(size, SeekOrigin.Current);
177-
SkipElementPadding(reader, size, smallBlock);
171+
SkipElementPadding(reader, size, isSmallBlock);
178172

179173
// Data
180174
switch (arrayClass)
@@ -209,12 +203,8 @@ static Matrix<T> PopulateDenseMatrix<T>(BinaryReader reader, bool complex, int r
209203
var count = rows*columns;
210204
var data = new T[count];
211205

212-
DataType type;
213-
int size;
214-
bool smallBlock;
215-
216206
// read real part array
217-
ReadElementTag(reader, out type, out size, out smallBlock);
207+
ReadElementTag(reader, out var type, out var size, out var isSmallBlock);
218208

219209
// direct copy if possible
220210
if ((type == DataType.Double && dataType == typeof(double)) || (type == DataType.Single && dataType == typeof(float)))
@@ -241,18 +231,18 @@ static Matrix<T> PopulateDenseMatrix<T>(BinaryReader reader, bool complex, int r
241231
}
242232
else if (dataType == typeof(Complex))
243233
{
244-
PopulateComplexArray(reader, (Complex[])(object)data, complex, type, ref size, ref smallBlock);
234+
PopulateComplexArray(reader, (Complex[])(object)data, complex, type, ref size, ref isSmallBlock);
245235
}
246236
else if (dataType == typeof(Complex32))
247237
{
248-
PopulateComplex32Array(reader, (Complex32[])(object)data, complex, type, ref size, ref smallBlock);
238+
PopulateComplex32Array(reader, (Complex32[])(object)data, complex, type, ref size, ref isSmallBlock);
249239
}
250240
else
251241
{
252242
throw new NotSupportedException();
253243
}
254244

255-
SkipElementPadding(reader, size, smallBlock);
245+
SkipElementPadding(reader, size, isSmallBlock);
256246
return Matrix<T>.Build.Dense(rows, columns, data);
257247
}
258248

@@ -273,22 +263,18 @@ static Matrix<T> PopulateSparseMatrix<T>(BinaryReader reader, bool complex, int
273263
// MATLAB sparse matrices are actually stored as CSC, so just read the data and then transpose.
274264
var storage = matrix.Storage as SparseCompressedRowMatrixStorage<T>;
275265

276-
DataType type;
277-
int size;
278-
bool smallBlock;
279-
280266
// populate the row data array
281-
ReadElementTag(reader, out type, out size, out smallBlock);
267+
ReadElementTag(reader, out var type, out var size, out var isSmallBlock);
282268
var ir = storage.ColumnIndices = new int[size/4];
283269
for (var i = 0; i < ir.Length; i++)
284270
{
285271
ir[i] = reader.ReadInt32();
286272
}
287273

288-
SkipElementPadding(reader, size, smallBlock);
274+
SkipElementPadding(reader, size, isSmallBlock);
289275

290276
// populate the column data array
291-
ReadElementTag(reader, out type, out size, out smallBlock);
277+
ReadElementTag(reader, out type, out size, out isSmallBlock);
292278
var jc = storage.RowPointers;
293279
if (jc.Length != size/4)
294280
{
@@ -300,10 +286,10 @@ static Matrix<T> PopulateSparseMatrix<T>(BinaryReader reader, bool complex, int
300286
jc[j] = reader.ReadInt32();
301287
}
302288

303-
SkipElementPadding(reader, size, smallBlock);
289+
SkipElementPadding(reader, size, isSmallBlock);
304290

305291
// populate the values
306-
ReadElementTag(reader, out type, out size, out smallBlock);
292+
ReadElementTag(reader, out type, out size, out isSmallBlock);
307293
var dataType = typeof(T);
308294
var data = storage.Values = new T[jc[columns]];
309295

@@ -327,18 +313,18 @@ static Matrix<T> PopulateSparseMatrix<T>(BinaryReader reader, bool complex, int
327313
}
328314
else if (dataType == typeof(Complex))
329315
{
330-
PopulateComplexArray(reader, (Complex[])(object)data, complex, type, ref size, ref smallBlock);
316+
PopulateComplexArray(reader, (Complex[])(object)data, complex, type, ref size, ref isSmallBlock);
331317
}
332318
else if (dataType == typeof(Complex32))
333319
{
334-
PopulateComplex32Array(reader, (Complex32[])(object)data, complex, type, ref size, ref smallBlock);
320+
PopulateComplex32Array(reader, (Complex32[])(object)data, complex, type, ref size, ref isSmallBlock);
335321
}
336322
else
337323
{
338324
throw new NotSupportedException();
339325
}
340326

341-
SkipElementPadding(reader, size, smallBlock);
327+
SkipElementPadding(reader, size, isSmallBlock);
342328
return matrix.Transpose();
343329
}
344330

src/Data.Text/DelimitedWriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ public static void Write<T>(TextWriter writer, Matrix<T> matrix, string delimite
5959
{
6060
if (matrix == null)
6161
{
62-
throw new ArgumentNullException("matrix");
62+
throw new ArgumentNullException(nameof(matrix));
6363
}
6464

6565
if (writer == null)
6666
{
67-
throw new ArgumentNullException("writer");
67+
throw new ArgumentNullException(nameof(writer));
6868
}
6969

7070
if (columnHeaders != null && columnHeaders.Count > 0)

src/Data.Text/MatrixMarketReader.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ public static Vector<T> ReadVector<T>(Stream stream) where T : struct, IEquatabl
123123

124124
public static Matrix<T> ReadMatrix<T>(TextReader reader) where T : struct, IEquatable<T>, IFormattable
125125
{
126-
bool complex, sparse;
127-
MatrixMarketSymmetry symmetry;
128-
ExpectHeader(reader, true, out complex, out sparse, out symmetry);
126+
ExpectHeader(reader, true, out var complex, out var sparse, out var symmetry);
129127

130128
var parse = CreateValueParser<T>(complex);
131129

@@ -200,9 +198,7 @@ public static Matrix<T> ReadMatrix<T>(TextReader reader) where T : struct, IEqua
200198
public static Vector<T> ReadVector<T>(TextReader reader)
201199
where T : struct, IEquatable<T>, IFormattable
202200
{
203-
bool complex, sparse;
204-
MatrixMarketSymmetry symmetry;
205-
ExpectHeader(reader, false, out complex, out sparse, out symmetry);
201+
ExpectHeader(reader, false, out var complex, out var sparse, out _);
206202

207203
var parse = CreateValueParser<T>(complex);
208204

0 commit comments

Comments
 (0)