Skip to content

Commit 1b18f0b

Browse files
committed
Add ValueTuple based creation of CompressedColumnStorage to avoid Tuple allocations
1 parent 5547c20 commit 1b18f0b

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
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, int, T)> 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.Item1, item.Item2, item.Item3);
308+
}
309+
310+
return storage;
311+
}
292312
}
293313
}

CSparse/Storage/CompressedColumnStorage.cs

+10
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ public static CompressedColumnStorage<T> OfIndexed(int rows, int columns, IEnume
153153
return Converter.ToCompressedColumnStorage(c);
154154
}
155155

156+
/// <summary>
157+
/// Create a new sparse matrix as a copy of the given indexed enumerable using a value tuple.
158+
/// </summary>
159+
public static CompressedColumnStorage<T> OfIndexed(int rows, int columns, IEnumerable<(int, int, T)> enumerable)
160+
{
161+
var c = Converter.FromEnumerable<T>(enumerable, rows, columns);
162+
163+
return Converter.ToCompressedColumnStorage(c);
164+
}
165+
156166
/// <summary>
157167
/// Create a new sparse matrix as a copy of the given array (row-major).
158168
/// </summary>

0 commit comments

Comments
 (0)