Skip to content

Commit

Permalink
HashSet.TryGetValue (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Mar 12, 2024
1 parent 191ea7b commit e1f0786
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
5 changes: 5 additions & 0 deletions api_list.include.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
* `Boolean Remove<TKey, TValue>(TKey, TValue&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.remove)


#### HashSet<T>

* `Boolean TryGetValue<T>(T, T&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.trygetvalue)


#### IEnumerable<TSource>

* `IEnumerable<KeyValuePair<TKey,TAccumulate>> AggregateBy<TSource, TKey, TAccumulate>(Func<TSource,TKey>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, IEqualityComparer<TKey>)` [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))))
Expand Down
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,11 @@ The class `Polyfill` includes the following extension methods:
* `Boolean Remove<TKey, TValue>(TKey, TValue&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.remove)


#### HashSet<T>

* `Boolean TryGetValue<T>(T, T&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.trygetvalue)


#### IEnumerable<TSource>

* `IEnumerable<KeyValuePair<TKey,TAccumulate>> AggregateBy<TSource, TKey, TAccumulate>(Func<TSource,TKey>, TAccumulate, Func<TAccumulate,TSource,TAccumulate>, IEqualityComparer<TKey>)` [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))))
Expand Down
10 changes: 9 additions & 1 deletion src/Consume/Consume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ void SpanStartsWith()
startsWith = "value".AsSpan().StartsWith("value", StringComparison.Ordinal);
}


void SpanEndsWith()
{
var result = "value".AsSpan().EndsWith("value");
Expand All @@ -263,6 +262,15 @@ void IsGenericMethodParameter()
var result = typeof(string).IsGenericMethodParameter();
}

void HashSetTryGetValue()
{
var set = new HashSet<string>
{
"value"
};
var found = set.TryGetValue("value", out var result);
}

void HasSameMetadataDefinitionAs(MemberInfo info)
{
var result = info.HasSameMetadataDefinitionAs(info);
Expand Down
42 changes: 42 additions & 0 deletions src/Polyfill/Polyfill_HashSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// <auto-generated />

#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
{
/// <summary>
/// Searches the set for a given value and returns the equal value it finds, if any.
/// </summary>
/// <param name="equalValue">The value to search for.</param>
/// <param name="actualValue">The value from the set that the search found, or the default value of T when the search yielded no match.</param>
/// <returns>A value indicating whether the search was successful.</returns>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.trygetvalue")]
public static bool TryGetValue<T>(
this HashSet<T> 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
18 changes: 18 additions & 0 deletions src/Tests/PolyfillTests_HashSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
partial class PolyfillTests
{
[Test]
public void TryGetValue()
{
var value = "value";
var set = new HashSet<string>
{
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);
}
}

0 comments on commit e1f0786

Please sign in to comment.