using System;
using System.Collections.Generic;
namespace JM.LinqFaster
{
public static partial class LinqFaster
{
///
/// Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.
///
/// The first sequence to merge.
/// The second sequence to merge.
/// A function that specifies how to merge the elements from the two sequences.
/// A sequence that contains merged elements of two input sequences.
public static R[] ZipF(this T[] first, U[] second, Func selector)
{
if (first == null)
{
throw Error.ArgumentNull("first");
}
if (second == null)
{
throw Error.ArgumentNull("second");
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
//maintain array bounds elision
if (first.Length < second.Length)
{
var result = new R[first.Length];
for (int i = 0; i < first.Length; i++)
{
result[i] = selector(first[i], second[i]);
}
return result;
}
else
{
var result = new R[second.Length];
for (int i = 0; i < second.Length; i++)
{
result[i] = selector(first[i], second[i]);
}
return result;
}
}
///
/// Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.
///
/// The first sequence to merge.
/// The second sequence to merge.
/// A function that specifies how to merge the elements from the two sequences.
/// A sequence that contains merged elements of two input sequences.
public static List ZipF(this List first, List second, Func selector)
{
if (first == null)
{
throw Error.ArgumentNull("first");
}
if (second == null)
{
throw Error.ArgumentNull("second");
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
//maintain array bounds elision
if (first.Count < second.Count)
{
var result = new List(first.Count);
for (int i = 0; i < first.Count; i++)
{
result.Add(selector(first[i], second[i]));
}
return result;
}
else
{
var result = new List(second.Count);
for (int i = 0; i < second.Count; i++)
{
result.Add(selector(first[i], second[i]));
}
return result;
}
}
}
}