1 using System.Collections.Generic;
4 using System.Collections.Immutable;
8 public static class IEnumerableExtensions
13 public static T[] Randomize<T>(
this IList<T> source, Rand.RandSync randSync = Rand.RandSync.Unsynced)
15 return source.OrderBy(i => Rand.Value(randSync)).ToArray();
21 public static void Shuffle<T>(
this IList<T> list, Rand.RandSync randSync = Rand.RandSync.Unsynced)
22 => list.Shuffle(Rand.GetRNG(randSync));
24 public static void Shuffle<T>(
this IList<T> list,
Random rng)
30 int k = rng.Next(n + 1);
37 public static T GetRandom<T>(
this IReadOnlyList<T> source, Func<T, bool> predicate, Rand.RandSync randSync)
39 if (predicate ==
null) {
return GetRandom(source, randSync); }
40 return source.Where(predicate).ToArray().GetRandom(randSync);
52 public static T GetRandom<T>(
this IReadOnlyList<T> source, Rand.RandSync randSync)
54 int count = source.Count;
55 return count == 0 ? default : source[Rand.Range(0, count, randSync)];
58 public static T GetRandom<T>(
this IReadOnlyList<T> source,
Random random)
60 int count = source.Count;
61 return count == 0 ? default : source[random.Next(0, count)];
67 public static T GetRandomUnsynced<T>(
this IEnumerable<T> source, Func<T, bool> predicate)
69 if (predicate ==
null) {
return GetRandomUnsynced(source); }
70 return source.Where(predicate).GetRandomUnsynced();
73 public static T GetRandomUnsynced<T>(
this IEnumerable<T> source)
75 if (source is IReadOnlyList<T> list)
77 return list.GetRandom(Rand.RandSync.Unsynced);
81 int count = source.Count();
82 return count == 0 ? default : source.ElementAt(Rand.Range(0, count, Rand.RandSync.Unsynced));
86 public static T GetRandom<T>(
this IEnumerable<T> source,
Random rand)
89 return source.OrderBy(p => p.UintIdentifier).ToArray().GetRandom(rand);
92 public static T GetRandom<T>(
this IEnumerable<T> source, Rand.RandSync randSync)
95 return source.OrderBy(p => p.UintIdentifier).ToArray().GetRandom(randSync);
98 public static T GetRandom<T>(
this IEnumerable<T> source, Func<T, bool> predicate, Rand.RandSync randSync)
101 return source.Where(predicate).OrderBy(p => p.UintIdentifier).ToArray().GetRandom(randSync);
104 public static T GetRandomByWeight<T>(
this IEnumerable<T> source, Func<T, float> weightSelector, Rand.RandSync randSync)
106 return ToolBox.SelectWeightedRandom(source, weightSelector, randSync);
113 public static void ForEachMod<T>(
this IEnumerable<T> source, Action<T> action)
115 if (source.None()) {
return; }
116 var temp =
new List<T>(source);
117 temp.ForEach(action);
124 public static void ForEach<T>(
this IEnumerable<T> source, Action<T> action)
126 foreach (var item
in source)
135 public static void Consume<T>(
this IEnumerable<T> enumerable)
137 foreach (var _
in enumerable) { }
143 public static bool None<T>(
this IEnumerable<T> source, Func<T, bool> predicate =
null)
145 if (predicate ==
null)
147 return !source.Any();
151 return !source.Any(predicate);
155 public static bool Multiple<T>(
this IEnumerable<T> source, Func<T, bool> predicate =
null)
157 if (predicate ==
null)
159 return source.Count() > 1;
163 return source.Count(predicate) > 1;
167 public static IEnumerable<T> ToEnumerable<T>(
this T item)
173 public static IEnumerable<T> SelectManyRecursive<T>(
this IEnumerable<T> source, Func<T, IEnumerable<T>> selector)
175 var result = source.SelectMany(selector);
180 return result.Concat(result.SelectManyRecursive(selector));
183 public static void AddIfNotNull<T>(
this IList<T> source, T value)
185 if (value !=
null) { source.Add(value); }
188 public static NetCollection<T> ToNetCollection<T>(
this IEnumerable<T> enumerable) =>
new NetCollection<T>(enumerable.ToImmutableArray());
197 public static bool AtLeast<T>(
this IEnumerable<T> source,
int amount, Predicate<T> predicate)
199 foreach (T elem
in source)
201 if (predicate(elem)) { amount--; }
202 if (amount <= 0) {
return true; }
213 public static ICollection<T> CollectionConcat<T>(
this IEnumerable<T>
self, IEnumerable<T> other)
214 =>
new CollectionConcat<T>(
self, other);
216 public static IReadOnlyList<T> ListConcat<T>(
this IEnumerable<T>
self, IEnumerable<T> other)
217 =>
new ListConcat<T>(
self, other);
Prefab that has a property serves as a deterministic hash of a prefab's identifier....