Class ListUtilities
Contains some utility functions related to the Lists and Arrays
Namespace: WeaverCore.Utilities
Assembly: WeaverCore.dll
Syntax
public static class ListUtilities
Methods
| Edit this page View SourceAddIfNotContained<T>(List<T>, T)
Adds an item to the list if it's not already in it
Declaration
public static bool AddIfNotContained<T>(this List<T> list, T item)
Parameters
Type | Name | Description |
---|---|---|
List<T> | list | The list to add to |
T | item | The item to add |
Returns
Type | Description |
---|---|
bool | Whether it was able to add the item to the list or not |
Type Parameters
Name | Description |
---|---|
T | The list type |
AreEquivalent<T>(IEnumerable<T>, IEnumerable<T>, EqualityComparer<T>)
Checks if the two lists are equivalent
Declaration
public static bool AreEquivalent<T>(this IEnumerable<T> listA, IEnumerable<T> listB, EqualityComparer<T> comparer = null)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | listA | The first list to compare |
IEnumerable<T> | listB | The second list to compare |
EqualityComparer<T> | comparer | The comparer used to check for element equality. If this is null, will use the default comparer |
Returns
Type | Description |
---|---|
bool |
Type Parameters
Name | Description |
---|---|
T | The type of element in the lists |
GetListHash<T>(IEnumerable<T>)
Gets a hash for all the elements in the list
Declaration
public static int GetListHash<T>(this IEnumerable<T> list)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | list | The list to get the hash code from |
Returns
Type | Description |
---|---|
int | Returns a hash for all the elements in the list |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the list |
GetRandomElement<T>(List<T>)
Returns a random item from the list
Declaration
public static T GetRandomElement<T>(this List<T> list)
Parameters
Type | Name | Description |
---|---|---|
List<T> | list | The list to select from |
Returns
Type | Description |
---|---|
T | A random element from the list |
Type Parameters
Name | Description |
---|---|
T | The type of items in the list |
GetRandomElement<T>(List<T>, int, int)
Declaration
public static T GetRandomElement<T>(this List<T> list, int startIndex, int endIndex)
Parameters
Type | Name | Description |
---|---|---|
List<T> | list | |
int | startIndex | |
int | endIndex |
Returns
Type | Description |
---|---|
T |
Type Parameters
Name | Description |
---|---|
T |
IndexOf<T>(T[], T)
Gets the index of a value in an array
Declaration
public static int IndexOf<T>(this T[] values, T value)
Parameters
Type | Name | Description |
---|---|---|
T[] | values | The array to check |
T | value | The value to find |
Returns
Type | Description |
---|---|
int | Returns the index of the value. Returns -1 if the value couldn't be found |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the array |
RandomizeList<T>(List<T>)
Randomizes the list
Declaration
public static void RandomizeList<T>(this List<T> list)
Parameters
Type | Name | Description |
---|---|---|
List<T> | list | The list to randomize |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the list |
ShuffleInPlace<T>(IList<T>)
Shuffles the elements in a list. Note: This modifies the list itself
Declaration
public static void ShuffleInPlace<T>(this IList<T> source)
Parameters
Type | Name | Description |
---|---|---|
IList<T> | source | The list to shuffle |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the list |
Shuffle<T>(IEnumerable<T>)
Takes in an IEnumerable, and returns a new IEnumerable with the elements shuffled around
Declaration
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The source IEnumerator to use |
Returns
Type | Description |
---|---|
IEnumerable<T> | Returns a new IEnumerable with the elements shuffled around |
Type Parameters
Name | Description |
---|---|
T | The type of the elements in the IEnumerable |
SortBy<T>(List<T>, Func<T, int>)
Declaration
public static void SortBy<T>(this List<T> list, Func<T, int> getter)
Parameters
Type | Name | Description |
---|---|---|
List<T> | list | |
Func<T, int> | getter |
Type Parameters
Name | Description |
---|---|
T |
TryGetEquivalent<T>(HashSet<T>, T, out T)
Attempts to find a value in a hashset that is equivalent to a source value
Declaration
public static bool TryGetEquivalent<T>(this HashSet<T> set, T source, out T equivalent)
Parameters
Type | Name | Description |
---|---|---|
HashSet<T> | set | The HashSet to check over |
T | source | The source value to find |
T | equivalent | The equivalent value found in the HashSet |
Returns
Type | Description |
---|---|
bool | Returns true if an equivalent has been found |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the HashSet |