using System; using System.Collections.Generic; namespace JM.LinqFaster { public static partial class LinqFaster { /// /// Returns a specified number of contiguous elements from the start of a sequence. /// /// The sequence to return elements from. /// The number of elements to return. /// A sequence that contains the specified number of elements from the start of the input sequence. public static T[] TakeF(this T[] source, int count) { if (source == null) { throw Error.ArgumentNull("source"); } if (count < 0) { count = 0; } else if (count > source.Length) { count = source.Length; } var result = new T[count]; Array.Copy(source, 0, result, 0, count); return result; } /// /// Returns elements from a sequence as long as a specified condition is true. /// /// 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 that occur before the element at which the test no longer passes. public static T[] TakeWhileF(this T[] source, Func predicate) { if (source == null) { throw Error.ArgumentNull("source"); } if (predicate == null) { throw Error.ArgumentNull("predicate"); } var result = new List(); for (int i = 0; i < source.Length; i++) { if (predicate(source[i])) result.Add(source[i]); else return result.ToArray(); } return result.ToArray(); } /// /// Returns elements from a sequence as long as a specified condition is true. The element's index is used in the logic of the predicate function. /// /// The sequence to return elements from. /// A function to test each source element for a condition; the second parameter of the function represents the index of the source element. /// A sequence that contains elements from the input sequence that occur before the element at which the test no longer passes. public static T[] TakeWhileF(this T[] source, Func predicate) { if (source == null) { throw Error.ArgumentNull("source"); } if (predicate == null) { throw Error.ArgumentNull("predicate"); } var result = new List(); for (int i = 0; i < source.Length; i++) { if (predicate(source[i],i)) result.Add(source[i]); else return result.ToArray(); } return result.ToArray(); } // ------------- Lists ---------------- /// /// Returns a specified number of contiguous elements from the start of a sequence. /// /// The sequence to return elements from. /// The number of elements to return. /// A sequence that contains the specified number of elements from the start of the input sequence. public static List TakeF(this List source, int count) { if (source == null) { throw Error.ArgumentNull("source"); } if (count < 0) { count = 0; } else if (count > source.Count) { count = source.Count; } var result = new List(count); for (int i = 0; i < count; i++) { result.Add(source[i]); } return result; } /// /// Returns elements from a sequence as long as a specified condition is true. /// /// 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 that occur before the element at which the test no longer passes. public static List TakeWhileF(this List source, Func predicate) { if (source == null) { throw Error.ArgumentNull("source"); } if (predicate == null) { throw Error.ArgumentNull("predicate"); } var result = new List(); for (int i = 0; i < source.Count; i++) { if (predicate(source[i])) { result.Add(source[i]); } else { return result; } } return result; } /// /// Returns elements from a sequence as long as a specified condition is true. The element's index is used in the logic of the predicate function. /// /// The sequence to return elements from. /// A function to test each source element for a condition; the second parameter of the function represents the index of the source element. /// A sequence that contains elements from the input sequence that occur before the element at which the test no longer passes. public static List TakeWhileF(this List source, Func predicate) { if (source == null) { throw Error.ArgumentNull("source"); } if (predicate == null) { throw Error.ArgumentNull("predicate"); } var result = new List(); for (int i = 0; i < source.Count; i++) { if (predicate(source[i], i)) result.Add(source[i]); else return result; } return result; } } }