Skip to content

Commit 965a5d9

Browse files
Add a couple of GetMostRecentUniqueItems overloads
This adds two overloads to GetMostRecentUniqueItems. The first overload takes a HashSet<T> instead of an IEqualityComparer<T>. This allows a caller to avoid an allocation by passing an empty set. The second overload doesn't take an additional argument. Instead, it acquires a set from HashSetPool<T>.
1 parent 83a3288 commit 965a5d9

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/ImmutableArrayExtensions.cs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license. See License.txt in the project root for license information.
33

44
using System.Collections.Generic;
5+
using System.Diagnostics;
56
using System.Runtime.InteropServices;
67
using Microsoft.AspNetCore.Razor.PooledObjects;
78
using Microsoft.AspNetCore.Razor.Utilities;
@@ -91,9 +92,30 @@ public static ImmutableArray<T> WhereAsArray<T>(this ImmutableArray<T> source, F
9192
}
9293

9394
/// <summary>
94-
/// Returns an <see cref="ImmutableArray{T}"/> that contains no duplicates from the <paramref name="source"/> array
95-
/// and returns the most recent copy of each item.
95+
/// Returns an <see cref="ImmutableArray{T}"/> that contains no duplicates from the <paramref name="source"/> array
96+
/// and returns the most recent copy of each item.
9697
/// </summary>
98+
/// <param name="source">The array to process.</param>
99+
public static ImmutableArray<T> GetMostRecentUniqueItems<T>(this ImmutableArray<T> source)
100+
{
101+
if (source.IsEmpty)
102+
{
103+
return [];
104+
}
105+
106+
using var _ = HashSetPool<T>.GetPooledObject(out var uniqueItems);
107+
108+
return source.GetMostRecentUniqueItems(uniqueItems);
109+
}
110+
111+
/// <summary>
112+
/// Returns an <see cref="ImmutableArray{T}"/> that contains no duplicates from the <paramref name="source"/> array
113+
/// and returns the most recent copy of each item.
114+
/// </summary>
115+
/// <param name="source">The array to process.</param>
116+
/// <param name="comparer">
117+
/// A comparer to use for uniqueness.
118+
/// </param>
97119
public static ImmutableArray<T> GetMostRecentUniqueItems<T>(this ImmutableArray<T> source, IEqualityComparer<T> comparer)
98120
{
99121
if (source.IsEmpty)
@@ -107,6 +129,32 @@ public static ImmutableArray<T> GetMostRecentUniqueItems<T>(this ImmutableArray<
107129
var uniqueItems = new HashSet<T>(comparer);
108130
#endif
109131

132+
return source.GetMostRecentUniqueItems(uniqueItems);
133+
}
134+
135+
/// <summary>
136+
/// Returns an <see cref="ImmutableArray{T}"/> that contains no duplicates from the <paramref name="source"/> array
137+
/// and returns the most recent copy of each item.
138+
/// </summary>
139+
/// <param name="source">The array to process.</param>
140+
/// <param name="uniqueItems">
141+
/// An empty <see cref="HashSet{T}"/> to use for uniqueness.
142+
/// Note that this may still contain items after processing.
143+
/// </param>
144+
public static ImmutableArray<T> GetMostRecentUniqueItems<T>(this ImmutableArray<T> source, HashSet<T> uniqueItems)
145+
{
146+
if (source.IsEmpty)
147+
{
148+
return [];
149+
}
150+
151+
return GetMostRecentUniqueItemsCore(source, uniqueItems);
152+
}
153+
154+
private static ImmutableArray<T> GetMostRecentUniqueItemsCore<T>(ImmutableArray<T> source, HashSet<T> uniqueItems)
155+
{
156+
Debug.Assert(uniqueItems.Count == 0, $"{nameof(uniqueItems)} should be empty!");
157+
110158
using var stack = new PooledArrayBuilder<T>(capacity: source.Length);
111159

112160
// Walk the next batch in reverse to identify unique items.

0 commit comments

Comments
 (0)