Table of Contents

Class SwiftExtensions

Namespace
SwiftCollections
Assembly
SwiftCollections.dll

Provides extension methods for collection manipulation and utility functions.

public static class SwiftExtensions
Inheritance
SwiftExtensions
Inherited Members

Methods

FromEnd<T>(SwiftList<T>, int)

Gets the element at the specified index from the end (1-based) from SwiftList.

public static T FromEnd<T>(this SwiftList<T> list, int reverseIndex)

Parameters

list SwiftList<T>
reverseIndex int

Returns

T

Type Parameters

T

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

Gets the element at the specified index from the end (1-based). For example, FromEnd(1) returns the last item, FromEnd(2) returns second-to-last.

public static T FromEnd<T>(this IEnumerable<T> source, int reverseIndex)

Parameters

source IEnumerable<T>
reverseIndex int

Returns

T

Type Parameters

T

IsPopulatedSafe<T>(IEnumerable<T>)

Determines whether the collection is not null and contains any elements.

public static bool IsPopulatedSafe<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The collection to check.

Returns

bool

True if the collection is not null and contains at least one element; otherwise, false.

Type Parameters

T

The type of elements in the collection.

IsPopulated<T>(IEnumerable<T>)

Determines whether a sequence contains any elements.

public static bool IsPopulated<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The IEnumerable<T> to check for emptiness.

Returns

bool

true if the source sequence contains any elements; otherwise, false.

Type Parameters

T

PopLast<T>(IEnumerable<T>)

Returns the last item in the sequence.

public static T PopLast<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

Returns

T

Type Parameters

T

Populate<T>(T[])

Populates an array with new instances of the specified type. The type must have a parameterless constructor.

public static T[] Populate<T>(this T[] array) where T : new()

Parameters

array T[]

The array to populate.

Returns

T[]

The populated array.

Type Parameters

T

The type of the elements in the array.

Populate<T>(T[], Func<int, T>)

Populates an array with values generated by a provider function that accepts the current index.

public static T[] Populate<T>(this T[] array, Func<int, T> provider)

Parameters

array T[]

The array to populate.

provider Func<int, T>

A function that generates a value for each element based on its index.

Returns

T[]

The populated array.

Type Parameters

T

The type of the elements in the array.

Populate<T>(T[], Func<T>)

Populates an array with values generated by a specified provider function.

public static T[] Populate<T>(this T[] array, Func<T> provider)

Parameters

array T[]

The array to populate.

provider Func<T>

A function that generates a value for each element in the array.

Returns

T[]

The populated array.

Type Parameters

T

The type of the elements in the array.

SecondToLast<T>(IEnumerable<T>)

Returns the second-to-last item in the sequence.

public static T SecondToLast<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

Returns

T

Type Parameters

T

ShuffleInPlace<T>(IList<T>, Random)

Shuffles the elements of the list in place using the specified random number generator.

public static void ShuffleInPlace<T>(this IList<T> list, Random rng)

Parameters

list IList<T>

The list to shuffle.

rng Random

The random number generator to use for shuffling.

Type Parameters

T

The type of elements in the list.

Shuffle<T>(IEnumerable<T>, Random)

An iterator that yields the elements of the source collection in a random order using the specified random number generator.

public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng)

Parameters

source IEnumerable<T>

The collection to shuffle.

rng Random

The random number generator to use for shuffling.

Returns

IEnumerable<T>

An iterator that yields the shuffled elements.

Type Parameters

T

The type of elements in the collection.

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

Bypasses a specified number of elements from the end and then returns the remaining elements.

public static IEnumerable<T> SkipFromEnd<T>(this IEnumerable<T> source, int count)

Parameters

source IEnumerable<T>
count int

Returns

IEnumerable<T>

Type Parameters

T

TryIndex<T>(T[], int, out T)

Attempts to retrieve an element from the array at the specified index. Returns true if the index is valid and the element is retrieved; otherwise, returns false.

public static bool TryIndex<T>(this T[] array, int index, out T result)

Parameters

array T[]

The array from which to retrieve the element.

index int

The index of the element to retrieve.

result T

When this method returns, contains the element at the specified index if the index is valid; otherwise, the default value for the type of the element.

Returns

bool

True if the element at the specified index was retrieved successfully; otherwise, false.

Type Parameters

T

The type of elements in the array.