Skip to content

Commit

Permalink
DistinctBy is mono incompatible
Browse files Browse the repository at this point in the history
  • Loading branch information
PunkPun committed Sep 26, 2023
1 parent 0817c26 commit 7a99aea
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions OpenRA.Mods.CA/Assets/OffsetUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenRA.Primitives;

namespace OpenRA.Mods.CA.Assets
Expand All @@ -27,5 +30,38 @@ public static uint ReadUInt32Offset(Stream stream)

return offset;
}

#if !NET5_0_OR_GREATER
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
if (source is null)
throw new ArgumentNullException(nameof(source));

if (keySelector is null)
throw new ArgumentNullException(nameof(keySelector));

return DistinctByIterator(source, keySelector);
}

static IEnumerable<TSource> DistinctByIterator<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
using (IEnumerator<TSource> enumerator = source.GetEnumerator())
{
if (enumerator.MoveNext())
{
var set = new HashSet<TKey>();
do
{
TSource element = enumerator.Current;
if (set.Add(keySelector(element)))
{
yield return element;
}
}
while (enumerator.MoveNext());
}
}
}
#endif
}
}

0 comments on commit 7a99aea

Please sign in to comment.