Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 109 additions & 9 deletions src/System.Linq/src/System/Linq/Enumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ public override IEnumerable<TResult2> Select<TResult2>(Func<TResult, TResult2> s
}


internal sealed class SelectArrayIterator<TSource, TResult> : Iterator<TResult>, IArrayProvider<TResult>
internal sealed class SelectArrayIterator<TSource, TResult> : Iterator<TResult>, IArrayProvider<TResult>, IListProvider<TResult>
{
private readonly TSource[] _source;
private readonly Func<TSource, TResult> _selector;
Expand Down Expand Up @@ -573,9 +573,20 @@ public TResult[] ToArray()
}
return results;
}

public List<TResult> ToList()
{
TSource[] source = _source;
var results = new List<TResult>(source.Length);
for (int i = 0; i < source.Length; i++)
{
Copy link
Member

Choose a reason for hiding this comment

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

You need to capture _source into a local here to let JIT optimize the range check. Otherwise JIT must assume that _source may change on every iteration and will not optimize.

results.Add(_selector(source[i]));
}
return results;
}
}

internal sealed class SelectListIterator<TSource, TResult> : Iterator<TResult>, IArrayProvider<TResult>
internal sealed class SelectListIterator<TSource, TResult> : Iterator<TResult>, IArrayProvider<TResult>, IListProvider<TResult>
{
private readonly List<TSource> _source;
private readonly Func<TSource, TResult> _selector;
Expand Down Expand Up @@ -628,9 +639,20 @@ public TResult[] ToArray()
}
return results;
}

public List<TResult> ToList()
{
int count = _source.Count;
var results = new List<TResult>(count);
for (int i = 0; i < count; i++)
{
Copy link
Member

Choose a reason for hiding this comment

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

for (int i = 0, cnt = _source.Count; i < cnt; i++)

Copy link
Member

Choose a reason for hiding this comment

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

(You probably want to move the setting of cnt to before constructing the list so you can use the cnt in the ctor call as well.)

results.Add(_selector(_source[i]));
}
return results;
}
}

internal sealed class SelectIListIterator<TSource, TResult> : Iterator<TResult>, IArrayProvider<TResult>
internal sealed class SelectIListIterator<TSource, TResult> : Iterator<TResult>, IArrayProvider<TResult>, IListProvider<TResult>
{
private readonly IList<TSource> _source;
private readonly Func<TSource, TResult> _selector;
Expand Down Expand Up @@ -693,6 +715,17 @@ public TResult[] ToArray()
}
return results;
}

public List<TResult> ToList()
{
int count = _source.Count;
var results = new List<TResult>(count);
for (int i = 0; i < count; i++)
{
results.Add(_selector(_source[i]));
}
return results;
}
}

//public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) {
Expand Down Expand Up @@ -1223,7 +1256,8 @@ public static TSource[] ToArray<TSource>(this IEnumerable<TSource> source)
public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source)
{
if (source == null) throw Error.ArgumentNull("source");
return new List<TSource>(source);
IListProvider<TSource> listProvider = source as IListProvider<TSource>;
return listProvider != null ? listProvider.ToList() : new List<TSource>(source);
}

public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
Expand Down Expand Up @@ -1710,7 +1744,7 @@ public static IEnumerable<int> Range(int start, int count)
return new RangeIterator(start, count);
}

private sealed class RangeIterator : Iterator<int>, IArrayProvider<int>
private sealed class RangeIterator : Iterator<int>, IArrayProvider<int>, IListProvider<int>
{
private readonly int _start;
private readonly int _end;
Expand Down Expand Up @@ -1760,6 +1794,17 @@ public int[] ToArray()

return array;
}

public List<int> ToList()
{
List<int> list = new List<int>(_end - _start);
for (int cur = _start; cur != _end; cur++)
{
list.Add(cur);
}

return list;
}
}

public static IEnumerable<TResult> Repeat<TResult>(TResult element, int count)
Expand All @@ -1768,7 +1813,7 @@ public static IEnumerable<TResult> Repeat<TResult>(TResult element, int count)
return new RepeatIterator<TResult>(element, count);
}

private sealed class RepeatIterator<TResult> : Iterator<TResult>, IArrayProvider<TResult>
private sealed class RepeatIterator<TResult> : Iterator<TResult>, IArrayProvider<TResult>, IListProvider<TResult>
{
private readonly int _count;
private int _sent;
Expand Down Expand Up @@ -1811,6 +1856,14 @@ public TResult[] ToArray()

return array;
}

public List<TResult> ToList()
{
List<TResult> list = new List<TResult>(_count);
for (int i = 0; i != _count; ++i) list.Add(current);

return list;
}
}

public static IEnumerable<TResult> Empty<TResult>()
Expand Down Expand Up @@ -3137,6 +3190,18 @@ internal interface IArrayProvider<TElement>
TElement[] ToArray();
}

/// <summary>
/// An iterator that can produce a <see cref="List{TElement}"/> through an optimized path.
/// </summary>
internal interface IListProvider<TElement>
{
/// <summary>
/// Produce a <see cref="List{TElement}"/> of the sequence through an optimized path.
/// </summary>
/// <returns>The <see cref="List{TElement}"/>.</returns>
List<TElement> ToList();
}

internal class IdentityFunction<TElement>
{
public static Func<TElement, TElement> Instance
Expand All @@ -3162,7 +3227,7 @@ public interface ILookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>
bool Contains(TKey key);
}

public class Lookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>, ILookup<TKey, TElement>, IArrayProvider<IGrouping<TKey, TElement>>
public class Lookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>, ILookup<TKey, TElement>, IArrayProvider<IGrouping<TKey, TElement>>, IListProvider<IGrouping<TKey, TElement>>
{
private IEqualityComparer<TKey> _comparer;
private Grouping<TKey, TElement>[] _groupings;
Expand Down Expand Up @@ -3250,6 +3315,21 @@ IGrouping<TKey, TElement>[] IArrayProvider<IGrouping<TKey, TElement>>.ToArray()
return array;
}

List<IGrouping<TKey, TElement>> IListProvider<IGrouping<TKey, TElement>>.ToList()
{
List<IGrouping<TKey, TElement>> list = new List<IGrouping<TKey, TElement>>(_count);
Grouping<TKey, TElement> g = _lastGrouping;
if (g != null)
{
do
{
g = g.next;
list.Add(g);
} while (g != _lastGrouping);
}
return list;
}

public IEnumerable<TResult> ApplyResultSelector<TResult>(Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
{
Grouping<TKey, TElement> g = _lastGrouping;
Expand Down Expand Up @@ -3575,7 +3655,7 @@ IEnumerator IEnumerable.GetEnumerator()
}
}

internal class GroupedEnumerable<TSource, TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>, IArrayProvider<IGrouping<TKey, TElement>>
internal class GroupedEnumerable<TSource, TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>, IArrayProvider<IGrouping<TKey, TElement>>, IListProvider<IGrouping<TKey, TElement>>
{
private IEnumerable<TSource> _source;
private Func<TSource, TKey> _keySelector;
Expand Down Expand Up @@ -3608,9 +3688,15 @@ public IGrouping<TKey, TElement>[] ToArray()
IArrayProvider<IGrouping<TKey, TElement>> lookup = Lookup<TKey, TElement>.Create<TSource>(_source, _keySelector, _elementSelector, _comparer);
return lookup.ToArray();
}

public List<IGrouping<TKey, TElement>> ToList()
{
IListProvider<IGrouping<TKey, TElement>> lookup = Lookup<TKey, TElement>.Create<TSource>(_source, _keySelector, _elementSelector, _comparer);
return lookup.ToList();
}
}

internal abstract class OrderedEnumerable<TElement> : IOrderedEnumerable<TElement>, IArrayProvider<TElement>
internal abstract class OrderedEnumerable<TElement> : IOrderedEnumerable<TElement>, IArrayProvider<TElement>, IListProvider<TElement>
{
internal IEnumerable<TElement> source;

Expand Down Expand Up @@ -3643,6 +3729,20 @@ public TElement[] ToArray()
return array;
}

public List<TElement> ToList()
{
Buffer<TElement> buffer = new Buffer<TElement>(source);
int count = buffer.count;
List<TElement> list = new List<TElement>(count);
if (count > 0)
{
int[] map = SortedMap(buffer);
for (int i = 0; i != count; i++) list.Add(buffer.items[map[i]]);
}

return list;
}

internal abstract EnumerableSorter<TElement> GetEnumerableSorter(EnumerableSorter<TElement> next);

IEnumerator IEnumerable.GetEnumerator()
Expand Down
36 changes: 36 additions & 0 deletions src/System.Linq/tests/GroupByTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -622,5 +622,41 @@ public void GroupingWithResultsToArray()
Assert.Equal(4, groupedArray.Length);
Assert.Equal(source.GroupBy(r => r.Name, (r, e) => e), groupedArray);
}

[Fact]
public void GroupingToList()
{
Record[] source = new Record[]
{
new Record { Name = "Tim", Score = 55 },
new Record { Name = "Chris", Score = 49 },
new Record { Name = "Robert", Score = -100 },
new Record { Name = "Chris", Score = 24 },
new Record { Name = "Prakash", Score = 9 },
new Record { Name = "Tim", Score = 25 }
};

List<IGrouping<string, Record>> groupedList = source.GroupBy(r => r.Name).ToList();
Assert.Equal(4, groupedList.Count);
Assert.Equal(source.GroupBy(r => r.Name), groupedList);
}

[Fact]
public void GroupingWithResultsToList()
{
Record[] source = new Record[]
{
new Record { Name = "Tim", Score = 55 },
new Record { Name = "Chris", Score = 49 },
new Record { Name = "Robert", Score = -100 },
new Record { Name = "Chris", Score = 24 },
new Record { Name = "Prakash", Score = 9 },
new Record { Name = "Tim", Score = 25 }
};

List<IEnumerable<Record>> groupedList = source.GroupBy(r => r.Name, (r, e) => e).ToList();
Assert.Equal(4, groupedList.Count);
Assert.Equal(source.GroupBy(r => r.Name, (r, e) => e), groupedList);
}
}
}
25 changes: 25 additions & 0 deletions src/System.Linq/tests/OrderByTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,31 @@ public void OrderedToArray()
Assert.Equal(expected, source.OrderBy(e => e.Score).ToArray());
}

[Fact]
public void OrderedToList()
{
Record[] source = new Record[]
{
new Record { Name = "Tim", Score = 90 },
new Record { Name = "Robert", Score = 90 },
new Record { Name = "Prakash", Score = 90 },
new Record { Name = "Jim", Score = 90 },
new Record { Name = "John", Score = 90 },
new Record { Name = "Albert", Score = 90 },
};
Record[] expected = new Record[]
{
new Record { Name = "Tim", Score = 90 },
new Record { Name = "Robert", Score = 90 },
new Record { Name = "Prakash", Score = 90 },
new Record { Name = "Jim", Score = 90 },
new Record { Name = "John", Score = 90 },
new Record { Name = "Albert", Score = 90 },
};

Assert.Equal(expected, source.OrderBy(e => e.Score).ToList());
}

//FIXME: This will hang with a larger source. Do we want to deal with that case?
[Fact]
public void SurviveBadComparerAlwaysReturnsPositive()
Expand Down
8 changes: 8 additions & 0 deletions src/System.Linq/tests/RangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public void Range_ToArray_ProduceCorrectResult()
Assert.Equal(i + 1, array[i]);
}

[Fact]
public void Range_ToList_ProduceCorrectResult()
{
var list = Enumerable.Range(1, 100).ToList();
Assert.Equal(list.Count, 100);
for (var i = 0; i < list.Count; i++)
Assert.Equal(i + 1, list[i]);
}

[Fact]
public void Range_ZeroCountLeadToEmptySequence()
Expand Down
9 changes: 9 additions & 0 deletions src/System.Linq/tests/RepeatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ public void Repeat_ToArray_ProduceCorrectResult()
Assert.Equal(1, array[i]);
}

[Fact]
public void Repeat_ToList_ProduceCorrectResult()
{
var list = Enumerable.Repeat(1, 100).ToList();
Assert.Equal(list.Count, 100);
for (var i = 0; i < list.Count; i++)
Assert.Equal(1, list[i]);
}

[Fact]
public void Repeat_ProduceSameObject()
{
Expand Down
Loading