Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
47 changes: 47 additions & 0 deletions Containers.Test/OrderedCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,53 @@ public void Remove_KeyComparerWithNoExactMatch_RemovesFirstOccurrence()
private static readonly IComparer<(int Key, string Name)> ByKey =
Comparer<(int Key, string Name)>.Create((x, y) => x.Key.CompareTo(y.Key));

[TestMethod]
public void IndexOf_KeyComparerWithDuplicateKeys_ReturnsIndexOfTheElementPassedIn()
{
// Arrange
OrderedCollection<(int Key, string Name)> collection = new(ByKey)
{
(1, "a"), (1, "b"), (0, "zero"), (2, "two"),
};

// Act
int index = collection.IndexOf((1, "b"));

// Assert
Assert.AreEqual(2, index);
Assert.AreEqual((1, "b"), collection[index]);
}

[TestMethod]
public void IndexOf_KeyComparerWithNoExactMatch_ReturnsFirstOccurrence()
{
// Arrange
OrderedCollection<(int Key, string Name)> collection = new(ByKey)
{
(1, "a"), (1, "b"), (0, "zero"),
};

// Act & Assert
Assert.AreEqual(1, collection.IndexOf((1, "missing")));
}

[TestMethod]
public void RemoveAtIndexOf_KeyComparerWithDuplicateKeys_RemovesSameElementAsRemove()
{
// Arrange
(int Key, string Name)[] source = [(1, "a"), (1, "b"), (1, "c"), (0, "zero")];
OrderedCollection<(int Key, string Name)> byIndex = new(source, ByKey);
OrderedCollection<(int Key, string Name)> byRemove = new(source, ByKey);

// Act
byIndex.RemoveAt(byIndex.IndexOf((1, "b")));
byRemove.Remove((1, "b"));

// Assert
Assert.AreSequenceEqual(byRemove, byIndex);
Assert.DoesNotContain((1, "b"), byIndex.ToList());
}

[TestMethod]
public void Add_KeyComparer_KeepsEqualElementsInInsertionOrder()
{
Expand Down
50 changes: 34 additions & 16 deletions Containers/OrderedCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,23 +246,12 @@ public void CopyTo(T[] array, int arrayIndex)
/// </remarks>
public bool Remove(T item)
{
int first = FindFirst(item);
if (first < 0)
int index = FindMatchIndex(item);
if (index < 0)
{
return false;
}

int index = first;
EqualityComparer<T> equality = EqualityComparer<T>.Default;
for (int i = first; i < items.Count && comparer.Compare(items[i], item) == 0; i++)
{
if (equality.Equals(items[i], item))
{
index = i;
break;
}
}

items.RemoveAt(index);
return true;
}
Expand Down Expand Up @@ -324,10 +313,39 @@ public int BinarySearch(T item)
/// </summary>
/// <param name="item">The element to locate.</param>
/// <returns>The zero-based index of the first occurrence if found; otherwise, -1.</returns>
public int IndexOf(T item)
/// <remarks>
/// When several elements compare equal to <paramref name="item"/>, the index of the first of
/// them that also equals it is returned, so a key-based comparer does not return the index of a
/// different element with the same key. If none of them equals it, the index of the first
/// element that compares equal is returned. This is the element <see cref="Remove(T)"/> removes.
/// </remarks>
public int IndexOf(T item) => FindMatchIndex(item);

/// <summary>
/// Finds the element that <see cref="IndexOf(T)"/> and <see cref="Remove(T)"/> act on: among the
/// run of elements that compare equal to <paramref name="item"/>, the first that also equals it,
/// or the first of the run if none does.
/// </summary>
/// <param name="item">The element to search for.</param>
/// <returns>The index of the matching element, or -1 if no element compares equal.</returns>
private int FindMatchIndex(T item)
{
int index = FindFirst(item);
return index >= 0 ? index : -1;
int first = FindFirst(item);
if (first < 0)
{
return -1;
}

EqualityComparer<T> equality = EqualityComparer<T>.Default;
for (int i = first; i < items.Count && comparer.Compare(items[i], item) == 0; i++)
{
if (equality.Equals(items[i], item))
{
return i;
}
}

return first;
}

/// <summary>
Expand Down
Loading