Aetheria/Assets/Scripts/ServerShared/NIH/LinqFaster/Where.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

127 lines
4.3 KiB
C#

using System;
using System.Collections.Generic;
namespace JM.LinqFaster
{
public static partial class LinqFaster
{
// -------------------------- ARRAYS --------------------------------------------
/// <summary>
/// Filters a sequence of values based on a predicate.
/// </summary>
/// <param name="source">A sequence to filter.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <returns>A sequence that contains elements from the input sequence that satisfy the condition.</returns>
public static T[] WhereF<T>(this T[] source, Func<T, bool> predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (predicate == null)
{
throw Error.ArgumentNull("predicate");
}
T[] result = new T[source.Length];
int idx = 0;
for (int i = 0; i < source.Length; i++)
{
if (predicate(source[i]))
{
result[idx] = source[i];
idx++;
}
}
Array.Resize(ref result, idx);
return result;
}
/// <summary>
/// Filters a sequence of values based on a predicate that includes the index in it's logic.
/// </summary>
/// <param name="source">A sequence to filter.</param>
/// <param name="predicate">A function to test each element for a condition along with the element's index.</param>
/// <returns>A sequence that contains elements from the input sequence that satisfy the condition.</returns>
public static T[] WhereF<T>(this T[] source, Func<T, int, bool> predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (predicate == null)
{
throw Error.ArgumentNull("predicate");
}
T[] result = new T[source.Length];
int idx = 0;
for (int i = 0; i < source.Length; i++)
{
if (predicate(source[i], i))
{
result[idx] = source[i];
idx++;
}
}
Array.Resize(ref result, idx);
return result;
}
// -------------------------- LISTS --------------------------------------------
/// <summary>
/// Filters a sequence of values based on a predicate.
/// </summary>
/// <param name="source">A sequence to filter.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <returns>A sequence that contains elements from the input sequence that satisfy the condition.</returns>
public static List<T> WhereF<T>(this List<T> source, Predicate<T> predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (predicate == null)
{
throw Error.ArgumentNull("predicate");
}
return source.FindAll(predicate);
}
/// <summary>
/// Filters a sequence of values based on a predicate that includes the index in it's logic.
/// </summary>
/// <param name="source">A sequence to filter.</param>
/// <param name="predicate">A function to test each element for a condition along with the element's index.</param>
/// <returns>A sequence that contains elements from the input sequence that satisfy the condition.</returns>
public static List<T> WhereF<T>(this List<T> source, Func<T, int, bool> predicate)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
if (predicate == null)
{
throw Error.ArgumentNull("predicate");
}
List<T> r = new List<T>();
for (int i = 0; i < source.Count; i++)
{
if (predicate(source[i], i)) r.Add(source[i]);
}
return r;
}
}
}