Skip to content

Flat elements

zmjack edited this page Jun 12, 2023 · 1 revision

Any.Flat

Creates a one-dimensional enumeration containing all elements of the specified multidimensional arrays.


Flat any array

Example 1 ( Flat multidimensional array )

var d2 = new string[2, 2]
{
    { "0", "1" },
    { "2", "3" }
};

Console.WriteLine(
    Any.Flat<string>(d2).Join(", ")
);

0, 1, 2, 3


Example 2 ( Flat jagged array )

var d1_d1 = new string[2][]
{
    new string[] { "0", "1" },
    new string[] { "2", "3" },
};

Console.WriteLine(
    Any.Flat<string>(d1_d1).Join(", ")
);

0, 1, 2, 3


Example 3(Flat jagged multidimensional array)

var d1_d2 = new string[2][,]
{
    new string[2, 2]
    {
        { "0", "1" },
        { "2", "3" }
    },
    new string[2, 2]
    {
        { "4", "5" },
        { "6", "7" }
    },
};

Console.WriteLine(
	Any.Flat<string>(d1_d2).Join(", ")
);

0, 1, 2, 3, 4, 5, 6, 7


Example 4 ( Flat nested array )

var array = new object[2]
{
    new string[2] { "0", "1" },
    new object[2]
    {
        "2",
        new string[2]
        {
            "3", "4"
        }
    }
};

Console.WriteLine(
    Any.Flat<string>(array).Join(", ")
);

0, 1, 2, 3, 4


Flat unmanaged array ( use pointer )

Example 5 ( Flat unmanaged multidimensional array )

var d2 = new int[2, 2]
{
    { 0, 1 },
    { 2, 3 }
};
var length = d2.GetSequenceLength();

fixed (int* pd2 = d2)
{
    Console.WriteLine(
        Any.Flat(pd2, length).Join(", ")
    );
}

0, 1, 2, 3

Example 6 ( Flat unmanaged jagged array )

var d1_d1 = new int[2][]
{
    new int[] { 0, 1 },
    new int[] { 2, 3 },
};
var lengths = d1_d1.Select(x => x.GetSequenceLength()).ToArray();

fixed (int* pd0 = d1_d1[0])
fixed (int* pd1 = d1_d1[1])
{
    Console.WriteLine(
        Any.Flat(new[] { pd0, pd1 }, lengths).Join(", ")
    );
}

0, 1, 2, 3


Cases

Sum all the elements of each array

var numbers = new[]
{
    new[] { 1, 2, 3 },
    new[] { 4, 5, 6 },
    new[] { 7, 8, 9 },
};

Console.WriteLine(
    Any.Flat<int>(numbers).Sum(x => x)
);

45