diff --git a/api_list.include.md b/api_list.include.md index 296d020a..a7b1ede2 100644 --- a/api_list.include.md +++ b/api_list.include.md @@ -15,6 +15,11 @@ * `Boolean Remove(TKey, TValue&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.remove) +#### HashSet + + * `Boolean TryGetValue(T, T&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.trygetvalue) + + #### IEnumerable * `IEnumerable> AggregateBy(Func, TAccumulate, Func, IEqualityComparer)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.aggregateby#system-linq-enumerable-aggregateby-3(system-collections-generic-ienumerable((-0))-system-func((-0-1))-system-func((-1-2))-system-func((-2-0-2))-system-collections-generic-iequalitycomparer((-1)))) diff --git a/readme.md b/readme.md index a813edd2..c5219e8c 100644 --- a/readme.md +++ b/readme.md @@ -372,6 +372,11 @@ The class `Polyfill` includes the following extension methods: * `Boolean Remove(TKey, TValue&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.remove) +#### HashSet + + * `Boolean TryGetValue(T, T&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.trygetvalue) + + #### IEnumerable * `IEnumerable> AggregateBy(Func, TAccumulate, Func, IEqualityComparer)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.aggregateby#system-linq-enumerable-aggregateby-3(system-collections-generic-ienumerable((-0))-system-func((-0-1))-system-func((-1-2))-system-func((-2-0-2))-system-collections-generic-iequalitycomparer((-1)))) diff --git a/src/Consume/Consume.cs b/src/Consume/Consume.cs index 8bd02d2d..ab96f56c 100644 --- a/src/Consume/Consume.cs +++ b/src/Consume/Consume.cs @@ -237,7 +237,6 @@ void SpanStartsWith() startsWith = "value".AsSpan().StartsWith("value", StringComparison.Ordinal); } - void SpanEndsWith() { var result = "value".AsSpan().EndsWith("value"); @@ -263,6 +262,15 @@ void IsGenericMethodParameter() var result = typeof(string).IsGenericMethodParameter(); } + void HashSetTryGetValue() + { + var set = new HashSet + { + "value" + }; + var found = set.TryGetValue("value", out var result); + } + void HasSameMetadataDefinitionAs(MemberInfo info) { var result = info.HasSameMetadataDefinitionAs(info); diff --git a/src/Polyfill/Polyfill_HashSet.cs b/src/Polyfill/Polyfill_HashSet.cs new file mode 100644 index 00000000..af3b72f2 --- /dev/null +++ b/src/Polyfill/Polyfill_HashSet.cs @@ -0,0 +1,42 @@ +// + +#pragma warning disable + +#if NET46X || NET47 || NET471 || NETSTANDARD2_0 + +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using Link = System.ComponentModel.DescriptionAttribute; + +static partial class Polyfill +{ + /// + /// Searches the set for a given value and returns the equal value it finds, if any. + /// + /// The value to search for. + /// The value from the set that the search found, or the default value of T when the search yielded no match. + /// A value indicating whether the search was successful. + [Link("https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.trygetvalue")] + public static bool TryGetValue( + this HashSet target, + T equalValue, + [MaybeNullWhen(false)] out T actualValue) + { + var comparer = target.Comparer; + var hashCode = comparer.GetHashCode(equalValue); + foreach (var item in target) + { + if (comparer.GetHashCode(item) == hashCode && + comparer.Equals(item, equalValue)) + { + actualValue = item; + return true; + } + } + + actualValue = default; + return false; + } +} +#endif \ No newline at end of file diff --git a/src/Tests/PolyfillTests_HashSet.cs b/src/Tests/PolyfillTests_HashSet.cs new file mode 100644 index 00000000..216474fc --- /dev/null +++ b/src/Tests/PolyfillTests_HashSet.cs @@ -0,0 +1,18 @@ +partial class PolyfillTests +{ + [Test] + public void TryGetValue() + { + var value = "value"; + var set = new HashSet + { + value + }; + var found = set.TryGetValue("value", out var result); + Assert.True(found); + Assert.AreSame(value, result!); + found = set.TryGetValue("value2", out result); + Assert.Null(result); + Assert.False(found); + } +} \ No newline at end of file