/// <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> /// 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)); }