22// Licensed under the MIT license. See License.txt in the project root for license information.
33
44using System . Collections . Generic ;
5+ using System . Diagnostics ;
56using System . Runtime . InteropServices ;
67using Microsoft . AspNetCore . Razor . PooledObjects ;
78using 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