-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Pad.cs
193 lines (172 loc) · 5.98 KB
/
Pad.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
namespace SuperLinq;
public static partial class SuperEnumerable
{
/// <summary>
/// Pads a sequence with default values if it is narrower (shorter in length) than a given width.
/// </summary>
/// <typeparam name="TSource">
/// The type of the elements of <paramref name="source"/>.
/// </typeparam>
/// <param name="source">
/// The sequence to pad.
/// </param>
/// <param name="width">
/// The width/length below which to pad.
/// </param>
/// <returns>
/// Returns a sequence that is at least as wide/long as the width/length specified by the <paramref
/// name="width"/> parameter.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="width"/> is less than <c>0</c>.
/// </exception>
/// <remarks>
/// This operator uses deferred execution and streams its results.
/// </remarks>
public static IEnumerable<TSource?> Pad<TSource>(this IEnumerable<TSource> source, int width)
{
return Pad(source, width, padding: default);
}
/// <summary>
/// Pads a sequence with default values if it is narrower (shorter in length) than a given width.
/// </summary>
/// <typeparam name="TSource">
/// The type of the elements of <paramref name="source"/>.
/// </typeparam>
/// <param name="source">
/// The sequence to pad.
/// </param>
/// <param name="width">
/// The width/length below which to pad.
/// </param>
/// <param name="padding">
/// The value to use for padding.
/// </param>
/// <returns>
/// Returns a sequence that is at least as wide/long as the width/length specified by the <paramref
/// name="width"/> parameter.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="width"/> is less than <c>0</c>.
/// </exception>
/// <remarks>
/// This operator uses deferred execution and streams its results.
/// </remarks>
public static IEnumerable<TSource> Pad<TSource>(this IEnumerable<TSource> source, int width, TSource padding)
{
return Pad(source, width, paddingSelector: _ => padding);
}
/// <summary>
/// Pads a sequence with default values if it is narrower (shorter in length) than a given width.
/// </summary>
/// <typeparam name="TSource">
/// The type of the elements of <paramref name="source"/>.
/// </typeparam>
/// <param name="source">
/// The sequence to pad.
/// </param>
/// <param name="width">
/// The width/length below which to pad.
/// </param>
/// <param name="paddingSelector">
/// A function to generate the value used as padding.
/// </param>
/// <returns>
/// Returns a sequence that is at least as wide/long as the width/length specified by the <paramref
/// name="width"/> parameter.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> or <paramref name="paddingSelector"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="width"/> is less than <c>0</c>.
/// </exception>
/// <remarks>
/// This operator uses deferred execution and streams its results.
/// </remarks>
public static IEnumerable<TSource> Pad<TSource>(this IEnumerable<TSource> source, int width, Func<int, TSource> paddingSelector)
{
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(paddingSelector);
ArgumentOutOfRangeException.ThrowIfNegative(width);
if (source is IList<TSource> list)
return new PadListIterator<TSource>(list, width, paddingSelector);
if (source.TryGetCollectionCount() is int)
return new PadCollectionIterator<TSource>(source, width, paddingSelector);
return PadCore(source, width, paddingSelector);
}
private static IEnumerable<TSource> PadCore<TSource>(
IEnumerable<TSource> source, int width,
Func<int, TSource> paddingSelector)
{
var count = 0;
foreach (var item in source)
{
yield return item;
count++;
}
while (count < width)
{
yield return paddingSelector(count);
count++;
}
}
private sealed class PadCollectionIterator<T>(
IEnumerable<T> source,
int width,
Func<int, T> paddingSelector
) : CollectionIterator<T>
{
public override int Count => Math.Max(source.GetCollectionCount(), width);
protected override IEnumerable<T> GetEnumerable() =>
PadCore(source, width, paddingSelector);
public override void CopyTo(T[] array, int arrayIndex)
{
ArgumentNullException.ThrowIfNull(array);
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length - Count);
var cnt = source.CopyTo(array, arrayIndex);
for (var i = cnt; i < width; i++)
array[arrayIndex + i] = paddingSelector(i);
}
}
private sealed class PadListIterator<T>(
IList<T> source,
int width, Func<int, T> paddingSelector
) : ListIterator<T>
{
public override int Count => Math.Max(source.Count, width);
protected override IEnumerable<T> GetEnumerable()
{
var src = source;
var cnt = (uint)src.Count;
for (var i = 0; i < cnt; i++)
yield return src[i];
for (var i = (int)cnt; i < width; i++)
yield return paddingSelector(i);
}
public override void CopyTo(T[] array, int arrayIndex)
{
ArgumentNullException.ThrowIfNull(array);
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
ArgumentOutOfRangeException.ThrowIfGreaterThan(arrayIndex, array.Length - Count);
source.CopyTo(array, arrayIndex);
for (var i = source.Count; i < width; i++)
array[arrayIndex + i] = paddingSelector(i);
}
protected override T ElementAt(int index)
{
ArgumentOutOfRangeException.ThrowIfNegative(index);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count);
return index < source.Count
? source[index]
: paddingSelector(index);
}
}
}