Aetheria/Assets/Scripts/ServerShared/NIH/LinqFaster/Single.cs
hyperion51 c24c155673 Context Menus & Progress Bars
We now have a context menu UI for in-game actions, right click on an item to activate
Database view has a progress bar (finally!)
Data structures for Megacorps and loadouts
Towing task complete? needs testing
2020-05-10 00:16:33 +01:00

275 lines
8.9 KiB
C#

using System;
using System.Collections.Generic;
namespace JM.LinqFaster
{
public static partial class LinqFaster
{
// --------------------------- Arrays ----------------------------
/// <summary>
/// Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.
/// </summary>
/// <param name="source">A sequence to return the single element of</param>
/// <returns>The single element of the input sequence or default if no elements exist.</returns>
public static T SingleF<T>(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];
}
/// <summary>
/// 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.
/// </summary>
/// <param name="source">A sequence to return the single element of</param>
/// <returns>The single element of the input sequence</returns>
public static T SingleOrDefaultF<T>(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];
}
/// <summary>
/// Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.
/// </summary>
/// <param name="source">A sequence to return a single element from.</param>
/// <param name="predicate">A function to test an element for a condition.</param>
/// <returns>The single element of the input sequence that satisfies a condition.</returns>
public static T SingleF<T>(this T[] source, Func<T, bool> 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();
}
}
/// <summary>
/// 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.
/// </summary>
/// <param name="source">A sequence to return a single element from.</param>
/// <param name="predicate">A function to test an element for a condition.</param>
/// <returns>The single element of the input sequence that satisfies a condition or default value if no such element is found.</returns>
public static T SingleOrDefaultF<T>(this T[] source, Func<T, bool> 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 ----------------------------
/// <summary>
/// Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.
/// </summary>
/// <param name="source">A sequence to return the single element of</param>
/// <returns>The single element of the input sequence</returns>
public static T SingleF<T>(this List<T> 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];
}
/// <summary>
/// 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.
/// </summary>
/// <param name="source">A sequence to return the single element of</param>
/// <returns>The single element of the input sequence or default if no elements exist.</returns>
public static T SingleOrDefaultF<T>(this List<T> 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];
}
/// <summary>
/// Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.
/// </summary>
/// <param name="source">A sequence to return a single element from.</param>
/// <param name="predicate">A function to test an element for a condition.</param>
/// <returns>The single element of the input sequence that satisfies a condition.</returns>
public static T SingleF<T>(this List<T> source, Func<T, bool> 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();
}
}
/// <summary>
/// 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.
/// </summary>
/// <param name="source">A sequence to return a single element from.</param>
/// <param name="predicate">A function to test an element for a condition.</param>
/// <returns>The single element of the input sequence that satisfies a condition or default value if no such element is found.</returns>
public static T SingleOrDefaultF<T>(this List<T> source, Func<T, bool> 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;
}
}
}