using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; /// /// CollectionExtensions class. /// public static class CollectionExtension { /// /// Add a range of objects to a Collection object. /// /// Object type. /// Collection object. /// Range of values to add. public static void AddRange(this Collection collection, IEnumerable values) { foreach (var item in values) { collection.Add(item); } } /// /// Find an object within a Collection object. /// /// Object type. /// Collection object. /// Predicate object. /// Returns default object if none were found or first object matching terms. public static T Find(this Collection collection, Predicate predicate) { foreach (var item in collection) { if (predicate(item)) { return item; } } return default(T); } /// /// Find all objects within a Collection object. /// /// Object type. /// Collection object. /// Predicate object. /// Returns default object if none were found or all objects matching terms. public static Collection FindAll(this Collection collection, Predicate predicate) { Collection all = new Collection(); foreach (var item in collection) { if (predicate(item)) { all.Add(item); } } return all; } /// /// Find the index of an object within a Collection object. /// /// Object type. /// Collection object. /// Predicate object. /// Returns object index. public static int FindIndex(this Collection collection, Predicate predicate) { return FindIndex(collection, 0, predicate); } /// /// Find the index of an object within a Collection object starting at a specified index. /// /// Object type. /// Collection object. /// Starting index. /// Predicate object. /// Returns object index. public static int FindIndex(this Collection collection, int startIndex, Predicate predicate) { return FindIndex(collection, startIndex, collection.Count, predicate); } /// /// Find the index of an object within a Collection object at a specified index and only a certain length. /// /// Object type. /// Collection object. /// Starting index. /// Count. /// Predicate object. /// Returns object index. public static int FindIndex(this Collection collection, int startIndex, int count, Predicate predicate) { for (int i = startIndex; i < count; i++) { if (predicate(collection[i])) { return i; } } return -1; } /// /// Find the last object within a Collection object. /// /// Object type. /// Collection object. /// Predicate object. /// Returns object. public static T FindLast(this Collection collection, Predicate predicate) { for (int i = collection.Count - 1; i >= 0; i--) { if (predicate(collection[i])) { return collection[i]; } } return default(T); } /// /// Find the last index of an object within a Collection object. /// /// Object type. /// Collection object. /// Predicate object. /// Returns object index. public static int FindLastIndex(this Collection collection, Predicate predicate) { return FindLastIndex(collection, collection.Count - 1, predicate); } /// /// Find the last index of an object within a Collection object starting at a specified index. /// /// Object type. /// Collection object. /// Start index. /// Predicate object. /// Returns object index. public static int FindLastIndex(this Collection collection, int startIndex, Predicate predicate) { return FindLastIndex(collection, startIndex, startIndex + 1, predicate); } /// /// Find the last index of an object within a Collection object starting at a specified index and length. /// /// Object type. /// Collection object. /// Start index. /// Count. /// Predicate object. /// Returns object index. public static int FindLastIndex(this Collection collection, int startIndex, int count, Predicate predicate) { for (int i = startIndex; i >= startIndex - count; i--) { if (predicate(collection[i])) { return i; } } return -1; } /// /// Performs a given action on each object in the Collectoin. /// /// Object type. /// Collection object. /// Action to perform. public static void ForEach(this Collection collection, Action action) { foreach (var item in collection) { action(item); } } /// /// Remove all object from a Collection. /// /// Object type. /// Collection object. /// Predicate object. /// Number of object removed. public static int RemoveAll(this Collection collection, Predicate match) { int count = 0; for (int i = 0; i < collection.Count; i++) { if (match(collection[i])) { collection.Remove(collection[i]); count++; i--; } } return count; } /// /// Determines if all objects within a Collection match a predicate. /// /// Object type. /// Collection object. /// Predicate object. /// A value indicating whether all objects match the predicate. public static bool TrueForAll(this Collection collection, Predicate match) { foreach (var item in collection) { if (!match(item)) { return false; } } return true; } }