using System.Collections.Generic; namespace FirstGearGames.Utilities.Objects { public static class Arrays { /// /// Randomizer used for shuffling. /// private static System.Random _random = new System.Random(); /// /// Adds an entry to a list if it does not exist already. /// /// /// /// public static void AddUnique(this List list, object value) { if (!list.Contains((T)value)) list.Add((T)value); } /// /// Removes an object from a list through re-ordering. This breaks the order of the list for a faster remove. /// /// /// /// /// public static bool FastReferenceRemove(this List list, object value) { for (int i = 0; i < list.Count; i++) { if (object.ReferenceEquals(list[i], value)) { FastIndexRemove(list, i); return true; } } return false; } /// /// Removes an index from a list through re-ordering. This breaks the order of the list for a faster remove. /// /// /// /// public static void FastIndexRemove(this List list, int index) { list[index] = list[list.Count - 1]; list.RemoveAt(list.Count - 1); } /// /// Shuffles an array. /// /// /// public static void Shuffle(this T[] array) { int n = array.Length; for (int i = 0; i < (n - 1); i++) { int r = i + _random.Next(n - i); T t = array[r]; array[r] = array[i]; array[i] = t; } } } }