Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

126 rindas
2.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace SUISS.Core.Utilities
  6. {
  7. internal static class EnumerableExtensions
  8. {
  9. public static T PickRandom<T>(this IEnumerable<T> e)
  10. {
  11. List<T> list = e.ToList<T>();
  12. return (list.Count != 0) ? list[UnityEngine.Random.Range(0, list.Count)] : default(T);
  13. }
  14. public static IList<T> Clone<T>(this IList<T> listToClone) where T : ICloneable
  15. {
  16. return (from item in listToClone
  17. select (T)((object)item.Clone())).ToList<T>();
  18. }
  19. public static List<T> Where<T>(this IList<T> inputList, Func<T, bool> predicate)
  20. {
  21. List<T> list = new List<T>();
  22. foreach (T t in inputList)
  23. {
  24. if (predicate(t))
  25. {
  26. list.Add(t);
  27. }
  28. }
  29. return list;
  30. }
  31. public static List<T> Except<T>(this IEnumerable<T> input, IEnumerable<T> other)
  32. {
  33. List<T> list = new List<T>(input);
  34. list.RemoveAll((T x) => other.Contains(x));
  35. return list;
  36. }
  37. public static void RemoveAll<Key, Value>(this Dictionary<Key, Value> dict, Func<KeyValuePair<Key, Value>, bool> predicate)
  38. {
  39. List<Key> list = new List<Key>();
  40. foreach (KeyValuePair<Key, Value> arg in dict)
  41. {
  42. if (predicate(arg))
  43. {
  44. list.Add(arg.Key);
  45. }
  46. }
  47. foreach (Key key in list)
  48. {
  49. dict.Remove(key);
  50. }
  51. }
  52. public static T MinOrMaxObject<T, U>(this IEnumerable<T> source, Func<T, U> selector, bool findMin) where U : IComparable<U>
  53. {
  54. if (source == null)
  55. {
  56. throw new ArgumentNullException("source");
  57. }
  58. bool flag = true;
  59. T t = default(T);
  60. U other = default(U);
  61. foreach (T t2 in source)
  62. {
  63. if (flag)
  64. {
  65. t = t2;
  66. other = selector(t);
  67. flag = false;
  68. }
  69. else
  70. {
  71. U u = selector(t2);
  72. bool flag2 = (!findMin) ? (u.CompareTo(other) > 0) : (u.CompareTo(other) < 0);
  73. if (flag2)
  74. {
  75. other = u;
  76. t = t2;
  77. }
  78. }
  79. }
  80. if (flag)
  81. {
  82. throw new InvalidOperationException("Sequence is empty.");
  83. }
  84. return t;
  85. }
  86. public static List<T> ToList<T>(this IEnumerable<T> enumerable)
  87. {
  88. List<T> list = new List<T>();
  89. foreach (T item in enumerable)
  90. {
  91. list.Add(item);
  92. }
  93. return list;
  94. }
  95. public static IList<TResult> Select<TSource, TResult>(this IList<TSource> source, Func<TSource, TResult> selector)
  96. {
  97. List<TResult> list = new List<TResult>();
  98. foreach (TSource arg in source)
  99. {
  100. list.Add(selector(arg));
  101. }
  102. return list;
  103. }
  104. public static T First<T>(this IEnumerable<T> source, Predicate<T> predicate) where T : class
  105. {
  106. foreach (T t in source)
  107. {
  108. if (predicate(t))
  109. {
  110. return t;
  111. }
  112. }
  113. return (T)((object)null);
  114. }
  115. }
  116. }