Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add span-based static Create overloads for immutable collections #87945

Merged
merged 1 commit into from
Jun 23, 2023
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 @@ -561,8 +561,10 @@ public static partial class ImmutableHashSet
public static System.Collections.Immutable.ImmutableHashSet<T> Create<T>(System.Collections.Generic.IEqualityComparer<T>? equalityComparer) { throw null; }
public static System.Collections.Immutable.ImmutableHashSet<T> Create<T>(System.Collections.Generic.IEqualityComparer<T>? equalityComparer, T item) { throw null; }
public static System.Collections.Immutable.ImmutableHashSet<T> Create<T>(System.Collections.Generic.IEqualityComparer<T>? equalityComparer, params T[] items) { throw null; }
public static System.Collections.Immutable.ImmutableHashSet<T> Create<T>(System.Collections.Generic.IEqualityComparer<T>? equalityComparer, System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.ImmutableHashSet<T> Create<T>(T item) { throw null; }
public static System.Collections.Immutable.ImmutableHashSet<T> Create<T>(params T[] items) { throw null; }
public static System.Collections.Immutable.ImmutableHashSet<T> Create<T>(System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.ImmutableHashSet<TSource> ToImmutableHashSet<TSource>(this System.Collections.Generic.IEnumerable<TSource> source) { throw null; }
public static System.Collections.Immutable.ImmutableHashSet<TSource> ToImmutableHashSet<TSource>(this System.Collections.Generic.IEnumerable<TSource> source, System.Collections.Generic.IEqualityComparer<TSource>? equalityComparer) { throw null; }
public static System.Collections.Immutable.ImmutableHashSet<TSource> ToImmutableHashSet<TSource>(this System.Collections.Immutable.ImmutableHashSet<TSource>.Builder builder) { throw null; }
Expand Down Expand Up @@ -687,6 +689,7 @@ public static partial class ImmutableList
public static System.Collections.Immutable.ImmutableList<T> Create<T>() { throw null; }
public static System.Collections.Immutable.ImmutableList<T> Create<T>(T item) { throw null; }
public static System.Collections.Immutable.ImmutableList<T> Create<T>(params T[] items) { throw null; }
public static System.Collections.Immutable.ImmutableList<T> Create<T>(System.ReadOnlySpan<T> items) { throw null; }
public static int IndexOf<T>(this System.Collections.Immutable.IImmutableList<T> list, T item) { throw null; }
public static int IndexOf<T>(this System.Collections.Immutable.IImmutableList<T> list, T item, System.Collections.Generic.IEqualityComparer<T>? equalityComparer) { throw null; }
public static int IndexOf<T>(this System.Collections.Immutable.IImmutableList<T> list, T item, int startIndex) { throw null; }
Expand Down Expand Up @@ -881,6 +884,7 @@ public static partial class ImmutableQueue
public static System.Collections.Immutable.ImmutableQueue<T> Create<T>() { throw null; }
public static System.Collections.Immutable.ImmutableQueue<T> Create<T>(T item) { throw null; }
public static System.Collections.Immutable.ImmutableQueue<T> Create<T>(params T[] items) { throw null; }
public static System.Collections.Immutable.ImmutableQueue<T> Create<T>(System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.IImmutableQueue<T> Dequeue<T>(this System.Collections.Immutable.IImmutableQueue<T> queue, out T value) { throw null; }
}
public sealed partial class ImmutableQueue<T> : System.Collections.Generic.IEnumerable<T>, System.Collections.IEnumerable, System.Collections.Immutable.IImmutableQueue<T>
Expand Down Expand Up @@ -1055,8 +1059,10 @@ public static partial class ImmutableSortedSet
public static System.Collections.Immutable.ImmutableSortedSet<T> Create<T>(System.Collections.Generic.IComparer<T>? comparer) { throw null; }
public static System.Collections.Immutable.ImmutableSortedSet<T> Create<T>(System.Collections.Generic.IComparer<T>? comparer, T item) { throw null; }
public static System.Collections.Immutable.ImmutableSortedSet<T> Create<T>(System.Collections.Generic.IComparer<T>? comparer, params T[] items) { throw null; }
public static System.Collections.Immutable.ImmutableSortedSet<T> Create<T>(System.Collections.Generic.IComparer<T>? comparer, System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.ImmutableSortedSet<T> Create<T>(T item) { throw null; }
public static System.Collections.Immutable.ImmutableSortedSet<T> Create<T>(params T[] items) { throw null; }
public static System.Collections.Immutable.ImmutableSortedSet<T> Create<T>(System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.ImmutableSortedSet<TSource> ToImmutableSortedSet<TSource>(this System.Collections.Generic.IEnumerable<TSource> source) { throw null; }
public static System.Collections.Immutable.ImmutableSortedSet<TSource> ToImmutableSortedSet<TSource>(this System.Collections.Generic.IEnumerable<TSource> source, System.Collections.Generic.IComparer<TSource>? comparer) { throw null; }
public static System.Collections.Immutable.ImmutableSortedSet<TSource> ToImmutableSortedSet<TSource>(this System.Collections.Immutable.ImmutableSortedSet<TSource>.Builder builder) { throw null; }
Expand Down Expand Up @@ -1187,6 +1193,7 @@ public static partial class ImmutableStack
public static System.Collections.Immutable.ImmutableStack<T> Create<T>() { throw null; }
public static System.Collections.Immutable.ImmutableStack<T> Create<T>(T item) { throw null; }
public static System.Collections.Immutable.ImmutableStack<T> Create<T>(params T[] items) { throw null; }
public static System.Collections.Immutable.ImmutableStack<T> Create<T>(System.ReadOnlySpan<T> items) { throw null; }
public static System.Collections.Immutable.IImmutableStack<T> Pop<T>(this System.Collections.Immutable.IImmutableStack<T> stack, out T value) { throw null; }
}
public sealed partial class ImmutableStack<T> : System.Collections.Generic.IEnumerable<T>, System.Collections.IEnumerable, System.Collections.Immutable.IImmutableStack<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ public static ImmutableHashSet<T> CreateRange<T>(IEqualityComparer<T>? equalityC
/// <param name="items">The items to prepopulate.</param>
/// <returns>The new immutable collection.</returns>
public static ImmutableHashSet<T> Create<T>(params T[] items)
{
Requires.NotNull(items, nameof(items));

return Create((ReadOnlySpan<T>)items);
}

/// <summary>
/// Creates a new immutable collection prefilled with the specified items.
/// </summary>
/// <typeparam name="T">The type of items stored by the collection.</typeparam>
/// <param name="items">The items to prepopulate.</param>
/// <returns>The new immutable collection.</returns>
public static ImmutableHashSet<T> Create<T>(ReadOnlySpan<T> items)
{
return ImmutableHashSet<T>.Empty.Union(items);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this predates your changes, but it strikes me as odd that we're defining a core factory method in terms of a "Union" operation. Not familiar with the Union implementation but it sounds like it might be introducing unnecessary checks on the LHS?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially. Happy for us to address any overheads here separately.

}
Expand All @@ -98,6 +111,20 @@ public static ImmutableHashSet<T> Create<T>(params T[] items)
/// <param name="items">The items to prepopulate.</param>
/// <returns>The new immutable collection.</returns>
public static ImmutableHashSet<T> Create<T>(IEqualityComparer<T>? equalityComparer, params T[] items)
{
Requires.NotNull(items, nameof(items));

return Create(equalityComparer, (ReadOnlySpan<T>)items);
}

/// <summary>
/// Creates a new immutable collection prefilled with the specified items.
/// </summary>
/// <typeparam name="T">The type of items stored by the collection.</typeparam>
/// <param name="equalityComparer">The equality comparer.</param>
/// <param name="items">The items to prepopulate.</param>
/// <returns>The new immutable collection.</returns>
public static ImmutableHashSet<T> Create<T>(IEqualityComparer<T>? equalityComparer, ReadOnlySpan<T> items)
{
return ImmutableHashSet<T>.Empty.WithComparer(equalityComparer).Union(items);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace System.Collections.Immutable
Expand Down Expand Up @@ -239,6 +238,14 @@ public ImmutableHashSet<T> Union(IEnumerable<T> other)
return this.Union(other, avoidWithComparer: false);
}

/// <summary>
/// See the <see cref="IImmutableSet{T}"/> interface.
/// </summary>
internal ImmutableHashSet<T> Union(ReadOnlySpan<T> other)
{
return Union(other, this.Origin).Finalize(this);
}

/// <summary>
/// See the <see cref="IImmutableSet{T}"/> interface.
/// </summary>
Expand Down Expand Up @@ -693,6 +700,29 @@ private static MutationResult Union(IEnumerable<T> other, MutationInput origin)
return new MutationResult(newRoot, count);
}

/// <summary>
/// Performs the set operation on a given data structure.
/// </summary>
private static MutationResult Union(ReadOnlySpan<T> other, MutationInput origin)
{
int count = 0;
SortedInt32KeyNode<ImmutableHashSet<T>.HashBucket> newRoot = origin.Root;
foreach (T item in other)
{
int hashCode = item != null ? origin.EqualityComparer.GetHashCode(item) : 0;
HashBucket bucket = newRoot.GetValueOrDefault(hashCode);
OperationResult result;
ImmutableHashSet<T>.HashBucket newBucket = bucket.Add(item, origin.EqualityComparer, out result);
if (result == OperationResult.SizeChanged)
{
newRoot = UpdateRoot(newRoot, hashCode, origin.HashBucketEqualityComparer, newBucket);
count++;
}
}

return new MutationResult(newRoot, count);
}

/// <summary>
/// Performs the set operation on a given data structure.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,23 @@ public static class ImmutableList
/// <summary>
/// Creates a new immutable collection prefilled with the specified items.
/// </summary>
/// <typeparam name="T">The type of items stored by the collection.</typeparam>
/// <param name="items">The items to prepopulate.</param>
/// <typeparam name="T">The type of items in the immutable list.</typeparam>
/// <param name="items">A span that contains the items to prepopulate the list with.</param>
/// <returns>The new immutable collection.</returns>
public static ImmutableList<T> Create<T>(params T[] items) => ImmutableList<T>.Empty.AddRange(items);
public static ImmutableList<T> Create<T>(params T[] items)
{
Requires.NotNull(items, nameof(items));

return Create((ReadOnlySpan<T>)items);
}

/// <summary>
/// Creates a new immutable list that contains the items from the specified span of items.
/// </summary>
/// <typeparam name="T">The type of items stored by the collection.</typeparam>
/// <param name="items">A span that contains the items to prepopulate the list with.</param>
/// <returns>A new immutable list that contains the specified items.</returns>
public static ImmutableList<T> Create<T>(ReadOnlySpan<T> items) => ImmutableList<T>.Empty.AddRange(items);

/// <summary>
/// Creates a new immutable list builder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public sealed partial class ImmutableList<T>
/// </summary>
/// <remarks>
/// <para>
/// While <see cref="ImmutableList{T}.AddRange"/> and other bulk change methods
/// While <see cref="M:ImmutableList{T}.AddRange"/> and other bulk change methods
/// already provide fast bulk change operations on the collection, this class allows
/// multiple combinations of changes to be made to a set with equal efficiency.
/// </para>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,25 @@ internal static Node NodeTreeFromList(IOrderedCollection<T> items, int start, in
return new Node(items[start + leftCount], left, right, true);
}

/// <summary>
/// Creates a node tree that contains the contents of a span.
/// </summary>
/// <param name="items">A span with the contents that the new node tree should contain.</param>
/// <returns>The root of the created node tree.</returns>
internal static Node NodeTreeFromList(ReadOnlySpan<T> items)
{
if (items.IsEmpty)
{
return EmptyNode;
}

int rightCount = (items.Length - 1) / 2;
int leftCount = (items.Length - 1) - rightCount;
Node left = NodeTreeFromList(items.Slice(0, leftCount));
Node right = NodeTreeFromList(items.Slice(leftCount + 1));
return new Node(items[leftCount], left, right, frozen: true);
}

/// <summary>
/// Adds the specified key to the tree.
/// </summary>
Expand Down Expand Up @@ -343,6 +362,23 @@ internal Node AddRange(IEnumerable<T> keys)
return result.BalanceMany();
}

/// <summary>
/// Adds the specified keys to this tree.
/// </summary>
/// <param name="keys">The keys.</param>
/// <returns>The new tree.</returns>
internal Node AddRange(ReadOnlySpan<T> keys)
{
if (this.IsEmpty)
{
return NodeTreeFromList(keys);
}

Node newRight = _right!.AddRange(keys);
Node result = this.MutateRight(newRight);
return result.BalanceMany();
}

/// <summary>
/// Adds the specified keys at a given index to this tree.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,31 @@ public ImmutableList<T> AddRange(IEnumerable<T> items)
return this.Wrap(result);
}

/// <summary>
/// See the <see cref="IImmutableList{T}"/> interface.
/// </summary>
internal ImmutableList<T> AddRange(ReadOnlySpan<T> items)
{
if (this.IsEmpty)
{
if (items.IsEmpty)
{
return Empty;
}

return new ImmutableList<T>(Node.NodeTreeFromList(items));
}
else
{
if (items.IsEmpty)
{
return this;
}

return this.Wrap(_root.AddRange(items));
}
}

/// <summary>
/// See the <see cref="IImmutableList{T}"/> interface.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,18 @@ public static ImmutableQueue<T> Create<T>(params T[] items)
{
Requires.NotNull(items, nameof(items));

if (items.Length == 0)
return Create((ReadOnlySpan<T>)items);
}

/// <summary>
/// Creates a new immutable queue that contains the specified array of items.
/// </summary>
/// <typeparam name="T">The type of items in the immutable queue.</typeparam>
/// <param name="items">A span that contains the items to prepopulate the queue with.</param>
/// <returns>A new immutable queue that contains the specified items.</returns>
public static ImmutableQueue<T> Create<T>(ReadOnlySpan<T> items)
{
if (items.IsEmpty)
{
return ImmutableQueue<T>.Empty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ public static ImmutableSortedSet<T> CreateRange<T>(IComparer<T>? comparer, IEnum
/// <param name="items">The items to prepopulate.</param>
/// <returns>The new immutable collection.</returns>
public static ImmutableSortedSet<T> Create<T>(params T[] items)
{
Requires.NotNull(items, nameof(items));
return Create((ReadOnlySpan<T>)items);
}

/// <summary>
/// Creates a new immutable sorted set that contains the specified array of items.
/// </summary>
/// <typeparam name="T">The type of items in the immutable set.</typeparam>
/// <param name="items">A span that contains the items to prepopulate the set with.</param>
/// <returns>A new immutable set that contains the specified items.</returns>
public static ImmutableSortedSet<T> Create<T>(ReadOnlySpan<T> items)
{
return ImmutableSortedSet<T>.Empty.Union(items);
}
Expand All @@ -98,6 +110,20 @@ public static ImmutableSortedSet<T> Create<T>(params T[] items)
/// <param name="items">The items to prepopulate.</param>
/// <returns>The new immutable collection.</returns>
public static ImmutableSortedSet<T> Create<T>(IComparer<T>? comparer, params T[] items)
{
Requires.NotNull(items, nameof(items));

return Create(comparer, (ReadOnlySpan<T>)items);
}

/// <summary>
/// Creates a new immutable collection prefilled with the specified items.
/// </summary>
/// <typeparam name="T">The type of items stored by the collection.</typeparam>
/// <param name="comparer">The comparer.</param>
/// <param name="items">The items to prepopulate.</param>
/// <returns>The new immutable collection.</returns>
public static ImmutableSortedSet<T> Create<T>(IComparer<T>? comparer, ReadOnlySpan<T> items)
{
return ImmutableSortedSet<T>.Empty.WithComparer(comparer).Union(items);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public sealed partial class ImmutableSortedSet<T>
/// </summary>
/// <remarks>
/// <para>
/// While <see cref="ImmutableSortedSet{T}.Union"/> and other bulk change methods
/// While <see cref="M:ImmutableSortedSet{T}.Union"/> and other bulk change methods
/// already provide fast bulk change operations on the collection, this class allows
/// multiple combinations of changes to be made to a set with equal efficiency.
/// </para>
Expand Down
Loading