Enhanced C#
Language of your choice: library documentation

Documentation moved to ecsharp.net

GitHub doesn't support HTTP redirects, so you'll be redirected in 3 seconds.

 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
Static Public Member Functions | List of all members
Loyc.Collections.ListExt Class Reference

Extension methods and helper methods for List{T}, IList{T}, IReadOnlyList{T}, arrays, IListSource{T}, and for related mutable interfaces such as IArray{T}. More...


Source files:

Remarks

Extension methods and helper methods for List{T}, IList{T}, IReadOnlyList{T}, arrays, IListSource{T}, and for related mutable interfaces such as IArray{T}.

Extension methods that only apply to Loyc's new interfaces, or adapt a list to those interfaces, will go in LCExt instead.

The source code for adapter extension methods such as the Slice() method for arrays, which returns an ArraySlice{T} adapter, is now placed in the source file for each adapter class (e.g. ArraySlice.cs) to make it easier to create custom versions of Loyc.Essentials with parts removed.

Static Public Member Functions

static ArraySlice< T > Slice< T > (this T[] list, int start, int length=int.MaxValue)
 
static ReversedList< T > Reverse< T > (this IList< T > list)
 Returns an editable reversed view of a list. More...
 
static ReversedList< T > Reverse< T > (this IListAndListSource< T > list)
 Returns a reversed view of a list. More...
 
static IRange< T > AsRange< T > (this IListSource< T > list)
 
static IRange< T > AsRange< T > (this IRange< T > list)
 
static int BinarySearch< T > (this IList< T > list, T value)
 
static int BinarySearch< T > (this IList< T > list, T value, IComparer< T > pred)
 
static int BinarySearch< T > (this IList< T > list, T find, Comparison< T > compare)
 
static int BinarySearch2< T, K > (this IList< T > list, K find, Func< T, K, int > compare, bool lowerBound=true)
 
static int BinarySearch< T > (this IReadOnlyList< T > list, T value)
 
static int BinarySearch< T > (this IReadOnlyList< T > list, T value, IComparer< T > comparer)
 
static int BinarySearch< T > (this IReadOnlyList< T > list, T find, Comparison< T > compare)
 
static int BinarySearch2< T, K > (this IReadOnlyList< T > list, K find, Func< T, K, int > compare, bool lowerBound=true)
 
static int BinarySearch< T > (this IListAndListSource< T > list, T value)
 
static int BinarySearch< T > (this IListAndListSource< T > list, T value, IComparer< T > comparer)
 
static int BinarySearch< T > (this IListAndListSource< T > list, T value, Comparison< T > compare)
 
static int BinarySearch2< T, K > (this IListAndListSource< T > list, K value, Func< T, K, int > compare, bool lowerBound=true)
 
static void CopyTo< T > (this IReadOnlyCollection< T > c, T[] array, int arrayIndex)
 
static T TryGet< T > (this T[] list, int index, T defaultValue)
 
static Maybe< T > TryGet< T > (this T[] list, int index)
 
static T TryGet< T > (this List< T > list, int index, T defaultValue)
 
static Maybe< T > TryGet< T > (this List< T > list, int index)
 
static T TryGet< T > (this IList< T > list, int index, T defaultValue)
 
static Maybe< T > TryGet< T > (this IList< T > list, int index)
 
static T TryGet< T > (this IListAndListSource< T > list, int index, T defaultValue)
 
static Maybe< T > TryGet< T > (this IListAndListSource< T > list, int index)
 
static T TryGet< T > (this IReadOnlyList< T > list, int index, T defaultValue)
 
static Maybe< T > TryGet< T > (this IReadOnlyList< T > list, int index)
 
static void RemoveRange< T > (this IList< T > list, int index, int count)
 
static void Resize< T > (this List< T > list, int newSize)
 
static void Resize< T > (this IList< T > list, int newSize)
 
static void MaybeEnlarge< T > (this List< T > list, int minSize)
 
static void MaybeEnlarge< T > (this IList< T > list, int minSize)
 
static IEnumerable< Pair< A, B > > Zip< A, B > (this IEnumerable< A > a, IEnumerable< B > b)
 
static IEnumerable< Pair< A, B > > ZipLeft< A, B > (this IEnumerable< A > a, IEnumerable< B > b, B defaultB)
 
static IEnumerable< C > ZipLeft< A, B, C > (this IEnumerable< A > a, IEnumerable< B > b, B defaultB, Func< A, B, C > resultSelector)
 
static IEnumerable< Pair< A, B > > ZipLonger< A, B > (this IEnumerable< A > a, IEnumerable< B > b)
 
static IEnumerable< Pair< A, B > > ZipLonger< A, B > (this IEnumerable< A > a, IEnumerable< B > b, A defaultA, B defaultB)
 
static IEnumerable< C > ZipLonger< A, B, C > (this IEnumerable< A > a, IEnumerable< B > b, A defaultA, B defaultB, Func< A, B, C > resultSelector)
 
static int[] RangeArray (int count)
 Returns an array of Length count containing the numbers 0 through count-1. More...
 
static void Sort< T > (this IList< T > list)
 
static void Sort< T > (this IList< T > list, Comparison< T > comp)
 
static void Sort< T > (this IList< T > list, int index, int count, Comparison< T > comp)
 Performs a quicksort using a Comparison function. More...
 
static void StableSort< T > (this IList< T > list, Comparison< T > comp)
 Performs a stable sort, i.e. a sort that preserves the relative order of items that compare equal. More...
 
static void StableSort< T > (this IList< T > list)
 
static ListSlice< T > SortLowestK< T > (this IList< T > list, int k)
 Uses a partial quicksort, known as "quickselect", to find and sort the lowest k elements in a list. More...
 
static ListSlice< T > SortLowestK< T > (this IList< T > list, int k, Comparison< T > comp)
 
static ListSlice< T > SortLowestK< T > (this IList< T > list, int index, int count, int k, Comparison< T > comp)
 
static ListSlice< T > SortLowestKStable< T > (this IList< T > list, int k)
 A stable version of SortLowestK{T}(IList{T},int). This means that when k>1 and adjacent results at the beginning of list compare equal, they keep the same order that they had originally. More...
 
static ListSlice< T > SortLowestKStable< T > (this IList< T > list, int k, Comparison< T > comp)
 
static void InsertionSort< T > (this IList< T > array, int index, int count, Comparison< T > comp)
 Performs an insertion sort. More...
 
static bool SortPair< T > (this IList< T > list, int i, int j, Comparison< T > comp)
 Sorts two items to ensure that list[i] is less than list[j]. More...
 
static void Swap< T > (this IList< T > list, int i, int j)
 Swaps list[i] with list[j]. More...
 
static void Swap< T > (this IArray< T > list, int i, int j)
 
static int LastIndexWhere< T > (this IList< T > list, Func< T, bool > pred)
 Gets the highest index at which a condition is true, or -1 if nowhere. More...
 
static int LastIndexWhere< T > (this IListAndListSource< T > list, Func< T, bool > pred)
 Gets the highest index at which a condition is true, or -1 if nowhere. More...
 
static void Randomize< T > (this IList< T > list)
 
static void Randomize< T > (this T[] list)
 
static T[] Randomized< T > (this IList< T > list)
 Quickly makes a copy of a list, as an array, in random order. More...
 
static T[] Randomized< T > (this IListSource< T > list)
 Quickly makes a copy of a list, as an array, in random order. More...
 
static T[] Randomized< T > (this IListAndListSource< T > list)
 Quickly makes a copy of a list, as an array, in random order. More...
 
static R[] SelectArray< T, R > (this T[] input, Func< T, R > selector)
 Maps an array to another array of the same length. More...
 
static R[] SelectArray< T, R > (this ICollection< T > input, Func< T, R > selector)
 Maps a list to an array of the same length. More...
 
static R[] SelectArray< T, R > (this IReadOnlyList< T > input, Func< T, R > selector)
 Maps a list to an array of the same length. More...
 
static R[] SelectArray< T, R > (this IListAndListSource< T > input, Func< T, R > selector)
 Maps a list to an array of the same length. More...
 
static int RemoveAll< T > (this IList< T > list, Predicate< T > match)
 Removes the all the elements that match the conditions defined by the specified predicate. More...
 
static void ReverseInPlace< T > (this IList< T > list)
 
static void ReverseInPlace< T > (this IArray< T > list)
 
static void AddRange< T > (this IList< T > list, IEnumerable< T > range)
 
static int InsertRange< T > (this IList< T > list, int index, IReadOnlyCollection< T > source)
 
static int InsertRange< T > (this IList< T > list, int index, ICollection< T > source)
 
static int InsertRange< T > (this IList< T > list, int index, IListAndListSource< T > source)
 
static int InsertRange< T > (this IList< T > list, int index, ICollectionAndReadOnly< T > source)
 
static int InsertRange< T > (this IList< T > list, int index, int count, IEnumerable< T > source)
 
static void InsertRangeHelper< T > (IList< T > list, int index, int spaceNeeded)
 Increases the list size by spaceNeeded and copies elements starting at list[index] "rightward" to make room for inserted elements that will be initialized by the caller. More...
 
static bool SequenceEqual< TSource > (this IList< TSource > first, IList< TSource > second)
 
static bool SequenceEqual< TSource > (this IReadOnlyCollection< TSource > first, IReadOnlyCollection< TSource > second)
 
static bool SequenceEqual< TSource > (this IListAndListSource< TSource > first, IListAndListSource< TSource > second)
 
static Repeated< T > Repeat< T > (T value, int count)
 Returns a helper object that stores one value, but acts like a read-only list that repeats the value the specified number of times. More...
 
static Repeated< T > Single< T > (T value)
 Returns a helper object that stores one value, but acts like a read-only list of one item. More...
 

Member Function Documentation

static void Loyc.Collections.ListExt.InsertionSort< T > ( this IList< T >  array,
int  index,
int  count,
Comparison< T >  comp 
)
inlinestatic

Performs an insertion sort.

The insertion sort is a stable sort algorithm that is slow in general (O(N^2)). It should be used only when (a) the list to be sorted is short (less than 10-20 elements) or (b) the list is very nearly sorted already.

See also
InternalList.InsertionSort
static void Loyc.Collections.ListExt.InsertRangeHelper< T > ( IList< T >  list,
int  index,
int  spaceNeeded 
)
inlinestatic

Increases the list size by spaceNeeded and copies elements starting at list[index] "rightward" to make room for inserted elements that will be initialized by the caller.

static int Loyc.Collections.ListExt.LastIndexWhere< T > ( this IList< T >  list,
Func< T, bool >  pred 
)
inlinestatic

Gets the highest index at which a condition is true, or -1 if nowhere.

static int Loyc.Collections.ListExt.LastIndexWhere< T > ( this IListAndListSource< T >  list,
Func< T, bool >  pred 
)
inlinestatic

Gets the highest index at which a condition is true, or -1 if nowhere.

static T [] Loyc.Collections.ListExt.Randomized< T > ( this IList< T >  list)
inlinestatic

Quickly makes a copy of a list, as an array, in random order.

static T [] Loyc.Collections.ListExt.Randomized< T > ( this IListSource< T >  list)
inlinestatic

Quickly makes a copy of a list, as an array, in random order.

static T [] Loyc.Collections.ListExt.Randomized< T > ( this IListAndListSource< T >  list)
inlinestatic

Quickly makes a copy of a list, as an array, in random order.

static int [] Loyc.Collections.ListExt.RangeArray ( int  count)
inlinestatic

Returns an array of Length count containing the numbers 0 through count-1.

static int Loyc.Collections.ListExt.RemoveAll< T > ( this IList< T >  list,
Predicate< T >  match 
)
inlinestatic

Removes the all the elements that match the conditions defined by the specified predicate.

Returns
The number of elements removed from the list
static Repeated<T> Loyc.Collections.ListExt.Repeat< T > ( value,
int  count 
)
inlinestatic

Returns a helper object that stores one value, but acts like a read-only list that repeats the value the specified number of times.

Returns
new Repeated<T>(value, count)
static ReversedList<T> Loyc.Collections.ListExt.Reverse< T > ( this IList< T >  list)
inlinestatic

Returns an editable reversed view of a list.

This was originally named ReverseView. Changed to Reverse to match Linq's Reverse(IEnumerable).

static ReversedList<T> Loyc.Collections.ListExt.Reverse< T > ( this IListAndListSource< T >  list)
inlinestatic

Returns a reversed view of a list.

This was originally named ReverseView. Changed to Reverse to match Linq's Reverse(IEnumerable).

static R [] Loyc.Collections.ListExt.SelectArray< T, R > ( this T[]  input,
Func< T, R >  selector 
)
inlinestatic

Maps an array to another array of the same length.

static R [] Loyc.Collections.ListExt.SelectArray< T, R > ( this ICollection< T >  input,
Func< T, R >  selector 
)
inlinestatic

Maps a list to an array of the same length.

static R [] Loyc.Collections.ListExt.SelectArray< T, R > ( this IReadOnlyList< T >  input,
Func< T, R >  selector 
)
inlinestatic

Maps a list to an array of the same length.

static R [] Loyc.Collections.ListExt.SelectArray< T, R > ( this IListAndListSource< T >  input,
Func< T, R >  selector 
)
inlinestatic

Maps a list to an array of the same length.

static Repeated<T> Loyc.Collections.ListExt.Single< T > ( value)
inlinestatic

Returns a helper object that stores one value, but acts like a read-only list of one item.

Returns
new Repeated<T>(value, 1)
static void Loyc.Collections.ListExt.Sort< T > ( this IList< T >  list,
int  index,
int  count,
Comparison< T >  comp 
)
inlinestatic

Performs a quicksort using a Comparison function.

Parameters
indexIndex at which to begin sorting a portion of the list.
countNumber of items to sort starting at 'index'.

This method exists because the .NET framework offers no method to sort IList{T}–you can sort arrays and List{T}, but not IList.

This quicksort algorithm uses a best-of-three pivot so that it remains performant (fast) if the input is already sorted. It is designed to perform reasonably well in case the data contains many duplicates (not verified). It is also designed to avoid using excessive stack space if a worst-case input occurs that requires O(N^2) time.

static ListSlice<T> Loyc.Collections.ListExt.SortLowestK< T > ( this IList< T >  list,
int  k 
)
inlinestatic

Uses a partial quicksort, known as "quickselect", to find and sort the lowest k elements in a list.

Template Parameters
T
Parameters
listA list that will be partially sorted.
kNumber of elements that will be sorted at the beginning of the list when this method returns. If k > list.Count, the entire list is sorted.
Returns
Although the list is modified in-place, a slice of the beginning of the same list is returned. The slice will have k elements (or list.Count elements, whichever is less).

Whereas quicksort typically runs in O(N log N) time, quickselect typically requires O(N) time for small values of k, although the worst-case performance remains O(N^2).

static ListSlice<T> Loyc.Collections.ListExt.SortLowestKStable< T > ( this IList< T >  list,
int  k 
)
inlinestatic

A stable version of SortLowestK{T}(IList{T},int). This means that when k>1 and adjacent results at the beginning of list compare equal, they keep the same order that they had originally.

Template Parameters
T
Parameters
listA list that will be partially sorted.
kNumber of elements that will be sorted at the beginning of the list when this method returns. If k > list.Count, the entire list is sorted.
Returns
This method uses the quickselect algorithm and stability is achieved using a temporary array of list.Count integers.
static bool Loyc.Collections.ListExt.SortPair< T > ( this IList< T >  list,
int  i,
int  j,
Comparison< T >  comp 
)
inlinestatic

Sorts two items to ensure that list[i] is less than list[j].

Returns
True if the array elements were swapped, false if not.
static void Loyc.Collections.ListExt.StableSort< T > ( this IList< T >  list,
Comparison< T >  comp 
)
inlinestatic

Performs a stable sort, i.e. a sort that preserves the relative order of items that compare equal.

This algorithm uses a quicksort and therefore runs in O(N log N) time, but it requires O(N) temporary space (specifically, an array of N integers) and is slower than a standard quicksort, so you should use it only if you need a stable sort.

static void Loyc.Collections.ListExt.Swap< T > ( this IList< T >  list,
int  i,
int  j 
)
inlinestatic

Swaps list[i] with list[j].