Class ListExtensions
Inheritance
System.Object
ListExtensions
Assembly: Eco.Shared.dll
Syntax
public static class ListExtensions : Object
Methods
AddNonNull<T>(ISet<T>, T)
Add to the hashset if it's not null.
Declaration
public static void AddNonNull<T>(this ISet<T> set, T val)
Parameters
Type |
Name |
Description |
System.Collections.Generic.ISet<T> |
set |
|
T |
val |
|
Type Parameters
AddNonNullKey<TK, TV>(IDictionary<TK, TV>, TK, TV)
Add to the dictionary if the key is not null.
Declaration
public static void AddNonNullKey<TK, TV>(this IDictionary<TK, TV> dict, TK key, TV val)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IDictionary<TK, TV> |
dict |
|
TK |
key |
|
TV |
val |
|
Type Parameters
AddOnce<T>(IList<T>, T)
Declaration
public static bool AddOnce<T>(this IList<T> list, T entry)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
T |
entry |
|
Returns
Type |
Description |
System.Boolean |
|
Type Parameters
AddOrReplace<T>(IList<T>, T, Predicate<T>)
Replaces first element matching the matcher
with item
or adds new item
if no one matched.
Declaration
public static void AddOrReplace<T>(this IList<T> list, T item, Predicate<T> matcher)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
T |
item |
|
System.Predicate<T> |
matcher |
|
Type Parameters
AddOrUpdate<T>(IList<T>, Func<T, Boolean>, Func<T>, Action<T>)
Declaration
public static void AddOrUpdate<T>(this IList<T> list, Func<T, bool> match, Func<T> create, Action<T> update)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
System.Func<T, System.Boolean> |
match |
|
System.Func<T> |
create |
|
System.Action<T> |
update |
|
Type Parameters
AddUniqueRange<T>(IList<T>, IEnumerable<T>)
Declaration
public static bool AddUniqueRange<T>(this IList<T> list, IEnumerable<T> items)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
System.Collections.Generic.IEnumerable<T> |
items |
|
Returns
Type |
Description |
System.Boolean |
|
Type Parameters
AllSame<T>(IEnumerable<T>)
Declaration
public static (bool, T) AllSame<T>(this IEnumerable<T> set)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IEnumerable<T> |
set |
|
Returns
Type |
Description |
System.ValueTuple<System.Boolean, T> |
|
Type Parameters
Clone<T>(IList<T>)
Declaration
public static IList<T> Clone<T>(this IList<T> listToClone)
where T : ICloneable
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
listToClone |
|
Returns
Type |
Description |
System.Collections.Generic.IList<T> |
|
Type Parameters
Count(IEnumerable)
Declaration
public static int Count(this IEnumerable source)
Parameters
Type |
Name |
Description |
System.Collections.IEnumerable |
source |
|
Returns
Type |
Description |
System.Int32 |
|
EnsureMinLength<T>(IEnumerable<T>, Int32)
Declaration
public static IEnumerable<T> EnsureMinLength<T>(this IEnumerable<T> list, int len)
where T : struct, ValueType
Parameters
Type |
Name |
Description |
System.Collections.Generic.IEnumerable<T> |
list |
|
System.Int32 |
len |
|
Returns
Type |
Description |
System.Collections.Generic.IEnumerable<T> |
|
Type Parameters
Enumerate<T>(BSONArray)
Declaration
public static IEnumerable<T> Enumerate<T>(this BSONArray bsonArray)
Parameters
Returns
Type |
Description |
System.Collections.Generic.IEnumerable<T> |
|
Type Parameters
EveryNthElement<T>(IEnumerable<T>, Int32)
Declaration
public static IEnumerable<T> EveryNthElement<T>(this IEnumerable<T> list, int n)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IEnumerable<T> |
list |
|
System.Int32 |
n |
|
Returns
Type |
Description |
System.Collections.Generic.IEnumerable<T> |
|
Type Parameters
FindNearestValue<T>(IList<T>, T, Func<T, T, Single>)
Finds nearest value in collection
Declaration
public static T FindNearestValue<T>(this IList<T> array, T value, Func<T, T, float> distanceFunc)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
array |
|
T |
value |
|
System.Func<T, T, System.Single> |
distanceFunc |
|
Returns
Type Parameters
FindNearestValueAndDistance<T>(IList<T>, T, Func<T, T, Single>)
Finds nearest value and distance in collection
Declaration
public static (T, float) FindNearestValueAndDistance<T>(this IList<T> array, T value, Func<T, T, float> distanceFunc)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
array |
|
T |
value |
|
System.Func<T, T, System.Single> |
distanceFunc |
|
Returns
Type |
Description |
System.ValueTuple<T, System.Single> |
|
Type Parameters
FindNearestValueIndex<T>(IList<T>, T, Func<T, T, Single>)
Finds nearest value index in collection
Declaration
public static int FindNearestValueIndex<T>(this IList<T> array, T value, Func<T, T, float> distanceFunc)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
array |
|
T |
value |
|
System.Func<T, T, System.Single> |
distanceFunc |
|
Returns
Type |
Description |
System.Int32 |
|
Type Parameters
FindNearestValueIndexAndDistance<T>(IList<T>, T, Func<T, T, Single>)
Finds nearest value index and distance in collection
Declaration
public static (int, float) FindNearestValueIndexAndDistance<T>(this IList<T> array, T value, Func<T, T, float> distanceFunc)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
array |
|
T |
value |
|
System.Func<T, T, System.Single> |
distanceFunc |
|
Returns
Type |
Description |
System.ValueTuple<System.Int32, System.Single> |
|
Type Parameters
FirstIndex<T>(IList<T>, Predicate<T>)
Declaration
public static int FirstIndex<T>(this IList<T> list, Predicate<T> func)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
System.Predicate<T> |
func |
|
Returns
Type |
Description |
System.Int32 |
|
Type Parameters
FirstOfTypeOrDefault<TElement, T>(List<TElement>)
Returns first item of type T
or default value if not found. It is a list specific implementation to avoid GC-allocations.
Declaration
public static T FirstOfTypeOrDefault<TElement, T>(this List<TElement> list)
where T : TElement
Parameters
Type |
Name |
Description |
System.Collections.Generic.List<TElement> |
list |
|
Returns
Type Parameters
Name |
Description |
TElement |
|
T |
|
ForEach<T>(IList<T>, Action<T>)
Declaration
public static void ForEach<T>(this IList<T> list, Action<T> action)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
System.Action<T> |
action |
|
Type Parameters
ForEachIndex<T>(IList<T>, Action<T, Int32>)
Declaration
public static void ForEachIndex<T>(this IList<T> list, Action<T, int> handler)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
System.Action<T, System.Int32> |
handler |
|
Type Parameters
FromBson<T>(IList<T>, BSONArray)
Declaration
public static void FromBson<T>(this IList<T> list, BSONArray bsonArray)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
BSONArray |
bsonArray |
|
Type Parameters
GetAtIndexOrDefault<T>(T[], Int32)
Declaration
public static T GetAtIndexOrDefault<T>(this T[] array, int index)
Parameters
Type |
Name |
Description |
T[] |
array |
|
System.Int32 |
index |
|
Returns
Type Parameters
GetAtIndexOrDefault<T>(IList<T>, Int32)
Declaration
public static T GetAtIndexOrDefault<T>(this IList<T> list, int index)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
System.Int32 |
index |
|
Returns
Type Parameters
GetOrAdd<T>(IList<T>, Func<T, Boolean>)
Declaration
public static T GetOrAdd<T>(this IList<T> list, Func<T, bool> match)
where T : new()
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
System.Func<T, System.Boolean> |
match |
|
Returns
Type Parameters
GetOrAdd<T>(IList<T>, Func<T, Boolean>, Func<T>)
Declaration
public static T GetOrAdd<T>(this IList<T> list, Func<T, bool> match, Func<T> create)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
System.Func<T, System.Boolean> |
match |
|
System.Func<T> |
create |
|
Returns
Type Parameters
IndexOf<T>(IEnumerable<T>, Predicate<T>)
Declaration
public static int IndexOf<T>(this IEnumerable<T> list, Predicate<T> test)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IEnumerable<T> |
list |
|
System.Predicate<T> |
test |
|
Returns
Type |
Description |
System.Int32 |
|
Type Parameters
MaxObj<T>(IList<T>, Func<T, Double>)
Declaration
public static T MaxObj<T>(this IList<T> list, Func<T, double> distanceFunc)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
System.Func<T, System.Double> |
distanceFunc |
|
Returns
Type Parameters
MaxObj<T>(IList<T>, Func<T, Single>)
Declaration
public static T MaxObj<T>(this IList<T> list, Func<T, float> distanceFunc)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
System.Func<T, System.Single> |
distanceFunc |
|
Returns
Type Parameters
Median<T>(IList<T>, Boolean)
Returns median value for list
. If sorted
is false
then it makes sorted copy of list to find the median otherwise it just returns medium element.
Declaration
public static T Median<T>(this IList<T> list, bool sorted = false)
where T : IComparable<T>
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
System.Boolean |
sorted |
|
Returns
Type Parameters
MinIndex<T>(IList<T>)
Declaration
public static T MinIndex<T>(this IList<T> list)
where T : IComparable
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
Returns
Type Parameters
MinIndex<T>(IList<T>, IComparer<T>)
Declaration
public static int MinIndex<T>(this IList<T> list, IComparer<T> comparer)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
System.Collections.Generic.IComparer<T> |
comparer |
|
Returns
Type |
Description |
System.Int32 |
|
Type Parameters
MinObj<T>(IList<T>, Func<T, Single>)
Declaration
public static T MinObj<T>(this IList<T> list, Func<T, float> distanceFunc)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
System.Func<T, System.Single> |
distanceFunc |
|
Returns
Type Parameters
NullIndices<T>(IEnumerable<T>)
Returns an enumerable of each index that has a null value in the passed list.
Declaration
public static IEnumerable<int> NullIndices<T>(this IEnumerable<T> list)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IEnumerable<T> |
list |
|
Returns
Type |
Description |
System.Collections.Generic.IEnumerable<System.Int32> |
|
Type Parameters
Random<T>(IList<T>)
Declaration
public static T Random<T>(this IList<T> list)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
Returns
Type Parameters
Random<T>(IList<T>, Random)
Declaration
public static T Random<T>(this IList<T> list, Random random)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
System.Random |
random |
|
Returns
Type Parameters
RandomWeighted<T>(IEnumerable<T>, Func<T, Single>)
Picks random element from list based on weights
Declaration
public static T RandomWeighted<T>(this IEnumerable<T> source, Func<T, float> weightSelector)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IEnumerable<T> |
source |
|
System.Func<T, System.Single> |
weightSelector |
weight to be evaluated
|
Returns
Type Parameters
RemoveAll<T, TPredicate>(List<T>, TPredicate, Action<T>)
Removes first item in list
matching matcher
and calls onRemove
action if provided for the removed item.
This method may be used with allocation-free struct based predicates instead of closures. Also it may inline predicate body for better performance.
Declaration
public static void RemoveAll<T, TPredicate>(this List<T> list, TPredicate matcher, Action<T> onRemove = null)
where TPredicate : IPredicate<T>
Parameters
Type |
Name |
Description |
System.Collections.Generic.List<T> |
list |
|
TPredicate |
matcher |
|
System.Action<T> |
onRemove |
|
Type Parameters
Name |
Description |
T |
|
TPredicate |
|
RemoveFirst<T>(IList<T>, Predicate<T>)
Removes first item matching the matcher
. Returns true
if item was removed.
Declaration
public static bool RemoveFirst<T>(this IList<T> list, Predicate<T> matcher)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
System.Predicate<T> |
matcher |
|
Returns
Type |
Description |
System.Boolean |
|
Type Parameters
RemoveFirst<T, TPredicate>(List<T>, TPredicate, Action<T>)
Removes first item in list
matching matcher
and calls onRemove
action if provided for the removed item.
This method may be used with allocation-free struct based predicates instead of closures. Also it may inline predicate body for better performance.
Declaration
public static bool RemoveFirst<T, TPredicate>(this List<T> list, TPredicate matcher, Action<T> onRemove = null)
where TPredicate : IPredicate<T>
Parameters
Type |
Name |
Description |
System.Collections.Generic.List<T> |
list |
|
TPredicate |
matcher |
|
System.Action<T> |
onRemove |
|
Returns
Type |
Description |
System.Boolean |
|
Type Parameters
Name |
Description |
T |
|
TPredicate |
|
RotateLeft<T>(IList<T>, Int32)
"Rotates" list by offset
. Every element has it's index shifted left by offset, if it results in negative index then it pushes to end of the list.
Declaration
public static IEnumerable<T> RotateLeft<T>(this IList<T> list, int offset)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
System.Int32 |
offset |
|
Returns
Type |
Description |
System.Collections.Generic.IEnumerable<T> |
|
Type Parameters
Examples
new[] { 1, 2, 3, 4 }.RotateLeft(2) // => 3, 4, 1, 2
SplitOnPredicate<T>(IEnumerable<T>, Func<T, Boolean>)
Declaration
public static (List<T>, List<T>) SplitOnPredicate<T>(this IEnumerable<T> list, Func<T, bool> predicate)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IEnumerable<T> |
list |
|
System.Func<T, System.Boolean> |
predicate |
|
Returns
Type |
Description |
System.ValueTuple<System.Collections.Generic.List<T>, System.Collections.Generic.List<T>> |
|
Type Parameters
TryGetFirst<T>(IList<T>, Predicate<T>, out T)
Declaration
public static bool TryGetFirst<T>(this IList<T> list, Predicate<T> func, out T value)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T> |
list |
|
System.Predicate<T> |
func |
|
T |
value |
|
Returns
Type |
Description |
System.Boolean |
|
Type Parameters
Types<T>(IEnumerable<T>)
Declaration
public static IEnumerable<Type> Types<T>(this IEnumerable<T> set)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IEnumerable<T> |
set |
|
Returns
Type |
Description |
System.Collections.Generic.IEnumerable<System.Type> |
|
Type Parameters
ZipApply<T>(IEnumerable<T>, IEnumerable<T>, Action<T, T>)
Declaration
public static void ZipApply<T>(this IEnumerable<T> left, IEnumerable<T> right, Action<T, T> zipFunc)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IEnumerable<T> |
left |
|
System.Collections.Generic.IEnumerable<T> |
right |
|
System.Action<T, T> |
zipFunc |
|
Type Parameters
ZipTuples<T1, T2>(IEnumerable<T1>, IEnumerable<T2>)
Declaration
public static IEnumerable<(T1, T2)> ZipTuples<T1, T2>(this IEnumerable<T1> list1, IEnumerable<T2> list2)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IEnumerable<T1> |
list1 |
|
System.Collections.Generic.IEnumerable<T2> |
list2 |
|
Returns
Type |
Description |
System.Collections.Generic.IEnumerable<System.ValueTuple<T1, T2>> |
|
Type Parameters
ZipTuples<T1, T2>(IList<T1>, IList<T2>)
Declaration
public static List<(T1, T2)> ZipTuples<T1, T2>(this IList<T1> list1, IList<T2> list2)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IList<T1> |
list1 |
|
System.Collections.Generic.IList<T2> |
list2 |
|
Returns
Type |
Description |
System.Collections.Generic.List<System.ValueTuple<T1, T2>> |
|
Type Parameters