/// <summary> /// Repeats a list of values a specified number of times. /// </summary> /// <typeparam name="T">The type of object contained in the list.</typeparam> /// <param name="list">The list of values to repeat.</param> /// <param name="count">The number of times to repeat all elements in the source list. If <paramref name="count"/> is less than or equal to 0, an empty list is returned.</param> /// <returns>A list containing the source list repeated the specified number of times.</returns> public static IList <T> Repeat <T>(this IList <T> list, int count) { if (count <= 0) { return(ListSource.Empty <T>()); } return(new Implementation.AnonymousReadOnlyList <T>(i => list[i % list.Count], () => list.Count * count)); }
/// <summary> /// Returns a read-only list that generates its elements when they are read, passing the element's index to the generator delegate. /// </summary> /// <typeparam name="T">The type of elements in the list.</typeparam> /// <param name="generator">The delegate that is used to generate the elements. This may be <c>null</c> if <paramref name="count"/> is less than or equal to 0.</param> /// <param name="count">The number of elements in the list. If <paramref name="count"/> is less than or equal to 0, an empty list is returned.</param> /// <returns>A read-only list that generates its elements on demand.</returns> public static IList <T> Generate <T>(Func <int, T> generator, int count) { if (count <= 0) { return(ListSource.Empty <T>()); } return(new Implementation.AnonymousReadOnlyList <T>(generator, () => count)); }
/// <summary> /// Converts a single value into a list containing that value the specified number of times. /// </summary> /// <typeparam name="T">The type of the value.</typeparam> /// <param name="source">The value.</param> /// <param name="count">The number of times <paramref name="source"/> is repeated. If <paramref name="count"/> is less than or equal to 0, an empty list is returned.</param> /// <returns>A read-only list containing <paramref name="count"/> elements, all equal to <paramref name="source"/>.</returns> public static IList <T> Repeat <T>(T source, int count) { if (count <= 0) { return(ListSource.Empty <T>()); } return(new Implementation.AnonymousReadOnlyList <T>(_ => source, () => count)); }
/// <summary> /// Returns a sliced list, which acts as a window into a subset of the original list. /// </summary> /// <typeparam name="T">The type of object contained in the list.</typeparam> /// <param name="list">The list to slice.</param> /// <param name="count">The number of elements in the slice. If <paramref name="count"/> is less than or equal to zero, then an empty list is returned; if <paramref name="count"/> is greater than or equal to the count of the source list, then a list equivalent to the source list is returned.</param> /// <returns>A list that is a slice of the source list.</returns> public static IList <T> Take <T>(this IList <T> list, int count) { if (count <= 0) { return(ListSource.Empty <T>()); } if (count >= list.Count) { return(list); } return(new SliceList <T>(list, 0, count)); }
/// <summary> /// Returns a sliced list, which acts as a window into a subset of the original list. /// </summary> /// <typeparam name="T">The type of object contained in the list.</typeparam> /// <param name="list">The list to slice.</param> /// <param name="offset">The offset into the list at which the slice begins. If <paramref name="offset"/> is less than or equal to zero, then a list equivalent to the source list is returned; if <paramref name="offset"/> is greater than or equal to the count of the source list, then an empty list is returned.</param> /// <returns>A list that is a slice of the source list.</returns> public static IList <T> Skip <T>(this IList <T> list, int offset) { if (offset <= 0) { return(list); } if (offset >= list.Count) { return(ListSource.Empty <T>()); } return(new SliceList <T>(list, offset, list.Count - offset)); }
/// <summary> /// Converts a single value into a sorted list containing that value the specified number of times. The list is treated as though it were sorted by the specified comparison object. /// </summary> /// <typeparam name="T">The type of the value.</typeparam> /// <param name="source">The value.</param> /// <param name="comparer">The comparison object that defines how the list is sorted.</param> /// <param name="count">The number of times <paramref name="source"/> is repeated. If <paramref name="count"/> is less than or equal to 0, an empty list is returned.</param> /// <returns>A sorted list containing <paramref name="count"/> elements, all equal to <paramref name="source"/>.</returns> public static ISortedList <T> Repeat <T>(T source, IComparer <T> comparer, int count) { return(new SortedListWrapper <T>(ListSource.Repeat(source, count), comparer)); }
/// <summary> /// Converts a single value into a sorted list containing a single value. The list is treated as though it were sorted by the specified comparison delegate. /// </summary> /// <typeparam name="T">The type of the value.</typeparam> /// <param name="source">The value.</param> /// <param name="comparer">The comparison delegate that defines how the list is sorted.</param> /// <returns>A sorted list containing a single element, <paramref name="source"/>.</returns> public static ISortedList <T> Return <T>(T source, Func <T, T, int> comparer) { return(new SortedListWrapper <T>(ListSource.Return(source), A.Comparer(comparer))); }
/// <summary> /// Converts a single value into a sorted list containing a single value. The list is treated as though it were sorted by the default comparison object. /// </summary> /// <typeparam name="T">The type of the value.</typeparam> /// <param name="source">The value.</param> /// <returns>A sorted list containing a single element, <paramref name="source"/>.</returns> public static ISortedList <T> Return <T>(T source) { return(new SortedListWrapper <T>(ListSource.Return(source), Comparer <T> .Default)); }
/// <summary> /// Converts a single value into a sorted list containing a single value. The list is treated as though it were sorted by the specified comparison object. /// </summary> /// <typeparam name="T">The type of the value.</typeparam> /// <param name="source">The value.</param> /// <param name="comparer">The comparison object that defines how the list is sorted.</param> /// <returns>A sorted list containing a single element, <paramref name="source"/>.</returns> public static ISortedList <T> Return <T>(T source, IComparer <T> comparer) { return(new SortedListWrapper <T>(ListSource.Return(source), comparer)); }
/// <summary> /// Creates an empty sorted list. The list is sorted by the specified comparison delegate. /// </summary> /// <typeparam name="T">The type of items in the list.</typeparam> /// <param name="comparer">The comparison delegate.</param> /// <returns>An empty sorted list.</returns> public static ISortedList <T> Empty <T>(Func <T, T, int> comparer) { return(new SortedListWrapper <T>(ListSource.Empty <T>(), A.Comparer(comparer))); }
/// <summary> /// Creates an empty sorted list. The list is sorted by the default comparison object. /// </summary> /// <typeparam name="T">The type of items in the list.</typeparam> /// <returns>An empty sorted list.</returns> public static ISortedList <T> Empty <T>() { return(new SortedListWrapper <T>(ListSource.Empty <T>(), Comparer <T> .Default)); }
/// <summary> /// Creates an empty sorted list. The list is sorted by the specified comparison object. /// </summary> /// <typeparam name="T">The type of items in the list.</typeparam> /// <param name="comparer">The comparison object.</param> /// <returns>An empty sorted list.</returns> public static ISortedList <T> Empty <T>(IComparer <T> comparer) { return(new SortedListWrapper <T>(ListSource.Empty <T>(), comparer)); }
/// <summary> /// Converts a single value into a sorted list containing that value the specified number of times. The list is treated as though it were sorted by the specified comparison delegate. /// </summary> /// <typeparam name="T">The type of the value.</typeparam> /// <param name="source">The value.</param> /// <param name="comparer">The comparison delegate that defines how the list is sorted.</param> /// <param name="count">The number of times <paramref name="source"/> is repeated. If <paramref name="count"/> is less than or equal to 0, an empty list is returned.</param> /// <returns>A sorted list containing <paramref name="count"/> elements, all equal to <paramref name="source"/>.</returns> public static ISortedList <T> Repeat <T>(T source, Func <T, T, int> comparer, int count) { return(new SortedListWrapper <T>(ListSource.Repeat(source, count), A.Comparer(comparer))); }
/// <summary> /// Converts a single value into a sorted list containing that value the specified number of times. The list is treated as though it were sorted by the default comparison object. /// </summary> /// <typeparam name="T">The type of the value.</typeparam> /// <param name="source">The value.</param> /// <param name="count">The number of times <paramref name="source"/> is repeated. If <paramref name="count"/> is less than or equal to 0, an empty list is returned.</param> /// <returns>A sorted list containing <paramref name="count"/> elements, all equal to <paramref name="source"/>.</returns> public static ISortedList <T> Repeat <T>(T source, int count) { return(new SortedListWrapper <T>(ListSource.Repeat(source, count), Comparer <T> .Default)); }