Skip to content

Commit e54a545

Browse files
committed
Code refactoring using the new helper
1 parent 5497eb8 commit e54a545

File tree

2 files changed

+7
-37
lines changed

2 files changed

+7
-37
lines changed

Microsoft.Toolkit.HighPerformance/Enumerables/ReadOnlyRefEnumerable{T}.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Runtime.InteropServices;
1111
#endif
1212
using Microsoft.Toolkit.HighPerformance.Extensions;
13+
using Microsoft.Toolkit.HighPerformance.Helpers.Internals;
1314
#if !SPAN_RUNTIME_SUPPORT
1415
using RuntimeHelpers = Microsoft.Toolkit.HighPerformance.Helpers.Internals.RuntimeHelpers;
1516
#endif
@@ -153,14 +154,8 @@ public readonly void CopyTo(Span<T> destination)
153154
}
154155

155156
ref T destinationRef = ref destination.DangerousGetReference();
156-
nint
157-
step = (nint)(uint)this.step,
158-
offset = 0;
159157

160-
for (int i = 0; i < length; i++, offset += step)
161-
{
162-
Unsafe.Add(ref destinationRef, i) = Unsafe.Add(ref sourceRef, offset);
163-
}
158+
RefEnumerableHelper.CopyTo(ref sourceRef, ref destinationRef, (nint)(uint)length, (nint)(uint)this.step);
164159
}
165160

166161
/// <summary>

Microsoft.Toolkit.HighPerformance/Enumerables/RefEnumerable{T}.cs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Runtime.InteropServices;
1111
#endif
1212
using Microsoft.Toolkit.HighPerformance.Extensions;
13+
using Microsoft.Toolkit.HighPerformance.Helpers.Internals;
1314
#if !SPAN_RUNTIME_SUPPORT
1415
using RuntimeHelpers = Microsoft.Toolkit.HighPerformance.Helpers.Internals.RuntimeHelpers;
1516
#endif
@@ -151,14 +152,8 @@ public readonly void Clear()
151152
ref T r0 = ref RuntimeHelpers.GetObjectDataAtOffsetOrPointerReference<T>(this.instance, this.offset);
152153
int length = this.length;
153154
#endif
154-
nint
155-
step = (nint)(uint)this.step,
156-
offset = 0;
157155

158-
for (int i = length; i > 0; i--, offset += step)
159-
{
160-
Unsafe.Add(ref r0, offset) = default!;
161-
}
156+
RefEnumerableHelper.Clear(ref r0, (nint)(uint)length, (nint)(uint)this.step);
162157
}
163158

164159
/// <summary>
@@ -190,14 +185,8 @@ public readonly void CopyTo(Span<T> destination)
190185
}
191186

192187
ref T destinationRef = ref destination.DangerousGetReference();
193-
nint
194-
step = (nint)(uint)this.step,
195-
offset = 0;
196188

197-
for (int i = 0; i < length; i++, offset += step)
198-
{
199-
Unsafe.Add(ref destinationRef, i) = Unsafe.Add(ref sourceRef, offset);
200-
}
189+
RefEnumerableHelper.CopyTo(ref sourceRef, ref destinationRef, (nint)(uint)length, (nint)(uint)this.step);
201190
}
202191

203192
/// <summary>
@@ -254,14 +243,7 @@ internal readonly void CopyFrom(ReadOnlySpan<T> source)
254243
ThrowArgumentExceptionForDestinationTooShort();
255244
}
256245

257-
nint
258-
step = (nint)(uint)this.step,
259-
offset = 0;
260-
261-
for (int i = 0; i < sourceLength; i++, offset += step)
262-
{
263-
Unsafe.Add(ref destinationRef, i) = Unsafe.Add(ref sourceRef, offset);
264-
}
246+
RefEnumerableHelper.CopyFrom(ref sourceRef, ref destinationRef, (nint)(uint)sourceLength, (nint)(uint)this.step);
265247
}
266248

267249
/// <summary>
@@ -312,14 +294,7 @@ public readonly void Fill(T value)
312294
int length = this.length;
313295
#endif
314296

315-
nint
316-
step = (nint)(uint)this.step,
317-
offset = 0;
318-
319-
for (int i = length; i > 0; i--, offset += step)
320-
{
321-
Unsafe.Add(ref r0, offset) = value;
322-
}
297+
RefEnumerableHelper.Fill(ref r0, (nint)(uint)length, (nint)(uint)this.step, value);
323298
}
324299

325300
/// <summary>

0 commit comments

Comments
 (0)