3 using System.Collections.Generic;
6 using System.Collections.Immutable;
10 public static class IEnumerableExtensions
15 public static T[] Randomize<T>(
this IList<T> source, Rand.RandSync randSync)
17 return source.OrderBy(i => Rand.Value(randSync)).ToArray();
23 public static void Shuffle<T>(
this IList<T> list, Rand.RandSync randSync)
24 => list.Shuffle(Rand.GetRNG(randSync));
26 public static void Shuffle<T>(
this IList<T> list,
Random rng)
32 int k = rng.Next(n + 1);
39 public static T? GetRandom<T>(
this IReadOnlyList<T> source, Func<T, bool> predicate, Rand.RandSync randSync)
41 if (predicate ==
null) {
return GetRandom(source, randSync); }
42 return source.Where(predicate).ToArray().GetRandom(randSync);
54 public static T? GetRandom<T>(
this IReadOnlyList<T> source, Rand.RandSync randSync)
56 int count = source.Count;
57 return count == 0 ? default : source[Rand.Range(0, count, randSync)];
60 public static T? GetRandom<T>(
this IReadOnlyList<T> source,
Random random)
62 int count = source.Count;
63 return count == 0 ? default : source[random.Next(0, count)];
69 public static T? GetRandomUnsynced<T>(
this IEnumerable<T> source, Func<T, bool> predicate)
71 if (predicate ==
null) {
return GetRandomUnsynced(source); }
72 return source.Where(predicate).GetRandomUnsynced();
75 public static T? GetRandomUnsynced<T>(
this IEnumerable<T> source)
77 if (source is IReadOnlyList<T> list)
79 return list.GetRandom(Rand.RandSync.Unsynced);
83 int count = source.Count();
84 return count == 0 ? default : source.ElementAt(Rand.Range(0, count, Rand.RandSync.Unsynced));
88 public static T? GetRandom<T>(
this IEnumerable<T> source,
Random rand)
91 return source.OrderBy(p => p.UintIdentifier).ToArray().GetRandom(rand);
94 public static T? GetRandom<T>(
this IEnumerable<T> source, Rand.RandSync randSync)
97 return source.OrderBy(p => p.UintIdentifier).ToArray().GetRandom(randSync);
100 public static T? GetRandom<T>(
this IEnumerable<T> source, Func<T, bool> predicate, Rand.RandSync randSync)
103 return source.Where(predicate).OrderBy(p => p.UintIdentifier).ToArray().GetRandom(randSync);
106 public static T GetRandomByWeight<T>(
this IEnumerable<T> source, Func<T, float> weightSelector, Rand.RandSync randSync)
108 return ToolBox.SelectWeightedRandom(source, weightSelector, randSync);
115 public static void ForEachMod<T>(
this IEnumerable<T> source, Action<T> action)
117 if (source.None()) {
return; }
118 var temp =
new List<T>(source);
119 temp.ForEach(action);
126 public static void ForEach<T>(
this IEnumerable<T> source, Action<T> action)
128 foreach (var item
in source)
137 public static void Consume<T>(
this IEnumerable<T> enumerable)
139 foreach (var _
in enumerable) { }
145 public static bool None<T>(
this IEnumerable<T> source, Func<T, bool>? predicate =
null)
147 if (predicate ==
null)
149 return !source.Any();
153 return !source.Any(predicate);
157 public static bool Multiple<T>(
this IEnumerable<T> source, Func<T, bool>? predicate =
null)
159 if (predicate ==
null)
161 return source.Count() > 1;
165 return source.Count(predicate) > 1;
169 public static IEnumerable<T> ToEnumerable<T>(
this T item)
174 public static IEnumerable<T> NotNull<T>(
this IEnumerable<T?> enumerable) where T :
class
176 foreach (var item
in enumerable)
186 public static IEnumerable<T> SelectManyRecursive<T>(
this IEnumerable<T> source, Func<T, IEnumerable<T>> selector)
188 var result = source.SelectMany(selector);
193 return result.Concat(result.SelectManyRecursive(selector));
196 public static void AddIfNotNull<T>(
this IList<T> source, T value)
198 if (value !=
null) { source.Add(value); }
202 public static NetCollection<T> ToNetCollection<T>(
this IEnumerable<T> enumerable) =>
new NetCollection<T>(enumerable.ToImmutableArray());
211 public static bool AtLeast<T>(
this IEnumerable<T> source,
int amount, Predicate<T> predicate)
213 foreach (T elem
in source)
215 if (predicate(elem)) { amount--; }
216 if (amount <= 0) {
return true; }
227 public static ICollection<T> CollectionConcat<T>(
this IEnumerable<T>
self, IEnumerable<T> other)
228 =>
new CollectionConcat<T>(
self, other);
230 public static IReadOnlyList<T> ListConcat<T>(
this IEnumerable<T>
self, IEnumerable<T> other)
231 =>
new ListConcat<T>(
self, other);
Prefab that has a property serves as a deterministic hash of a prefab's identifier....