Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Emit;
Expand Down
233 changes: 140 additions & 93 deletions src/Dependencies/Collections/Extensions/ImmutableArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,99 +1031,6 @@ public static bool IsSubsetOf<TElement>(this ImmutableArray<TElement> array, Imm
set.Free();
return true;
}

#region Binary Compat

// These helpers should be in System.Linq namespace below. However, they are currently
// used by partners with IVT access to Roslyn, so they have to stay here for now.

/// <summary>
/// Specialization of <see cref="System.Linq.Enumerable.Concat{TSource}(IEnumerable{TSource}, IEnumerable{TSource})"/> for <see cref="ImmutableArray{T}"/>.
/// </summary>
public static ImmutableArray<T> Concat<T>(this ImmutableArray<T> first, ImmutableArray<T> second)
=> first.AddRange(second);

/// <summary>
/// Variant of <see cref="System.Linq.Enumerable.Concat{TSource}(IEnumerable{TSource}, IEnumerable{TSource})"/>.
/// </summary>
public static ImmutableArray<T> Concat<T>(this ImmutableArray<T> first, ImmutableArray<T> second, ImmutableArray<T> third)
{
var builder = new T[first.Length + second.Length + third.Length];

var index = 0;
first.CopyTo(builder, index);
index += first.Length;
second.CopyTo(builder, index);
index += second.Length;
third.CopyTo(builder, index);

return ImmutableCollectionsMarshal.AsImmutableArray(builder);
}

/// <summary>
/// Variant of <see cref="System.Linq.Enumerable.Concat{TSource}(IEnumerable{TSource}, IEnumerable{TSource})"/>.
/// </summary>
public static ImmutableArray<T> Concat<T>(this ImmutableArray<T> first, ImmutableArray<T> second, ImmutableArray<T> third, ImmutableArray<T> fourth)
{
var builder = new T[first.Length + second.Length + third.Length + fourth.Length];

var index = 0;
first.CopyTo(builder, index);
index += first.Length;
second.CopyTo(builder, index);
index += second.Length;
third.CopyTo(builder, index);
index += third.Length;
fourth.CopyTo(builder, index);

return ImmutableCollectionsMarshal.AsImmutableArray(builder);
}

/// <summary>
/// Variant of <see cref="System.Linq.Enumerable.Concat{TSource}(IEnumerable{TSource}, IEnumerable{TSource})"/>.
/// </summary>
public static ImmutableArray<T> Concat<T>(this ImmutableArray<T> first, ImmutableArray<T> second, ImmutableArray<T> third, ImmutableArray<T> fourth, ImmutableArray<T> fifth)
{
var builder = new T[first.Length + second.Length + third.Length + fourth.Length + fifth.Length];

var index = 0;
first.CopyTo(builder, index);
index += first.Length;
second.CopyTo(builder, index);
index += second.Length;
third.CopyTo(builder, index);
index += third.Length;
fourth.CopyTo(builder, index);
index += fourth.Length;
fifth.CopyTo(builder, index);

return ImmutableCollectionsMarshal.AsImmutableArray(builder);
}

/// <summary>
/// Variant of <see cref="System.Linq.Enumerable.Concat{TSource}(IEnumerable{TSource}, IEnumerable{TSource})"/>.
/// </summary>
public static ImmutableArray<T> Concat<T>(this ImmutableArray<T> first, ImmutableArray<T> second, ImmutableArray<T> third, ImmutableArray<T> fourth, ImmutableArray<T> fifth, ImmutableArray<T> sixth)
{
var builder = new T[first.Length + second.Length + third.Length + fourth.Length + fifth.Length + sixth.Length];

var index = 0;
first.CopyTo(builder, index);
index += first.Length;
second.CopyTo(builder, index);
index += second.Length;
third.CopyTo(builder, index);
index += third.Length;
fourth.CopyTo(builder, index);
index += fourth.Length;
fifth.CopyTo(builder, index);
index += fifth.Length;
sixth.CopyTo(builder, index);

return ImmutableCollectionsMarshal.AsImmutableArray(builder);
}

#endregion
}
}

Expand Down Expand Up @@ -1262,12 +1169,152 @@ public static int Sum<T>(this ImmutableArray<T> items, Func<T, int, int> selecto
return sum;
}

/// <summary>
/// Specialization of <see cref="System.Linq.Enumerable.Concat{TSource}(IEnumerable{TSource}, IEnumerable{TSource})"/> for <see cref="ImmutableArray{T}"/>.
/// </summary>
public static ImmutableArray<T> Concat<T>(this ImmutableArray<T> first, ImmutableArray<T> second)
=> first.AddRange(second);

/// <summary>
/// Variant of <see cref="System.Linq.Enumerable.Concat{TSource}(IEnumerable{TSource}, IEnumerable{TSource})"/>.
/// </summary>
public static ImmutableArray<T> Concat<T>(this ImmutableArray<T> first, T second)
=> first.Add(second);

/// <summary>
/// Variant of <see cref="System.Linq.Enumerable.Concat{TSource}(IEnumerable{TSource}, IEnumerable{TSource})"/>.
/// </summary>
public static ImmutableArray<T> Concat<T>(this ImmutableArray<T> first, ImmutableArray<T> second, ImmutableArray<T> third)
{
var builder = new T[first.Length + second.Length + third.Length];
var index = 0;

foreach (var item in first)
{
builder[index++] = item;
}

foreach (var item in second)
{
builder[index++] = item;
}

foreach (var item in third)
{
builder[index++] = item;
}

return ImmutableCollectionsMarshal.AsImmutableArray(builder);
}

/// <summary>
/// Variant of <see cref="System.Linq.Enumerable.Concat{TSource}(IEnumerable{TSource}, IEnumerable{TSource})"/>.
/// </summary>
public static ImmutableArray<T> Concat<T>(this ImmutableArray<T> first, ImmutableArray<T> second, ImmutableArray<T> third, ImmutableArray<T> fourth)
{
var builder = new T[first.Length + second.Length + third.Length + fourth.Length];
var index = 0;

foreach (var item in first)
{
builder[index++] = item;
}

foreach (var item in second)
{
builder[index++] = item;
}

foreach (var item in third)
{
builder[index++] = item;
}

foreach (var item in fourth)
{
builder[index++] = item;
}

return ImmutableCollectionsMarshal.AsImmutableArray(builder);
}

/// <summary>
/// Variant of <see cref="System.Linq.Enumerable.Concat{TSource}(IEnumerable{TSource}, IEnumerable{TSource})"/>.
/// </summary>
public static ImmutableArray<T> Concat<T>(this ImmutableArray<T> first, ImmutableArray<T> second, ImmutableArray<T> third, ImmutableArray<T> fourth, ImmutableArray<T> fifth)
{
var builder = new T[first.Length + second.Length + third.Length + fourth.Length + fifth.Length];
var index = 0;

foreach (var item in first)
{
builder[index++] = item;
}

foreach (var item in second)
{
builder[index++] = item;
}

foreach (var item in third)
{
builder[index++] = item;
}

foreach (var item in fourth)
{
builder[index++] = item;
}

foreach (var item in fifth)
{
builder[index++] = item;
}

return ImmutableCollectionsMarshal.AsImmutableArray(builder);
}

/// <summary>
/// Variant of <see cref="System.Linq.Enumerable.Concat{TSource}(IEnumerable{TSource}, IEnumerable{TSource})"/>.
/// </summary>
public static ImmutableArray<T> Concat<T>(this ImmutableArray<T> first, ImmutableArray<T> second, ImmutableArray<T> third, ImmutableArray<T> fourth, ImmutableArray<T> fifth, ImmutableArray<T> sixth)
{
var builder = new T[first.Length + second.Length + third.Length + fourth.Length + fifth.Length + sixth.Length];
var index = 0;

foreach (var item in first)
{
builder[index++] = item;
}

foreach (var item in second)
{
builder[index++] = item;
}

foreach (var item in third)
{
builder[index++] = item;
}

foreach (var item in fourth)
{
builder[index++] = item;
}

foreach (var item in fifth)
{
builder[index++] = item;
}

foreach (var item in sixth)
{
builder[index++] = item;
}

return ImmutableCollectionsMarshal.AsImmutableArray(builder);
}

/// <summary>
/// Returns an array of distinct elements, preserving the order in the original array.
/// If the array has no duplicates, the original array is returned. The original array must not be null.
Expand Down