using System; using System.Collections.Generic; namespace JM.LinqFaster { public static partial class LinqFaster { /// /// Bypasses a specified number of elements in a sequence and then returns the remaining elements. /// /// A sequence to return elements from. /// The number of elements to skip before returning the remaining elements. /// A sequence that contains the elements that occur after the specified index in the input sequence. public static T[] SkipF(this T[] source, int count) { if (source == null) { throw Error.ArgumentNull("source"); } if (count < 0) { count = 0; } else if (count > source.Length) { return new T[0]; } var result = new T[source.Length - count]; Array.Copy(source, count, result, 0, result.Length); return result; } /// /// Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements. /// /// A sequence to return elements from. /// A function to test each element for a condition. /// A sequence that contains the elements from the input sequence starting at the first element in the linear series that does not pass the test specified by predicate. public static T[] SkipWhileF(this T[] source, Func predicate) { if (source == null) { throw Error.ArgumentNull("source"); } if (predicate == null) { throw Error.ArgumentNull("predicate"); } int i = 0; for (; i < source.Length; i++) { if (!predicate(source[i])) break; } var result = new T[source.Length - i]; Array.Copy(source, i, result, 0, result.Length); return result; } // ------------- Lists ---------------- /// /// Bypasses a specified number of elements in a sequence and then returns the remaining elements. /// /// A sequence to return elements from. /// The number of elements to skip before returning the remaining elements. /// A sequence that contains the elements that occur after the specified index in the input sequence. public static List SkipF(this List source, int count) { if (source == null) { throw Error.ArgumentNull("source"); } if (count < 0) { count = 0; } else if (count > source.Count) { return new List(); } var result = new List(source.Count - count); for (int i = count; i < source.Count; i++) { result.Add(source[i]); } return result; } /// /// Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements. /// /// A sequence to return elements from. /// A function to test each element for a condition. /// A sequence that contains the elements from the input sequence starting at the first element in the linear series that does not pass the test specified by predicate. public static List SkipWhileF(this List source, Func predicate) { if (source == null) { throw Error.ArgumentNull("source"); } if (predicate == null) { throw Error.ArgumentNull("predicate"); } int i = 0; for (; i < source.Count; i++) { if (!predicate(source[i])) { break; } } var result = new List(source.Count - i); for (; i < source.Count; i++) { result.Add(source[i]); } return result; } } }