Add 'Fill' list ext fn

This commit is contained in:
John Colvin committed 2022-04-19 11:42:41 -04:00
1 parent 7b3e0d4a59
commit bef2c9310e
1 file changed
+26
@@ -225,6 +225,32 @@ namespace bts.collections {
return -1;
}
public static void Fill<T>(this IList<T> list, T value) =>
Fill(list, value, 0, list.Count);
public static void Fill<T>(
this IList<T> 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<TResult> ProjectToVector<TCell, TColumn, TResult>(
this IEnumerable<IEnumerable<TCell>> rows,
IEnumerable<TColumn> columns,