Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append TryGetValue to ImmutableHashSet1.Builder and ImmutableSortedSet1.Builder #34869

Merged
merged 2 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ public void IntersectWith(System.Collections.Generic.IEnumerable<T> other) { }
public bool Remove(T item) { throw null; }
public bool SetEquals(System.Collections.Generic.IEnumerable<T> other) { throw null; }
public void SymmetricExceptWith(System.Collections.Generic.IEnumerable<T> other) { }
public bool TryGetValue(T equalValue, out T actualValue) { throw null; }
void System.Collections.Generic.ICollection<T>.Add(T item) { }
void System.Collections.Generic.ICollection<T>.CopyTo(T[] array, int arrayIndex) { }
System.Collections.Generic.IEnumerator<T> System.Collections.Generic.IEnumerable<T>.GetEnumerator() { throw null; }
Expand Down Expand Up @@ -1021,6 +1022,7 @@ public void IntersectWith(System.Collections.Generic.IEnumerable<T> other) { }
public System.Collections.Generic.IEnumerable<T> Reverse() { throw null; }
public bool SetEquals(System.Collections.Generic.IEnumerable<T> other) { throw null; }
public void SymmetricExceptWith(System.Collections.Generic.IEnumerable<T> other) { }
public bool TryGetValue(T equalValue, out T actualValue) { throw null; }
void System.Collections.Generic.ICollection<T>.Add(T item) { }
void System.Collections.Generic.ICollection<T>.CopyTo(T[] array, int arrayIndex) { }
System.Collections.Generic.IEnumerator<T> System.Collections.Generic.IEnumerable<T>.GetEnumerator() { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,24 @@ public ImmutableHashSet<T> ToImmutable()
return _immutable;
}

/// <summary>
/// Searches a given value in the set and returns the equal value if it finds
/// </summary>
/// <param name="equalValue">The value to search.</param>
/// <param name="actualValue">The value from the set that the search found, else the original value.</param>
/// <returns>A value indicating whether the search was successful.</returns>
public bool TryGetValue(T equalValue, out T actualValue)
{
int hashCode = equalValue != null ? _equalityComparer.GetHashCode(equalValue) : 0;
if (_root.TryGetValue(hashCode, out HashBucket bucket))
{
return bucket.TryExchange(equalValue, _equalityComparer, out actualValue);
}

actualValue = equalValue;
return false;
}

#endregion

#region ISet<T> Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,25 @@ public ImmutableSortedSet<T> ToImmutable()
return _immutable;
}

/// <summary>
/// Searches a given value in the set and returns the equal value if it finds
/// </summary>
/// <param name="equalValue">The value to search.</param>
/// <param name="actualValue">The value from the set that the search found, else the original value.</param>
/// <returns>A value indicating whether the search was successful.</returns>
public bool TryGetValue(T equalValue, out T actualValue)
{
Node searchResult = _root.Search(equalValue, _comparer);
if (!searchResult.IsEmpty)
{
actualValue = searchResult.Key;
return true;
}

actualValue = equalValue;
return false;
}

#region ICollection members

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,5 +332,31 @@ public void ToImmutableHashSet()
ImmutableHashSet<int>.Builder nullBuilder = null;
AssertExtensions.Throws<ArgumentNullException>("builder", () => nullBuilder.ToImmutableHashSet());
}

[Fact]
public void TryGetValue()
{
var builder = ImmutableHashSet.Create(1, 2, 3).ToBuilder();
Assert.True(builder.TryGetValue(2, out _));

builder = ImmutableHashSet.Create(CustomEqualityComparer.Instance, 1, 2, 3, 4).ToBuilder();
var existing = 0;
Assert.True(builder.TryGetValue(5, out existing));
Assert.NotEqual(5, existing);
Assert.Equal(4, existing);
}

private class CustomEqualityComparer : IEqualityComparer<int>
{
private CustomEqualityComparer()
{
}

public static CustomEqualityComparer Instance { get; } = new CustomEqualityComparer();

public bool Equals(int x, int y) => x >> 1 == y >> 1;

public int GetHashCode(int obj) => (obj >> 1).GetHashCode();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -399,5 +399,32 @@ public void ToImmutableSortedSet()
ImmutableSortedSet<int>.Builder nullBuilder = null;
AssertExtensions.Throws<ArgumentNullException>("builder", () => nullBuilder.ToImmutableSortedSet());
}

[Fact]
public void TryGetValue()
{
var builder = ImmutableSortedSet.Create(1, 2, 3).ToBuilder();
Assert.True(builder.TryGetValue(2, out _));

builder = ImmutableSortedSet.Create(CustomComparer.Instance, 1, 2, 3, 4).ToBuilder();
var existing = 0;
Assert.True(builder.TryGetValue(5, out existing));
Assert.NotEqual(5, existing);
Assert.Equal(4, existing);
}

private class CustomComparer : IComparer<int>
{
private CustomComparer()
{
}

public static CustomComparer Instance { get; } = new CustomComparer();

public int Compare(int x, int y) =>
x >> 1 == y >> 1 ? 0 :
x < y ? -1 :
1;
}
}
}