using System;
using System.Collections.Generic;
namespace JM.LinqFaster
{
public static partial class LinqFaster
{
// -------------------------- Arrays --------------------------------------------
///
/// Returns the last element of a sequence.
///
/// An sequence to return the last element of.
/// The value at the last position in the source sequence.
public static T LastF(this T[] source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Length == 0)
{
throw Error.NoElements();
}
return source[source.Length - 1];
}
///
/// Returns the last element of a sequence that satisfies a specified condition.
///
/// A sequence to return an element from.
/// A function to test each element for a condition.
///
public static T LastF(this T[] source, Predicate predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (predicate == null)
{
throw Error.ArgumentNull("predicate");
}
var lastIndex = Array.FindLastIndex(source, predicate);
if (lastIndex == -1)
throw Error.NoMatch();
else
return source[lastIndex];
}
///
/// Returns the last element of a sequence, or a default value if the sequence contains no elements.
///
/// An sequence to return the last element of.
/// default value if the source sequence is empty; otherwise,
/// the last element in the sequence
public static T LastOrDefaultF(this T[] source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Length == 0)
{
return default(T);
}
return source[source.Length - 1];
}
///
/// Returns the last element of a sequence that satisfies a condition or a default value if no such element is found.
///
/// A sequence to return the last element of.
/// A function to test each element for a condition.
/// default value if the sequence is empty or if no elements pass the test
/// in the predicate function; otherwise, the last element that passes the test in the
/// predicate function.
public static T LastOrDefaultF(this T[] source, Predicate predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (predicate == null)
{
throw Error.ArgumentNull("predicate");
}
var lastIndex = Array.FindLastIndex(source, predicate);
if (lastIndex == -1)
return default(T);
else
return source[lastIndex];
}
// -------------------------- Lists --------------------------------------------
///
/// Returns the last element of a sequence.
///
/// An sequence to return the last element of.
/// The value at the last position in the source sequence.
public static T LastF(this List source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Count == 0)
{
throw Error.NoElements();
}
return source[source.Count - 1];
}
///
/// Returns the last element of a sequence that satisfies a specified condition.
///
/// A sequence to return an element from.
/// A function to test each element for a condition.
///
public static T LastF(this List source, Predicate predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (predicate == null)
{
throw Error.ArgumentNull("predicate");
}
var lastIndex = source.FindLastIndex(predicate);
if (lastIndex == -1)
throw Error.NoMatch();
else
return source[lastIndex];
}
///
/// Returns the last element of a sequence, or a default value if the sequence contains no elements.
///
/// An sequence to return the last element of.
/// default value if the source sequence is empty; otherwise,
/// the last element in the sequence
public static T LastOrDefaultF(this List source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Count == 0)
{
return default(T);
}
return source[source.Count - 1];
}
///
/// Returns the last element of a sequence that satisfies a condition or a default value if no such element is found.
///
/// A sequence to return the last element of.
/// A function to test each element for a condition.
/// default value if the sequence is empty or if no elements pass the test
/// in the predicate function; otherwise, the last element that passes the test in the
/// predicate function.
public static T LastOrDefaultF(this List source, Predicate predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (predicate == null)
{
throw Error.ArgumentNull("predicate");
}
var lastIndex = source.FindLastIndex(predicate);
if (lastIndex == -1)
return default(T);
else
return source[lastIndex];
}
}
}