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 ROLSlice< IReadOnlyList< T >, T > Slice< T > (this IReadOnlyList< T > list, int start, int count=int.MaxValue)
 
static IRange< T > AsRange< T > (this IListSource< T > list)
 
static IRange< T > AsRange< T > (this IRange< T > list)
 
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 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 bool AddIfNotPresent< TList, T > (this TList list, T item)
 
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 void Randomize< T > (this IList< T > list)
 
static void Randomize< T > (this T[] list)
 
static T[] Randomized< T > (this IReadOnlyList< 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 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 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

◆ InsertionSort< T >()

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

◆ InsertRangeHelper< T >()

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.

◆ Randomized< T >()

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

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

◆ RangeArray()

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

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

Referenced by Loyc.Collections.ListExt.StableSort< T >().

◆ RemoveAll< T >()

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

◆ RemoveRange< T >()

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.

◆ Repeat< T >()

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)

◆ Resize< T >() [1/2]

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).

◆ Resize< T >() [2/2]

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).

◆ SelectArray< T, R >() [1/3]

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.

◆ SelectArray< T, R >() [2/3]

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.

◆ SelectArray< T, R >() [3/3]

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.

◆ Single< T >()

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)

◆ Sort< T >()

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.

◆ SortLowestK< T >()

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).

◆ SortLowestKStable< T >()

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.

◆ SortPair< T >()

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.

◆ StableSort< T >()

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).

References Loyc.Collections.ListExt.RangeArray().

◆ Swap< T >()

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

Swaps list[i] with list[j].

◆ Zip< A, B >()

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.

◆ ZipLeft< A, B >()

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.

◆ ZipLeft< A, B, C >()

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.

◆ ZipLonger< A, B >() [1/2]

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.

◆ ZipLonger< A, B >() [2/2]

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.

◆ ZipLonger< A, B, C >()

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.