-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
340 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...c{``1,``2},System.Func{``2,``0,``2},System.Collections.Generic.IEqualityComparer{``1}).cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
static partial class PolyfillExtensions | ||
{ | ||
public static IEnumerable<KeyValuePair<TKey, TAccumulate>> AggregateBy<TSource, TKey, TAccumulate>( | ||
this IEnumerable<TSource> source, | ||
Func<TSource, TKey> keySelector, | ||
Func<TKey, TAccumulate> seedSelector, | ||
Func<TAccumulate, TSource, TAccumulate> func, | ||
IEqualityComparer<TKey>? keyComparer = null) where TKey : notnull | ||
{ | ||
if (source is null) | ||
{ | ||
throw new ArgumentNullException(nameof(source)); | ||
} | ||
if (keySelector is null) | ||
{ | ||
throw new ArgumentNullException(nameof(keySelector)); | ||
} | ||
if (seedSelector is null) | ||
{ | ||
throw new ArgumentNullException(nameof(keySelector)); | ||
} | ||
if (func is null) | ||
{ | ||
throw new ArgumentNullException(nameof(func)); | ||
} | ||
|
||
return Helpers.AggregateByIterator(source, keySelector, seedSelector, func, keyComparer); | ||
} | ||
} | ||
|
||
file class Helpers | ||
{ | ||
public static IEnumerable<KeyValuePair<TKey, TAccumulate>> AggregateByIterator<TSource, TKey, TAccumulate>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TAccumulate> seedSelector, Func<TAccumulate, TSource, TAccumulate> func, IEqualityComparer<TKey>? keyComparer) where TKey : notnull | ||
{ | ||
using IEnumerator<TSource> enumerator = source.GetEnumerator(); | ||
|
||
if (!enumerator.MoveNext()) | ||
{ | ||
yield break; | ||
} | ||
|
||
foreach (KeyValuePair<TKey, TAccumulate> countBy in PopulateDictionary(enumerator, keySelector, seedSelector, func, keyComparer)) | ||
{ | ||
yield return countBy; | ||
} | ||
|
||
static Dictionary<TKey, TAccumulate> PopulateDictionary(IEnumerator<TSource> enumerator, Func<TSource, TKey> keySelector, Func<TKey, TAccumulate> seedSelector, Func<TAccumulate, TSource, TAccumulate> func, IEqualityComparer<TKey>? keyComparer) | ||
{ | ||
Dictionary<TKey, TAccumulate> dict = new(keyComparer); | ||
|
||
do | ||
{ | ||
TSource value = enumerator.Current; | ||
TKey key = keySelector(value); | ||
|
||
#if NET | ||
ref TAccumulate? acc = ref System.Runtime.InteropServices.CollectionsMarshal.GetValueRefOrAddDefault(dict, key, out bool exists); | ||
acc = func(exists ? acc! : seedSelector(key), value); | ||
#else | ||
var exists = dict.TryGetValue(key, out var acc); | ||
dict[key] = func(exists ? acc! : seedSelector(key), value); | ||
#endif | ||
} | ||
while (enumerator.MoveNext()); | ||
|
||
return dict; | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...0,``1},``2,System.Func{``2,``0,``2},System.Collections.Generic.IEqualityComparer{``1}).cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
static partial class PolyfillExtensions | ||
{ | ||
public static IEnumerable<KeyValuePair<TKey, TAccumulate>> AggregateBy<TSource, TKey, TAccumulate>( | ||
this IEnumerable<TSource> source, | ||
Func<TSource, TKey> keySelector, | ||
TAccumulate seed, | ||
Func<TAccumulate, TSource, TAccumulate> func, | ||
IEqualityComparer<TKey>? keyComparer = null) where TKey : notnull | ||
{ | ||
if (source is null) | ||
{ | ||
throw new ArgumentNullException(nameof(source)); | ||
} | ||
if (keySelector is null) | ||
{ | ||
throw new ArgumentNullException(nameof(keySelector)); | ||
} | ||
if (func is null) | ||
{ | ||
throw new ArgumentNullException(nameof(func)); | ||
} | ||
|
||
return Helpers.AggregateByIterator(source, keySelector, seed, func, keyComparer); | ||
} | ||
} | ||
|
||
file class Helpers | ||
{ | ||
public static IEnumerable<KeyValuePair<TKey, TAccumulate>> AggregateByIterator<TSource, TKey, TAccumulate>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, IEqualityComparer<TKey>? keyComparer) where TKey : notnull | ||
{ | ||
using IEnumerator<TSource> enumerator = source.GetEnumerator(); | ||
|
||
if (!enumerator.MoveNext()) | ||
{ | ||
yield break; | ||
} | ||
|
||
foreach (KeyValuePair<TKey, TAccumulate> countBy in PopulateDictionary(enumerator, keySelector, seed, func, keyComparer)) | ||
{ | ||
yield return countBy; | ||
} | ||
|
||
static Dictionary<TKey, TAccumulate> PopulateDictionary(IEnumerator<TSource> enumerator, Func<TSource, TKey> keySelector, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, IEqualityComparer<TKey>? keyComparer) | ||
{ | ||
Dictionary<TKey, TAccumulate> dict = new(keyComparer); | ||
|
||
do | ||
{ | ||
TSource value = enumerator.Current; | ||
TKey key = keySelector(value); | ||
|
||
#if NET | ||
ref TAccumulate? acc = ref System.Runtime.InteropServices.CollectionsMarshal.GetValueRefOrAddDefault(dict, key, out bool exists); | ||
acc = func(exists ? acc! : seed, value); | ||
#else | ||
var exists = dict.TryGetValue(key, out var acc); | ||
dict[key] = func(exists ? acc! : seed, value); | ||
#endif | ||
} | ||
while (enumerator.MoveNext()); | ||
|
||
return dict; | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...numerable{``0},System.Func{``0,``1},System.Collections.Generic.IEqualityComparer{``1}).cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
static partial class PolyfillExtensions | ||
{ | ||
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? keyComparer = null) where TKey : notnull | ||
{ | ||
if (source is null) | ||
{ | ||
throw new ArgumentNullException(nameof(source)); | ||
} | ||
|
||
if (keySelector is null) | ||
{ | ||
throw new ArgumentNullException(nameof(keySelector)); | ||
} | ||
|
||
return Helpers.CountByIterator(source, keySelector, keyComparer); | ||
} | ||
} | ||
|
||
file class Helpers | ||
{ | ||
public static IEnumerable<KeyValuePair<TKey, int>> CountByIterator<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? keyComparer) where TKey : notnull | ||
{ | ||
using IEnumerator<TSource> enumerator = source.GetEnumerator(); | ||
|
||
if (!enumerator.MoveNext()) | ||
{ | ||
yield break; | ||
} | ||
|
||
foreach (KeyValuePair<TKey, int> countBy in BuildCountDictionary(enumerator, keySelector, keyComparer)) | ||
{ | ||
yield return countBy; | ||
} | ||
} | ||
|
||
public static Dictionary<TKey, int> BuildCountDictionary<TSource, TKey>(IEnumerator<TSource> enumerator, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? keyComparer) where TKey : notnull | ||
{ | ||
Dictionary<TKey, int> countsBy = new(keyComparer); | ||
|
||
do | ||
{ | ||
TSource value = enumerator.Current; | ||
TKey key = keySelector(value); | ||
|
||
#if NET | ||
ref int currentCount = ref System.Runtime.InteropServices.CollectionsMarshal.GetValueRefOrAddDefault(countsBy, key, out _); | ||
checked | ||
{ | ||
currentCount++; | ||
} | ||
#else | ||
if (countsBy.TryGetValue(key, out var currentCount)) | ||
{ | ||
checked | ||
{ | ||
countsBy[key] = currentCount + 1; | ||
} | ||
} | ||
else | ||
{ | ||
countsBy[key] = 1; | ||
} | ||
#endif | ||
} | ||
while (enumerator.MoveNext()); | ||
|
||
return countsBy; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
....Editor/M;System.Linq.Enumerable.Index``1(System.Collections.Generic.IEnumerable{``0}).cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Collections.Generic; | ||
|
||
static partial class PolyfillExtensions | ||
{ | ||
public static IEnumerable<(int Index, TSource Item)> Index<TSource>(this IEnumerable<TSource> source) | ||
{ | ||
int index = -1; | ||
foreach (TSource element in source) | ||
{ | ||
checked | ||
{ | ||
index++; | ||
} | ||
|
||
yield return (index, element); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ringBuilder.Replace(System.ReadOnlySpan{System.Char},System.ReadOnlySpan{System.Char}).cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
using System.Text; | ||
|
||
static partial class PolyfillExtensions | ||
{ | ||
public static StringBuilder Replace(this StringBuilder target, ReadOnlySpan<char> oldValue, ReadOnlySpan<char> newValue) | ||
{ | ||
return target.Replace(oldValue.ToString(), newValue.ToString()); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
....ReadOnlySpan{System.Char},System.ReadOnlySpan{System.Char},System.Int32,System.Int32).cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
using System.Text; | ||
|
||
static partial class PolyfillExtensions | ||
{ | ||
public static StringBuilder Replace(this StringBuilder target, ReadOnlySpan<char> oldValue, ReadOnlySpan<char> newValue, int startIndex, int count) | ||
{ | ||
return target.Replace(oldValue.ToString(), newValue.ToString(), startIndex, count); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Meziantou.Polyfill.SourceGenerator.Tests/Meziantou.Polyfill.SourceGenerator.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.