Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ public int IndexOf(T item, int startIndex, int count, IEqualityComparer<T>? equa
return -1;
}

Requires.Range(startIndex >= 0 && startIndex < this.Count, nameof(startIndex));
Requires.Range(startIndex >= 0 && startIndex <= this.Count, nameof(startIndex));
Requires.Range(count >= 0 && startIndex + count <= this.Count, nameof(count));

equalityComparer ??= EqualityComparer<T>.Default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public int IndexOf(T item, int startIndex, int count, IEqualityComparer<T>? equa
return -1;
}

Requires.Range(startIndex >= 0 && startIndex < self.Length, nameof(startIndex));
Requires.Range(startIndex >= 0 && startIndex <= self.Length, nameof(startIndex));
Requires.Range(count >= 0 && startIndex + count <= self.Length, nameof(count));

equalityComparer ??= EqualityComparer<T>.Default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -759,9 +759,13 @@ internal int BinarySearch(int index, int count, T item, IComparer<T>? comparer)
/// </returns>
internal int IndexOf(T item, int index, int count, IEqualityComparer<T>? equalityComparer)
{
Requires.Range(index >= 0, nameof(index));
Requires.Range(count >= 0, nameof(count));
Requires.Range(count <= this.Count, nameof(count));
if (count == 0 && index == 0)
{
return -1;
Copy link
Member

Choose a reason for hiding this comment

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

Why is this fast path needed?

Copy link
Contributor Author

@prozolic prozolic Feb 27, 2026

Choose a reason for hiding this comment

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

I added to match the behavior of ImmutableArray.IndexOf.
This fast path can be removed because -1 is returned even without it.

}

Requires.Range(index >= 0 && index <= this.Count, nameof(index));
Requires.Range(count >= 0 && count <= this.Count, nameof(count));
Requires.Range(index + count <= this.Count, nameof(count));

equalityComparer ??= EqualityComparer<T>.Default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ public void IndexOf()
(b, v) => b.IndexOf(v),
(b, v, i) => b.IndexOf(v, i),
(b, v, i, c) => b.IndexOf(v, i, c),
(b, v, i, c, eq) => b.IndexOf(v, i, c, eq));
(b, v, i, c, eq) => b.IndexOf(v, i, c, eq),
"startIndex");
}

[Fact]
Expand All @@ -268,6 +269,23 @@ public void IndexOf_WithoutCountParam()
Assert.Equal(-1, builder.IndexOf(-5, 2, absComparer));
}

[Fact]
public void IndexOfConsistentWithArray()
{
ImmutableArray<int>.Builder builder = ImmutableArray.Create(1, 2, 3, 4).ToBuilder();
int[] array = new[] { 1, 2, 3, 4 };

Assert.Equal(-1, builder.IndexOf(2, builder.Count, 0));
Assert.Equal(-1, Array.IndexOf(array, 2, array.Length, 0));

for (int i = 0; i < array.Length; i++)
{
int builderResult = builder.IndexOf(builder[i], 0, builder.Count);
int arrayResult = Array.IndexOf(array, array[i], 0, array.Length);
Assert.Equal(builderResult, arrayResult);
}
}

[Fact]
public void LastIndexOf()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,25 @@ public void IndexOf()
(b, v) => b.IndexOf(v),
(b, v, i) => b.IndexOf(v, i),
(b, v, i, c) => b.IndexOf(v, i, c),
(b, v, i, c, eq) => b.IndexOf(v, i, c, eq));
(b, v, i, c, eq) => b.IndexOf(v, i, c, eq),
"startIndex");
}

[Fact]
public void IndexOfConsistentWithArray()
{
ImmutableArray<int> immutableArray = ImmutableArray.Create(1, 2, 3, 4);
int[] array = new[] { 1, 2, 3, 4 };

Assert.Equal(-1, immutableArray.IndexOf(2, immutableArray.Length, 0));
Assert.Equal(-1, Array.IndexOf(array, 2, array.Length, 0));

for (int i = 0; i < array.Length; i++)
{
int immutableArrayResult = immutableArray.IndexOf(immutableArray[i], 0, immutableArray.Length);
int arrayResult = Array.IndexOf(array, array[i], 0, array.Length);
Assert.Equal(immutableArrayResult, arrayResult);
}
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ public void IndexOf()
(b, v) => b.IndexOf(v),
(b, v, i) => b.IndexOf(v, i),
(b, v, i, c) => b.IndexOf(v, i, c),
(b, v, i, c, eq) => b.IndexOf(v, i, c, eq));
(b, v, i, c, eq) => b.IndexOf(v, i, c, eq),
"index");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,13 +494,32 @@ public void IndexOf()
(b, v) => b.IndexOf(v),
(b, v, i) => b.IndexOf(v, i),
(b, v, i, c) => b.IndexOf(v, i, c),
(b, v, i, c, eq) => b.IndexOf(v, i, c, eq));
(b, v, i, c, eq) => b.IndexOf(v, i, c, eq),
"index");
IndexOfTests.IndexOfTest(
seq => (IImmutableList<int>)ImmutableList.CreateRange(seq),
(b, v) => b.IndexOf(v),
(b, v, i) => b.IndexOf(v, i),
(b, v, i, c) => b.IndexOf(v, i, c),
(b, v, i, c, eq) => b.IndexOf(v, i, c, eq));
(b, v, i, c, eq) => b.IndexOf(v, i, c, eq),
"index");
}

[Fact]
public void IndexOfConsistentWithArray()
{
ImmutableList<int> list = ImmutableList.Create(1, 2, 3, 4);
int[] array = new[] { 1, 2, 3, 4 };

Assert.Equal(-1, list.IndexOf(2, list.Count, 0));
Assert.Equal(-1, Array.IndexOf(array, 2, array.Length, 0));

for (int i = 0; i < array.Length; i++)
{
int listResult = list.IndexOf(list[i], 0, list.Count);
int arrayResult = Array.IndexOf(array, array[i], 0, array.Length);
Assert.Equal(listResult, arrayResult);
}
}

[Fact]
Expand Down
42 changes: 32 additions & 10 deletions src/libraries/System.Collections.Immutable/tests/IndexOfTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,48 @@ public static void IndexOfTest<TCollection>(
Func<TCollection, int, int> indexOfItem,
Func<TCollection, int, int, int> indexOfItemIndex,
Func<TCollection, int, int, int, int> indexOfItemIndexCount,
Func<TCollection, int, int, int, IEqualityComparer<int>, int> indexOfItemIndexCountEQ)
Func<TCollection, int, int, int, IEqualityComparer<int>, int> indexOfItemIndexCountEQ,
string indexParameterName)
{
TCollection emptyCollection = factory(new int[0]);
TCollection singleCollection = factory(new[] { 10 });
TCollection collection1256 = factory(new[] { 1, 2, 5, 6 });

Assert.Throws<ArgumentOutOfRangeException>(() => indexOfItemIndexCountEQ(emptyCollection, 100, 1, 1, EqualityComparer<int>.Default));
Assert.Throws<ArgumentOutOfRangeException>(() => indexOfItemIndexCountEQ(emptyCollection, 100, -1, 1, EqualityComparer<int>.Default));
Assert.Throws<ArgumentOutOfRangeException>(() => indexOfItemIndexCountEQ(collection1256, 100, 1, 20, EqualityComparer<int>.Default));
Assert.Throws<ArgumentOutOfRangeException>(() => indexOfItemIndexCountEQ(collection1256, 100, 1, -1, EqualityComparer<int>.Default));
Assert.Throws<ArgumentOutOfRangeException>(() => indexOfItemIndexCountEQ(emptyCollection, 100, 1, 1, new CustomComparer(50)));
Assert.Throws<ArgumentOutOfRangeException>(() => indexOfItemIndexCountEQ(emptyCollection, 100, -1, 1, new CustomComparer(50)));
Assert.Throws<ArgumentOutOfRangeException>(() => indexOfItemIndexCountEQ(collection1256, 100, 1, 20, new CustomComparer(1)));
Assert.Throws<ArgumentOutOfRangeException>(() => indexOfItemIndexCountEQ(collection1256, 100, 1, -1, new CustomComparer(1)));
AssertExtensions.Throws<ArgumentOutOfRangeException>(indexParameterName, () => indexOfItemIndexCountEQ(emptyCollection, 100, 1, 1, EqualityComparer<int>.Default));
AssertExtensions.Throws<ArgumentOutOfRangeException>(indexParameterName, () => indexOfItemIndexCountEQ(emptyCollection, 100, -1, 1, EqualityComparer<int>.Default));
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => indexOfItemIndexCountEQ(singleCollection, 100, 1, 1, EqualityComparer<int>.Default));
AssertExtensions.Throws<ArgumentOutOfRangeException>(indexParameterName, () => indexOfItemIndexCountEQ(singleCollection, 100, -1, 1, EqualityComparer<int>.Default));
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => indexOfItemIndexCountEQ(collection1256, 100, 1, 20, EqualityComparer<int>.Default));
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => indexOfItemIndexCountEQ(collection1256, 100, 1, -1, EqualityComparer<int>.Default));
AssertExtensions.Throws<ArgumentOutOfRangeException>(indexParameterName, () => indexOfItemIndexCountEQ(emptyCollection, 100, 1, 1, new CustomComparer(50)));
AssertExtensions.Throws<ArgumentOutOfRangeException>(indexParameterName, () => indexOfItemIndexCountEQ(emptyCollection, 100, -1, 1, new CustomComparer(50)));
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => indexOfItemIndexCountEQ(singleCollection, 100, 1, 1, new CustomComparer(50)));
AssertExtensions.Throws<ArgumentOutOfRangeException>(indexParameterName, () => indexOfItemIndexCountEQ(singleCollection, 100, -1, 1, new CustomComparer(50)));
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => indexOfItemIndexCountEQ(collection1256, 100, 1, 20, new CustomComparer(1)));
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => indexOfItemIndexCountEQ(collection1256, 100, 1, -1, new CustomComparer(1)));
AssertExtensions.Throws<ArgumentOutOfRangeException>(indexParameterName, () => indexOfItemIndex(collection1256, 2, 5));
AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => indexOfItemIndexCountEQ(collection1256, 6, 2, 4, EqualityComparer<int>.Default));

Assert.Equal(-1, indexOfItem(emptyCollection, 5));
Assert.Equal(-1, indexOfItemIndex(emptyCollection, 5, 0));
Assert.Equal(2, indexOfItemIndex(collection1256, 5, 1));
Assert.Equal(-1, indexOfItemIndexCount(emptyCollection, 5, 0, 0));
Assert.Equal(-1, indexOfItemIndexCountEQ(emptyCollection, 5, 0, 0, EqualityComparer<int>.Default));

Assert.Equal(0, indexOfItem(singleCollection, 10));
Assert.Equal(0, indexOfItemIndex(singleCollection, 10, 0));
Assert.Equal(0, indexOfItemIndexCount(singleCollection, 10, 0, 1));
Assert.Equal(0, indexOfItemIndexCountEQ(singleCollection, 10, 0, 1, EqualityComparer<int>.Default));
Assert.Equal(-1, indexOfItemIndexCountEQ(singleCollection, 100, 0, 1, EqualityComparer<int>.Default));
Assert.Equal(-1, indexOfItemIndexCountEQ(singleCollection, 10, 1, 0, EqualityComparer<int>.Default));

Assert.Equal(2, indexOfItem(collection1256, 5));
Assert.Equal(2, indexOfItemIndex(collection1256, 5, 1));
Assert.Equal(-1, indexOfItemIndexCount(collection1256, 5, 1, 1));
Assert.Equal(2, indexOfItemIndexCount(collection1256, 5, 1, 2));
Assert.Equal(2, indexOfItemIndexCountEQ(collection1256, 5, 0, 4, EqualityComparer<int>.Default));
Assert.Equal(3, indexOfItemIndexCountEQ(collection1256, 6, 0, 4, EqualityComparer<int>.Default));
Assert.Equal(-1, indexOfItemIndexCountEQ(collection1256, 100, 0, 4, EqualityComparer<int>.Default));
Assert.Equal(-1, indexOfItemIndexCountEQ(collection1256, 100, 4, 0, EqualityComparer<int>.Default));

// Create a list with contents: 100,101,102,103,104,100,101,102,103,104
ImmutableList<int> list = ImmutableList<int>.Empty.AddRange(Enumerable.Range(100, 5).Concat(Enumerable.Range(100, 5)));
Expand Down
Loading