// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Microsoft.ClearScript.Util
{
internal static class ArrayHelpers
{
public static void Iterate(this Array array, Action action)
{
if (array.Rank > 0)
{
var dimensions = Enumerable.Range(0, array.Rank);
if (dimensions.Aggregate(1, (count, dimension) => count * array.GetLength(dimension)) > 0)
{
Iterate(array, new int[array.Rank], 0, action);
}
}
}
private static void Iterate(Array array, int[] indices, int dimension, Action action)
{
if (dimension >= indices.Length)
{
action(indices);
}
else
{
var lowerBound = array.GetLowerBound(dimension);
var upperBound = array.GetUpperBound(dimension);
for (var index = lowerBound; index <= upperBound; index++)
{
indices[dimension] = index;
Iterate(array, indices, dimension + 1, action);
}
}
}
public static T[] GetEmptyArray()
{
return EmptyArray.Value;
}
public static async IAsyncEnumerable AsAsyncEnumerable(this T[] array)
{
foreach (var item in array)
{
await Task.CompletedTask;
yield return item;
}
}
#region Nested type: EmptyArray
private static class EmptyArray
{
public static readonly T[] Value = new T[0];
}
#endregion
}
}