未验证 提交 d8e8b3fe 编写于 作者: T Tomáš Matoušek 提交者: GitHub

Add SelectAsArray, DangerousCreateFromUnderlyingArray (#34559)

* Add SelectAsArray, DangerousCreateFromUnderlyingArray

* Add SerializableBytes.ToImmutableArray
上级 5d7bccfc
......@@ -11,7 +11,7 @@
namespace Microsoft.CodeAnalysis.UnitTests.Collections
{
public class ReadOnlyArrayTests
public class ImmutableArrayExtensionsTests
{
[Fact]
public void CreateFrom()
......@@ -365,6 +365,28 @@ public void SelectAsArray()
AssertEx.Equal(new[] { 10, 20, 30, 40, 50 }, ImmutableArray.Create(1, 2, 3, 4, 5).SelectAsArray(i => 10 * i));
}
[Fact]
public void SelectAsArrayWithPredicate()
{
Assert.Empty(ImmutableArray<object>.Empty.SelectAsArray<object, int>(item => throw null, item => throw null));
var array = ImmutableArray.Create(1, 2, 3, 4, 5);
AssertEx.Equal(new[] { 2, 3, 4, 5, 6 }, array.SelectAsArray(item => true, item => item + 1));
AssertEx.Equal(new[] { 3, 5 }, array.SelectAsArray(item => item % 2 == 0, item => item + 1));
Assert.Empty(array.SelectAsArray<int, int>(item => item < 0, item => throw null));
}
[Fact]
public void DangerousCreateFromUnderlyingArray()
{
var array = new[] { 1, 2, 3, 4 };
var copy = array;
var immutable = ImmutableArrayExtensions.DangerousCreateFromUnderlyingArray(ref copy);
Assert.Null(copy);
AssertEx.Equal(array, immutable);
Assert.Same(array, ImmutableArrayExtensions.DangerousGetUnderlyingArray(immutable));
}
[Fact]
public void ZipAsArray()
{
......
......@@ -188,6 +188,34 @@ public static ImmutableArray<byte> ToImmutable(this MemoryStream stream)
}
}
/// <summary>
/// Maps a subset of immutable array to another immutable array.
/// </summary>
/// <typeparam name="TItem">Type of the source array items</typeparam>
/// <typeparam name="TResult">Type of the transformed array items</typeparam>
/// <param name="array">The array to transform</param>
/// <param name="predicate">The condition to use for filtering the array content.</param>
/// <param name="selector">A transform function to apply to each element that is not filtered out by <paramref name="predicate"/>.</param>
/// <returns>If the items's length is 0, this will return an empty immutable array.</returns>
public static ImmutableArray<TResult> SelectAsArray<TItem, TResult>(this ImmutableArray<TItem> array, Func<TItem, bool> predicate, Func<TItem, TResult> selector)
{
if (array.Length == 0)
{
return ImmutableArray<TResult>.Empty;
}
var builder = ArrayBuilder<TResult>.GetInstance();
foreach (var item in array)
{
if (predicate(item))
{
builder.Add(selector(item));
}
}
return builder.ToImmutableAndFree();
}
/// <summary>
/// Zips two immutable arrays together through a mapping function, producing another immutable array.
/// </summary>
......@@ -513,6 +541,13 @@ internal static T[] DangerousGetUnderlyingArray<T>(this ImmutableArray<T> array)
internal static ReadOnlySpan<T> AsSpan<T>(this ImmutableArray<T> array)
=> array.DangerousGetUnderlyingArray();
internal static ImmutableArray<T> DangerousCreateFromUnderlyingArray<T>(ref T[] array)
{
var proxy = new ImmutableArrayProxy<T> { MutableArray = array };
array = null;
return Unsafe.As<ImmutableArrayProxy<T>, ImmutableArray<T>>(ref proxy);
}
internal static Dictionary<K, ImmutableArray<T>> ToDictionary<K, T>(this ImmutableArray<T> items, Func<T, K> keySelector, IEqualityComparer<K> comparer = null)
{
if (items.Length == 1)
......
......@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
......@@ -244,6 +245,12 @@ public byte[] ToArray()
return array;
}
public ImmutableArray<byte> ToImmutableArray()
{
var array = ToArray();
return ImmutableArrayExtensions.DangerousCreateFromUnderlyingArray(ref array);
}
protected int CurrentChunkIndex { get { return GetChunkIndex(this.position); } }
protected int CurrentChunkOffset { get { return GetChunkOffset(this.position); } }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册