UnityEssentials
Small but useful tools and features for Unity
IEnumerableExtensions.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 
5 namespace UnityEngine
6 {
10 
11  public static class IEnumerableExtensions
12  {
17  public static IEnumerable<T> CloneAll<T>(this IEnumerable<T> enumerable) where T: ICloneable
18  {
19  return enumerable.Select(item => (T) item.Clone());
20  }
21 
28  public static void DebugLog<T>(this IEnumerable<T> enumerable, string separator = ", ", string message = "", Object context = null)
29  {
30  Debug.Log(message + enumerable.ToStringAllElements(separator), context);
31  }
32 
39  public static void DebugLogWarning<T>(this IEnumerable<T> enumerable, string separator = ", ", string message = "", Object context = null)
40  {
41  Debug.LogWarning(message + enumerable.ToStringAllElements(separator), context);
42  }
43 
50  public static void DebugLogError<T>(this IEnumerable<T> enumerable, string separator = ", ", string message = "", Object context = null)
51  {
52  Debug.LogError(message + enumerable.ToStringAllElements(separator), context);
53  }
54 
60  public static string ToStringAllElements<T>(this IEnumerable<T> enumerable, string separator = ", ")
61  {
62  return string.Join(separator, new List<T>(enumerable));
63  }
64 
69  public static IEnumerable<T> GetShuffled<T>(this IEnumerable<T> enumerable)
70  {
71  return enumerable.GetShuffled(new System.Random());
72  }
73 
79  private static IEnumerable<T> GetShuffled<T>(this IEnumerable<T> source, System.Random rnd)
80  {
81  List<T> buffer = source.ToList();
82  for (int i = 0; i < buffer.Count; i++)
83  {
84  int j = rnd.Next(i, buffer.Count);
85  yield return buffer[j];
86 
87  buffer[j] = buffer[i];
88  }
89  }
90 
95  public static T GetRandomElement<T>(this IEnumerable<T> enumerable)
96  {
97  return enumerable.GetRandomElement<T>(new System.Random());
98  }
99 
105  public static T GetRandomElement<T>(this IEnumerable<T> enumerable, System.Random rnd)
106  {
107  int index = rnd.Next(0, enumerable.Count());
108  return enumerable.ElementAt(index);
109  }
110 
116  public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source, IEqualityComparer<T> comparer = null)
117  {
118  return new HashSet<T>(source, comparer);
119  }
120 
125  public static bool IsNullOrEmpty<T>(this IEnumerable<T> source)
126  {
127  return source == null || !source.Any();
128  }
129 
130  }
131 }
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.