Skip to content

Commit

Permalink
Append TryGetValue to ImmutableHashSet`1.Builder and ImmutableSortedS…
Browse files Browse the repository at this point in the history
…et`1.Builder (#34869)

* Append TryGetValue method.

Append method to builder of ImmutableHashSet and ImmutableSortedSet.

Implements #28160.

* Small cleanup.
  • Loading branch information
GeorgeAlexandria authored Apr 13, 2020
1 parent f0ede00 commit 2bcc336
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
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 the set for a given value and returns the equal value it finds, if any.
/// </summary>
/// <param name="equalValue">The value for which to search.</param>
/// <param name="actualValue">The value from the set that the search found, or the original value if the search yielded no match.</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 the set for a given value and returns the equal value it finds, if any.
/// </summary>
/// <param name="equalValue">The value for which to search.</param>
/// <param name="actualValue">The value from the set that the search found, or the original value if the search yielded no match.</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,30 @@ 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.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,31 @@ 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.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;
}
}
}

0 comments on commit 2bcc336

Please sign in to comment.