Skip to content

Commit

Permalink
Add ValueTuple based creation of CompressedColumnStorage to avoid Tup…
Browse files Browse the repository at this point in the history
…le allocations
  • Loading branch information
miniksa committed Jun 11, 2024
1 parent 5547c20 commit 1b18f0b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
20 changes: 20 additions & 0 deletions CSparse/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,25 @@ public static CoordinateStorage<T> FromEnumerable<T>(IEnumerable<Tuple<int, int,

return storage;
}

/// <summary>
/// Convert a row major array to coordinate storage.
/// </summary>
/// <param name="enumerable">Enumerates the entries of a matrix with value tuples.</param>
/// <param name="rowCount">Number of rows.</param>
/// <param name="columnCount">Number of columns.</param>
/// <returns>Coordinate storage.</returns>
public static CoordinateStorage<T> FromEnumerable<T>(IEnumerable<(int, int, T)> enumerable, int rowCount, int columnCount)
where T : struct, IEquatable<T>, IFormattable
{
var storage = new CoordinateStorage<T>(rowCount, columnCount, Math.Max(rowCount, columnCount));

foreach (var item in enumerable)
{
storage.At(item.Item1, item.Item2, item.Item3);
}

return storage;
}
}
}
10 changes: 10 additions & 0 deletions CSparse/Storage/CompressedColumnStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ public static CompressedColumnStorage<T> OfIndexed(int rows, int columns, IEnume
return Converter.ToCompressedColumnStorage(c);
}

/// <summary>
/// Create a new sparse matrix as a copy of the given indexed enumerable using a value tuple.
/// </summary>
public static CompressedColumnStorage<T> OfIndexed(int rows, int columns, IEnumerable<(int, int, T)> enumerable)
{
var c = Converter.FromEnumerable<T>(enumerable, rows, columns);

return Converter.ToCompressedColumnStorage(c);
}

/// <summary>
/// Create a new sparse matrix as a copy of the given array (row-major).
/// </summary>
Expand Down

0 comments on commit 1b18f0b

Please sign in to comment.