Permalink
67414d8 Jan 4, 2017
@jamesqo @stephentoub @JonHanna
782 lines (657 sloc) 24.3 KB
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Diagnostics;
using static System.Linq.Utilities;
namespace System.Linq
{
public static partial class Enumerable
{
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
if (source == null)
{
throw Error.ArgumentNull(nameof(source));
}
if (predicate == null)
{
throw Error.ArgumentNull(nameof(predicate));
}
Iterator<TSource> iterator = source as Iterator<TSource>;
if (iterator != null)
{
return iterator.Where(predicate);
}
TSource[] array = source as TSource[];
if (array != null)
{
return array.Length == 0 ?
(IEnumerable<TSource>)EmptyPartition<TSource>.Instance :
new WhereArrayIterator<TSource>(array, predicate);
}
List<TSource> list = source as List<TSource>;
if (list != null)
{
return new WhereListIterator<TSource>(list, predicate);
}
return new WhereEnumerableIterator<TSource>(source, predicate);
}
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
{
if (source == null)
{
throw Error.ArgumentNull(nameof(source));
}
if (predicate == null)
{
throw Error.ArgumentNull(nameof(predicate));
}
return WhereIterator(source, predicate);
}
private static IEnumerable<TSource> WhereIterator<TSource>(IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
{
int index = -1;
foreach (TSource element in source)
{
checked
{
index++;
}
if (predicate(element, index))
{
yield return element;
}
}
}
/// <summary>
/// An iterator that filters each item of an <see cref="IEnumerable{TSource}"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source enumerable.</typeparam>
internal sealed class WhereEnumerableIterator<TSource> : Iterator<TSource>, IIListProvider<TSource>
{
private readonly IEnumerable<TSource> _source;
private readonly Func<TSource, bool> _predicate;
private IEnumerator<TSource> _enumerator;
public WhereEnumerableIterator(IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
Debug.Assert(source != null);
Debug.Assert(predicate != null);
_source = source;
_predicate = predicate;
}
public override Iterator<TSource> Clone()
{
return new WhereEnumerableIterator<TSource>(_source, _predicate);
}
public override void Dispose()
{
if (_enumerator != null)
{
_enumerator.Dispose();
_enumerator = null;
}
base.Dispose();
}
public int GetCount(bool onlyIfCheap)
{
if (onlyIfCheap)
{
return -1;
}
int count = 0;
foreach (TSource item in _source)
{
if (_predicate(item))
{
checked
{
count++;
}
}
}
return count;
}
public override bool MoveNext()
{
switch (_state)
{
case 1:
_enumerator = _source.GetEnumerator();
_state = 2;
goto case 2;
case 2:
while (_enumerator.MoveNext())
{
TSource item = _enumerator.Current;
if (_predicate(item))
{
_current = item;
return true;
}
}
Dispose();
break;
}
return false;
}
public override IEnumerable<TResult> Select<TResult>(Func<TSource, TResult> selector)
{
return new WhereSelectEnumerableIterator<TSource, TResult>(_source, _predicate, selector);
}
public TSource[] ToArray()
{
var builder = new LargeArrayBuilder<TSource>(initialize: true);
foreach (TSource item in _source)
{
if (_predicate(item))
{
builder.Add(item);
}
}
return builder.ToArray();
}
public List<TSource> ToList()
{
var list = new List<TSource>();
foreach (TSource item in _source)
{
if (_predicate(item))
{
list.Add(item);
}
}
return list;
}
public override IEnumerable<TSource> Where(Func<TSource, bool> predicate)
{
return new WhereEnumerableIterator<TSource>(_source, CombinePredicates(_predicate, predicate));
}
}
/// <summary>
/// An iterator that filters each item of a <see cref="T:TSource[]"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source array.</typeparam>
internal sealed class WhereArrayIterator<TSource> : Iterator<TSource>, IIListProvider<TSource>
{
private readonly TSource[] _source;
private readonly Func<TSource, bool> _predicate;
public WhereArrayIterator(TSource[] source, Func<TSource, bool> predicate)
{
Debug.Assert(source != null && source.Length > 0);
Debug.Assert(predicate != null);
_source = source;
_predicate = predicate;
}
public override Iterator<TSource> Clone()
{
return new WhereArrayIterator<TSource>(_source, _predicate);
}
public int GetCount(bool onlyIfCheap)
{
if (onlyIfCheap)
{
return -1;
}
int count = 0;
foreach (TSource item in _source)
{
if (_predicate(item))
{
checked
{
count++;
}
}
}
return count;
}
public override bool MoveNext()
{
int index = _state - 1;
TSource[] source = _source;
while ((uint)index < (uint)source.Length)
{
TSource item = source[index];
index = _state++;
if (_predicate(item))
{
_current = item;
return true;
}
}
Dispose();
return false;
}
public override IEnumerable<TResult> Select<TResult>(Func<TSource, TResult> selector)
{
return new WhereSelectArrayIterator<TSource, TResult>(_source, _predicate, selector);
}
public TSource[] ToArray()
{
var builder = new LargeArrayBuilder<TSource>(_source.Length);
foreach (TSource item in _source)
{
if (_predicate(item))
{
builder.Add(item);
}
}
return builder.ToArray();
}
public List<TSource> ToList()
{
var list = new List<TSource>();
foreach (TSource item in _source)
{
if (_predicate(item))
{
list.Add(item);
}
}
return list;
}
public override IEnumerable<TSource> Where(Func<TSource, bool> predicate)
{
return new WhereArrayIterator<TSource>(_source, CombinePredicates(_predicate, predicate));
}
}
/// <summary>
/// An iterator that filters each item of a <see cref="List{TSource}"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source list.</typeparam>
internal sealed class WhereListIterator<TSource> : Iterator<TSource>, IIListProvider<TSource>
{
private readonly List<TSource> _source;
private readonly Func<TSource, bool> _predicate;
private List<TSource>.Enumerator _enumerator;
public WhereListIterator(List<TSource> source, Func<TSource, bool> predicate)
{
Debug.Assert(source != null);
Debug.Assert(predicate != null);
_source = source;
_predicate = predicate;
}
public override Iterator<TSource> Clone()
{
return new WhereListIterator<TSource>(_source, _predicate);
}
public int GetCount(bool onlyIfCheap)
{
if (onlyIfCheap)
{
return -1;
}
int count = 0;
for (int i = 0; i < _source.Count; i++)
{
TSource item = _source[i];
if (_predicate(item))
{
checked
{
count++;
}
}
}
return count;
}
public override bool MoveNext()
{
switch (_state)
{
case 1:
_enumerator = _source.GetEnumerator();
_state = 2;
goto case 2;
case 2:
while (_enumerator.MoveNext())
{
TSource item = _enumerator.Current;
if (_predicate(item))
{
_current = item;
return true;
}
}
Dispose();
break;
}
return false;
}
public override IEnumerable<TResult> Select<TResult>(Func<TSource, TResult> selector)
{
return new WhereSelectListIterator<TSource, TResult>(_source, _predicate, selector);
}
public TSource[] ToArray()
{
var builder = new LargeArrayBuilder<TSource>(_source.Count);
for (int i = 0; i < _source.Count; i++)
{
TSource item = _source[i];
if (_predicate(item))
{
builder.Add(item);
}
}
return builder.ToArray();
}
public List<TSource> ToList()
{
var list = new List<TSource>();
for (int i = 0; i < _source.Count; i++)
{
TSource item = _source[i];
if (_predicate(item))
{
list.Add(item);
}
}
return list;
}
public override IEnumerable<TSource> Where(Func<TSource, bool> predicate)
{
return new WhereListIterator<TSource>(_source, CombinePredicates(_predicate, predicate));
}
}
/// <summary>
/// An iterator that filters, then maps, each item of a <see cref="T:TSource[]"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source array.</typeparam>
/// <typeparam name="TResult">The type of the mapped items.</typeparam>
internal sealed class WhereSelectArrayIterator<TSource, TResult> : Iterator<TResult>, IIListProvider<TResult>
{
private readonly TSource[] _source;
private readonly Func<TSource, bool> _predicate;
private readonly Func<TSource, TResult> _selector;
public WhereSelectArrayIterator(TSource[] source, Func<TSource, bool> predicate, Func<TSource, TResult> selector)
{
Debug.Assert(source != null && source.Length > 0);
Debug.Assert(predicate != null);
Debug.Assert(selector != null);
_source = source;
_predicate = predicate;
_selector = selector;
}
public override Iterator<TResult> Clone()
{
return new WhereSelectArrayIterator<TSource, TResult>(_source, _predicate, _selector);
}
public int GetCount(bool onlyIfCheap)
{
// In case someone uses Count() to force evaluation of
// the selector, run it provided `onlyIfCheap` is false.
if (onlyIfCheap)
{
return -1;
}
int count = 0;
foreach (TSource item in _source)
{
if (_predicate(item))
{
_selector(item);
checked
{
count++;
}
}
}
return count;
}
public override bool MoveNext()
{
int index = _state - 1;
TSource[] source = _source;
while ((uint)index < (uint)source.Length)
{
TSource item = source[index];
index = _state++;
if (_predicate(item))
{
_current = _selector(item);
return true;
}
}
Dispose();
return false;
}
public override IEnumerable<TResult2> Select<TResult2>(Func<TResult, TResult2> selector)
{
return new WhereSelectArrayIterator<TSource, TResult2>(_source, _predicate, CombineSelectors(_selector, selector));
}
public TResult[] ToArray()
{
var builder = new LargeArrayBuilder<TResult>(_source.Length);
foreach (TSource item in _source)
{
if (_predicate(item))
{
builder.Add(_selector(item));
}
}
return builder.ToArray();
}
public List<TResult> ToList()
{
var list = new List<TResult>();
foreach (TSource item in _source)
{
if (_predicate(item))
{
list.Add(_selector(item));
}
}
return list;
}
}
/// <summary>
/// An iterator that filters, then maps, each item of a <see cref="List{TSource}"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source list.</typeparam>
/// <typeparam name="TResult">The type of the mapped items.</typeparam>
internal sealed class WhereSelectListIterator<TSource, TResult> : Iterator<TResult>, IIListProvider<TResult>
{
private readonly List<TSource> _source;
private readonly Func<TSource, bool> _predicate;
private readonly Func<TSource, TResult> _selector;
private List<TSource>.Enumerator _enumerator;
public WhereSelectListIterator(List<TSource> source, Func<TSource, bool> predicate, Func<TSource, TResult> selector)
{
Debug.Assert(source != null);
Debug.Assert(predicate != null);
Debug.Assert(selector != null);
_source = source;
_predicate = predicate;
_selector = selector;
}
public override Iterator<TResult> Clone()
{
return new WhereSelectListIterator<TSource, TResult>(_source, _predicate, _selector);
}
public int GetCount(bool onlyIfCheap)
{
// In case someone uses Count() to force evaluation of
// the selector, run it provided `onlyIfCheap` is false.
if (onlyIfCheap)
{
return -1;
}
int count = 0;
for (int i = 0; i < _source.Count; i++)
{
TSource item = _source[i];
if (_predicate(item))
{
_selector(item);
checked
{
count++;
}
}
}
return count;
}
public override bool MoveNext()
{
switch (_state)
{
case 1:
_enumerator = _source.GetEnumerator();
_state = 2;
goto case 2;
case 2:
while (_enumerator.MoveNext())
{
TSource item = _enumerator.Current;
if (_predicate(item))
{
_current = _selector(item);
return true;
}
}
Dispose();
break;
}
return false;
}
public override IEnumerable<TResult2> Select<TResult2>(Func<TResult, TResult2> selector)
{
return new WhereSelectListIterator<TSource, TResult2>(_source, _predicate, CombineSelectors(_selector, selector));
}
public TResult[] ToArray()
{
var builder = new LargeArrayBuilder<TResult>(_source.Count);
for (int i = 0; i < _source.Count; i++)
{
TSource item = _source[i];
if (_predicate(item))
{
builder.Add(_selector(item));
}
}
return builder.ToArray();
}
public List<TResult> ToList()
{
var list = new List<TResult>();
for (int i = 0; i < _source.Count; i++)
{
TSource item = _source[i];
if (_predicate(item))
{
list.Add(_selector(item));
}
}
return list;
}
}
/// <summary>
/// An iterator that filters, then maps, each item of an <see cref="IEnumerable{TSource}"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source enumerable.</typeparam>
/// <typeparam name="TResult">The type of the mapped items.</typeparam>
internal sealed class WhereSelectEnumerableIterator<TSource, TResult> : Iterator<TResult>, IIListProvider<TResult>
{
private readonly IEnumerable<TSource> _source;
private readonly Func<TSource, bool> _predicate;
private readonly Func<TSource, TResult> _selector;
private IEnumerator<TSource> _enumerator;
public WhereSelectEnumerableIterator(IEnumerable<TSource> source, Func<TSource, bool> predicate, Func<TSource, TResult> selector)
{
Debug.Assert(source != null);
Debug.Assert(predicate != null);
Debug.Assert(selector != null);
_source = source;
_predicate = predicate;
_selector = selector;
}
public override Iterator<TResult> Clone()
{
return new WhereSelectEnumerableIterator<TSource, TResult>(_source, _predicate, _selector);
}
public override void Dispose()
{
if (_enumerator != null)
{
_enumerator.Dispose();
_enumerator = null;
}
base.Dispose();
}
public int GetCount(bool onlyIfCheap)
{
// In case someone uses Count() to force evaluation of
// the selector, run it provided `onlyIfCheap` is false.
if (onlyIfCheap)
{
return -1;
}
int count = 0;
foreach (TSource item in _source)
{
if (_predicate(item))
{
_selector(item);
checked
{
count++;
}
}
}
return count;
}
public override bool MoveNext()
{
switch (_state)
{
case 1:
_enumerator = _source.GetEnumerator();
_state = 2;
goto case 2;
case 2:
while (_enumerator.MoveNext())
{
TSource item = _enumerator.Current;
if (_predicate(item))
{
_current = _selector(item);
return true;
}
}
Dispose();
break;
}
return false;
}
public override IEnumerable<TResult2> Select<TResult2>(Func<TResult, TResult2> selector)
{
return new WhereSelectEnumerableIterator<TSource, TResult2>(_source, _predicate, CombineSelectors(_selector, selector));
}
public TResult[] ToArray()
{
var builder = new LargeArrayBuilder<TResult>(initialize: true);
foreach (TSource item in _source)
{
if (_predicate(item))
{
builder.Add(_selector(item));
}
}
return builder.ToArray();
}
public List<TResult> ToList()
{
var list = new List<TResult>();
foreach (TSource item in _source)
{
if (_predicate(item))
{
list.Add(_selector(item));
}
}
return list;
}
}
}
}