using System;
using System.Collections.Generic;
namespace JM.LinqFaster
{
public static partial class LinqFaster
{
// --------------------------- Arrays ----------------------------
///
/// Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.
///
/// A sequence to return the single element of
/// The single element of the input sequence or default if no elements exist.
public static T SingleF(this T[] source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Length == 0)
{
throw Error.NoElements();
}
if (source.Length > 1) {
throw Error.MoreThanOneElement();
}
return source[0];
}
///
/// Returns the only element of a sequence, or the default if no elements exist, and throws an exception if there is not exactly one element in the sequence.
///
/// A sequence to return the single element of
/// The single element of the input sequence
public static T SingleOrDefaultF(this T[] source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Length == 0)
{
return default(T);
}
if (source.Length > 1) {
throw Error.MoreThanOneElement();
}
return source[0];
}
///
/// Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.
///
/// A sequence to return a single element from.
/// A function to test an element for a condition.
/// The single element of the input sequence that satisfies a condition.
public static T SingleF(this T[] source, Func predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (predicate == null)
{
throw Error.ArgumentNull("predicate");
}
T result = default(T);
bool foundMatch = false;
for (int i = 0; i < source.Length; i++)
{
if (predicate(source[i]))
{
if (foundMatch)
{
throw Error.MoreThanOneMatch();
}
result = source[i];
foundMatch = true;
}
}
if (foundMatch)
{
return result;
}
else
{
throw Error.NoMatch();
}
}
///
/// Returns the only element of a sequence that satisfies a specified condition, or a default value if
/// no such element exists, and throws an exception if more than one such element exists.
///
/// A sequence to return a single element from.
/// A function to test an element for a condition.
/// The single element of the input sequence that satisfies a condition or default value if no such element is found.
public static T SingleOrDefaultF(this T[] source, Func predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (predicate == null)
{
throw Error.ArgumentNull("predicate");
}
T result = default(T);
bool foundMatch = false;
for (int i = 0; i < source.Length; i++)
{
if (predicate(source[i]))
{
if (foundMatch)
{
throw Error.MoreThanOneMatch();
}
result = source[i];
foundMatch = true;
}
}
return result;
}
// --------------------------- Lists ----------------------------
///
/// Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.
///
/// A sequence to return the single element of
/// The single element of the input sequence
public static T SingleF(this List source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Count == 0)
{
throw Error.NoElements();
}
if (source.Count > 1) {
throw Error.MoreThanOneElement();
}
return source[0];
}
///
/// Returns the only element of a sequence, or default if no elements exist, and throws an exception if there is not exactly one element in the sequence.
///
/// A sequence to return the single element of
/// The single element of the input sequence or default if no elements exist.
public static T SingleOrDefaultF(this List source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (source.Count == 0)
{
return default(T);
}
if (source.Count > 1) {
throw Error.MoreThanOneElement();
}
return source[0];
}
///
/// Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.
///
/// A sequence to return a single element from.
/// A function to test an element for a condition.
/// The single element of the input sequence that satisfies a condition.
public static T SingleF(this List source, Func predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (predicate == null)
{
throw Error.ArgumentNull("predicate");
}
T result = default(T);
bool foundMatch = false;
for (int i = 0; i < source.Count; i++)
{
if (predicate(source[i]))
{
if (foundMatch)
{
throw Error.MoreThanOneMatch();
}
result = source[i];
foundMatch = true;
}
}
if (foundMatch)
{
return result;
}
else
{
throw Error.NoMatch();
}
}
///
/// Returns the only element of a sequence that satisfies a specified condition, or a default value if
/// no such element exists, and throws an exception if more than one such element exists.
///
/// A sequence to return a single element from.
/// A function to test an element for a condition.
/// The single element of the input sequence that satisfies a condition or default value if no such element is found.
public static T SingleOrDefaultF(this List source, Func predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (predicate == null)
{
throw Error.ArgumentNull("predicate");
}
T result = default(T);
bool foundMatch = false;
for (int i = 0; i < source.Count; i++)
{
if (predicate(source[i]))
{
if (foundMatch)
{
throw Error.MoreThanOneMatch();
}
result = source[i];
foundMatch = true;
}
}
return result;
}
}
}