using System;
using System.Globalization;
using System.Linq;
using CommunityToolkit.Diagnostics;
namespace ComputeSharp.Sample;
///
/// Internal helpers for the sample to format values.
///
internal static class Formatting
{
///
/// Prints a matrix in a properly formatted way.
///
/// The input array representing the matrix to print.
/// The width of the array to print.
/// The height of the array to print.
/// The name of the matrix to print.
public static void PrintMatrix(float[] array, int width, int height, string name)
{
Guard.IsNotNull(array);
Guard.IsGreaterThan(width, 0);
Guard.IsGreaterThan(height, 0);
Guard.IsEqualTo(array.Length, (long)width * height, nameof(array));
Guard.IsNotNullOrEmpty(name);
int pad = 48 - name.Length;
string title = $"{new string('=', pad / 2)} {name} {new string('=', (pad + 1) / 2)}";
Console.WriteLine(title);
int numberWidth = Math.Max(array.Max().ToString(CultureInfo.InvariantCulture).Length, 4);
for (int i = 0; i < height; i++)
{
var row = array.AsSpan(i * width, width).ToArray();
var text = string.Join(",", row.Select(x => x.ToString(CultureInfo.InvariantCulture).PadLeft(numberWidth)));
Console.WriteLine(text);
}
Console.WriteLine(new string('=', 50));
}
}