using System.Collections.Generic; using System.Linq; namespace SanAndreasUnity.Utilities { public static class CollectionExtensions { public static T RemoveLast(this IList list) { T lastElement = list[list.Count - 1]; list.RemoveAt(list.Count - 1); return lastElement; } public static T RemoveFirst(this IList list) { T firstElement = list[0]; list.RemoveAt(0); return firstElement; } public static Queue ToQueue(this IEnumerable enumerable) { return new Queue(enumerable); } public static Queue ToQueueWithCapacity(this IEnumerable enumerable, int capacity) { var queue = new Queue(capacity); foreach (var item in enumerable) queue.Enqueue(item); return queue; } public static T[] ToArrayOfLength(this IEnumerable enumerable, int length) { T[] array = new T[length]; int i = 0; foreach (var item in enumerable) { array[i] = item; i++; } return array; } public static IEnumerable AppendIf(this IEnumerable enumerable, bool condition, T element) { return condition ? enumerable.Append(element) : enumerable; } public static void AddMultiple(this ICollection collection, T value, int count) { for (int i = 0; i < count; i++) collection.Add(value); } public static void AddMultiple(this ICollection collection, int count) { collection.AddMultiple(default, count); } } }