using System;
using System.Collections.Generic;
namespace JM.LinqFaster
{
public static partial class LinqFaster
{
// -------------------------- Arrays --------------------------------------------
///
/// Projects each element of a sequence to another sequence and flattens the resulting sequences into one sequence.
/// Yo dawg, I heard you like sequences.
///
/// A sequence of values to project.
/// A transform function to apply to each element.
/// A sequence whose elements are the result of invoking the one-to-many transform function on each element of the input sequence.
public static TResult[] SelectManyF(this TSource[] source, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
var result = new List(source.Length);
for (int i = 0; i < source.Length; i++)
{
var va = selector(source[i]);
for (int j = 0; j < va.Length; j++)
{
result.Add(va[j]);
}
}
return result.ToArray();
}
///
/// Projects each element of a sequence to another sequence and flattens the resulting sequences into one sequence
/// utilizing the index of each element.
///
/// A sequence of values to project.
/// A transform function to apply to each element and it's index.
/// A sequence whose elements are the result of invoking the one-to-many transform function on each element and index of the input sequence.
public static TResult[] SelectManyF(this TSource[] source, Func selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
var result = new List(source.Length);
for (int i = 0; i < source.Length; i++)
{
var va = selector(source[i], i);
for (int j = 0; j < va.Length; j++)
{
result.Add(va[j]);
}
}
return result.ToArray();
}
// -------------------------- LISTS --------------------------------------------
///
/// Projects each element of a sequence to another sequence and flattens the resulting sequences into one sequence.
/// Yo dawg, I heard you like sequences.
///
/// A sequence of values to project.
/// A transform function to apply to each element.
/// A sequence whose elements are the result of invoking the one-to-many transform function on each element of the input sequence.
public static List SelectManyF(this List source, Func> selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
var result = new List(source.Count);
for (int i = 0; i < source.Count; i++)
{
var va = selector(source[i]);
for (int j = 0; j < va.Count; j++)
{
result.Add(va[j]);
}
}
return result;
}
///
/// Projects each element of a sequence to another sequence and flattens the resulting sequences into one sequence
/// utilizing the index of each element.
///
/// A sequence of values to project.
/// A transform function to apply to each element and it's index.
/// A sequence whose elements are the result of invoking the one-to-many transform function on each element and index of the input sequence.
public static List SelectManyF(this List source, Func> selector)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (selector == null)
{
throw Error.ArgumentNull("selector");
}
var result = new List(source.Count);
for (int i = 0; i < source.Count; i++)
{
var va = selector(source[i], i);
for (int j = 0; j < va.Count; j++)
{
result.Add(va[j]);
}
}
return result;
}
}
}