using System;
using System.Collections.Generic;
namespace JM.LinqFaster
{
public static partial class LinqFaster
{
// -------------------------- ARRAYS --------------------------------------------
///
/// Combines Select and Where into a single call for optimal
/// performance.
///
/// The input sequence to filter and select
/// The transformation to apply before filtering.
/// The predicate with which to filter result.
/// A seqence transformed and then filtered by selector and predicate.
public static TResult[] SelectWhereF(this T[] source, Func selector, Func predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (predicate == null)
{
throw Error.ArgumentNull("predicate");
}
var result = new TResult[source.Length];
int idx = 0;
for (int i = 0; i < source.Length; i++)
{
var s = selector(source[i]);
if (predicate(s))
{
result[idx] = s;
idx++;
}
}
Array.Resize(ref result, idx);
return result;
}
///
/// Combines Select and Where with indexes into a single call for optimal
/// performance.
///
/// The input sequence to filter and select
/// The transformation with index to apply before filtering.
/// The predicate with index with which to filter result.
/// A seqence transformed and then filtered by selector and predicate with indexes.
public static TResult[] SelectWhereF(this T[] source, Func selector, Func predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (predicate == null)
{
throw Error.ArgumentNull("predicate");
}
var result = new TResult[source.Length];
int idx = 0;
for (int i = 0; i < source.Length; i++)
{
var s = selector(source[i], i);
if (predicate(s, i))
{
result[idx] = s;
idx++;
}
}
Array.Resize(ref result, idx);
return result;
}
// -------------------------- LISTS --------------------------------------------
///
/// Combines Select and Where into a single call for optimal
/// performance.
///
/// The input sequence to filter and select
/// The transformation to apply before filtering.
/// The predicate with which to filter result.
/// A seqence transformed and then filtered by selector and predicate.
public static List SelectWhereF(this List source, Func selector, Func predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (predicate == null)
{
throw Error.ArgumentNull("predicate");
}
var r = new List();
for (int i = 0; i < source.Count; i++)
{
var s = selector(source[i]);
if (predicate(s)) r.Add(s);
}
return r;
}
///
/// Combines Select and Where with indexes into a single call for optimal
/// performance.
///
/// The input sequence to filter and select
/// The transformation with index to apply before filtering.
/// The predicate with index with which to filter result.
/// A seqence transformed and then filtered by selector and predicate with indexes.
public static List SelectWhereF(this List source, Func selector, Func predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (predicate == null)
{
throw Error.ArgumentNull("predicate");
}
var r = new List();
for (int i = 0; i < source.Count; i++)
{
var s = selector(source[i], i);
if (predicate(s, i)) r.Add(s);
}
return r;
}
}
}