| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
using System; |
| 3 |
using System.Collections.Generic; |
| 4 |
using System.Linq; |
| 5 |
using System.Threading; |
| 6 |
|
| 7 |
namespace System.Linq |
| 8 |
{ |
| 9 |
public static partial class EnumerableEx |
| 10 |
{ |
| 11 |
/// <summary> |
| 12 |
/// Concatenates the input sequences. |
| 13 |
/// </summary> |
| 14 |
/// <typeparam name="TSource">Source sequence element type.</typeparam> |
| 15 |
/// <param name="sources">Source sequences.</param> |
| 16 |
/// <returns>Sequence with the elements of the source sequences concatenated.</returns> |
| 17 |
public static IEnumerable<TSource> Concat<TSource>(this IEnumerable<IEnumerable<TSource>> sources) |
| 18 |
{ |
| 19 |
if (sources == null) |
| 20 |
throw new ArgumentNullException("sources"); |
| 21 |
|
| 22 |
return sources.Concat_(); |
| 23 |
} |
| 24 |
|
| 25 |
/// <summary> |
| 26 |
/// Concatenates the input sequences. |
| 27 |
/// </summary> |
| 28 |
/// <typeparam name="TSource">Source sequence element type.</typeparam> |
| 29 |
/// <param name="sources">Source sequences.</param> |
| 30 |
/// <returns>Sequence with the elements of the source sequences concatenated.</returns> |
| 31 |
public static IEnumerable<TSource> Concat<TSource>(params IEnumerable<TSource>[] sources) |
| 32 |
{ |
| 33 |
if (sources == null) |
| 34 |
throw new ArgumentNullException("sources"); |
| 35 |
|
| 36 |
return sources.Concat_(); |
| 37 |
} |
| 38 |
|
| 39 |
private static IEnumerable<TSource> Concat_<TSource>(this IEnumerable<IEnumerable<TSource>> sources) |
| 40 |
{ |
| 41 |
foreach (var source in sources) |
| 42 |
foreach (var item in source) |
| 43 |
yield return item; |
| 44 |
} |
| 45 |
|
| 46 |
/// <summary> |
| 47 |
/// Projects each element of a sequence to an given sequence and flattens the resulting sequences into one sequence. |
| 48 |
/// </summary> |
| 49 |
/// <typeparam name="TSource">First source sequence element type.</typeparam> |
| 50 |
/// <typeparam name="TOther">Second source sequence element type.</typeparam> |
| 51 |
/// <param name="source">A sequence of values to project.</param> |
| 52 |
/// <param name="other">Inner sequence each source sequenec element is projected onto.</param> |
| 53 |
/// <returns>Sequence flattening the sequences that result from projecting elements in the source sequence.</returns> |
| 54 |
public static IEnumerable<TOther> SelectMany<TSource, TOther>(this IEnumerable<TSource> source, IEnumerable<TOther> other) |
| 55 |
{ |
| 56 |
if (source == null) |
| 57 |
throw new ArgumentNullException("source"); |
| 58 |
if (other == null) |
| 59 |
throw new ArgumentNullException("other"); |
| 60 |
|
| 61 |
return source.SelectMany(_ => other); |
| 62 |
} |
| 63 |
|
| 64 |
#if NO_ZIP |
| 65 |
/// <summary> |
| 66 |
/// Merges two sequences by applying the specified selector function on index-based corresponding element pairs from both sequences. |
| 67 |
/// </summary> |
| 68 |
/// <typeparam name="TFirst">The type of the elements of the first input sequence.</typeparam> |
| 69 |
/// <typeparam name="TSecond">The type of the elements of the second input sequence.</typeparam> |
| 70 |
/// <typeparam name="TResult">The type of the elements of the result sequence.</typeparam> |
| 71 |
/// <param name="first">The first sequence to merge.</param> |
| 72 |
/// <param name="second">The second sequence to merge.</param> |
| 73 |
/// <param name="resultSelector">Function to apply to each pair of elements from both sequences.</param> |
| 74 |
/// <returns>Sequence consisting of the result of pairwise application of the selector function over pairs of elements from the source sequences.</returns> |
| 75 |
public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector) |
| 76 |
{ |
| 77 |
if (first == null) |
| 78 |
throw new ArgumentNullException("first"); |
| 79 |
if (second == null) |
| 80 |
throw new ArgumentNullException("second"); |
| 81 |
if (resultSelector == null) |
| 82 |
throw new ArgumentNullException("resultSelector"); |
| 83 |
|
| 84 |
return Zip_(first, second, resultSelector); |
| 85 |
} |
| 86 |
|
| 87 |
private static IEnumerable<TResult> Zip_<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector) |
| 88 |
{ |
| 89 |
using (var e1 = first.GetEnumerator()) |
| 90 |
using (var e2 = second.GetEnumerator()) |
| 91 |
while (e1.MoveNext() && e2.MoveNext()) |
| 92 |
yield return resultSelector(e1.Current, e2.Current); |
| 93 |
} |
| 94 |
#endif |
| 95 |
} |
| 96 |
} |