using System; using System.Collections.Generic; using System.Collections; public static class Collection { /// /// Bypasses elements in a collection as long as a specified condition is true // and then returns the remaining elements. /// public static IEnumerable SkipWhile(IEnumerable collection, Predicate match) { if (collection == null) { throw new ArgumentNullException("collection"); } if (match == null) { throw new ArgumentNullException("match"); } foreach (T item in collection) { if (match(item) == false) { yield return item; } } } /// /// Returns a specified number of contiguous elements from the start of a collection. /// public static IEnumerable Take(IEnumerable collection, int count) { if (collection == null) { throw new ArgumentNullException("collection"); } if (count < 0) { throw new IndexOutOfRangeException("count"); } int i = 0; foreach (T item in collection) { i++; if (i > count) { yield return item; } } } /// /// Returns elements from a collection as long as a specified condition is true. /// /// /// /// /// An System.Collections.Generic.IEnumerable that contains the elements from // the input collection that occur before the element at which the test no longer // passes. public static IEnumerable TakeWhile(IEnumerable collection, Predicate match) { if (collection == null) { throw new ArgumentNullException("collection"); } if (match == null) { throw new ArgumentNullException("match"); } foreach (T item in collection) { if (match(item)) { yield break; } yield return item; } } /// /// Determines whether two collections are equal by comparing the elements by using /// public static bool SequenceEqual(IEnumerable first, IEnumerable second) where T : IEquatable { if (first == null && second == null) { return true; } if (first == null) { throw new ArgumentNullException("first"); } if (second == null) { throw new ArgumentNullException("second"); } if (Count(first) != Count(second)) { return false; } using (IEnumerator enumerator1 = first.GetEnumerator()) using (IEnumerator enumerator2 = second.GetEnumerator()) { int count = Count(first); int index = 1; while (enumerator1.Current.Equals(enumerator2.Current)) { index++; continue; } return index == count; } } /// /// Bypasses a specified number of elements in a collection and then returns the /// public static IEnumerable Skip(IEnumerable collection, int count) { if (collection == null) { throw new ArgumentNullException("collection"); } if (count < 0) { throw new IndexOutOfRangeException("count"); } int i = 0; foreach (T item in collection) { i++; if (i > count) { yield return item; } } } /// /// Generates a collection that contains one repeated value. /// public static IEnumerable Repeat(T element, int count) { if (count <= 0) { throw new ArgumentOutOfRangeException("count"); } for (int i = 0; i < count; i++) { yield return element; } } /// /// Returns the element at a specified index in a collection. /// /// /// An System.Collections.Generic.IEnumerable to return an element from /// The zero-based index of the element to retrieve. /// public static T ElementAt(IEnumerable collection, int index) { if (collection == null) { throw new ArgumentNullException("collection"); } if (index < 0 || index > Count(collection) - 1) { throw new ArgumentOutOfRangeException("index"); } return ToArray(collection)[index]; } /// /// Returns the element at a specified index in a collection. /// /// /// An System.Collections.Generic.IEnumerable to return an element from /// The zero-based index of the element to retrieve. /// public static T ElementAtOrDefault(IEnumerable collection, int index) { if (index < 0 || index > Count(collection) - 1 || collection == null) { return default(T); } return ElementAt(collection, index); } /// /// Concatenates two collections. /// public static IEnumerable Concat(IEnumerable first, IEnumerable second) { if (first == null) { throw new ArgumentNullException("first"); } if (second == null) { throw new ArgumentNullException("second"); } foreach (T item in first) { yield return item; } foreach (T item in second) { yield return item; } } /// /// Returns true if collection contains the item /// /// /// /// /// public static bool Contains(IEnumerable collection, T item) where T : IEquatable { if (collection == null) { throw new ArgumentNullException("collection"); } foreach (T t in collection) { if (t.Equals(item)) { return true; } } return false; } /// /// Converts all the items of type T in collection to a new collection of type U according to converter /// /// /// /// /// /// public static IEnumerable ConvertAll(IEnumerable collection, Converter converter) { if (collection == null) { throw new ArgumentNullException("collection"); } if (converter == null) { throw new ArgumentNullException("converter"); } foreach (T item in collection) { yield return converter(item); } } /// /// Returns true if collection contains an item that satisfies the predicate /// /// /// /// /// public static bool Any(IEnumerable collection, Predicate match) { if (collection == null) { throw new ArgumentNullException("collection"); } if (match == null) { throw new ArgumentNullException("match"); } foreach (T item in collection) { if (match(item)) { return true; } } return false; } /// /// Determines whether a collection contains any elements /// /// /// /// /// public static bool Any(IEnumerable collection) { if (collection == null) { throw new ArgumentNullException("collection"); } using (IEnumerator iterator = collection.GetEnumerator()) { return iterator.MoveNext(); } } /// /// Finds the only item in the collection /// /// /// /// /// public static T Single(IEnumerable collection) { if (collection == null) { throw new ArgumentNullException("collection"); } if (Count(collection) != 1) { throw new InvalidOperationException("collection"); } return ToList(collection)[0]; } /// /// Finds the only item in the collection that satisfies the predicate /// /// /// /// /// public static T Single(IEnumerable collection, Predicate match) { if (collection == null) { throw new ArgumentNullException("collection"); } if (match == null) { throw new ArgumentNullException("match"); } if (Count(collection) == 0) { throw new InvalidOperationException("collection"); } IList list = ToList(FindAll(collection, match)); if (list.Count != 1) { throw new InvalidOperationException("Multiple matches found"); } return list[0]; } /// /// Finds the only item in the collection /// /// /// /// /// public static T SingleOrDefault(IEnumerable collection) { if (collection == null || Count(collection) != 1) { return default(T); } return Single(collection); } /// /// Finds the only item in the collection that satisfies the predicate /// /// /// /// /// public static T SingleOrDefault(IEnumerable collection, Predicate match) { if (collection == null || match == null) { return default(T); } if (Count(collection) == 0) { return default(T); } IList list = ToList(FindAll(collection, match)); if (list.Count != 1) { return default(T); } return list[0]; } /// /// Finds the first item in the collection /// /// /// /// /// public static T First(IEnumerable collection) { if (collection == null) { throw new ArgumentNullException("collection"); } if (Count(collection) == 0) { throw new InvalidOperationException("collection"); } return ToList(collection)[0]; } /// /// Finds the first item in the collection /// /// /// /// /// public static T FirstOrDefault(IEnumerable collection) { if (Count(collection) == 0 || collection == null) { return default(T); } return ToList(collection)[0]; } /// /// Finds the first occurrence of item in collection that satisfy the predicate /// /// /// /// /// public static T First(IEnumerable collection, Predicate match) { if (collection == null) { throw new ArgumentNullException("collection"); } if (match == null) { throw new ArgumentNullException("match"); } foreach (T item in collection) { if (match(item)) { return item; } } return default(T); } /// /// Finds all the items in collection that satisfy the predicate match. Same as LINQ's Where /// /// /// /// /// public static IEnumerable FindAll(IEnumerable collection, Predicate match) { if (collection == null) { throw new ArgumentNullException("collection"); } if (match == null) { throw new ArgumentNullException("match"); } foreach (T item in collection) { if (match(item)) { yield return item; } } } /// /// Finds all the items in iterator1 that are not in collection2 /// /// /// /// /// public static IEnumerable Complement(IEnumerable collection1, IEnumerable collection2) where T : IEquatable { foreach (T item in collection1) { if (Contains(collection2, item) == false) { yield return item; } } } /// /// Finds all the items that are not in the intersection of collection1 and collection2 /// /// /// /// /// public static IEnumerable Except(IEnumerable collection1, IEnumerable collection2) where T : IEquatable { IEnumerable complement1 = Complement(collection1, collection2); IEnumerable complement2 = Complement(collection2, collection1); return Union(complement1, complement2); } /// /// Returns a collection of all the distinct items in collection (no duplicates) /// /// /// /// public static IEnumerable Distinct(IEnumerable collection) { if (collection == null) { throw new ArgumentNullException("collection"); } IList list = new List(); foreach (T item in collection) { if (list.Contains(item) == false) { list.Add(item); } } foreach (T item in list) { yield return item; } } /// /// Find the index of the first occurrence of item in collection /// /// /// /// /// public static int FindIndex(IEnumerable collection, T value) where T : IEquatable { if (collection == null) { throw new ArgumentNullException("collection"); } int index = 0; foreach (T item in collection) { if (item.Equals(value) == false) { index++; } else { return index; } } return -1; } /// /// Finds the intersection of collection1 and collection2 /// /// /// /// /// public static IEnumerable Intersect(IEnumerable collection1, IEnumerable collection2) where T : IEquatable { Predicate existsInCollection2 = delegate(T t) { Predicate exist = delegate(T item) { return item.Equals(t); }; return Any(collection2, exist); }; return FindAll(collection1, existsInCollection2); } /// /// Find the index of the last occurrence of item in collection that satisfy the predicate match /// /// /// /// /// public static T LastOrDefault(IEnumerable collection, Predicate match) { if (collection == null) { return default(T); } if (Count(collection) == 0) { return default(T); } if (match == null) { throw new ArgumentNullException("match"); } return Last(collection, match); } /// /// Find the index of the last occurrence of item in collection /// /// /// /// /// public static T LastOrDefault(IEnumerable collection) { if (collection == null) { return default(T); } if (Count(collection) == 0) { return default(T); } return Last(collection); } /// /// Find the index of the last occurrence of item in collection /// /// /// /// /// public static T Last(IEnumerable collection) { if (collection == null) { throw new ArgumentNullException("collection"); } return ToArray(collection)[Count(collection) - 1]; } /// /// Find the index of the last occurrence of item in collection that satisfy the predicate match /// /// /// /// /// public static T Last(IEnumerable collection, Predicate match) { if (collection == null) { throw new ArgumentNullException("collection"); } if (match == null) { throw new ArgumentNullException("match"); } T last = default(T); foreach (T item in collection) { if (match(item)) { last = item; } } return last; } /// /// Find the index of the last occurrence of item in collection /// /// /// /// /// public static int FindLastIndex(IEnumerable collection, T value) where T : IEquatable { if (collection == null) { throw new ArgumentNullException("collection"); } int last = -1; foreach (T item in collection) { if (item.Equals(value)) { last++; } } if (last >= 0) { return last; } else { return -1; } } /// /// Finds the union of collection1 and collection2 /// /// /// /// /// public static IEnumerable Union(IEnumerable collection1, IEnumerable collection2) where T : IEquatable { LinkedList union = new LinkedList(Distinct(collection1)); Action action = delegate(T t) { if (union.Find(t) == null) { union.AddLast(t); } }; ForEach(collection2, action); foreach (T item in union) { yield return item; } } /// /// Performs the action on every item in collection /// /// /// /// public static void ForEach(IEnumerable collection, Action action) { if (collection == null) { throw new ArgumentNullException("collection"); } if (action == null) { throw new ArgumentNullException("action"); } foreach (T item in collection) { action(item); } } /// /// Returns the number of items in collection that satisfy a condition /// /// /// /// public static int Count(IEnumerable collection, Predicate match) { int length = 0; Action add = delegate(T item) { if (match(item)) { length++; } }; ForEach(collection, add); return length; } /// /// Returns the number of items in collection /// /// /// /// public static int Count(IEnumerable collection) { int length = 0; Action add = delegate { length++; }; ForEach(collection, add); return length; } /// /// Returns a collection with a reverse order of items /// /// /// /// public static IEnumerable Reverse(IEnumerable collection) { if (collection == null) { throw new ArgumentNullException("collection"); } List list = new List(collection); list.Reverse(); foreach (T item in list) { yield return item; } } /// /// Sorts the collection /// /// /// /// public static IEnumerable Sort(IEnumerable collection) { if (collection == null) { throw new ArgumentNullException("collection"); } List list = new List(collection); list.Sort(); foreach (T item in list) { yield return item; } } /// /// Converts collection to an array /// /// /// /// public static T[] ToArray(IEnumerable collection) { if (collection == null) { throw new ArgumentNullException("collection"); } List list = new List(); foreach (T item in collection) { list.Add(item); } return list.ToArray(); } /// /// Converts iterator to an array /// /// /// /// public static T[] ToArray(IEnumerator iterator) { if (iterator == null) { throw new ArgumentNullException("iterator"); } List list = new List(); while (iterator.MoveNext()) { list.Add(iterator.Current); } return list.ToArray(); } /// /// Converts the items in collection to an array /// /// /// /// Initial size for optimization /// public static T[] ToArray(IEnumerable collection, int count) { if (collection == null) { throw new ArgumentNullException("iterator"); } return ToArray(collection.GetEnumerator(), count); } /// /// Converts the items in iterator to an array /// /// /// /// Initial size for optimization /// public static T[] ToArray(IEnumerator iterator, int count) { if (iterator == null) { throw new ArgumentNullException("iterator"); } List list = new List(count); while (iterator.MoveNext()) { list.Add(iterator.Current); } return list.ToArray(); } /// /// Converts the items in collection to an array of type U according to the converter /// /// /// /// /// /// static U[] ToArray(IEnumerable collection, Converter converter) { int count = Count(collection); return ToArray(collection, converter, count); } /// /// Converts the items in collection to an array of type U according to the converter /// /// /// /// Initial size for optimization /// static U[] ToArray(IEnumerable collection, Converter converter, int count) { List list = new List(count); foreach (T t in collection) { list.Add(converter(t)); } return list.ToArray(); } /// /// Returns a list out of collection /// /// /// /// public static IList ToList(IEnumerable collection) { if (collection == null) { throw new ArgumentNullException("collection"); } IList list = new List(); foreach (T item in collection) { list.Add(item); } return list; } /// /// Returns all the items in collection that satisfy the predicate match /// /// /// /// /// public static bool All(IEnumerable collection, Predicate match) { if (collection == null) { throw new ArgumentNullException("collection"); } if (match == null) { throw new ArgumentNullException("match"); } foreach (T item in collection) { if (match(item) == false) { return false; } } return true; } /// /// Converts all the items in the object-based collection of the type T to a new array of type U according to converter /// /// /// /// public static U[] UnsafeToArray(IEnumerable collection, Converter converter) { if (collection == null) { throw new ArgumentNullException("collection"); } if (converter == null) { throw new ArgumentNullException("converter"); } IEnumerable newCollection = UnsafeConvertAll(collection, converter); return ToArray(newCollection); } /// /// Converts all the items in the object-based iterator of the type T to a new array of type U according to converter /// /// /// /// public static U[] UnsafeToArray(IEnumerator iterator, Converter converter) { if (iterator == null) { throw new ArgumentNullException("iterator"); } if (converter == null) { throw new ArgumentNullException("converter"); } IEnumerator newIterator = UnsafeConvertAll(iterator, converter); return ToArray(newIterator); } /// /// Converts collection to an array /// /// /// /// public static T[] UnsafeToArray(IEnumerable collection) { if (collection == null) { throw new ArgumentNullException("collection"); } IEnumerator iterator = collection.GetEnumerator(); using (iterator as IDisposable) { return UnsafeToArray(iterator); } } /// /// Converts an iterator to an array /// /// /// /// public static T[] UnsafeToArray(IEnumerator iterator) { if (iterator == null) { throw new ArgumentNullException("iterator"); } Converter innerConverter = delegate(object item) { return (T)item; }; return UnsafeToArray(iterator, innerConverter); } /// /// Converts all the items of type T in the object-based collection to a new collection of type U according to converter /// /// /// /// /// /// public static IEnumerable UnsafeConvertAll(IEnumerable collection, Converter converter) { if (collection == null) { throw new ArgumentNullException("collection"); } if (converter == null) { throw new ArgumentNullException("converter"); } foreach (object item in collection) { yield return converter((T)item); } } /// /// Converts all the items of type T in the object-based IEnumerator to a new collection of type U according to converter /// /// /// /// /// /// public static IEnumerator UnsafeConvertAll(IEnumerator iterator, Converter converter) { if (iterator == null) { throw new ArgumentNullException("collection"); } if (converter == null) { throw new ArgumentNullException("converter"); } while (iterator.MoveNext()) { yield return converter((T)iterator.Current); } } }