Skip to content

Commit f145e93

Browse files
authored
Merge pull request #46 from miniksa/valuetuple
Add ValueTuple overload for creating CompressedColumnStorage
2 parents 5547c20 + 24611b8 commit f145e93

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

CSparse/Converter.cs

+20
Original file line numberDiff line numberDiff line change
@@ -289,5 +289,25 @@ public static CoordinateStorage<T> FromEnumerable<T>(IEnumerable<Tuple<int, int,
289289

290290
return storage;
291291
}
292+
293+
/// <summary>
294+
/// Convert a row major array to coordinate storage.
295+
/// </summary>
296+
/// <param name="enumerable">Enumerates the entries of a matrix with value tuples.</param>
297+
/// <param name="rowCount">Number of rows.</param>
298+
/// <param name="columnCount">Number of columns.</param>
299+
/// <returns>Coordinate storage.</returns>
300+
public static CoordinateStorage<T> FromEnumerable<T>(IEnumerable<(int row, int column, T value)> enumerable, int rowCount, int columnCount)
301+
where T : struct, IEquatable<T>, IFormattable
302+
{
303+
var storage = new CoordinateStorage<T>(rowCount, columnCount, Math.Max(rowCount, columnCount));
304+
305+
foreach (var item in enumerable)
306+
{
307+
storage.At(item.row, item.column, item.value);
308+
}
309+
310+
return storage;
311+
}
292312
}
293313
}

CSparse/Matrix.cs

+9
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,18 @@ protected Matrix(int rowCount, int columnCount)
132132
/// <summary>
133133
/// Enumerates all values of the matrix.
134134
/// </summary>
135+
/// <remarks>
136+
/// <see cref="EnumerateIndexedAsValueTuples"/> for a version that returns stack-allocated value tuples to save transient heap allocations (saves performance overhead of allocations + garbage collection) of the <see cref="Tuple"/> class.
137+
/// </remarks>
135138
/// <returns>Enumeration of tuples (i, j, a[i, j]).</returns>
136139
public abstract IEnumerable<Tuple<int, int, T>> EnumerateIndexed();
137140

141+
/// <summary>
142+
/// Enumerates all values of the matrix, but returns as stack-allocated value tuples instead of heap-allocated tuples.
143+
/// </summary>
144+
/// <returns>Enumeration of tuples (i, j, a[i, j]).</returns>
145+
public abstract IEnumerable<(int row, int column, T value)> EnumerateIndexedAsValueTuples();
146+
138147
/// <summary>
139148
/// Enumerates all values of the matrix.
140149
/// </summary>

CSparse/Storage/CompressedColumnStorage.cs

+27-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public CompressedColumnStorage(int rowCount, int columnCount, T[] values, int[]
110110
/// </summary>
111111
public static CompressedColumnStorage<T> OfMatrix(Matrix<T> matrix)
112112
{
113-
var c = Converter.FromEnumerable<T>(matrix.EnumerateIndexed(), matrix.RowCount, matrix.ColumnCount);
113+
var c = Converter.FromEnumerable<T>(matrix.EnumerateIndexedAsValueTuples(), matrix.RowCount, matrix.ColumnCount);
114114

115115
return Converter.ToCompressedColumnStorage(c);
116116
}
@@ -146,13 +146,29 @@ public static CompressedColumnStorage<T> OfIndexed(CoordinateStorage<T> coordina
146146
/// <summary>
147147
/// Create a new sparse matrix as a copy of the given indexed enumerable.
148148
/// </summary>
149+
/// <param name="rows">The number of rows.</param>
150+
/// <param name="columns">The number of columns.</param>
151+
/// <param name="enumerable">Tuples with the three elements of row, column, and the value that belongs at that position.</param>
149152
public static CompressedColumnStorage<T> OfIndexed(int rows, int columns, IEnumerable<Tuple<int, int, T>> enumerable)
150153
{
151154
var c = Converter.FromEnumerable<T>(enumerable, rows, columns);
152155

153156
return Converter.ToCompressedColumnStorage(c);
154157
}
155158

159+
/// <summary>
160+
/// Create a new sparse matrix as a copy of the given indexed enumerable using a value tuple.
161+
/// </summary>
162+
/// <param name="rows">The number of rows.</param>
163+
/// <param name="columns">The number of columns.</param>
164+
/// <param name="enumerable">Value tuples with the three elements of row, column, and the value that belongs at that position.</param>
165+
public static CompressedColumnStorage<T> OfIndexed(int rows, int columns, IEnumerable<(int row, int column, T value)> enumerable)
166+
{
167+
var c = Converter.FromEnumerable<T>(enumerable, rows, columns);
168+
169+
return Converter.ToCompressedColumnStorage(c);
170+
}
171+
156172
/// <summary>
157173
/// Create a new sparse matrix as a copy of the given array (row-major).
158174
/// </summary>
@@ -564,6 +580,15 @@ public CompressedColumnStorage<T> Clone(bool values = true)
564580

565581
/// <inheritdoc />
566582
public override IEnumerable<Tuple<int, int, T>> EnumerateIndexed()
583+
{
584+
foreach (var valueTuple in EnumerateIndexedAsValueTuples())
585+
{
586+
yield return Tuple.Create(valueTuple.row, valueTuple.column, valueTuple.value);
587+
}
588+
}
589+
590+
/// <inheritdoc />
591+
public override IEnumerable<(int row, int column, T value)> EnumerateIndexedAsValueTuples()
567592
{
568593
var ax = Values;
569594
var ap = ColumnPointers;
@@ -574,7 +599,7 @@ public override IEnumerable<Tuple<int, int, T>> EnumerateIndexed()
574599
var end = ap[i + 1];
575600
for (var j = ap[i]; j < end; j++)
576601
{
577-
yield return new Tuple<int, int, T>(ai[j], i, ax[j]);
602+
yield return (ai[j], i, ax[j]);
578603
}
579604
}
580605
}

CSparse/Storage/DenseColumnMajorStorage.cs

+12-3
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ public static DenseColumnMajorStorage<T> OfDiagonalArray(T[] diagonal)
165165
{
166166
int order = diagonal.Length;
167167

168-
var A = Create(order, order);
169-
168+
var A = Create(order, order);
169+
170170
for (int i = 0; i < order; i++)
171171
{
172172
A.At(i, i, diagonal[i]);
@@ -547,12 +547,21 @@ public override void Clear()
547547

548548
/// <inheritdoc />
549549
public override IEnumerable<Tuple<int, int, T>> EnumerateIndexed()
550+
{
551+
foreach (var valueTuple in EnumerateIndexedAsValueTuples())
552+
{
553+
yield return Tuple.Create(valueTuple.row, valueTuple.column, valueTuple.value);
554+
}
555+
}
556+
557+
/// <inheritdoc />
558+
public override IEnumerable<(int row, int column, T value)> EnumerateIndexedAsValueTuples()
550559
{
551560
for (int row = 0; row < rows; row++)
552561
{
553562
for (int column = 0; column < columns; column++)
554563
{
555-
yield return new Tuple<int, int, T>(row, column, Values[(column * rows) + row]);
564+
yield return (row, column, Values[(column * rows) + row]);
556565
}
557566
}
558567
}

0 commit comments

Comments
 (0)