2 using System.Collections.Generic;
17 public static IEnumerable<T>
CloneAll<T>(
this IEnumerable<T> enumerable) where T: ICloneable
19 return enumerable.Select(item => (T) item.Clone());
28 public static void DebugLog<T>(
this IEnumerable<T> enumerable,
string separator =
", ",
string message =
"", Object context =
null)
30 Debug.Log(message + enumerable.ToStringAllElements(separator), context);
39 public static void DebugLogWarning<T>(
this IEnumerable<T> enumerable,
string separator =
", ",
string message =
"", Object context =
null)
41 Debug.LogWarning(message + enumerable.ToStringAllElements(separator), context);
50 public static void DebugLogError<T>(
this IEnumerable<T> enumerable,
string separator =
", ",
string message =
"", Object context =
null)
52 Debug.LogError(message + enumerable.ToStringAllElements(separator), context);
62 return string.Join(separator,
new List<T>(enumerable));
71 return enumerable.GetShuffled(
new System.Random());
79 private static IEnumerable<T>
GetShuffled<T>(
this IEnumerable<T> source, System.Random rnd)
81 List<T> buffer = source.ToList();
82 for (
int i = 0; i < buffer.Count; i++)
84 int j = rnd.Next(i, buffer.Count);
85 yield
return buffer[j];
87 buffer[j] = buffer[i];
97 return enumerable.GetRandomElement<T>(
new System.Random());
107 int index = rnd.Next(0, enumerable.Count());
108 return enumerable.ElementAt(index);
116 public static HashSet<T>
ToHashSet<T>(
this IEnumerable<T> source, IEqualityComparer<T> comparer =
null)
118 return new HashSet<T>(source, comparer);
127 return source ==
null || !source.Any();
Extensions for IEnumerable
static T GetRandomElement< T >(this IEnumerable< T > enumerable)
Return a random element.
static void DebugLogError< T >(this IEnumerable< T > enumerable, string separator=", ", string message="", Object context=null)
Creates a 'Debug.LogError' message with all the contents in the enumerable.
static IEnumerable< T > GetShuffled< T >(this IEnumerable< T > enumerable)
Shuffles the enumerable using the Fisher-Yates-Durstenfeld method.
static bool IsNullOrEmpty< T >(this IEnumerable< T > source)
Indicates whether the IEnumerable is null or does not contain any element.
static void DebugLogWarning< T >(this IEnumerable< T > enumerable, string separator=", ", string message="", Object context=null)
Creates a 'Debug.LogWarning' message with all the contents in the enumerable.
static IEnumerable< T > CloneAll< T >(this IEnumerable< T > enumerable)
Creates a new enumerable with all the elements of the original one cloned in it.
static string ToStringAllElements< T >(this IEnumerable< T > enumerable, string separator=", ")
Get an string of all elements.
static HashSet< T > ToHashSet< T >(this IEnumerable< T > source, IEqualityComparer< T > comparer=null)
Copies the elements to a new HashSet.
static void DebugLog< T >(this IEnumerable< T > enumerable, string separator=", ", string message="", Object context=null)
Creates a 'Debug.Log' message with all the contents in the enumerable.