From bef2c9310e7ef2ec82106b7d1a58ae5c1a90887f Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sat, 2 Apr 2022 14:00:13 -0400 Subject: [PATCH] Add 'Fill' list ext fn --- .../Collections/Extensions/ListExtensions.cs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/client/spark_core/Collections/Extensions/ListExtensions.cs b/src/client/spark_core/Collections/Extensions/ListExtensions.cs index 4cc5d8988..7bf4c6ede 100644 --- a/src/client/spark_core/Collections/Extensions/ListExtensions.cs +++ b/src/client/spark_core/Collections/Extensions/ListExtensions.cs @@ -225,6 +225,32 @@ namespace bts.collections { return -1; } + public static void Fill(this IList list, T value) => + Fill(list, value, 0, list.Count); + + public static void Fill( + this IList list, T value, int index, int count, bool allow_overfill = false) { + if (index > list.Count || !allow_overfill && index == list.Count) { + throw new IndexOutOfRangeException(); + } + var stop = index + count; + if (!allow_overfill && stop >= list.Count) { + throw new ArgumentException( + $"Exceeds the final index (not allowed when {nameof(allow_overfill)} is false)", + nameof(count)); + } + var rem = stop - list.Count; + if (rem > 0) { + stop = list.Count; + } + for (; index < stop; index++) { + list[index] = value; + } + while (rem-- > 0) { + list.Add(value); + } + } + public static IEnumerable ProjectToVector( this IEnumerable> rows, IEnumerable columns,