Repository navigation
Added a TryGetValue method to HashSet and SortedSet - #16034
Conversation
Also fixed a bug in a SortedSet test as subsequent calls to CreateT with the same seed should return an equivalent value. The implementation completely ignored the seed.
|
Hi @TylerBrinkley, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution! TTYL, DNFBOT; |
| /// </remarks> | ||
| public bool TryGetValue(T equalValue, out T actualValue) | ||
| { | ||
| int i = _buckets != null ? InternalIndexOf(equalValue) : -1; |
There was a problem hiding this comment.
Nit: it would be better to do:
if (_buckets != null)
{
int i = InternalIndexOf(equalValue);
if (i >= 0)
{
actualValue = _slots[i].value;
return true;
}
}
actualValue = default(T);
return false;While the version you have is more concise, it introduces an unnecessary branch in the case of _buckets == null.
|
@TylerBrinkley, Thanks for signing the contribution license agreement so quickly! Actual humans will now validate the agreement and then evaluate the PR. |
| HashSet<T> set = new HashSet<T> { value }; | ||
| T equalValue = CreateT(2); | ||
| T actualValue; | ||
| Assert.False(set.TryGetValue(equalValue, out actualValue)); |
There was a problem hiding this comment.
Should assert that actualValue is default(T) here, and also a test that actualValue gets over-written with default(T) in the case of it already being initialised.
| [Fact] | ||
| public void HashSet_Generic_TryGetValue_Contains() | ||
| { | ||
| T value = CreateT(1); |
There was a problem hiding this comment.
Should probably test for at least one of reference types, primitive type and non-primitive value type. And for both default and custom comparers.
There was a problem hiding this comment.
I'm not sure how to go about doing that. Would that be done in the file HashSet.Generic.cs?
There was a problem hiding this comment.
My bad. I see now that inheritance was already being used to make sure that that happens, so this is already done.
| SortedSet<T> set = new SortedSet<T> { value }; | ||
| T equalValue = CreateT(2); | ||
| T actualValue; | ||
| Assert.False(set.TryGetValue(equalValue, out actualValue)); |
There was a problem hiding this comment.
As per the hash case, check on default being set to the out parameter.
|
@dotnet-bot test Innerloop Windows_NT Release Build and Test |
safern
left a comment
There was a problem hiding this comment.
Thanks @TylerBrinkley for your proposal and contributing!
) * Added a TryGetValue method to HashSet and SortedSet. Commit migrated from dotnet/corefx@2d61850
Closes #15691
Added a
TryGetValuemethod toHashSetandSortedSet.Also fixed a bug in a
SortedSettest as subsequent calls toCreateTwith the sameseedshould return an equivalent value. The current implementation completely ignored theseed.