Enhanced C#
Language of your choice: library documentation
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 ArraySlice< T > Slice< T > (this T[] list, NumRange< int, MathI > range)
 
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 IRange< T > Slice< T > (this IListSource< T > list, NumRange< int, MathI > range)
 
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)
 Removes count items from list starting at the specified index. More...
 
static void Resize< T > (this List< T > list, int newSize)
 Resizes a list by removing items from the list (if it is too long) or adding default(T) values to the end (if it is too short). More...
 
static void Resize< T > (this IList< T > list, int newSize)
 Resizes a list by removing items from the list (if it is too long) or adding default(T) values to the end (if it is too short). More...
 
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)
 Returns a sequence of length Min(a.Count(), b.Count()) of items from a and b paired together. The output is produced lazily, so infinite input sequences are supported. More...
 
static IEnumerable< Pair< A, B > > ZipLeft< A, B > (this IEnumerable< A > a, IEnumerable< B > b, B defaultB)
 Returns a sequence that has the same length as the first sequence, with items from the first and second sequence paired together. More...
 
static IEnumerable< C > ZipLeft< A, B, C > (this IEnumerable< A > a, IEnumerable< B > b, B defaultB, Func< A, B, C > resultSelector)
 An alternate version of ZipLeft<A, B>(IEnumerable<A>, IEnumerable<B>, B) in which a user-defined function is used to combine the items from the two lists. More...
 
static IEnumerable< Pair< A, B > > ZipLonger< A, B > (this IEnumerable< A > a, IEnumerable< B > b)
 Returns a sequence as long as the longer of two sequences. Items from the first and second sequence are initially paired together, and when one sequence ends, the other sequence's remaining values are paired with a default value. More...
 
static IEnumerable< Pair< A, B > > ZipLonger< A, B > (this IEnumerable< A > a, IEnumerable< B > b, A defaultA, B defaultB)
 Returns a sequence as long as the longer of two sequences. Items from the first and second sequence are initially paired together, and when one sequence ends, the other sequence's remaining values are paired with a default value provided as a parameter. More...
 
static IEnumerable< C > ZipLonger< A, B, C > (this IEnumerable< A > a, IEnumerable< B > b, A defaultA, B defaultB, Func< A, B, C > resultSelector)
 An alternate version of ZipLonger<A, B>(IEnumerable<A>, IEnumerable<B>, A, B) in which a user-defined function is used to combine the items from the two lists. More...
 
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 void Loyc.Collections.ListExt.RemoveRange< T > ( this IList< T >  list,
int  index,
int  count 
)
inlinestatic

Removes count items from list starting at the specified index.

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 void Loyc.Collections.ListExt.Resize< T > ( this List< T >  list,
int  newSize 
)
inlinestatic

Resizes a list by removing items from the list (if it is too long) or adding default(T) values to the end (if it is too short).

static void Loyc.Collections.ListExt.Resize< T > ( this IList< T >  list,
int  newSize 
)
inlinestatic

Resizes a list by removing items from the list (if it is too long) or adding default(T) values to the end (if it is too short).

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.

On larger inputs, 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 or if the input is usually small (in which case insertion sort is used).

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

Swaps list[i] with list[j].

static IEnumerable<Pair<A, B> > Loyc.Collections.ListExt.Zip< A, B > ( this IEnumerable< A >  a,
IEnumerable< B >  b 
)
inlinestatic

Returns a sequence of length Min(a.Count(), b.Count()) of items from a and b paired together. The output is produced lazily, so infinite input sequences are supported.

static IEnumerable<Pair<A, B> > Loyc.Collections.ListExt.ZipLeft< A, B > ( this IEnumerable< A >  a,
IEnumerable< B >  b,
defaultB 
)
inlinestatic

Returns a sequence that has the same length as the first sequence, with items from the first and second sequence paired together.

Parameters
defaultBIf the second sequence b is shorter than the first sequence, the remaining items of the first sequence are paired with this value. Otherwise, this value is not used.
static IEnumerable<C> Loyc.Collections.ListExt.ZipLeft< A, B, C > ( this IEnumerable< A >  a,
IEnumerable< B >  b,
defaultB,
Func< A, B, C >  resultSelector 
)
inlinestatic

An alternate version of ZipLeft<A, B>(IEnumerable<A>, IEnumerable<B>, B) in which a user-defined function is used to combine the items from the two lists.

Parameters
defaultBIf the second sequence b is shorter than the first sequence, the remaining items of the first sequence are paired with this value. Otherwise, this value is not used.
resultSelectorA function that combines items from the two lists.
static IEnumerable<Pair<A, B> > Loyc.Collections.ListExt.ZipLonger< A, B > ( this IEnumerable< A >  a,
IEnumerable< B >  b 
)
inlinestatic

Returns a sequence as long as the longer of two sequences. Items from the first and second sequence are initially paired together, and when one sequence ends, the other sequence's remaining values are paired with a default value.

static IEnumerable<Pair<A, B> > Loyc.Collections.ListExt.ZipLonger< A, B > ( this IEnumerable< A >  a,
IEnumerable< B >  b,
defaultA,
defaultB 
)
inlinestatic

Returns a sequence as long as the longer of two sequences. Items from the first and second sequence are initially paired together, and when one sequence ends, the other sequence's remaining values are paired with a default value provided as a parameter.

Parameters
defaultAIf the first sequence a is shorter than the second sequence, the remaining items of the second sequence are paired with this value. Otherwise, this value is not used.
defaultBIf the second sequence b is shorter than the first sequence, the remaining items of the first sequence are paired with this value. Otherwise, this value is not used.
static IEnumerable<C> Loyc.Collections.ListExt.ZipLonger< A, B, C > ( this IEnumerable< A >  a,
IEnumerable< B >  b,
defaultA,
defaultB,
Func< A, B, C >  resultSelector 
)
inlinestatic

An alternate version of ZipLonger<A, B>(IEnumerable<A>, IEnumerable<B>, A, B) in which a user-defined function is used to combine the items from the two lists.

Parameters
resultSelectorA function that combines items from the two lists.