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

Align API surface of immutable collections and their corresponding builder types #66550

Merged
merged 14 commits into from
May 4, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public void Insert(int index, T item) { }
public int LastIndexOf(T item, int startIndex, int count, System.Collections.Generic.IEqualityComparer<T>? equalityComparer) { throw null; }
public System.Collections.Immutable.ImmutableArray<T> MoveToImmutable() { throw null; }
public bool Remove(T element) { throw null; }
public void Remove(T element, System.Collections.Generic.IEqualityComparer<T>? equalityComparer) { throw null; }
public bool Remove(T element, System.Collections.Generic.IEqualityComparer<T>? equalityComparer) { throw null; }
public void RemoveAll(Predicate<T> match) { throw null; }
public void RemoveAt(int index) { }
public void RemoveRange(int index, int length) { throw null; }
Expand Down Expand Up @@ -718,7 +718,7 @@ public void InsertRange(int index, System.Collections.Generic.IEnumerable<T> ite
public int LastIndexOf(T item, int startIndex, int count) { throw null; }
public int LastIndexOf(T item, int startIndex, int count, System.Collections.Generic.IEqualityComparer<T>? equalityComparer) { throw null; }
public bool Remove(T item) { throw null; }
public void Remove(T item, System.Collections.Generic.IEqualityComparer<T>? equalityComparer) { throw null; }
public bool Remove(T item, System.Collections.Generic.IEqualityComparer<T>? equalityComparer) { throw null; }
public int RemoveAll(System.Predicate<T> match) { throw null; }
public void RemoveAt(int index) { }
public void RemoveRange(int index, int count) { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ public void AddRange<TDerived>(ImmutableArray<TDerived>.Builder items) where TDe

/// <summary>
/// Removes the first occurrence of the specified element from the builder.
/// If no match is found, the builder remains unchanged.
Copy link
Member

Choose a reason for hiding this comment

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

If we're adding this, shouldn't it be included in the new overload as well?

/// </summary>
/// <param name="element">The element.</param>
/// <returns>A value indicating whether the specified element was found and removed from the collection.</returns>
Expand All @@ -486,14 +487,17 @@ public bool Remove(T element)
/// The equality comparer to use in the search.
/// If <c>null</c>, <see cref="EqualityComparer{T}.Default"/> is used.
/// </param>
Copy link
Member

Choose a reason for hiding this comment

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

Please add a <returns /> element as above.

public void Remove(T element, IEqualityComparer<T>? equalityComparer)
public bool Remove(T element, IEqualityComparer<T>? equalityComparer)
{
int index = this.IndexOf(element, 0, _count, equalityComparer);

if (index >= 0)
{
this.RemoveAt(index);
return true;
}

return false;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -758,13 +758,16 @@ public int RemoveAll(Predicate<T> match)
/// The equality comparer to use in the search.
/// If <c>null</c>, <see cref="EqualityComparer{T}.Default"/> is used.
/// </param>
public void Remove(T item, IEqualityComparer<T>? equalityComparer)
public bool Remove(T item, IEqualityComparer<T>? equalityComparer)
{
int index = this.IndexOf(item, 0, this.Count, equalityComparer);
if (index >= 0)
{
this.RemoveAt(index);
return true;
}

return false;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public void Remove_EqualityComparer()
var builder = ImmutableArray.Create(1.5, 2.5, 3.5).ToBuilder();
var absComparer = new DelegateEqualityComparer<double>(equals: (x, y) => Math.Abs(x) == Math.Abs(y));

builder.Remove(-1.5, absComparer);
Assert.True(builder.Remove(-1.5, absComparer));
Assert.Equal(new[] { 2.5, 3.5 }, builder);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@ public void Remove_EqualityComparer()
mutable.Add(2.4);
mutable.Add(3.6);

mutable.Remove(2.4, null);
Assert.True(mutable.Remove(2.4, null));
Assert.Equal(new[] { 1.5, 3.6 }, mutable);

var absComparer = new DelegateEqualityComparer<double>(equals: (x, y) => Math.Abs(x) == Math.Abs(y));
mutable.Remove(-1.5, absComparer);
Assert.True(mutable.Remove(-1.5, absComparer));
Assert.Equal(new[] { 3.6 }, mutable);
}

Expand Down