Skip to main content
Tweeted twitter.com/StackCodeReview/status/1354489398428135434
Update question description
Source Link
JimmyHu
  • 7.6k
  • 2
  • 11
  • 48

The output of the test code above:

Two dimensional case
0.1     1.1
2.1     3.1

Three dimensional case
dim1 = 0
0.1     1.1
2.1     3.1

dim1 = 1
0.1     1.1
2.1     3.1

Four dimensional case
dim1 = 0, dim2 = 0
0.1     1.1
2.1     3.1

dim1 = 0, dim2 = 1
0.1     1.1
2.1     3.1


dim1 = 1, dim2 = 0
0.1     1.1
2.1     3.1

dim1 = 1, dim2 = 1
0.1     1.1
2.1     3.1

The output of the test code above:

Two dimensional case
0.1     1.1
2.1     3.1

Three dimensional case
dim1 = 0
0.1     1.1
2.1     3.1

dim1 = 1
0.1     1.1
2.1     3.1

Four dimensional case
dim1 = 0, dim2 = 0
0.1     1.1
2.1     3.1

dim1 = 0, dim2 = 1
0.1     1.1
2.1     3.1


dim1 = 1, dim2 = 0
0.1     1.1
2.1     3.1

dim1 = 1, dim2 = 1
0.1     1.1
2.1     3.1
Update test cases
Source Link
JimmyHu
  • 7.6k
  • 2
  • 11
  • 48

The test cases listed here include two dimensional case and, three dimensional case and four dimensional case.

Console.WriteLine("Two dimensional case");

int[,] ii = { { 0, 1 }, { 2, 3 } };
double[,] dd = Converters.ConvertAll(ii, x => x + 0.1);

for (intlong row = 0; row < dd.GetLengthGetLongLength(0); row++)
{
    for (intlong column = 0; column < dd.GetLengthGetLongLength(1); column++)
    {
        Console.Write(dd[row, column].ToString() + "\t");
    }
    Console.WriteLine();
}

Console.WriteLine();
Console.WriteLine("Three dimensional case");

int[,,] iii = { { { 0, 1 }, { 2, 3 } } , { { 0, 1 }, { 2, 3 } } };
double[,,] ddd = Converters.ConvertAll(iii, x => x + 0.1);

for (intlong dim1 = 0; dim1 < ddd.GetLengthGetLongLength(0); dim1++)
{
    Console.WriteLine($"dim1 = {dim1}");
    for (intlong dim2 = 0; dim2 < ddd.GetLengthGetLongLength(0); dim2++)
    {
        for (intlong dim3 = 0; dim3 < ddddd.GetLengthGetLongLength(1); dim3++)
        {
            Console.Write(ddd[dim1, dim2, dim3].ToString() + "\t");
        }
        Console.WriteLine();
    }
    Console.WriteLine();
}

Console.WriteLine("Four dimensional case");

int[,,,] iiii = { { { { 0, 1 }, { 2, 3 } }, { { 0, 1 }, { 2, 3 } } }, { { { 0, 1 }, { 2, 3 } }, { { 0, 1 }, { 2, 3 } } } };
var dddd = Converters.ConvertAll(iiii, x => x + 0.1);

for (long dim1 = 0; dim1 < dddd.GetLongLength(0); dim1++)
{
    for (long dim2 = 0; dim2 < dddd.GetLongLength(1); dim2++)
    {
        Console.WriteLine($"dim1 = {dim1}, dim2 = {dim2}");
        for (long dim3 = 0; dim3 < dddd.GetLongLength(2); dim3++)
        {
            for (long dim4 = 0; dim4 < dddd.GetLongLength(3); dim4++)
            {
                Console.Write(dddd[dim1, dim2, dim3, dim4].ToString() + "\t");
            }
            Console.WriteLine();
        }
        Console.WriteLine();
    }
    Console.WriteLine();
}

The test cases listed here include two dimensional case and three dimensional case.

Console.WriteLine("Two dimensional case");

int[,] ii = { { 0, 1 }, { 2, 3 } };
double[,] dd = Converters.ConvertAll(ii, x => x + 0.1);

for (int row = 0; row < dd.GetLength(0); row++)
{
    for (int column = 0; column < dd.GetLength(1); column++)
    {
        Console.Write(dd[row, column].ToString() + "\t");
    }
    Console.WriteLine();
}

Console.WriteLine("Three dimensional case");

int[,,] iii = { { { 0, 1 }, { 2, 3 } } , { { 0, 1 }, { 2, 3 } } };
double[,,] ddd = Converters.ConvertAll(iii, x => x + 0.1);

for (int dim1 = 0; dim1 < ddd.GetLength(0); dim1++)
{
    for (int dim2 = 0; dim2 < ddd.GetLength(0); dim2++)
    {
        for (int dim3 = 0; dim3 < dd.GetLength(1); dim3++)
        {
            Console.Write(ddd[dim1, dim2, dim3].ToString() + "\t");
        }
        Console.WriteLine();
    }
    Console.WriteLine();
    Console.WriteLine();
}

The test cases listed here include two dimensional case, three dimensional case and four dimensional case.

Console.WriteLine("Two dimensional case");

int[,] ii = { { 0, 1 }, { 2, 3 } };
double[,] dd = Converters.ConvertAll(ii, x => x + 0.1);

for (long row = 0; row < dd.GetLongLength(0); row++)
{
    for (long column = 0; column < dd.GetLongLength(1); column++)
    {
        Console.Write(dd[row, column].ToString() + "\t");
    }
    Console.WriteLine();
}

Console.WriteLine();
Console.WriteLine("Three dimensional case");

int[,,] iii = { { { 0, 1 }, { 2, 3 } } , { { 0, 1 }, { 2, 3 } } };
double[,,] ddd = Converters.ConvertAll(iii, x => x + 0.1);

for (long dim1 = 0; dim1 < ddd.GetLongLength(0); dim1++)
{
    Console.WriteLine($"dim1 = {dim1}");
    for (long dim2 = 0; dim2 < ddd.GetLongLength(0); dim2++)
    {
        for (long dim3 = 0; dim3 < ddd.GetLongLength(1); dim3++)
        {
            Console.Write(ddd[dim1, dim2, dim3].ToString() + "\t");
        }
        Console.WriteLine();
    }
    Console.WriteLine();
}

Console.WriteLine("Four dimensional case");

int[,,,] iiii = { { { { 0, 1 }, { 2, 3 } }, { { 0, 1 }, { 2, 3 } } }, { { { 0, 1 }, { 2, 3 } }, { { 0, 1 }, { 2, 3 } } } };
var dddd = Converters.ConvertAll(iiii, x => x + 0.1);

for (long dim1 = 0; dim1 < dddd.GetLongLength(0); dim1++)
{
    for (long dim2 = 0; dim2 < dddd.GetLongLength(1); dim2++)
    {
        Console.WriteLine($"dim1 = {dim1}, dim2 = {dim2}");
        for (long dim3 = 0; dim3 < dddd.GetLongLength(2); dim3++)
        {
            for (long dim4 = 0; dim4 < dddd.GetLongLength(3); dim4++)
            {
                Console.Write(dddd[dim1, dim2, dim3, dim4].ToString() + "\t");
            }
            Console.WriteLine();
        }
        Console.WriteLine();
    }
    Console.WriteLine();
}
Update code block content
Source Link
JimmyHu
  • 7.6k
  • 2
  • 11
  • 48
public class Converters
{
    public static TOutput[,] ConvertAll<TInput, TOutput>(TInput[,] array, Converter<TInput, TOutput> converter)
    {
        if (array is null)
        {
            throw new ArgumentNullException(nameof(array));
        }

        if (converter is null)
        {
            throw new ArgumentNullException(nameof(converter));
        }

        var output = new TOutput[array.GetLengthGetLongLength(0), array.GetLengthGetLongLength(1)];
        for (intlong row = 0; row < array.GetLengthGetLongLength(0); row++)
        {
            for (intlong column = 0; column < array.GetLengthGetLongLength(1); column++)
            {
                output[row, column] = converter(array[row, column]);
            }
        }
        return output;
    }

    public static TOutput[,,] ConvertAll<TInput, TOutput>(TInput[,,] array, Converter<TInput, TOutput> converter)
    {
        if (array is null)
        {
            throw new ArgumentNullException(nameof(array));
        }

        if (converter is null)
        {
            throw new ArgumentNullException(nameof(converter));
        }

        var output = new TOutput[array.GetLengthGetLongLength(0), array.GetLengthGetLongLength(1), array.GetLengthGetLongLength(2)];
        for (intlong dim1 = 0; dim1 < array.GetLengthGetLongLength(0); dim1++)
        {
            for (intlong dim2 = 0; dim2 < array.GetLengthGetLongLength(1); dim2++)
            {
                for (intlong dim3 = 0; dim3 < array.GetLengthGetLongLength(2); dim3++)
                {
                    output[dim1, dim2, dim3] = converter(array[dim1, dim2, dim3]);
                }
            }
        }
        return output;
    }

    public static TOutput[,,,] ConvertAll<TInput, TOutput>(TInput[,,,] array, Converter<TInput, TOutput> converter)
    {
        if (array is null)
        {
            throw new ArgumentNullException(nameof(array));
        }

        if (converter is null)
        {
            throw new ArgumentNullException(nameof(converter));
        }

        var output = new TOutput[array.GetLengthGetLongLength(0), array.GetLengthGetLongLength(1), array.GetLengthGetLongLength(2), array.GetLengthGetLongLength(3)];
        for (intlong dim1 = 0; dim1 < array.GetLengthGetLongLength(0); dim1++)
        {
            for (intlong dim2 = 0; dim2 < array.GetLengthGetLongLength(1); dim2++)
            {
                for (intlong dim3 = 0; dim3 < array.GetLengthGetLongLength(2); dim3++)
                {
                    for (intlong dim4 = 0; dim4 < array.GetLengthGetLongLength(3); dim4++)
                    {
                        output[dim1, dim2, dim3, dim4] = converter(array[dim1, dim2, dim3, dim4]);
                    }
                }
            }
        }
        return output;
    }

    public static TOutput[,,,,] ConvertAll<TInput, TOutput>(TInput[,,,,] array, Converter<TInput, TOutput> converter)
    {
        if (array is null)
        {
            throw new ArgumentNullException(nameof(array));
        }

        if (converter is null)
        {
            throw new ArgumentNullException(nameof(converter));
        }

        var output = new TOutput[array.GetLengthGetLongLength(0), array.GetLengthGetLongLength(1), array.GetLengthGetLongLength(2), array.GetLengthGetLongLength(3), array.GetLengthGetLongLength(4)];
        for (intlong dim1 = 0; dim1 < array.GetLengthGetLongLength(0); dim1++)
        {
            for (intlong dim2 = 0; dim2 < array.GetLengthGetLongLength(1); dim2++)
            {
                for (intlong dim3 = 0; dim3 < array.GetLengthGetLongLength(2); dim3++)
                {
                    for (intlong dim4 = 0; dim4 < array.GetLengthGetLongLength(3); dim4++)
                    {
                        for (intlong dim5 = 0; dim5 < array.GetLengthGetLongLength(4); dim5++)
                        {
                            output[dim1, dim2, dim3, dim4, dim5] = converter(array[dim1, dim2, dim3, dim4, dim5]);
                        }
                    }
                }
            }
        }
        return output;
    }

    public static TOutput[,,,,,] ConvertAll<TInput, TOutput>(TInput[,,,,,] array, Converter<TInput, TOutput> converter)
    {
        if (array is null)
        {
            throw new ArgumentNullException(nameof(array));
        }

        if (converter is null)
        {
            throw new ArgumentNullException(nameof(converter));
        }

        var output = new TOutput[array.GetLengthGetLongLength(0), array.GetLengthGetLongLength(1), array.GetLengthGetLongLength(2), array.GetLengthGetLongLength(3), array.GetLengthGetLongLength(4), array.GetLengthGetLongLength(5)];
        for (intlong dim1 = 0; dim1 < array.GetLengthGetLongLength(0); dim1++)
        {
            for (intlong dim2 = 0; dim2 < array.GetLengthGetLongLength(1); dim2++)
            {
                for (intlong dim3 = 0; dim3 < array.GetLengthGetLongLength(2); dim3++)
                {
                    for (intlong dim4 = 0; dim4 < array.GetLengthGetLongLength(3); dim4++)
                    {
                        for (intlong dim5 = 0; dim5 < array.GetLengthGetLongLength(4); dim5++)
                        {
                            for (intlong dim6 = 0; dim6 < array.GetLengthGetLongLength(5); dim6++)
                            {
                                output[dim1, dim2, dim3, dim4, dim5, dim6] = converter(array[dim1, dim2, dim3, dim4, dim5, dim6]);
                            }
                        }
                    }
                }
            }
        }
        return output;
    }
}
public class Converters
{
    public static TOutput[,] ConvertAll<TInput, TOutput>(TInput[,] array, Converter<TInput, TOutput> converter)
    {
        if (array is null)
        {
            throw new ArgumentNullException(nameof(array));
        }

        if (converter is null)
        {
            throw new ArgumentNullException(nameof(converter));
        }

        var output = new TOutput[array.GetLength(0), array.GetLength(1)];
        for (int row = 0; row < array.GetLength(0); row++)
        {
            for (int column = 0; column < array.GetLength(1); column++)
            {
                output[row, column] = converter(array[row, column]);
            }
        }
        return output;
    }

    public static TOutput[,,] ConvertAll<TInput, TOutput>(TInput[,,] array, Converter<TInput, TOutput> converter)
    {
        if (array is null)
        {
            throw new ArgumentNullException(nameof(array));
        }

        if (converter is null)
        {
            throw new ArgumentNullException(nameof(converter));
        }

        var output = new TOutput[array.GetLength(0), array.GetLength(1), array.GetLength(2)];
        for (int dim1 = 0; dim1 < array.GetLength(0); dim1++)
        {
            for (int dim2 = 0; dim2 < array.GetLength(1); dim2++)
            {
                for (int dim3 = 0; dim3 < array.GetLength(2); dim3++)
                {
                    output[dim1, dim2, dim3] = converter(array[dim1, dim2, dim3]);
                }
            }
        }
        return output;
    }

    public static TOutput[,,,] ConvertAll<TInput, TOutput>(TInput[,,,] array, Converter<TInput, TOutput> converter)
    {
        if (array is null)
        {
            throw new ArgumentNullException(nameof(array));
        }

        if (converter is null)
        {
            throw new ArgumentNullException(nameof(converter));
        }

        var output = new TOutput[array.GetLength(0), array.GetLength(1), array.GetLength(2), array.GetLength(3)];
        for (int dim1 = 0; dim1 < array.GetLength(0); dim1++)
        {
            for (int dim2 = 0; dim2 < array.GetLength(1); dim2++)
            {
                for (int dim3 = 0; dim3 < array.GetLength(2); dim3++)
                {
                    for (int dim4 = 0; dim4 < array.GetLength(3); dim4++)
                    {
                        output[dim1, dim2, dim3, dim4] = converter(array[dim1, dim2, dim3, dim4]);
                    }
                }
            }
        }
        return output;
    }

    public static TOutput[,,,,] ConvertAll<TInput, TOutput>(TInput[,,,,] array, Converter<TInput, TOutput> converter)
    {
        if (array is null)
        {
            throw new ArgumentNullException(nameof(array));
        }

        if (converter is null)
        {
            throw new ArgumentNullException(nameof(converter));
        }

        var output = new TOutput[array.GetLength(0), array.GetLength(1), array.GetLength(2), array.GetLength(3), array.GetLength(4)];
        for (int dim1 = 0; dim1 < array.GetLength(0); dim1++)
        {
            for (int dim2 = 0; dim2 < array.GetLength(1); dim2++)
            {
                for (int dim3 = 0; dim3 < array.GetLength(2); dim3++)
                {
                    for (int dim4 = 0; dim4 < array.GetLength(3); dim4++)
                    {
                        for (int dim5 = 0; dim5 < array.GetLength(4); dim5++)
                        {
                            output[dim1, dim2, dim3, dim4, dim5] = converter(array[dim1, dim2, dim3, dim4, dim5]);
                        }
                    }
                }
            }
        }
        return output;
    }

    public static TOutput[,,,,,] ConvertAll<TInput, TOutput>(TInput[,,,,,] array, Converter<TInput, TOutput> converter)
    {
        if (array is null)
        {
            throw new ArgumentNullException(nameof(array));
        }

        if (converter is null)
        {
            throw new ArgumentNullException(nameof(converter));
        }

        var output = new TOutput[array.GetLength(0), array.GetLength(1), array.GetLength(2), array.GetLength(3), array.GetLength(4), array.GetLength(5)];
        for (int dim1 = 0; dim1 < array.GetLength(0); dim1++)
        {
            for (int dim2 = 0; dim2 < array.GetLength(1); dim2++)
            {
                for (int dim3 = 0; dim3 < array.GetLength(2); dim3++)
                {
                    for (int dim4 = 0; dim4 < array.GetLength(3); dim4++)
                    {
                        for (int dim5 = 0; dim5 < array.GetLength(4); dim5++)
                        {
                            for (int dim6 = 0; dim6 < array.GetLength(5); dim6++)
                            {
                                output[dim1, dim2, dim3, dim4, dim5, dim6] = converter(array[dim1, dim2, dim3, dim4, dim5, dim6]);
                            }
                        }
                    }
                }
            }
        }
        return output;
    }
}
public class Converters
{
    public static TOutput[,] ConvertAll<TInput, TOutput>(TInput[,] array, Converter<TInput, TOutput> converter)
    {
        if (array is null)
        {
            throw new ArgumentNullException(nameof(array));
        }

        if (converter is null)
        {
            throw new ArgumentNullException(nameof(converter));
        }

        var output = new TOutput[array.GetLongLength(0), array.GetLongLength(1)];
        for (long row = 0; row < array.GetLongLength(0); row++)
        {
            for (long column = 0; column < array.GetLongLength(1); column++)
            {
                output[row, column] = converter(array[row, column]);
            }
        }
        return output;
    }

    public static TOutput[,,] ConvertAll<TInput, TOutput>(TInput[,,] array, Converter<TInput, TOutput> converter)
    {
        if (array is null)
        {
            throw new ArgumentNullException(nameof(array));
        }

        if (converter is null)
        {
            throw new ArgumentNullException(nameof(converter));
        }

        var output = new TOutput[array.GetLongLength(0), array.GetLongLength(1), array.GetLongLength(2)];
        for (long dim1 = 0; dim1 < array.GetLongLength(0); dim1++)
        {
            for (long dim2 = 0; dim2 < array.GetLongLength(1); dim2++)
            {
                for (long dim3 = 0; dim3 < array.GetLongLength(2); dim3++)
                {
                    output[dim1, dim2, dim3] = converter(array[dim1, dim2, dim3]);
                }
            }
        }
        return output;
    }

    public static TOutput[,,,] ConvertAll<TInput, TOutput>(TInput[,,,] array, Converter<TInput, TOutput> converter)
    {
        if (array is null)
        {
            throw new ArgumentNullException(nameof(array));
        }

        if (converter is null)
        {
            throw new ArgumentNullException(nameof(converter));
        }

        var output = new TOutput[array.GetLongLength(0), array.GetLongLength(1), array.GetLongLength(2), array.GetLongLength(3)];
        for (long dim1 = 0; dim1 < array.GetLongLength(0); dim1++)
        {
            for (long dim2 = 0; dim2 < array.GetLongLength(1); dim2++)
            {
                for (long dim3 = 0; dim3 < array.GetLongLength(2); dim3++)
                {
                    for (long dim4 = 0; dim4 < array.GetLongLength(3); dim4++)
                    {
                        output[dim1, dim2, dim3, dim4] = converter(array[dim1, dim2, dim3, dim4]);
                    }
                }
            }
        }
        return output;
    }

    public static TOutput[,,,,] ConvertAll<TInput, TOutput>(TInput[,,,,] array, Converter<TInput, TOutput> converter)
    {
        if (array is null)
        {
            throw new ArgumentNullException(nameof(array));
        }

        if (converter is null)
        {
            throw new ArgumentNullException(nameof(converter));
        }

        var output = new TOutput[array.GetLongLength(0), array.GetLongLength(1), array.GetLongLength(2), array.GetLongLength(3), array.GetLongLength(4)];
        for (long dim1 = 0; dim1 < array.GetLongLength(0); dim1++)
        {
            for (long dim2 = 0; dim2 < array.GetLongLength(1); dim2++)
            {
                for (long dim3 = 0; dim3 < array.GetLongLength(2); dim3++)
                {
                    for (long dim4 = 0; dim4 < array.GetLongLength(3); dim4++)
                    {
                        for (long dim5 = 0; dim5 < array.GetLongLength(4); dim5++)
                        {
                            output[dim1, dim2, dim3, dim4, dim5] = converter(array[dim1, dim2, dim3, dim4, dim5]);
                        }
                    }
                }
            }
        }
        return output;
    }

    public static TOutput[,,,,,] ConvertAll<TInput, TOutput>(TInput[,,,,,] array, Converter<TInput, TOutput> converter)
    {
        if (array is null)
        {
            throw new ArgumentNullException(nameof(array));
        }

        if (converter is null)
        {
            throw new ArgumentNullException(nameof(converter));
        }

        var output = new TOutput[array.GetLongLength(0), array.GetLongLength(1), array.GetLongLength(2), array.GetLongLength(3), array.GetLongLength(4), array.GetLongLength(5)];
        for (long dim1 = 0; dim1 < array.GetLongLength(0); dim1++)
        {
            for (long dim2 = 0; dim2 < array.GetLongLength(1); dim2++)
            {
                for (long dim3 = 0; dim3 < array.GetLongLength(2); dim3++)
                {
                    for (long dim4 = 0; dim4 < array.GetLongLength(3); dim4++)
                    {
                        for (long dim5 = 0; dim5 < array.GetLongLength(4); dim5++)
                        {
                            for (long dim6 = 0; dim6 < array.GetLongLength(5); dim6++)
                            {
                                output[dim1, dim2, dim3, dim4, dim5, dim6] = converter(array[dim1, dim2, dim3, dim4, dim5, dim6]);
                            }
                        }
                    }
                }
            }
        }
        return output;
    }
}
Update code block content
Source Link
JimmyHu
  • 7.6k
  • 2
  • 11
  • 48
Loading
Add generics tag
Link
JimmyHu
  • 7.6k
  • 2
  • 11
  • 48
Loading
Add lambda tag
Link
JimmyHu
  • 7.6k
  • 2
  • 11
  • 48
Loading
Source Link
JimmyHu
  • 7.6k
  • 2
  • 11
  • 48
Loading