Class CollectionExtensions
- Namespace
- Gamelogic.Extensions.Algorithms
- Assembly
- Assembly-CSharp.dll
This class provides useful extension methods for collections, mostly IEnumerable.
[Version(1, 0, 0)]
public static class CollectionExtensions
- Inheritance
-
CollectionExtensions
- Inherited Members
Methods
AddRange<T>(ICollection<T>, IEnumerable<T>)
Add all elements of other to the given source.
public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> other)
Parameters
collectionICollection<T>otherIEnumerable<T>
Type Parameters
T
AreSequencesEqual<T>(IEnumerable<T>, IEnumerable<T>)
Checks whether the sequences are equal.
[Version(1, 3, 0)]
public static bool AreSequencesEqual<T>(IEnumerable<T> s1, IEnumerable<T> s2) where T : IComparable
Parameters
s1IEnumerable<T>s2IEnumerable<T>
Returns
- bool
trueif the number of elements in the sequences are equal, and all the elements compared item by item are equal (using the CompareTo method),falseotherwise.
Type Parameters
T
BinarySearch<TCollection, TElement>(ICollection<TCollection>, TElement, Func<TCollection, TElement>)
Find an element in a collection by binary searching. This requires the collection to be sorted on the values returned by getSubElement This will compare some derived property of the elements in the collection, rather than the elements themselves.
public static int BinarySearch<TCollection, TElement>(this ICollection<TCollection> source, TElement value, Func<TCollection, TElement> getSubElement)
Parameters
sourceICollection<TCollection>valueTElementgetSubElementFunc<TCollection, TElement>
Returns
Type Parameters
TCollectionTElement
BinarySearch<TCollection, TElement>(ICollection<TCollection>, TElement, Func<TCollection, TElement>, IComparer<TElement>)
Find an element in a collection by binary searching. This requires the collection to be sorted on the values returned by getSubElement This will compare some derived property of the elements in the collection, rather than the elements themselves.
public static int BinarySearch<TCollection, TElement>(this ICollection<TCollection> source, TElement value, Func<TCollection, TElement> getSubElement, IComparer<TElement> comparer)
Parameters
sourceICollection<TCollection>valueTElementgetSubElementFunc<TCollection, TElement>comparerIComparer<TElement>
Returns
Type Parameters
TCollectionTElement
BinarySearch<TCollection, TElement>(ICollection<TCollection>, TElement, Func<TCollection, TElement>, int, int)
Find an element in a collection by binary searching. This requires the collection to be sorted on the values returned by getSubElement This will compare some derived property of the elements in the collection, rather than the elements themselves.
public static int BinarySearch<TCollection, TElement>(this ICollection<TCollection> source, TElement value, Func<TCollection, TElement> getSubElement, int index, int length)
Parameters
sourceICollection<TCollection>valueTElementgetSubElementFunc<TCollection, TElement>indexintlengthint
Returns
Type Parameters
TCollectionTElement
BinarySearch<TCollection, TElement>(ICollection<TCollection>, TElement, Func<TCollection, TElement>, int, int, IComparer<TElement>)
Find an element in a collection by binary searching. This requires the collection to be sorted on the values returned by getSubElement This will compare some derived property of the elements in the collection, rather than the elements themselves.
public static int BinarySearch<TCollection, TElement>(this ICollection<TCollection> source, TElement value, Func<TCollection, TElement> getSubElement, int index, int length, IComparer<TElement> comparer)
Parameters
sourceICollection<TCollection>valueTElementgetSubElementFunc<TCollection, TElement>indexintlengthintcomparerIComparer<TElement>
Returns
Type Parameters
TCollectionTElement
ButFirst<T>(IEnumerable<T>)
Returns an enumerable of all elements of the given list but the first, keeping them in order.
public static IEnumerable<T> ButFirst<T>(this IEnumerable<T> source)
Parameters
sourceIEnumerable<T>
Returns
- IEnumerable<T>
Type Parameters
T
ButLast<T>(IEnumerable<T>)
Returns an enumerable of all elements in the given list but the last, keeping them in order.
public static IEnumerable<T> ButLast<T>(this IEnumerable<T> source)
Parameters
sourceIEnumerable<T>
Returns
- IEnumerable<T>
Type Parameters
T
FilterByType<T, TFilter>(IEnumerable<T>)
Returns all elements of the source which are of FilterType.
public static IEnumerable<TFilter> FilterByType<T, TFilter>(this IEnumerable<T> source) where T : class where TFilter : class, T
Parameters
sourceIEnumerable<T>
Returns
- IEnumerable<TFilter>
Type Parameters
TTFilter
IsEmpty<T>(ICollection<T>)
Returns whether this source is empty.
public static bool IsEmpty<T>(this ICollection<T> collection)
Parameters
collectionICollection<T>
Returns
Type Parameters
T
ListToString<T>(IEnumerable<T>)
Returns a pretty string representation of the given list. The resulting string looks something like
[a, b, c].
public static string ListToString<T>(this IEnumerable<T> source)
Parameters
sourceIEnumerable<T>
Returns
Type Parameters
T
MaxBy<T>(IEnumerable<T>, Func<T, IComparable>)
Finds the maximum element in the source as scored by the given function.
public static T MaxBy<T>(this IEnumerable<T> source, Func<T, IComparable> score)
Parameters
sourceIEnumerable<T>scoreFunc<T, IComparable>
Returns
- T
Type Parameters
T
MaxBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)
Finds the minimum element in the source as scored by its projection.
public static TSource MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector)
Parameters
sourceIEnumerable<TSource>selectorFunc<TSource, TKey>
Returns
- TSource
Type Parameters
TSourceTKey
MaxBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IComparer<TKey>)
Finds the minimum element in the source as scored by the given function applied to a projection on the elements.
public static TSource MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector, IComparer<TKey> comparer)
Parameters
sourceIEnumerable<TSource>selectorFunc<TSource, TKey>comparerIComparer<TKey>
Returns
- TSource
Type Parameters
TSourceTKey
MinBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)
Finds the minimum element in the source as scored by its projection.
public static TSource MinBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector)
Parameters
sourceIEnumerable<TSource>selectorFunc<TSource, TKey>
Returns
- TSource
Type Parameters
TSourceTKey
MinBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IComparer<TKey>)
Finds the minimum element in the source as scored by the given function applied to a projection on the elements.
public static TSource MinBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector, IComparer<TKey> comparer)
Parameters
sourceIEnumerable<TSource>selectorFunc<TSource, TKey>comparerIComparer<TKey>
Returns
- TSource
Type Parameters
TSourceTKey
RandomItem<T>(IEnumerable<T>)
Returns a random element from a source.
public static T RandomItem<T>(this IEnumerable<T> source)
Parameters
sourceIEnumerable<T>The list.
Returns
- T
An item randomly selected from the source.
Type Parameters
TThe type of items generated from the source.
RandomItem<T>(IEnumerable<T>, IRandom)
Returns a random element from a source.
public static T RandomItem<T>(this IEnumerable<T> source, IRandom random)
Parameters
sourceIEnumerable<T>The list.
randomIRandomThe random generator to use.
Returns
- T
An item randomly selected from the source.
Type Parameters
TThe type of items generated from the source.
RemoveAllBut<T>(List<T>, Predicate<T>)
Removes all the elements in the list that does not satisfy the predicate.
public static void RemoveAllBut<T>(this List<T> source, Predicate<T> predicate)
Parameters
sourceList<T>The list to remove elements from.
predicatePredicate<T>The predicate used to filter elements. All elements that don't satisfy the predicate will be matched.
Type Parameters
TThe type of elements in the list.
RotateLeft<T>(IEnumerable<T>)
Returns an enumerable with elements in order, but the first element is moved to the end.
public static IEnumerable<T> RotateLeft<T>(this IEnumerable<T> source)
Parameters
sourceIEnumerable<T>
Returns
- IEnumerable<T>
Type Parameters
T
RotateRight<T>(IEnumerable<T>)
Returns an enumerable with elements in order, but the last element is moved to the front.
public static IEnumerable<T> RotateRight<T>(this IEnumerable<T> source)
Parameters
sourceIEnumerable<T>
Returns
- IEnumerable<T>
Type Parameters
T
SampleRandom<T>(IEnumerable<T>, int)
Returns a random sample from a source.
public static IEnumerable<T> SampleRandom<T>(this IEnumerable<T> source, int sampleCount)
Parameters
sourceIEnumerable<T>The source from which to sample.
sampleCountintThe number of samples to return.
Returns
- IEnumerable<T>
Generates a ransom subset from a given source.
Type Parameters
TThe type of elements of the source.
SampleRandom<T>(IEnumerable<T>, int, IRandom)
Returns a random sample from a source.
public static IEnumerable<T> SampleRandom<T>(this IEnumerable<T> source, int sampleCount, IRandom random)
Parameters
sourceIEnumerable<T>The source from which to sample.
sampleCountintThe number of samples to return.
randomIRandomThe random generator to use.
Returns
- IEnumerable<T>
Generates a ransom subset from a given source.
Type Parameters
TThe type of elements of the source.
Shuffle<T>(IList<T>)
Shuffles a list.
public static void Shuffle<T>(this IList<T> list)
Parameters
listIList<T>The list to shuffle.
Type Parameters
TThe type of items in the list.
Shuffle<T>(IList<T>, IRandom)
Shuffles a list.
public static void Shuffle<T>(this IList<T> list, IRandom random)
Parameters
Type Parameters
TThe type of items in the list.
TakeHalf<T>(IEnumerable<T>)
Returns the first half of elements from a source.
[Version(1, 2, 0)]
public static IEnumerable<T> TakeHalf<T>(this IEnumerable<T> source)
Parameters
sourceIEnumerable<T>
Returns
- IEnumerable<T>
Type Parameters
T
TakeLast<T>(IEnumerable<T>, int)
Returns the last n elements from a source.
[Version(1, 2, 0)]
public static IEnumerable<T> TakeLast<T>(this IEnumerable<T> source, int n)
Parameters
sourceIEnumerable<T>nint
Returns
- IEnumerable<T>
Type Parameters
T