Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Added a TryGetValue method to HashSet and SortedSet - #16034

Merged
safern merged 3 commits into
dotnet:masterfrom
TylerBrinkley:hashset-trygetvalue
Feb 13, 2017
Merged

safern merged 3 commits into
dotnet:masterfrom
TylerBrinkley:hashset-trygetvalue

Conversation

@TylerBrinkley

Copy link
Copy Markdown

Closes #15691

Added a TryGetValue method to HashSet and SortedSet.

Also fixed a bug in a SortedSet test as subsequent calls to CreateT with the same seed should return an equivalent value. The current implementation completely ignored the seed.

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.
@dnfclas

dnfclas commented Feb 10, 2017

Copy link
Copy Markdown

Hi @TylerBrinkley, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution!

In order for us to evaluate and accept your PR, we ask that you sign a contribution license agreement. It's all electronic and will take just minutes. I promise there's no faxing. https://cla2.dotnetfoundation.org.

TTYL, DNFBOT;

/// </remarks>
public bool TryGetValue(T equalValue, out T actualValue)
{
int i = _buckets != null ? InternalIndexOf(equalValue) : -1;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Good thinking.

@dnfclas

dnfclas commented Feb 10, 2017

Copy link
Copy Markdown

@TylerBrinkley, Thanks for signing the contribution license agreement so quickly! Actual humans will now validate the agreement and then evaluate the PR.

Thanks, DNFBOT;

HashSet<T> set = new HashSet<T> { value };
T equalValue = CreateT(2);
T actualValue;
Assert.False(set.TryGetValue(equalValue, out actualValue));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These have been added.

[Fact]
public void HashSet_Generic_TryGetValue_Contains()
{
T value = CreateT(1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably test for at least one of reference types, primitive type and non-primitive value type. And for both default and custom comparers.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how to go about doing that. Would that be done in the file HashSet.Generic.cs?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the hash case, check on default being set to the out parameter.

@TylerBrinkley

Copy link
Copy Markdown
Author

@dotnet-bot test Innerloop Windows_NT Release Build and Test

@safern safern left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @TylerBrinkley for your proposal and contributing!

@safern
safern merged commit 2d61850 into dotnet:master Feb 13, 2017
@TylerBrinkley
TylerBrinkley deleted the hashset-trygetvalue branch February 13, 2017 19:11
@karelz karelz modified the milestone: 2.0.0 Feb 22, 2017
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
)

* Added a TryGetValue method to HashSet and SortedSet.


Commit migrated from dotnet/corefx@2d61850
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add HashSet TryGetValue

7 participants