Throw
}
- class PredicateOf<T> {
+#if !FULL_AOT_RUNTIME
+ static class PredicateOf<T> {
public static readonly Func<T, bool> Always = (t) => true;
}
+#endif
+
+ static class Function<T> {
+ public static readonly Func<T, T> Identity = (t) => t;
+ }
+
+ static class EmptyOf<T> {
+ public static readonly T[] Instance = new T [0];
+ }
+
+ static class ReadOnlyCollectionOf<T> {
+ public static readonly ReadOnlyCollection<T> Empty = new ReadOnlyCollection<T> (EmptyOf<T>.Instance);
+ }
#region Aggregate
// if zero elements and treat the first element differently
using (var enumerator = source.GetEnumerator ()) {
if (!enumerator.MoveNext ())
- throw new InvalidOperationException ("No elements in source list");
+ throw EmptySequence ();
TSource folded = enumerator.Current;
while (enumerator.MoveNext ())
{
Check.Source (source);
+ var collection = source as ICollection<TSource>;
+ if (collection != null)
+ return collection.Count > 0;
+
using (var enumerator = source.GetEnumerator ())
return enumerator.MoveNext ();
}
public static double Average (this IEnumerable<int> source)
{
- return Average<int, long, double> (source, (a, b) => a + b, (a, b) => (double) a / (double) b);
+ Check.Source (source);
+
+ long total = 0;
+ int count = 0;
+ foreach (var element in source){
+ total = checked (total + element);
+ count++;
+ }
+ if (count == 0)
+ throw EmptySequence ();
+ return total / (double) count;
}
public static double Average (this IEnumerable<long> source)
{
- return Average<long, long, double> (source, (a, b) => a + b, (a, b) => (double) a / (double) b);
+ Check.Source (source);
+
+ long total = 0;
+ long count = 0;
+ foreach (var element in source){
+ total += element;
+ count++;
+ }
+ if (count == 0)
+ throw EmptySequence ();
+ return total / (double) count;
}
public static double Average (this IEnumerable<double> source)
{
- return Average<double, double, double> (source, (a, b) => a + b, (a, b) => a / b);
+ Check.Source (source);
+
+ double total = 0;
+ long count = 0;
+ foreach (var element in source){
+ total += element;
+ count++;
+ }
+ if (count == 0)
+ throw EmptySequence ();
+ return total / count;
}
public static float Average (this IEnumerable<float> source)
{
- return Average<float, double, float> (source, (a, b) => a + b, (a, b) => (float) a / (float) b);
- }
+ Check.Source (source);
- public static decimal Average (this IEnumerable<decimal> source)
- {
- return Average<decimal, decimal, decimal> (source, (a, b) => a + b, (a, b) => a / b);
+ float total = 0;
+ long count = 0;
+ foreach (var element in source){
+ total += element;
+ count++;
+ }
+ if (count == 0)
+ throw EmptySequence ();
+ return total / count;
}
- static TResult Average<TElement, TAggregate, TResult> (this IEnumerable<TElement> source,
- Func<TAggregate, TElement, TAggregate> func, Func<TAggregate, long, TResult> result)
- where TElement : struct
- where TAggregate : struct
- where TResult : struct
+ public static decimal Average (this IEnumerable<decimal> source)
{
Check.Source (source);
- var total = default (TAggregate);
- long counter = 0;
- foreach (var element in source) {
- total = func (total, element);
- ++counter;
+ decimal total = 0;
+ long count = 0;
+ foreach (var element in source){
+ total += element;
+ count++;
}
-
- if (counter == 0)
- throw new InvalidOperationException ();
-
- return result (total, counter);
+ if (count == 0)
+ throw EmptySequence ();
+ return total / count;
}
static TResult? AverageNullable<TElement, TAggregate, TResult> (this IEnumerable<TElement?> source,
{
Check.Source (source);
- return source.AverageNullable<int, long, double> ((a, b) => a + b, (a, b) => (double) a / (double) b);
+ long total = 0;
+ long counter = 0;
+
+ foreach (var element in source) {
+ if (!element.HasValue)
+ continue;
+
+ total = total + element.Value;
+ counter++;
+ }
+
+ if (counter == 0)
+ return null;
+
+ return new double? (total / (double) counter);
}
public static double? Average (this IEnumerable<long?> source)
{
Check.Source (source);
- return source.AverageNullable<long, long, double> ((a, b) => a + b, (a, b) => a / b);
+ long total = 0;
+ long counter = 0;
+
+ foreach (var element in source) {
+ if (!element.HasValue)
+ continue;
+
+ total = checked (total + element.Value);
+ counter++;
+ }
+
+ if (counter == 0)
+ return null;
+
+ return new double? (total / (double) counter);
+
}
public static double? Average (this IEnumerable<double?> source)
{
Check.Source (source);
- return source.AverageNullable<double, double, double> ((a, b) => a + b, (a, b) => a / b);
+ double total = 0;
+ long counter = 0;
+
+ foreach (var element in source) {
+ if (!element.HasValue)
+ continue;
+
+ total = total + element.Value;
+ counter++;
+ }
+
+ if (counter == 0)
+ return null;
+
+ return new double? (total / counter);
+
}
public static decimal? Average (this IEnumerable<decimal?> source)
{
Check.Source (source);
- return source.AverageNullable<decimal, decimal, decimal> ((a, b) => a + b, (a, b) => a / b);
+ decimal total = 0;
+ long counter = 0;
+
+ foreach (var element in source) {
+ if (!element.HasValue)
+ continue;
+
+ total = total + element.Value;
+ counter++;
+ }
+
+ if (counter == 0)
+ return null;
+
+ return new decimal? (total / counter);
+
}
public static float? Average (this IEnumerable<float?> source)
{
Check.Source (source);
- return source.AverageNullable<float, double, float> ((a, b) => a + b, (a, b) => (float) a / (float) b);
+ float total = 0;
+ long counter = 0;
+
+ foreach (var element in source) {
+ if (!element.HasValue)
+ continue;
+
+ total = total + element.Value;
+ counter++;
+ }
+
+ if (counter == 0)
+ return null;
+
+ return new float? (total / counter);
+
}
public static double Average<TSource> (this IEnumerable<TSource> source, Func<TSource, int> selector)
{
Check.SourceAndSelector (source, selector);
- return source.Select (selector).Average<int, long, double> ((a, b) => a + b, (a, b) => (double) a / (double) b);
+ long total = 0;
+ long count = 0;
+ foreach (var element in source){
+ total += selector (element);
+ count++;
+ }
+ if (count == 0)
+ throw EmptySequence ();
+ return total / (double) count;
}
public static double? Average<TSource> (this IEnumerable<TSource> source, Func<TSource, int?> selector)
{
Check.SourceAndSelector (source, selector);
- return source.Select (selector).AverageNullable<int, long, double> ((a, b) => a + b, (a, b) => (double) a / (double) b);
+ long total = 0;
+ long counter = 0;
+
+ foreach (var element in source) {
+ var value = selector (element);
+ if (!value.HasValue)
+ continue;
+
+ total = total + value.Value;
+ counter++;
+ }
+
+ if (counter == 0)
+ return null;
+
+ return new double? (total / (double) counter);
}
public static double Average<TSource> (this IEnumerable<TSource> source, Func<TSource, long> selector)
{
Check.SourceAndSelector (source, selector);
- return source.Select (selector).Average<long, long, double> ((a, b) => a + b, (a, b) => (double) a / (double) b);
+ long total = 0;
+ long count = 0;
+ foreach (var element in source){
+ total = checked (total + selector (element));
+ count++;
+ }
+ if (count == 0)
+ throw EmptySequence ();
+ return total / (double) count;
+
}
public static double? Average<TSource> (this IEnumerable<TSource> source, Func<TSource, long?> selector)
{
Check.SourceAndSelector (source, selector);
- return source.Select (selector).AverageNullable<long, long, double> ((a, b) => a + b, (a, b) => (double) a / (double) b);
+ long total = 0;
+ long counter = 0;
+
+ foreach (var element in source) {
+ var value = selector (element);
+ if (!value.HasValue)
+ continue;
+
+ total = checked (total + value.Value);
+ counter++;
+ }
+
+ if (counter == 0)
+ return null;
+
+ return new double? (total / (double) counter);
}
public static double Average<TSource> (this IEnumerable<TSource> source, Func<TSource, double> selector)
{
Check.SourceAndSelector (source, selector);
- return source.Select (selector).Average<double, double, double> ((a, b) => a + b, (a, b) => a / b);
+ double total = 0;
+ long count = 0;
+ foreach (var element in source){
+ total += selector (element);
+ count++;
+ }
+ if (count == 0)
+ throw EmptySequence ();
+ return total / count;
+
}
public static double? Average<TSource> (this IEnumerable<TSource> source, Func<TSource, double?> selector)
{
Check.SourceAndSelector (source, selector);
- return source.Select (selector).AverageNullable<double, double, double> ((a, b) => a + b, (a, b) => a / b);
+ double total = 0;
+ long counter = 0;
+
+ foreach (var element in source) {
+ var value = selector (element);
+ if (!value.HasValue)
+ continue;
+
+ total = total + value.Value;
+ counter++;
+ }
+
+ if (counter == 0)
+ return null;
+
+ return new double? (total / counter);
+
}
public static float Average<TSource> (this IEnumerable<TSource> source, Func<TSource, float> selector)
{
Check.SourceAndSelector (source, selector);
- return source.Select (selector).Average<float, double, float> ((a, b) => a + b, (a, b) => (float) a / (float) b);
+ float total = 0;
+ long count = 0;
+ foreach (var element in source){
+ total += selector (element);
+ count++;
+ }
+ if (count == 0)
+ throw EmptySequence ();
+ return total / count;
}
public static float? Average<TSource> (this IEnumerable<TSource> source, Func<TSource, float?> selector)
{
Check.SourceAndSelector (source, selector);
- return source.Select (selector).AverageNullable<float, double, float> ((a, b) => a + b, (a, b) => (float) a / (float) b);
+ float total = 0;
+ long counter = 0;
+
+ foreach (var element in source) {
+ var value = selector (element);
+ if (!value.HasValue)
+ continue;
+
+ total = total + value.Value;
+ counter++;
+ }
+
+ if (counter == 0)
+ return null;
+
+ return new float? (total / counter);
}
public static decimal Average<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal> selector)
{
Check.SourceAndSelector (source, selector);
- return source.Select (selector).Average<decimal, decimal, decimal> ((a, b) => a + b, (a, b) => a / b);
+ decimal total = 0;
+ long count = 0;
+ foreach (var element in source){
+ total += selector (element);
+ count++;
+ }
+ if (count == 0)
+ throw EmptySequence ();
+ return total / count;
}
public static decimal? Average<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
{
Check.SourceAndSelector (source, selector);
- return source.Select (selector).AverageNullable<decimal, decimal, decimal> ((a, b) => a + b, (a, b) => a / b);
+ decimal total = 0;
+ long counter = 0;
+
+ foreach (var element in source) {
+ var value = selector (element);
+ if (!value.HasValue)
+ continue;
+
+ total = total + value.Value;
+ counter++;
+ }
+
+ if (counter == 0)
+ return null;
+
+ return new decimal? (total / counter);
}
#endregion
{
Check.Source (source);
+ var actual = source as IEnumerable<TResult>;
+ if (actual != null)
+ return actual;
+
return CreateCastIterator<TResult> (source);
}
static IEnumerable<TResult> CreateCastIterator<TResult> (IEnumerable source)
{
- foreach (object element in source)
- yield return (TResult) element;
+ foreach (TResult element in source)
+ yield return element;
}
#endregion
int counter = 0;
using (var enumerator = source.GetEnumerator ())
while (enumerator.MoveNext ())
- counter++;
+ checked { counter++; }
return counter;
}
- public static int Count<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> selector)
+ public static int Count<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
- Check.SourceAndSelector (source, selector);
+ Check.SourceAndSelector (source, predicate);
int counter = 0;
foreach (var element in source)
- if (selector (element))
- counter++;
+ if (predicate (element))
+ checked { counter++; }
return counter;
}
public static IEnumerable<TResult> Empty<TResult> ()
{
- return new TResult [0];
+ return EmptyOf<TResult>.Instance;
}
#endregion
{
var items = new HashSet<TSource> (second, comparer);
foreach (var element in first) {
- if (!items.Contains (element, comparer))
+ if (items.Add (element))
yield return element;
}
}
return element;
if (fallback == Fallback.Throw)
- throw new InvalidOperationException ();
+ throw NoMatchingElement ();
return default (TSource);
}
{
Check.Source (source);
- return source.First (PredicateOf<TSource>.Always, Fallback.Throw);
+ var list = source as IList<TSource>;
+ if (list != null) {
+ if (list.Count != 0)
+ return list [0];
+ } else {
+ using (var enumerator = source.GetEnumerator ()) {
+ if (enumerator.MoveNext ())
+ return enumerator.Current;
+ }
+ }
+
+ throw EmptySequence ();
}
public static TSource First<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
Check.Source (source);
+#if !FULL_AOT_RUNTIME
return source.First (PredicateOf<TSource>.Always, Fallback.Default);
+#else
+ // inline the code to reduce dependency o generic causing AOT errors on device (e.g. bug #3285)
+ foreach (var element in source)
+ return element;
+
+ return default (TSource);
+#endif
}
public static TSource FirstOrDefault<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
Check.SourceAndKeySelector (source, keySelector);
- Dictionary<TKey, List<TSource>> groups = new Dictionary<TKey, List<TSource>> ();
- List<TSource> nullList = new List<TSource> ();
+ return CreateGroupByIterator (source, keySelector, comparer);
+ }
+
+ static IEnumerable<IGrouping<TKey, TSource>> CreateGroupByIterator<TSource, TKey> (this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
+ {
+ var groups = new Dictionary<TKey, List<TSource>> ();
+ var nullList = new List<TSource> ();
int counter = 0;
int nullCounter = -1;
}
counter = 0;
- foreach (KeyValuePair<TKey, List<TSource>> group in groups) {
+ foreach (var group in groups) {
if (counter == nullCounter) {
- Grouping<TKey, TSource> nullGroup = new Grouping<TKey, TSource> (default (TKey), nullList);
- yield return nullGroup;
+ yield return new Grouping<TKey, TSource> (default (TKey), nullList);
counter++;
}
- Grouping<TKey, TSource> grouping = new Grouping<TKey, TSource> (group.Key, group.Value);
- yield return grouping;
+
+ yield return new Grouping<TKey, TSource> (group.Key, group.Value);
counter++;
}
- }
+ if (counter == nullCounter) {
+ yield return new Grouping<TKey, TSource> (default (TKey), nullList);
+ counter++;
+ }
+ }
public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement> (this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
{
Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
- Dictionary<TKey, List<TElement>> groups = new Dictionary<TKey, List<TElement>> ();
- List<TElement> nullList = new List<TElement> ();
+ return CreateGroupByIterator (source, keySelector, elementSelector, comparer);
+ }
+
+ static IEnumerable<IGrouping<TKey, TElement>> CreateGroupByIterator<TSource, TKey, TElement> (this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
+ {
+ var groups = new Dictionary<TKey, List<TElement>> ();
+ var nullList = new List<TElement> ();
int counter = 0;
int nullCounter = -1;
}
counter = 0;
- foreach (KeyValuePair<TKey, List<TElement>> group in groups) {
+ foreach (var group in groups) {
if (counter == nullCounter) {
- Grouping<TKey, TElement> nullGroup = new Grouping<TKey, TElement> (default (TKey), nullList);
- yield return nullGroup;
+ yield return new Grouping<TKey, TElement> (default (TKey), nullList);
counter++;
}
- Grouping<TKey, TElement> grouping = new Grouping<TKey, TElement> (group.Key, group.Value);
- yield return grouping;
+
+ yield return new Grouping<TKey, TElement> (group.Key, group.Value);
+ counter++;
+ }
+
+ if (counter == nullCounter) {
+ yield return new Grouping<TKey, TElement> (default (TKey), nullList);
counter++;
}
}
Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector,
Func<TKey, IEnumerable<TElement>, TResult> resultSelector,
IEqualityComparer<TKey> comparer)
+ {
+ Check.GroupBySelectors (source, keySelector, elementSelector, resultSelector);
+
+ return CreateGroupByIterator (source, keySelector, elementSelector, resultSelector, comparer);
+ }
+
+ static IEnumerable<TResult> CreateGroupByIterator<TSource, TKey, TElement, TResult> (this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector,
+ Func<TKey, IEnumerable<TElement>, TResult> resultSelector,
+ IEqualityComparer<TKey> comparer)
{
IEnumerable<IGrouping<TKey, TElement>> groups = GroupBy<TSource, TKey, TElement> (
source, keySelector, elementSelector, comparer);
Func<TSource, TKey> keySelector,
Func<TKey, IEnumerable<TSource>, TResult> resultSelector,
IEqualityComparer<TKey> comparer)
+ {
+ Check.SourceAndKeyResultSelectors (source, keySelector, resultSelector);
+
+ return CreateGroupByIterator (source, keySelector, resultSelector, comparer);
+ }
+
+ static IEnumerable<TResult> CreateGroupByIterator<TSource, TKey, TResult> (this IEnumerable<TSource> source,
+ Func<TSource, TKey> keySelector,
+ Func<TKey, IEnumerable<TSource>, TResult> resultSelector,
+ IEqualityComparer<TKey> comparer)
{
IEnumerable<IGrouping<TKey,TSource>> groups = GroupBy<TSource, TKey> (source, keySelector, comparer);
if (comparer == null)
comparer = EqualityComparer<TKey>.Default;
+ return CreateGroupJoinIterator (outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
+ }
+
+ static IEnumerable<TResult> CreateGroupJoinIterator<TOuter, TInner, TKey, TResult> (this IEnumerable<TOuter> outer,
+ IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector,
+ Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector,
+ IEqualityComparer<TKey> comparer)
+ {
ILookup<TKey, TInner> innerKeys = ToLookup<TInner, TKey> (inner, innerKeySelector, comparer);
/*Dictionary<K, List<U>> innerKeys = new Dictionary<K, List<U>> ();
foreach (U element in inner)
{
var items = new HashSet<TSource> (second, comparer);
foreach (TSource element in first) {
- if (items.Contains (element))
+ if (items.Remove (element))
yield return element;
}
}
if (comparer == null)
comparer = EqualityComparer<TKey>.Default;
+ return CreateJoinIterator (outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
+ }
+
+ static IEnumerable<TResult> CreateJoinIterator<TOuter, TInner, TKey, TResult> (this IEnumerable<TOuter> outer,
+ IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector,
+ Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector, IEqualityComparer<TKey> comparer)
+ {
ILookup<TKey, TInner> innerKeys = ToLookup<TInner, TKey> (inner, innerKeySelector, comparer);
/*Dictionary<K, List<U>> innerKeys = new Dictionary<K, List<U>> ();
foreach (U element in inner)
IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector,
Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector)
{
- return Join<TOuter, TInner, TKey, TResult> (outer, inner, outerKeySelector, innerKeySelector, resultSelector, null);
+ return outer.Join (inner, outerKeySelector, innerKeySelector, resultSelector, null);
}
#endregion
return item;
if (fallback == Fallback.Throw)
- throw new InvalidOperationException ();
+ throw NoMatchingElement ();
return item;
}
{
Check.Source (source);
+ var collection = source as ICollection<TSource>;
+ if (collection != null && collection.Count == 0)
+ throw EmptySequence ();
+
+ var list = source as IList<TSource>;
+ if (list != null)
+ return list [list.Count - 1];
+
+#if !FULL_AOT_RUNTIME
return source.Last (PredicateOf<TSource>.Always, Fallback.Throw);
- }
+#else
+ var empty = true;
+ var item = default (TSource);
+
+ foreach (var element in source) {
+ item = element;
+ empty = false;
+ }
+
+ if (!empty)
+ return item;
+
+ throw EmptySequence ();
+#endif
+ }
public static TSource Last<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
if (list != null)
return list.Count > 0 ? list [list.Count - 1] : default (TSource);
+#if !FULL_AOT_RUNTIME
return source.Last (PredicateOf<TSource>.Always, Fallback.Default);
+#else
+ var empty = true;
+ var item = default (TSource);
+
+ foreach (var element in source) {
+ item = element;
+ empty = false;
+ }
+
+ if (!empty)
+ return item;
+
+ return item;
+#endif
}
public static TSource LastOrDefault<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
return counter;
}
- public static long LongCount<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> selector)
+ public static long LongCount<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
- Check.SourceAndSelector (source, selector);
+ Check.SourceAndSelector (source, predicate);
long counter = 0;
foreach (TSource element in source)
- if (selector (element))
+ if (predicate (element))
counter++;
return counter;
{
Check.Source (source);
- return Iterate (source, int.MinValue, (a, b) => Math.Max (a, b));
+ bool empty = true;
+ var max = int.MinValue;
+ foreach (var element in source){
+ max = Math.Max (element, max);
+ empty = false;
+ }
+ if (empty)
+ throw EmptySequence();
+ return max;
}
public static long Max (this IEnumerable<long> source)
{
Check.Source (source);
- return Iterate (source, long.MinValue, (a, b) => Math.Max (a, b));
+ bool empty = true;
+ var max = long.MinValue;
+ foreach (var element in source){
+ max = Math.Max (element, max);
+ empty = false;
+ }
+ if (empty)
+ throw EmptySequence ();
+ return max;
}
public static double Max (this IEnumerable<double> source)
{
Check.Source (source);
- return Iterate (source, double.MinValue, (a, b) => Math.Max (a, b));
- }
-
- public static float Max (this IEnumerable<float> source)
+ bool empty = true;
+ var max = double.MinValue;
+ foreach (var element in source){
+ max = Math.Max (element, max);
+ empty = false;
+ }
+ if (empty)
+ throw EmptySequence ();
+ return max;
+ }
+
+ public static float Max (this IEnumerable<float> source)
{
Check.Source (source);
- return Iterate (source, float.MinValue, (a, b) => Math.Max (a, b));
+ bool empty = true;
+ var max = float.MinValue;
+ foreach (var element in source){
+ max = Math.Max (element, max);
+ empty = false;
+ }
+ if (empty)
+ throw EmptySequence ();
+ return max;
}
public static decimal Max (this IEnumerable<decimal> source)
{
Check.Source (source);
- return Iterate (source, decimal.MinValue, (a, b) => Math.Max (a, b));
+ bool empty = true;
+ var max = decimal.MinValue;
+ foreach (var element in source){
+ max = Math.Max (element, max);
+ empty = false;
+ }
+ if (empty)
+ throw EmptySequence ();
+ return max;
}
public static int? Max (this IEnumerable<int?> source)
{
Check.Source (source);
- return IterateNullable (source, int.MinValue, (a, b) => a > b);
+ bool empty = true;
+ var max = int.MinValue;
+
+ foreach (var element in source) {
+ if (!element.HasValue)
+ continue;
+
+ max = Math.Max (element.Value, max);
+ empty = false;
+ }
+
+ if (empty)
+ return null;
+
+ return max;
}
public static long? Max (this IEnumerable<long?> source)
{
Check.Source (source);
- return IterateNullable (source, long.MinValue, (a, b) => a > b);
+ bool empty = true;
+ var max = long.MinValue;
+
+ foreach (var element in source) {
+ if (!element.HasValue)
+ continue;
+
+ max = Math.Max (element.Value, max);
+ empty = false;
+ }
+
+ if (empty)
+ return null;
+
+ return max;
}
public static double? Max (this IEnumerable<double?> source)
{
Check.Source (source);
- return IterateNullable (source, double.MinValue, (a, b) => a > b);
+ bool empty = true;
+ var max = double.MinValue;
+
+ foreach (var element in source) {
+ if (!element.HasValue)
+ continue;
+
+ max = Math.Max (element.Value, max);
+ empty = false;
+ }
+
+ if (empty)
+ return null;
+
+ return max;
}
public static float? Max (this IEnumerable<float?> source)
{
Check.Source (source);
- return IterateNullable (source, float.MinValue, (a, b) => a > b);
+ bool empty = true;
+ var max = float.MinValue;
+
+ foreach (var element in source) {
+ if (!element.HasValue)
+ continue;
+
+ max = Math.Max (element.Value, max);
+ empty = false;
+ }
+
+ if (empty)
+ return null;
+
+ return max;
}
public static decimal? Max (this IEnumerable<decimal?> source)
{
Check.Source (source);
- return IterateNullable (source, decimal.MinValue, (a, b) => a > b);
- }
-
- static T? IterateNullable<T> (IEnumerable<T?> source, T initValue, Func<T?, T?, bool> selector) where T : struct
- {
- int counter = 0;
- T? value = initValue;
+ bool empty = true;
+ var max = decimal.MinValue;
+
foreach (var element in source) {
if (!element.HasValue)
continue;
- if (selector (element.Value, value))
- value = element;
- ++counter;
+ max = Math.Max (element.Value, max);
+ empty = false;
}
- if (counter == 0)
+ if (empty)
return null;
- return value;
+ return max;
}
+ // TODO: test nullable and non-nullable
public static TSource Max<TSource> (this IEnumerable<TSource> source)
{
Check.Source (source);
- bool notAssigned = true;
- TSource maximum = default (TSource);
- int counter = 0;
- foreach (TSource element in source) {
- if (notAssigned) {
- maximum = element;
- notAssigned = false;
- } else {
- int comparison;
- if (element is IComparable<TSource>)
- comparison = ((IComparable<TSource>) element).CompareTo (maximum);
- else if (element is System.IComparable)
- comparison = ((System.IComparable) element).CompareTo (maximum);
- else
- throw new ArgumentNullException ();
+ var comparer = Comparer<TSource>.Default;
- if (comparison > 0)
- maximum = element;
+ TSource max = default (TSource);
+
+ if (default (TSource) == null){
+ foreach (var element in source) {
+ if (element == null)
+ continue;
+
+ if (max == null || comparer.Compare (element, max) > 0)
+ max = element;
}
- counter++;
+ } else {
+ bool empty = true;
+ foreach (var element in source) {
+ if (empty){
+ max = element;
+ empty = false;
+ continue;
+ }
+ if (comparer.Compare (element, max) > 0)
+ max = element;
+ }
+ if (empty)
+ throw EmptySequence ();
}
-
- if (counter == 0)
- throw new InvalidOperationException ();
- else
- return maximum;
+ return max;
}
public static int Max<TSource> (this IEnumerable<TSource> source, Func<TSource, int> selector)
{
Check.SourceAndSelector (source, selector);
- return Iterate (source, int.MinValue, (a, b) => Math.Max (selector (a), b));
+ bool empty = true;
+ var max = int.MinValue;
+ foreach (var element in source){
+ max = Math.Max (selector (element), max);
+ empty = false;
+ }
+ if (empty)
+ throw NoMatchingElement ();
+ return max;
}
public static long Max<TSource> (this IEnumerable<TSource> source, Func<TSource, long> selector)
{
Check.SourceAndSelector (source, selector);
- return Iterate (source, long.MinValue, (a, b) => Math.Max (selector (a), b));
+ bool empty = true;
+ var max = long.MinValue;
+ foreach (var element in source){
+ max = Math.Max (selector (element), max);
+ empty = false;
+ }
+ if (empty)
+ throw NoMatchingElement ();
+ return max;
}
public static double Max<TSource> (this IEnumerable<TSource> source, Func<TSource, double> selector)
{
Check.SourceAndSelector (source, selector);
- return Iterate (source, double.MinValue, (a, b) => Math.Max (selector (a), b));
+ bool empty = true;
+ var max = double.MinValue;
+ foreach (var element in source){
+ max = Math.Max (selector (element), max);
+ empty = false;
+ }
+ if (empty)
+ throw NoMatchingElement ();
+ return max;
}
public static float Max<TSource> (this IEnumerable<TSource> source, Func<TSource, float> selector)
{
Check.SourceAndSelector (source, selector);
- return Iterate (source, float.MinValue, (a, b) => Math.Max (selector (a), b));
+ bool empty = true;
+ var max = float.MinValue;
+ foreach (var element in source){
+ max = Math.Max (selector (element), max);
+ empty = false;
+ }
+ if (empty)
+ throw NoMatchingElement ();
+ return max;
}
public static decimal Max<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal> selector)
{
Check.SourceAndSelector (source, selector);
- return Iterate (source, decimal.MinValue, (a, b) => Math.Max (selector (a), b));
+ bool empty = true;
+ var max = decimal.MinValue;
+ foreach (var element in source){
+ max = Math.Max (selector (element), max);
+ empty = false;
+ }
+ if (empty)
+ throw NoMatchingElement ();
+ return max;
}
static U Iterate<T, U> (IEnumerable<T> source, U initValue, Func<T, U, U> selector)
{
- int counter = 0;
+ bool empty = true;
foreach (var element in source) {
initValue = selector (element, initValue);
- ++counter;
+ empty = false;
}
- if (counter == 0)
- throw new InvalidOperationException ();
+ if (empty)
+ throw NoMatchingElement ();
return initValue;
}
- static U? IterateNullable<T, U> (IEnumerable<T> source, U initialValue, Func<T, U?, U?> selector) where U : struct
+ public static int? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, int?> selector)
{
- int counter = 0;
- U? value = initialValue;
+ Check.SourceAndSelector (source, selector);
+
+ bool empty = true;
+ int? max = null;
foreach (var element in source) {
- value = selector (element, value);
- if (!value.HasValue)
- continue;
+ int? item = selector (element);
- ++counter;
+ if (!max.HasValue)
+ max = item;
+ else if (item > max)
+ max = item;
+ empty = false;
}
- if (counter == 0)
+ if (empty)
return null;
-
- return value;
+ return max;
}
- public static int? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, int?> selector)
+ public static long? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, long?> selector)
{
Check.SourceAndSelector (source, selector);
- return IterateNullable (source, int.MinValue, (a, b) => {
- var v = selector (a); return v > b ? v : b;
- });
- }
+ bool empty = true;
+ long? max = null;
+ foreach (var element in source) {
+ long? item = selector (element);
- public static long? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, long?> selector)
- {
- Check.SourceAndSelector (source, selector);
+ if (!max.HasValue)
+ max = item;
+ else if (item > max)
+ max = item;
+ empty = false;
+ }
- return IterateNullable (source, long.MinValue, (a, b) => {
- var v = selector (a); return v > b ? v : b;
- });
+ if (empty)
+ return null;
+ return max;
}
public static double? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, double?> selector)
{
Check.SourceAndSelector (source, selector);
- return IterateNullable (source, double.MinValue, (a, b) => {
- var v = selector (a); return v > b ? v : b;
- });
+ bool empty = true;
+ double? max = null;
+ foreach (var element in source) {
+ double? item = selector (element);
+
+ if (!max.HasValue)
+ max = item;
+ else if (item > max)
+ max = item;
+ empty = false;
+ }
+
+ if (empty)
+ return null;
+ return max;
}
public static float? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, float?> selector)
{
Check.SourceAndSelector (source, selector);
- return IterateNullable (source, float.MinValue, (a, b) => {
- var v = selector (a); return v > b ? v : b;
- });
+ bool empty = true;
+ float? max = null;
+ foreach (var element in source) {
+ float? item = selector (element);
+
+ if (!max.HasValue)
+ max = item;
+ else if (item > max)
+ max = item;
+ empty = false;
+ }
+
+ if (empty)
+ return null;
+ return max;
}
public static decimal? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
{
Check.SourceAndSelector (source, selector);
- return IterateNullable (source, decimal.MinValue, (a, b) => {
- var v = selector (a); return v > b ? v : b;
- });
+ bool empty = true;
+ decimal? max = null;
+ foreach (var element in source) {
+ decimal? item = selector (element);
+
+ if (!max.HasValue)
+ max = item;
+ else if (item > max)
+ max = item;
+ empty = false;
+ }
+
+ if (empty)
+ return null;
+ return max;
}
public static TResult Max<TSource, TResult> (this IEnumerable<TSource> source, Func<TSource, TResult> selector)
{
Check.SourceAndSelector (source, selector);
- bool notAssigned = true;
- TResult maximum = default (TResult);
- int counter = 0;
- foreach (TSource item in source) {
- TResult element = selector (item);
- if (notAssigned) {
- maximum = element;
- notAssigned = false;
- } else {
- int comparison;
- if (element is IComparable<TResult>)
- comparison = ((IComparable<TResult>) element).CompareTo (maximum);
- else if (element is System.IComparable)
- comparison = ((System.IComparable) element).CompareTo (maximum);
- else
- throw new ArgumentNullException ();
-
- if (comparison > 0)
- maximum = element;
- }
- counter++;
- }
-
- if (counter == 0)
- throw new InvalidOperationException ();
- else
- return maximum;
+ // TODO: inline
+ return source.Select (selector).Max ();
}
#endregion
{
Check.Source (source);
- return Iterate (source, int.MaxValue, (a, b) => Math.Min (a, b));
+ bool empty = true;
+ var min = int.MaxValue;
+ foreach (var element in source){
+ min = Math.Min (element, min);
+ empty = false;
+ }
+ if (empty)
+ throw EmptySequence ();
+ return min;
}
public static long Min (this IEnumerable<long> source)
{
Check.Source (source);
- return Iterate (source, long.MaxValue, (a, b) => Math.Min (a, b));
+ bool empty = true;
+ var min = long.MaxValue;
+ foreach (var element in source){
+ min = Math.Min (element, min);
+ empty = false;
+ }
+ if (empty)
+ throw EmptySequence ();
+ return min;
}
public static double Min (this IEnumerable<double> source)
{
Check.Source (source);
- return Iterate (source, double.MaxValue, (a, b) => Math.Min (a, b));
+ bool empty = true;
+ var min = double.MaxValue;
+ foreach (var element in source){
+ min = Math.Min (element, min);
+ empty = false;
+ }
+ if (empty)
+ throw EmptySequence ();
+ return min;
}
public static float Min (this IEnumerable<float> source)
{
Check.Source (source);
- return Iterate (source, float.MaxValue, (a, b) => Math.Min (a, b));
+ bool empty = true;
+ var min = float.MaxValue;
+ foreach (var element in source){
+ min = Math.Min (element, min);
+ empty = false;
+ }
+ if (empty)
+ throw EmptySequence ();
+ return min;
}
public static decimal Min (this IEnumerable<decimal> source)
{
Check.Source (source);
- return Iterate (source, decimal.MaxValue, (a, b) => Math.Min (a, b));
+ bool empty = true;
+ var min = decimal.MaxValue;
+ foreach (var element in source){
+ min = Math.Min (element, min);
+ empty = false;
+ }
+ if (empty)
+ throw EmptySequence ();
+ return min;
}
public static int? Min (this IEnumerable<int?> source)
{
Check.Source (source);
- return IterateNullable (source, int.MaxValue, (a, b) => a < b);
+ bool empty = true;
+ var min = int.MaxValue;
+
+ foreach (var element in source) {
+ if (!element.HasValue)
+ continue;
+
+ min = Math.Min (element.Value, min);
+ empty = false;
+ }
+
+ if (empty)
+ return null;
+
+ return min;
}
public static long? Min (this IEnumerable<long?> source)
{
Check.Source (source);
- return IterateNullable (source, long.MaxValue, (a, b) => a < b);
+ bool empty = true;
+ var min = long.MaxValue;
+
+ foreach (var element in source) {
+ if (!element.HasValue)
+ continue;
+
+ min = Math.Min (element.Value, min);
+ empty = false;
+ }
+
+ if (empty)
+ return null;
+
+ return min;
}
public static double? Min (this IEnumerable<double?> source)
{
Check.Source (source);
- return IterateNullable (source, double.MaxValue, (a, b) => a < b);
+ bool empty = true;
+ var min = double.MaxValue;
+
+ foreach (var element in source) {
+ if (!element.HasValue)
+ continue;
+
+ min = Math.Min (element.Value, min);
+ empty = false;
+ }
+
+ if (empty)
+ return null;
+
+ return min;
}
public static float? Min (this IEnumerable<float?> source)
{
Check.Source (source);
- return IterateNullable (source, float.MaxValue, (a, b) => a < b);
+ bool empty = true;
+ var min = float.MaxValue;
+
+ foreach (var element in source) {
+ if (!element.HasValue)
+ continue;
+
+ min = Math.Min (element.Value, min);
+ empty = false;
+ }
+
+ if (empty)
+ return null;
+
+ return min;
}
public static decimal? Min (this IEnumerable<decimal?> source)
{
Check.Source (source);
- return IterateNullable (source, decimal.MaxValue, (a, b) => a < b);
+ bool empty = true;
+ var min = decimal.MaxValue;
+
+ foreach (var element in source) {
+ if (!element.HasValue)
+ continue;
+
+ min = Math.Min (element.Value, min);
+ empty = false;
+ }
+
+ if (empty)
+ return null;
+
+ return min;
}
public static TSource Min<TSource> (this IEnumerable<TSource> source)
{
Check.Source (source);
- bool notAssigned = true;
- TSource minimum = default (TSource);
- int counter = 0;
- foreach (TSource element in source) {
- if (notAssigned) {
- minimum = element;
- notAssigned = false;
- } else {
- int comparison;
- if (element is IComparable<TSource>)
- comparison = ((IComparable<TSource>) element).CompareTo (minimum);
- else if (element is System.IComparable)
- comparison = ((System.IComparable) element).CompareTo (minimum);
- else
- throw new ArgumentNullException ();
+ var comparer = Comparer<TSource>.Default;
+
+ TSource min = default (TSource);
+
+ if (default (TSource) == null){
+ foreach (var element in source) {
+ if (element == null)
+ continue;
- if (comparison < 0)
- minimum = element;
+ if (min == null || comparer.Compare (element, min) < 0)
+ min = element;
}
- counter++;
+ } else {
+ bool empty = true;
+ foreach (var element in source) {
+ if (empty){
+ min = element;
+ empty = false;
+ continue;
+ }
+ if (comparer.Compare (element, min) < 0)
+ min = element;
+ }
+ if (empty)
+ throw EmptySequence ();
}
-
- if (counter == 0)
- throw new InvalidOperationException ();
- else
- return minimum;
+ return min;
}
public static int Min<TSource> (this IEnumerable<TSource> source, Func<TSource, int> selector)
{
Check.SourceAndSelector (source, selector);
- return Iterate (source, int.MaxValue, (a, b) => Math.Min (selector (a), b));
+ bool empty = true;
+ var min = int.MaxValue;
+ foreach (var element in source){
+ min = Math.Min (selector (element), min);
+ empty = false;
+ }
+ if (empty)
+ throw NoMatchingElement ();
+ return min;
}
public static long Min<TSource> (this IEnumerable<TSource> source, Func<TSource, long> selector)
{
Check.SourceAndSelector (source, selector);
- return Iterate (source, long.MaxValue, (a, b) => Math.Min (selector (a), b));
+ bool empty = true;
+ var min = long.MaxValue;
+ foreach (var element in source){
+ min = Math.Min (selector (element), min);
+ empty = false;
+ }
+ if (empty)
+ throw NoMatchingElement ();
+ return min;
}
public static double Min<TSource> (this IEnumerable<TSource> source, Func<TSource, double> selector)
{
Check.SourceAndSelector (source, selector);
- return Iterate (source, double.MaxValue, (a, b) => Math.Min (selector (a), b));
+ bool empty = true;
+ var min = double.MaxValue;
+ foreach (var element in source){
+ min = Math.Min (selector (element), min);
+ empty = false;
+ }
+ if (empty)
+ throw NoMatchingElement ();
+ return min;
}
public static float Min<TSource> (this IEnumerable<TSource> source, Func<TSource, float> selector)
{
Check.SourceAndSelector (source, selector);
- return Iterate (source, float.MaxValue, (a, b) => Math.Min (selector (a), b));
+ bool empty = true;
+ var min = float.MaxValue;
+ foreach (var element in source){
+ min = Math.Min (selector (element), min);
+ empty = false;
+ }
+ if (empty)
+ throw NoMatchingElement ();
+ return min;
}
public static decimal Min<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal> selector)
{
Check.SourceAndSelector (source, selector);
- return Iterate (source, decimal.MaxValue, (a, b) => Math.Min (selector (a), b));
+ bool empty = true;
+ var min = decimal.MaxValue;
+ foreach (var element in source){
+ min = Math.Min (selector (element), min);
+ empty = false;
+ }
+ if (empty)
+ throw NoMatchingElement ();
+ return min;
}
public static int? Min<TSource> (this IEnumerable<TSource> source, Func<TSource, int?> selector)
{
Check.SourceAndSelector (source, selector);
- return IterateNullable (source, int.MaxValue, (a, b) => {
- var v = selector (a); return v < b ? v : b;
- });
+ bool empty = true;
+ int? min = null;
+ foreach (var element in source) {
+ int? item = selector (element);
+
+ if (!min.HasValue)
+ min = item;
+ else if (item < min)
+ min = item;
+ empty = false;
+ }
+
+ if (empty)
+ return null;
+ return min;
}
public static long? Min<TSource> (this IEnumerable<TSource> source, Func<TSource, long?> selector)
{
Check.SourceAndSelector (source, selector);
- return IterateNullable (source, long.MaxValue, (a, b) => {
- var v = selector (a); return v < b ? v : b;
- });
+ bool empty = true;
+ long? min = null;
+ foreach (var element in source) {
+ long? item = selector (element);
+
+ if (!min.HasValue)
+ min = item;
+ else if (item < min)
+ min = item;
+ empty = false;
+ }
+
+ if (empty)
+ return null;
+ return min;
}
public static float? Min<TSource> (this IEnumerable<TSource> source, Func<TSource, float?> selector)
{
Check.SourceAndSelector (source, selector);
- return IterateNullable (source, float.MaxValue, (a, b) => {
- var v = selector (a); return v < b ? v : b;
- });
+ bool empty = true;
+ float? min = null;
+ foreach (var element in source) {
+ float? item = selector (element);
+
+ if (!min.HasValue)
+ min = item;
+ else if (item < min)
+ min = item;
+ empty = false;
+ }
+
+ if (empty)
+ return null;
+ return min;
}
public static double? Min<TSource> (this IEnumerable<TSource> source, Func<TSource, double?> selector)
{
Check.SourceAndSelector (source, selector);
- return IterateNullable (source, double.MaxValue, (a, b) => {
- var v = selector (a); return v < b ? v : b;
- });
+ bool empty = true;
+ double? min = null;
+ foreach (var element in source) {
+ double? item = selector (element);
+
+ if (!min.HasValue)
+ min = item;
+ else if (item < min)
+ min = item;
+ empty = false;
+ }
+
+ if (empty)
+ return null;
+ return min;
}
public static decimal? Min<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
{
Check.SourceAndSelector (source, selector);
- return IterateNullable (source, decimal.MaxValue, (a, b) => {
- var v = selector (a); return v < b ? v : b;
- });
+ bool empty = true;
+ decimal? min = null;
+ foreach (var element in source) {
+ decimal? item = selector (element);
+
+ if (!min.HasValue)
+ min = item;
+ else if (item < min)
+ min = item;
+ empty = false;
+ }
+
+ if (empty)
+ return null;
+ return min;
}
public static TResult Min<TSource, TResult> (this IEnumerable<TSource> source, Func<TSource, TResult> selector)
{
Check.SourceAndSelector (source, selector);
- bool notAssigned = true;
- TResult minimum = default (TResult);
- int counter = 0;
- foreach (TSource item in source) {
- TResult element = selector (item);
- if (notAssigned) {
- minimum = element;
- notAssigned = false;
- } else {
- int comparison;
- if (element is IComparable<TResult>)
- comparison = ((IComparable<TResult>) element).CompareTo (minimum);
- else if (element is System.IComparable)
- comparison = ((System.IComparable) element).CompareTo (minimum);
- else
- throw new ArgumentNullException ();
-
- if (comparison < 0)
- minimum = element;
- }
- counter++;
- }
-
- if (counter == 0)
- throw new InvalidOperationException ();
- else
- return minimum;
+ // TODO: inline
+ return source.Select (selector).Min ();
}
#endregion
if (count < 0)
throw new ArgumentOutOfRangeException ("count");
- long upto = ((long) start + count) - 1;
-
- if (upto > int.MaxValue)
+ if (((long) start + count) - 1L > int.MaxValue)
throw new ArgumentOutOfRangeException ();
- return CreateRangeIterator (start, (int) upto);
+ return CreateRangeIterator (start, count);
}
- static IEnumerable<int> CreateRangeIterator (int start, int upto)
+ static IEnumerable<int> CreateRangeIterator (int start, int count)
{
- for (int i = start; i <= upto; i++)
- yield return i;
+ for (int i = 0; i < count; i++)
+ yield return start + i;
}
#endregion
{
Check.Source (source);
- var list = source as IList<TSource>;
- if (list == null)
- list = new List<TSource> (source);
-
- return CreateReverseIterator (list);
+ return CreateReverseIterator (source);
}
- static IEnumerable<TSource> CreateReverseIterator<TSource> (IList<TSource> source)
+ static IEnumerable<TSource> CreateReverseIterator<TSource> (IEnumerable<TSource> source)
{
- for (int i = source.Count; i > 0; --i)
- yield return source [i - 1];
+ var array = source.ToArray ();
+
+ for (int i = array.Length - 1; i >= 0; i--)
+ yield return array [i];
}
#endregion
}
public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult> (this IEnumerable<TSource> source,
- Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> selector)
+ Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
{
- Check.SourceAndCollectionSelectors (source, collectionSelector, selector);
+ Check.SourceAndCollectionSelectors (source, collectionSelector, resultSelector);
- return CreateSelectManyIterator (source, collectionSelector, selector);
+ return CreateSelectManyIterator (source, collectionSelector, resultSelector);
}
static IEnumerable<TResult> CreateSelectManyIterator<TSource, TCollection, TResult> (IEnumerable<TSource> source,
}
public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult> (this IEnumerable<TSource> source,
- Func<TSource, int, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> selector)
+ Func<TSource, int, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
{
- Check.SourceAndCollectionSelectors (source, collectionSelector, selector);
+ Check.SourceAndCollectionSelectors (source, collectionSelector, resultSelector);
- return CreateSelectManyIterator (source, collectionSelector, selector);
+ return CreateSelectManyIterator (source, collectionSelector, resultSelector);
}
static IEnumerable<TResult> CreateSelectManyIterator<TSource, TCollection, TResult> (IEnumerable<TSource> source,
continue;
if (found)
- throw new InvalidOperationException ();
+ throw MoreThanOneMatchingElement ();
found = true;
item = element;
}
if (!found && fallback == Fallback.Throw)
- throw new InvalidOperationException ();
+ throw NoMatchingElement ();
return item;
}
{
Check.Source (source);
+#if !FULL_AOT_RUNTIME
return source.Single (PredicateOf<TSource>.Always, Fallback.Throw);
- }
+#else
+ var found = false;
+ var item = default (TSource);
+
+ foreach (var element in source) {
+ if (found)
+ throw MoreThanOneElement ();
+
+ found = true;
+ item = element;
+ }
+
+ if (!found)
+ throw NoMatchingElement ();
+
+ return item;
+#endif
+ }
public static TSource Single<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
{
Check.Source (source);
+#if !FULL_AOT_RUNTIME
return source.Single (PredicateOf<TSource>.Always, Fallback.Default);
- }
+#else
+ var found = false;
+ var item = default (TSource);
+
+ foreach (var element in source) {
+ if (found)
+ throw MoreThanOneMatchingElement ();
+
+ found = true;
+ item = element;
+ }
+
+ return item;
+#endif
+ }
public static TSource SingleOrDefault<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
static IEnumerable<TSource> CreateSkipIterator<TSource> (IEnumerable<TSource> source, int count)
{
- int i = 0;
- foreach (var element in source) {
- if (i++ < count)
- continue;
+ var enumerator = source.GetEnumerator ();
+ try {
+ while (count-- > 0)
+ if (!enumerator.MoveNext ())
+ yield break;
- yield return element;
+ while (enumerator.MoveNext ())
+ yield return enumerator.Current;
+
+ } finally {
+ enumerator.Dispose ();
}
}
public static int Sum (this IEnumerable<int> source)
{
Check.Source (source);
-
- return Sum<int, int> (source, (a, b) => a + b);
+ int total = 0;
+
+ foreach (var element in source)
+ total = checked (total + element);
+ return total;
}
public static int? Sum (this IEnumerable<int?> source)
{
Check.Source (source);
- return source.SumNullable<int?, int?> (0, (a, b) => a.HasValue ? a + b : a);
+ int total = 0;
+ foreach (var element in source) {
+ if (element.HasValue)
+ total = checked (total + element.Value);
+ }
+ return total;
}
public static int Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, int> selector)
{
Check.SourceAndSelector (source, selector);
+ int total = 0;
- return Sum<TSource, int> (source, (a, b) => a + selector (b));
+ foreach (var element in source)
+ total = checked (total + selector (element));
+
+ return total;
}
public static int? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, int?> selector)
{
Check.SourceAndSelector (source, selector);
- return source.SumNullable<TSource, int?> (0, (a, b) => {
- var value = selector (b);
- return value.HasValue ? a + value.Value : a;
- });
+ int total = 0;
+ foreach (var element in source) {
+ var value = selector (element);
+ if (value.HasValue)
+ total = checked (total + value.Value);
+ }
+ return total;
}
public static long Sum (this IEnumerable<long> source)
{
Check.Source (source);
- return Sum<long, long> (source, (a, b) => a + b);
+ long total = 0;
+
+ foreach (var element in source)
+ total = checked (total + element);
+ return total;
}
public static long? Sum (this IEnumerable<long?> source)
{
Check.Source (source);
- return source.SumNullable<long?, long?> (0, (a, b) => a.HasValue ? a + b : a);
+ long total = 0;
+ foreach (var element in source) {
+ if (element.HasValue)
+ total = checked (total + element.Value);
+ }
+ return total;
}
public static long Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, long> selector)
{
Check.SourceAndSelector (source, selector);
- return Sum<TSource, long> (source, (a, b) => a + selector (b));
+ long total = 0;
+ foreach (var element in source)
+ total = checked (total + selector (element));
+ return total;
}
public static long? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, long?> selector)
{
Check.SourceAndSelector (source, selector);
- return source.SumNullable<TSource, long?> (0, (a, b) => {
- var value = selector (b);
- return value.HasValue ? a + value.Value : a;
- });
+ long total = 0;
+ foreach (var element in source) {
+ var value = selector (element);
+ if (value.HasValue)
+ total = checked (total + value.Value);
+ }
+ return total;
}
public static double Sum (this IEnumerable<double> source)
{
Check.Source (source);
- return Sum<double, double> (source, (a, b) => a + b);
+ double total = 0;
+
+ foreach (var element in source)
+ total = checked (total + element);
+ return total;
}
public static double? Sum (this IEnumerable<double?> source)
{
Check.Source (source);
- return source.SumNullable<double?, double?> (0, (a, b) => a.HasValue ? a + b : a);
+ double total = 0;
+ foreach (var element in source) {
+ if (element.HasValue)
+ total = checked (total + element.Value);
+ }
+ return total;
}
public static double Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, double> selector)
{
Check.SourceAndSelector (source, selector);
- return Sum<TSource, double> (source, (a, b) => a + selector (b));
+ double total = 0;
+
+ foreach (var element in source)
+ total = checked (total + selector (element));
+ return total;
}
public static double? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, double?> selector)
{
Check.SourceAndSelector (source, selector);
- return source.SumNullable<TSource, double?> (0, (a, b) => {
- var value = selector (b);
- return value.HasValue ? a + value.Value : a;
- });
+ double total = 0;
+ foreach (var element in source) {
+ var value = selector (element);
+ if (value.HasValue)
+ total = checked (total + value.Value);
+ }
+ return total;
}
public static float Sum (this IEnumerable<float> source)
{
Check.Source (source);
- return Sum<float, float> (source, (a, b) => a + b);
+ float total = 0;
+
+ foreach (var element in source)
+ total = checked (total + element);
+ return total;
}
public static float? Sum (this IEnumerable<float?> source)
{
Check.Source (source);
- return source.SumNullable<float?, float?> (0, (a, b) => a.HasValue ? a + b : a);
+ float total = 0;
+ foreach (var element in source) {
+ if (element.HasValue)
+ total = checked (total + element.Value);
+ }
+ return total;
+
}
public static float Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, float> selector)
{
Check.SourceAndSelector (source, selector);
-
- return Sum<TSource, float> (source, (a, b) => a + selector (b));
+ float total = 0;
+ foreach (var element in source)
+ total = checked (total + selector (element));
+ return total;
}
public static float? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, float?> selector)
{
Check.SourceAndSelector (source, selector);
- return source.SumNullable<TSource, float?> (0, (a, b) => {
- var value = selector (b);
- return value.HasValue ? a + value.Value : a;
- });
+ float total = 0;
+ foreach (var element in source) {
+ var value = selector (element);
+ if (value.HasValue)
+ total = checked (total + value.Value);
+ }
+ return total;
}
public static decimal Sum (this IEnumerable<decimal> source)
{
Check.Source (source);
-
- return Sum<decimal, decimal> (source, (a, b) => a + b);
+ decimal total = 0;
+
+ foreach (var element in source)
+ total = checked (total + element);
+ return total;
}
public static decimal? Sum (this IEnumerable<decimal?> source)
{
Check.Source (source);
- return source.SumNullable<decimal?, decimal?> (0, (a, b) => a.HasValue ? a + b : a);
+ decimal total = 0;
+ foreach (var element in source) {
+ if (element.HasValue)
+ total = checked (total + element.Value);
+ }
+ return total;
+
}
public static decimal Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal> selector)
{
Check.SourceAndSelector (source, selector);
-
- return Sum<TSource, decimal> (source, (a, b) => a + selector (b));
+ decimal total = 0;
+
+ foreach (var element in source)
+ total = checked (total + selector (element));
+ return total;
}
public static decimal? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
{
Check.SourceAndSelector (source, selector);
- return source.SumNullable<TSource, decimal?> (0, (a, b) => {
- var value = selector (b);
- return value.HasValue ? a + value.Value : a;
- });
- }
-
- static TR Sum<TA, TR> (this IEnumerable<TA> source, Func<TR, TA, TR> selector)
- {
- TR total = default (TR);
- long counter = 0;
- foreach (var element in source) {
- total = selector (total, element);
- ++counter;
- }
-
- return total;
- }
-
- static TR SumNullable<TA, TR> (this IEnumerable<TA> source, TR zero, Func<TR, TA, TR> selector)
- {
- TR total = zero;
+ decimal total = 0;
foreach (var element in source) {
- total = selector (total, element);
+ var value = selector (element);
+ if (value.HasValue)
+ total = checked (total + value.Value);
}
-
return total;
}
int counter = 0;
foreach (TSource element in source) {
- if (counter++ == count)
- yield break;
-
yield return element;
+
+ if (++counter == count)
+ yield break;
}
}
{
Check.SourceAndKeySelector (source, keySelector);
+#if FULL_AOT_RUNTIME
+ var oe = source as OrderedEnumerable <TSource>;
+ if (oe != null)
+ return oe.CreateOrderedEnumerable (keySelector, comparer, false);
+#endif
+
return source.CreateOrderedEnumerable (keySelector, comparer, false);
}
{
Check.SourceAndKeySelector (source, keySelector);
+#if FULL_AOT_RUNTIME
+ var oe = source as OrderedEnumerable <TSource>;
+ if (oe != null)
+ return oe.CreateOrderedEnumerable (keySelector, comparer, true);
+#endif
return source.CreateOrderedEnumerable (keySelector, comparer, true);
}
{
Check.Source (source);
+ TSource[] array;
var collection = source as ICollection<TSource>;
if (collection != null) {
- var array = new TSource [collection.Count];
+ if (collection.Count == 0)
+ return EmptyOf<TSource>.Instance;
+
+ array = new TSource [collection.Count];
collection.CopyTo (array, 0);
return array;
}
- return new List<TSource> (source).ToArray ();
+ int pos = 0;
+ array = EmptyOf<TSource>.Instance;
+ foreach (var element in source) {
+ if (pos == array.Length) {
+ if (pos == 0)
+ array = new TSource [4];
+ else
+ Array.Resize (ref array, pos * 2);
+ }
+
+ array[pos++] = element;
+ }
+
+ if (pos != array.Length)
+ Array.Resize (ref array, pos);
+
+ return array;
}
#endregion
public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey> (this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
{
- Check.SourceAndKeySelector (source, keySelector);
-
- if (comparer == null)
- comparer = EqualityComparer<TKey>.Default;
-
- var dict = new Dictionary<TKey, TSource> (comparer);
- foreach (var e in source)
- dict.Add (keySelector (e), e);
-
- return dict;
+ return ToDictionary<TSource, TKey, TSource> (source, keySelector, Function<TSource>.Identity, comparer);
}
#endregion
public static ILookup<TKey, TSource> ToLookup<TSource, TKey> (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
- return ToLookup<TSource, TKey> (source, keySelector, null);
+ return ToLookup<TSource, TKey, TSource> (source, keySelector, Function<TSource>.Identity, null);
}
public static ILookup<TKey, TSource> ToLookup<TSource, TKey> (this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
{
- Check.SourceAndKeySelector (source, keySelector);
-
- var dictionary = new Dictionary<TKey, List<TSource>> (comparer ?? EqualityComparer<TKey>.Default);
- foreach (TSource element in source) {
- TKey key = keySelector (element);
- if (key == null)
- throw new ArgumentNullException ();
- if (!dictionary.ContainsKey (key))
- dictionary.Add (key, new List<TSource> ());
- dictionary [key].Add (element);
- }
- return new Lookup<TKey, TSource> (dictionary);
+ return ToLookup<TSource, TKey, TSource> (source, keySelector, Function<TSource>.Identity, comparer);
}
public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement> (this IEnumerable<TSource> source,
{
Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
- Dictionary<TKey, List<TElement>> dictionary = new Dictionary<TKey, List<TElement>> (comparer ?? EqualityComparer<TKey>.Default);
- foreach (TSource element in source) {
- TKey key = keySelector (element);
- if (key == null)
- throw new ArgumentNullException ();
- if (!dictionary.ContainsKey (key))
- dictionary.Add (key, new List<TElement> ());
- dictionary [key].Add (elementSelector (element));
+ List<TElement> nullKeyElements = null;
+
+ var dictionary = new Dictionary<TKey, List<TElement>> (comparer ?? EqualityComparer<TKey>.Default);
+ foreach (var element in source) {
+ var key = keySelector (element);
+
+ List<TElement> list;
+
+ if (key == null) {
+ if (nullKeyElements == null)
+ nullKeyElements = new List<TElement> ();
+
+ list = nullKeyElements;
+ } else if (!dictionary.TryGetValue (key, out list)) {
+ list = new List<TElement> ();
+ dictionary.Add (key, list);
+ }
+
+ list.Add (elementSelector (element));
}
- return new Lookup<TKey, TElement> (dictionary);
+
+ return new Lookup<TKey, TElement> (dictionary, nullKeyElements);
}
#endregion
if (comparer == null)
comparer = EqualityComparer<TSource>.Default;
- var first_enumerator = first.GetEnumerator ();
- var second_enumerator = second.GetEnumerator ();
+ using (IEnumerator<TSource> first_enumerator = first.GetEnumerator (),
+ second_enumerator = second.GetEnumerator ()) {
- while (first_enumerator.MoveNext ()) {
- if (!second_enumerator.MoveNext ())
- return false;
+ while (first_enumerator.MoveNext ()) {
+ if (!second_enumerator.MoveNext ())
+ return false;
- if (!comparer.Equals (first_enumerator.Current, second_enumerator.Current))
- return false;
- }
+ if (!comparer.Equals (first_enumerator.Current, second_enumerator.Current))
+ return false;
+ }
- return !second_enumerator.MoveNext ();
+ return !second_enumerator.MoveNext ();
+ }
}
#endregion
}
foreach (var element in second) {
- if (! items.Contains (element, comparer)) {
+ if (! items.Contains (element)) {
items.Add (element);
yield return element;
}
}
#endregion
+
+#if NET_4_0
+ #region Zip
+
+ public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult> (this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)
+ {
+ Check.FirstAndSecond (first, second);
+ if (resultSelector == null)
+ throw new ArgumentNullException ("resultSelector");
+
+ return CreateZipIterator (first, second, resultSelector);
+ }
+
+ static IEnumerable<TResult> CreateZipIterator<TFirst, TSecond, TResult> (IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> selector)
+ {
+ using (IEnumerator<TFirst> first_enumerator = first.GetEnumerator ()) {
+ using (IEnumerator<TSecond> second_enumerator = second.GetEnumerator ()) {
+
+ while (first_enumerator.MoveNext () && second_enumerator.MoveNext ()) {
+ yield return selector (first_enumerator.Current, second_enumerator.Current);
+ }
+ }
+ }
+ }
+
+ #endregion
+#endif
#region Where
{
Check.SourceAndPredicate (source, predicate);
+ // It cannot be IList<TSource> because it may break on user implementation
+ var array = source as TSource[];
+ if (array != null)
+ return CreateWhereIterator (array, predicate);
+
return CreateWhereIterator (source, predicate);
}
yield return element;
}
+ static IEnumerable<TSource> CreateWhereIterator<TSource> (TSource[] source, Func<TSource, bool> predicate)
+ {
+ for (int i = 0; i < source.Length; ++i) {
+ var element = source [i];
+ if (predicate (element))
+ yield return element;
+ }
+ }
+
public static IEnumerable<TSource> Where<TSource> (this IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
{
Check.SourceAndPredicate (source, predicate);
+ var array = source as TSource[];
+ if (array != null)
+ return CreateWhereIterator (array, predicate);
+
return CreateWhereIterator (source, predicate);
}
- static IEnumerable<TSource> CreateWhereIterator<TSource> (this IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
+ static IEnumerable<TSource> CreateWhereIterator<TSource> (IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
{
int counter = 0;
foreach (TSource element in source) {
}
}
- #endregion
-
- class ReadOnlyCollectionOf<T> {
- public static readonly ReadOnlyCollection<T> Empty = new ReadOnlyCollection<T> (new T [0]);
+ static IEnumerable<TSource> CreateWhereIterator<TSource> (TSource[] source, Func<TSource, int, bool> predicate)
+ {
+ for (int i = 0; i < source.Length; ++i) {
+ var element = source [i];
+ if (predicate (element, i))
+ yield return element;
+ }
}
+ #endregion
+
internal static ReadOnlyCollection<TSource> ToReadOnlyCollection<TSource> (this IEnumerable<TSource> source)
{
if (source == null)
return new ReadOnlyCollection<TSource> (source.ToArray<TSource> ());
}
+
+ #region Exception helpers
+
+ static Exception EmptySequence ()
+ {
+ return new InvalidOperationException (Locale.GetText ("Sequence contains no elements"));
+ }
+ static Exception NoMatchingElement ()
+ {
+ return new InvalidOperationException (Locale.GetText ("Sequence contains no matching element"));
+ }
+ static Exception MoreThanOneElement ()
+ {
+ return new InvalidOperationException (Locale.GetText ("Sequence contains more than one element"));
+ }
+ static Exception MoreThanOneMatchingElement ()
+ {
+ return new InvalidOperationException (Locale.GetText ("Sequence contains more than one matching element"));
+ }
+
+ #endregion
}
}