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

Fix LINQ Last() in ConcatNIterator to also check base case (Concat2Iterator) #108486

Merged
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
12 changes: 7 additions & 5 deletions src/libraries/System.Linq/src/System/Linq/Concat.SpeedOpt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,19 +326,21 @@ private TSource[] PreallocatingToArray()

public override TSource? TryGetLast(out bool found)
{
ConcatNIterator<TSource>? node = this;
ConcatNIterator<TSource>? node, previousN = this;
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
TSource? result;
do
{
TSource? result = node._head.TryGetLast(out found);
node = previousN;
result = node._head.TryGetLast(out found);
if (found)
{
return result;
}
}
while ((node = node!.PreviousN) is not null);
while ((previousN = node.PreviousN) is not null);

found = false;
return default;
Debug.Assert(node._tail is Concat2Iterator<TSource>);
return node._tail.TryGetLast(out found);
}
}

Expand Down
27 changes: 27 additions & 0 deletions src/libraries/System.Linq/tests/ConcatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public void VerifyEquals(IEnumerable<int> expected, IEnumerable<int> actual)
[MemberData(nameof(ConcatWithSelfData))]
[MemberData(nameof(ChainedCollectionConcatData))]
[MemberData(nameof(AppendedPrependedConcatAlternationsData))]
[MemberData(nameof(ConcatWithEmptyEnumerableData))]
public void First_Last_ElementAt(IEnumerable<int> _, IEnumerable<int> actual)
{
int count = actual.Count();
Expand Down Expand Up @@ -232,6 +233,32 @@ public static IEnumerable<object[]> AppendedPrependedConcatAlternationsData()
}
}

public static IEnumerable<object[]> ConcatWithEmptyEnumerableData()
{
List<int> baseList = [0, 1, 2, 3, 4];

yield return new object[]
{
Enumerable.Range(0, 5),
Enumerable.Concat(Enumerable.Concat(new List<int>(), new List<int>()), baseList)
};
yield return new object[]
{
Enumerable.Range(0, 5),
Enumerable.Concat(new List<int>(), baseList)
};
yield return new object[]
{
Enumerable.Range(0, 5),
Enumerable.Concat(Enumerable.Concat(baseList, new List<int>()), new List<int>())
};
yield return new object[]
{
Enumerable.Range(0, 5),
Enumerable.Concat(baseList, new List<int>())
};
}

private static IEnumerable<object[]> GenerateSourcesData(
Func<IEnumerable<int>, IEnumerable<int>> outerTransform = null,
Func<IEnumerable<int>, IEnumerable<int>> innerTransform = null)
Expand Down