Table of Contents

Class CollectionExtensions

Namespace
Gamelogic.Extensions.Algorithms
Assembly
Gamelogic.Extensions.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.

Aggregate<T>(IEnumerable<T>, Func<T, T, T>, Func<T, T, T>)

Aggregates the source collection using two different aggregation functions.

AreSequencesEqual<T>(IEnumerable<T>, IEnumerable<T>)

Checks whether the sequences are equal.

AsCountable<T>(IEnumerable<T>)

Converts the source to an IReadOnlyCollection<T>.

AsList<T>(IEnumerable<T>)

Converts the source to an IReadOnlyList<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.

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.

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.

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.

ButFirst<T>(IEnumerable<T>)

Returns an enumerable of all elements of the given list but the first, keeping them in order.

ButLast<T>(IEnumerable<T>)

Returns an enumerable of all elements in the given list but the last, keeping them in order.

FillWithDefault<T>(IList<T>)

Fills the list with the default value of the type.

Fill<T>(IList<T>, T)

Fills the list with the given value.

FilterByType<T, TFilter>(IEnumerable<T>)

Returns all elements of the source which are of FilterType.

HasSameElementsAs<T>(IEnumerable<T>, IEnumerable<T>)

Checks if two lists have the same elements, regardless of their order.

HasSingle<T>(IEnumerable<T>)

Determines whether the source collection contains exactly one element.

IsEmpty<T>(ICollection<T>)

Returns whether this source is empty.

ListToString<T>(IEnumerable<T>)

Returns a pretty string representation of the given list. The resulting string looks something like [a, b, c].

MaxBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)

Finds the minimum element in the source as scored by its projection.

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.

MaxItemsBy<T, TComparable>(IEnumerable<T>, Func<T, TComparable>)

Returns the maximum elements in the source collection based on a selector function. This overload uses the default comparer for the type TComparable.

MaxItemsBy<T, TComparable>(IEnumerable<T>, Func<T, TComparable>, Comparer<TComparable>)

Returns the maximum elements in the source collection based on a selector function and a specified comparer.

MinBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)

Finds the minimum element in the source as scored by its projection.

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.

MinItemsBy<T, TComparable>(IEnumerable<T>, Func<T, TComparable>)

Returns the minimum elements in the source collection based on a selector function. This overload uses the default comparer for the type TComparable.

MinItemsBy<T, TComparable>(IEnumerable<T>, Func<T, TComparable>, Comparer<TComparable>)

Returns the minimum elements in the source collection based on a selector function and a specified comparer.

MinMax(IEnumerable<int>)

Finds the minimum and maximum values in a collection of integers.

MinMax(IEnumerable<float>)

Finds the minimum and maximum values in a collection of floats.

RandomItem<T>(IEnumerable<T>)

Returns a random element from a source.

RandomItem<T>(IEnumerable<T>, IRandom)

Returns a random element from a source.

RemoveAllBut<T>(List<T>, Predicate<T>)

Removes all the elements in the list that does not satisfy the predicate.

RotateLeft<T>(IEnumerable<T>)

Returns an enumerable with elements in order, but the first element is moved to the end.

RotateRight<T>(IEnumerable<T>)

Returns an enumerable with elements in order, but the last element is moved to the front.

SampleRandom<T>(IEnumerable<T>, int)

Returns a random sample from a source.

SampleRandom<T>(IEnumerable<T>, int, IRandom)

Returns a random sample from a source.

Shuffle<T>(IList<T>)

Shuffles a list.

Shuffle<T>(IList<T>, IRandom)

Shuffles a list.

SlidingWindow<T>(IEnumerable<T>, int)

Gives sliding window of the given size over the source.

TakeHalf<T>(IEnumerable<T>)

Returns the first half of elements from a source.

TakeLast<T>(IEnumerable<T>, int)

Returns the last n elements from a source.

TryFirst<T>(IEnumerable<T>, Func<T, bool>, out T)

Try to get the first element of a sequence that satisfies a predicate, if it exists.

TryFirst<T>(IEnumerable<T>, out T)

Try to get the first element of a sequence, if it exists.

TryLast<T>(IEnumerable<T>, Func<T, bool>, out T)

Try to get the last element of a sequence that satisfies a predicate, if it exists.

TryLast<T>(IEnumerable<T>, out T)

Try to get the last element of a sequence, if it exists.

TrySingle<T>(IEnumerable<T>, out T)

Tries to retrieve the single element from the source collection.

WithIndices<T>(IEnumerable<T>)

Iterates over a collection, yielding each element along with its index.