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
collection
ICollection<T>other
IEnumerable<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
s1
IEnumerable<T>s2
IEnumerable<T>
Returns
- bool
true
if the number of elements in the sequences are equal, and all the elements compared item by item are equal (using the CompareTo method),false
otherwise.
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
source
ICollection<TCollection>value
TElementgetSubElement
Func<TCollection, TElement>
Returns
Type Parameters
TCollection
TElement
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
source
ICollection<TCollection>value
TElementgetSubElement
Func<TCollection, TElement>comparer
IComparer<TElement>
Returns
Type Parameters
TCollection
TElement
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
source
ICollection<TCollection>value
TElementgetSubElement
Func<TCollection, TElement>index
intlength
int
Returns
Type Parameters
TCollection
TElement
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
source
ICollection<TCollection>value
TElementgetSubElement
Func<TCollection, TElement>index
intlength
intcomparer
IComparer<TElement>
Returns
Type Parameters
TCollection
TElement
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
source
IEnumerable<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
source
IEnumerable<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
source
IEnumerable<T>
Returns
- IEnumerable<TFilter>
Type Parameters
T
TFilter
IsEmpty<T>(ICollection<T>)
Returns whether this source is empty.
public static bool IsEmpty<T>(this ICollection<T> collection)
Parameters
collection
ICollection<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
source
IEnumerable<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
source
IEnumerable<T>score
Func<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
source
IEnumerable<TSource>selector
Func<TSource, TKey>
Returns
- TSource
Type Parameters
TSource
TKey
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
source
IEnumerable<TSource>selector
Func<TSource, TKey>comparer
IComparer<TKey>
Returns
- TSource
Type Parameters
TSource
TKey
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
source
IEnumerable<TSource>selector
Func<TSource, TKey>
Returns
- TSource
Type Parameters
TSource
TKey
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
source
IEnumerable<TSource>selector
Func<TSource, TKey>comparer
IComparer<TKey>
Returns
- TSource
Type Parameters
TSource
TKey
RandomItem<T>(IEnumerable<T>)
Returns a random element from a source.
public static T RandomItem<T>(this IEnumerable<T> source)
Parameters
source
IEnumerable<T>The list.
Returns
- T
An item randomly selected from the source.
Type Parameters
T
The 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
source
IEnumerable<T>The list.
random
IRandomThe random generator to use.
Returns
- T
An item randomly selected from the source.
Type Parameters
T
The 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
source
List<T>The list to remove elements from.
predicate
Predicate<T>The predicate used to filter elements. All elements that don't satisfy the predicate will be matched.
Type Parameters
T
The 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
source
IEnumerable<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
source
IEnumerable<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
source
IEnumerable<T>The source from which to sample.
sampleCount
intThe number of samples to return.
Returns
- IEnumerable<T>
Generates a ransom subset from a given source.
Type Parameters
T
The 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
source
IEnumerable<T>The source from which to sample.
sampleCount
intThe number of samples to return.
random
IRandomThe random generator to use.
Returns
- IEnumerable<T>
Generates a ransom subset from a given source.
Type Parameters
T
The type of elements of the source.
Shuffle<T>(IList<T>)
Shuffles a list.
public static void Shuffle<T>(this IList<T> list)
Parameters
list
IList<T>The list to shuffle.
Type Parameters
T
The 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
T
The 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
source
IEnumerable<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
source
IEnumerable<T>n
int
Returns
- IEnumerable<T>
Type Parameters
T