Skip to content

Commit c2fc4ad

Browse files
authored
Merge branch 'master' into test-chop-imageex
2 parents 9d9f253 + 4e54ec5 commit c2fc4ad

File tree

58 files changed

+1299
-258
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1299
-258
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,4 @@ dotnet_diagnostic.SA1413.severity = none # UseTrailingCommasInMultiLineInitializ
327327
dotnet_diagnostic.SA1314.severity = none # TypeParameterNamesMustBeginWithT: We do have a few templates that don't start with T. We need to double check that changing this is not a breaking change. If not, we can re-enable this.
328328
dotnet_diagnostic.SA1000.severity = none # Hide warnings when using the new() expression from C# 9.
329329
dotnet_diagnostic.SA1313.severity = none # Hide warnings for record parameters.
330-
dotnet_diagnostic.SA1101.severity = none # Hide warnings when accessing properties without "this".
330+
dotnet_diagnostic.SA1101.severity = none # Hide warnings when accessing properties without "this".

Microsoft.Toolkit.HighPerformance/Buffers/MemoryOwner{T}.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private MemoryOwner(int start, int length, ArrayPool<T> pool, T[] array)
9191
public static MemoryOwner<T> Empty
9292
{
9393
[MethodImpl(MethodImplOptions.AggressiveInlining)]
94-
get => new MemoryOwner<T>(0, ArrayPool<T>.Shared, AllocationMode.Default);
94+
get => new(0, ArrayPool<T>.Shared, AllocationMode.Default);
9595
}
9696

9797
/// <summary>
@@ -103,7 +103,7 @@ public static MemoryOwner<T> Empty
103103
/// <remarks>This method is just a proxy for the <see langword="private"/> constructor, for clarity.</remarks>
104104
[Pure]
105105
[MethodImpl(MethodImplOptions.AggressiveInlining)]
106-
public static MemoryOwner<T> Allocate(int size) => new MemoryOwner<T>(size, ArrayPool<T>.Shared, AllocationMode.Default);
106+
public static MemoryOwner<T> Allocate(int size) => new(size, ArrayPool<T>.Shared, AllocationMode.Default);
107107

108108
/// <summary>
109109
/// Creates a new <see cref="MemoryOwner{T}"/> instance with the specified parameters.
@@ -115,7 +115,7 @@ public static MemoryOwner<T> Empty
115115
/// <remarks>This method is just a proxy for the <see langword="private"/> constructor, for clarity.</remarks>
116116
[Pure]
117117
[MethodImpl(MethodImplOptions.AggressiveInlining)]
118-
public static MemoryOwner<T> Allocate(int size, ArrayPool<T> pool) => new MemoryOwner<T>(size, pool, AllocationMode.Default);
118+
public static MemoryOwner<T> Allocate(int size, ArrayPool<T> pool) => new(size, pool, AllocationMode.Default);
119119

120120
/// <summary>
121121
/// Creates a new <see cref="MemoryOwner{T}"/> instance with the specified parameters.
@@ -127,7 +127,7 @@ public static MemoryOwner<T> Empty
127127
/// <remarks>This method is just a proxy for the <see langword="private"/> constructor, for clarity.</remarks>
128128
[Pure]
129129
[MethodImpl(MethodImplOptions.AggressiveInlining)]
130-
public static MemoryOwner<T> Allocate(int size, AllocationMode mode) => new MemoryOwner<T>(size, ArrayPool<T>.Shared, mode);
130+
public static MemoryOwner<T> Allocate(int size, AllocationMode mode) => new(size, ArrayPool<T>.Shared, mode);
131131

132132
/// <summary>
133133
/// Creates a new <see cref="MemoryOwner{T}"/> instance with the specified parameters.
@@ -140,7 +140,7 @@ public static MemoryOwner<T> Empty
140140
/// <remarks>This method is just a proxy for the <see langword="private"/> constructor, for clarity.</remarks>
141141
[Pure]
142142
[MethodImpl(MethodImplOptions.AggressiveInlining)]
143-
public static MemoryOwner<T> Allocate(int size, ArrayPool<T> pool, AllocationMode mode) => new MemoryOwner<T>(size, pool, mode);
143+
public static MemoryOwner<T> Allocate(int size, ArrayPool<T> pool, AllocationMode mode) => new(size, pool, mode);
144144

145145
/// <summary>
146146
/// Gets the number of items in the current instance

Microsoft.Toolkit.HighPerformance/Buffers/SpanOwner{T}.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private SpanOwner(int length, ArrayPool<T> pool, AllocationMode mode)
8080
public static SpanOwner<T> Empty
8181
{
8282
[MethodImpl(MethodImplOptions.AggressiveInlining)]
83-
get => new SpanOwner<T>(0, ArrayPool<T>.Shared, AllocationMode.Default);
83+
get => new(0, ArrayPool<T>.Shared, AllocationMode.Default);
8484
}
8585

8686
/// <summary>
@@ -92,7 +92,7 @@ public static SpanOwner<T> Empty
9292
/// <remarks>This method is just a proxy for the <see langword="private"/> constructor, for clarity.</remarks>
9393
[Pure]
9494
[MethodImpl(MethodImplOptions.AggressiveInlining)]
95-
public static SpanOwner<T> Allocate(int size) => new SpanOwner<T>(size, ArrayPool<T>.Shared, AllocationMode.Default);
95+
public static SpanOwner<T> Allocate(int size) => new(size, ArrayPool<T>.Shared, AllocationMode.Default);
9696

9797
/// <summary>
9898
/// Creates a new <see cref="SpanOwner{T}"/> instance with the specified parameters.
@@ -104,7 +104,7 @@ public static SpanOwner<T> Empty
104104
/// <remarks>This method is just a proxy for the <see langword="private"/> constructor, for clarity.</remarks>
105105
[Pure]
106106
[MethodImpl(MethodImplOptions.AggressiveInlining)]
107-
public static SpanOwner<T> Allocate(int size, ArrayPool<T> pool) => new SpanOwner<T>(size, pool, AllocationMode.Default);
107+
public static SpanOwner<T> Allocate(int size, ArrayPool<T> pool) => new(size, pool, AllocationMode.Default);
108108

109109
/// <summary>
110110
/// Creates a new <see cref="SpanOwner{T}"/> instance with the specified parameters.
@@ -116,7 +116,7 @@ public static SpanOwner<T> Empty
116116
/// <remarks>This method is just a proxy for the <see langword="private"/> constructor, for clarity.</remarks>
117117
[Pure]
118118
[MethodImpl(MethodImplOptions.AggressiveInlining)]
119-
public static SpanOwner<T> Allocate(int size, AllocationMode mode) => new SpanOwner<T>(size, ArrayPool<T>.Shared, mode);
119+
public static SpanOwner<T> Allocate(int size, AllocationMode mode) => new(size, ArrayPool<T>.Shared, mode);
120120

121121
/// <summary>
122122
/// Creates a new <see cref="SpanOwner{T}"/> instance with the specified parameters.
@@ -129,7 +129,7 @@ public static SpanOwner<T> Empty
129129
/// <remarks>This method is just a proxy for the <see langword="private"/> constructor, for clarity.</remarks>
130130
[Pure]
131131
[MethodImpl(MethodImplOptions.AggressiveInlining)]
132-
public static SpanOwner<T> Allocate(int size, ArrayPool<T> pool, AllocationMode mode) => new SpanOwner<T>(size, pool, mode);
132+
public static SpanOwner<T> Allocate(int size, ArrayPool<T> pool, AllocationMode mode) => new(size, pool, mode);
133133

134134
/// <summary>
135135
/// Gets the number of items in the current instance
@@ -183,7 +183,7 @@ public ref T DangerousGetReference()
183183
[MethodImpl(MethodImplOptions.AggressiveInlining)]
184184
public ArraySegment<T> DangerousGetArray()
185185
{
186-
return new ArraySegment<T>(array!, 0, this.length);
186+
return new(array!, 0, this.length);
187187
}
188188

189189
/// <summary>

Microsoft.Toolkit.HighPerformance/Buffers/StringPool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ static void FindFactors(int size, int factor, out int x, out int y)
137137
/// process. Since <see cref="StringPool"/> is thread-safe, the shared instance can be used
138138
/// concurrently by multiple threads without the need for manual synchronization.
139139
/// </remarks>
140-
public static StringPool Shared { get; } = new StringPool();
140+
public static StringPool Shared { get; } = new();
141141

142142
/// <summary>
143143
/// Gets the total number of <see cref="string"/> that can be stored in the current instance.

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#endif
1111
using Microsoft.Toolkit.HighPerformance.Extensions;
1212
using Microsoft.Toolkit.HighPerformance.Helpers.Internals;
13+
using Microsoft.Toolkit.HighPerformance.Memory.Internals;
14+
1315
#if !SPAN_RUNTIME_SUPPORT
1416
using RuntimeHelpers = Microsoft.Toolkit.HighPerformance.Helpers.Internals.RuntimeHelpers;
1517
#endif
@@ -76,6 +78,32 @@ internal ReadOnlyRefEnumerable(in T reference, int length, int step)
7678
this.span = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(reference), length);
7779
this.step = step;
7880
}
81+
82+
/// <summary>
83+
/// Creates a new instance of the <see cref="ReadOnlyRefEnumerable{T}"/> struct with the specified parameters.
84+
/// </summary>
85+
/// <param name="value">The reference to the first <typeparamref name="T"/> item to map.</param>
86+
/// <param name="length">The number of items in the sequence.</param>
87+
/// <param name="step">The distance between items in the sequence to enumerate.</param>
88+
/// <returns>A <see cref="ReadOnlyRefEnumerable{T}"/> instance with the specified parameters.</returns>
89+
/// <exception cref="ArgumentOutOfRangeException">Thrown when one of the parameters are negative.</exception>
90+
[Pure]
91+
public static ReadOnlyRefEnumerable<T> DangerousCreate(in T value, int length, int step)
92+
{
93+
if (length < 0)
94+
{
95+
ThrowArgumentOutOfRangeExceptionForLength();
96+
}
97+
98+
if (step < 0)
99+
{
100+
ThrowArgumentOutOfRangeExceptionForStep();
101+
}
102+
103+
OverflowHelper.EnsureIsInNativeIntRange(length, 1, step);
104+
105+
return new ReadOnlyRefEnumerable<T>(in value, length, step);
106+
}
79107
#else
80108
/// <summary>
81109
/// Initializes a new instance of the <see cref="ReadOnlyRefEnumerable{T}"/> struct.
@@ -360,6 +388,22 @@ public readonly ref readonly T Current
360388
}
361389
}
362390

391+
/// <summary>
392+
/// Throws an <see cref="ArgumentOutOfRangeException"/> when the "length" parameter is invalid.
393+
/// </summary>
394+
private static void ThrowArgumentOutOfRangeExceptionForLength()
395+
{
396+
throw new ArgumentOutOfRangeException("length");
397+
}
398+
399+
/// <summary>
400+
/// Throws an <see cref="ArgumentOutOfRangeException"/> when the "step" parameter is invalid.
401+
/// </summary>
402+
private static void ThrowArgumentOutOfRangeExceptionForStep()
403+
{
404+
throw new ArgumentOutOfRangeException("step");
405+
}
406+
363407
/// <summary>
364408
/// Throws an <see cref="ArgumentException"/> when the target span is too short.
365409
/// </summary>

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#endif
1111
using Microsoft.Toolkit.HighPerformance.Extensions;
1212
using Microsoft.Toolkit.HighPerformance.Helpers.Internals;
13-
#if !SPAN_RUNTIME_SUPPORT
13+
#if SPAN_RUNTIME_SUPPORT
14+
using Microsoft.Toolkit.HighPerformance.Memory.Internals;
15+
#else
1416
using RuntimeHelpers = Microsoft.Toolkit.HighPerformance.Helpers.Internals.RuntimeHelpers;
1517
#endif
1618

@@ -64,6 +66,32 @@ internal RefEnumerable(ref T reference, int length, int step)
6466
Span = MemoryMarshal.CreateSpan(ref reference, length);
6567
Step = step;
6668
}
69+
70+
/// <summary>
71+
/// Creates a new instance of the <see cref="RefEnumerable{T}"/> struct with the specified parameters.
72+
/// </summary>
73+
/// <param name="value">The reference to the first <typeparamref name="T"/> item to map.</param>
74+
/// <param name="length">The number of items in the sequence.</param>
75+
/// <param name="step">The distance between items in the sequence to enumerate.</param>
76+
/// <returns>A <see cref="RefEnumerable{T}"/> instance with the specified parameters.</returns>
77+
/// <exception cref="ArgumentOutOfRangeException">Thrown when one of the parameters are negative.</exception>
78+
[Pure]
79+
public static RefEnumerable<T> DangerousCreate(ref T value, int length, int step)
80+
{
81+
if (length < 0)
82+
{
83+
ThrowArgumentOutOfRangeExceptionForLength();
84+
}
85+
86+
if (step < 0)
87+
{
88+
ThrowArgumentOutOfRangeExceptionForStep();
89+
}
90+
91+
OverflowHelper.EnsureIsInNativeIntRange(length, 1, step);
92+
93+
return new RefEnumerable<T>(ref value, length, step);
94+
}
6795
#else
6896
/// <summary>
6997
/// Initializes a new instance of the <see cref="RefEnumerable{T}"/> struct.
@@ -453,6 +481,22 @@ public readonly ref T Current
453481
}
454482
}
455483

484+
/// <summary>
485+
/// Throws an <see cref="ArgumentOutOfRangeException"/> when the "length" parameter is invalid.
486+
/// </summary>
487+
private static void ThrowArgumentOutOfRangeExceptionForLength()
488+
{
489+
throw new ArgumentOutOfRangeException("length");
490+
}
491+
492+
/// <summary>
493+
/// Throws an <see cref="ArgumentOutOfRangeException"/> when the "step" parameter is invalid.
494+
/// </summary>
495+
private static void ThrowArgumentOutOfRangeExceptionForStep()
496+
{
497+
throw new ArgumentOutOfRangeException("step");
498+
}
499+
456500
/// <summary>
457501
/// Throws an <see cref="ArgumentException"/> when the target span is too short.
458502
/// </summary>

Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.1D.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public static int Count<T>(this T[] array, T value)
146146
[MethodImpl(MethodImplOptions.AggressiveInlining)]
147147
public static SpanEnumerable<T> Enumerate<T>(this T[] array)
148148
{
149-
return new SpanEnumerable<T>(array);
149+
return new(array);
150150
}
151151

152152
/// <summary>
@@ -172,7 +172,7 @@ public static SpanEnumerable<T> Enumerate<T>(this T[] array)
172172
public static SpanTokenizer<T> Tokenize<T>(this T[] array, T separator)
173173
where T : IEquatable<T>
174174
{
175-
return new SpanTokenizer<T>(array, separator);
175+
return new(array, separator);
176176
}
177177

178178
/// <summary>

Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.2D.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public static RefEnumerable<T> GetColumn<T>(this T[,] array, int column)
207207
[MethodImpl(MethodImplOptions.AggressiveInlining)]
208208
public static Span2D<T> AsSpan2D<T>(this T[,]? array)
209209
{
210-
return new Span2D<T>(array);
210+
return new(array);
211211
}
212212

213213
/// <summary>
@@ -231,7 +231,7 @@ public static Span2D<T> AsSpan2D<T>(this T[,]? array)
231231
[MethodImpl(MethodImplOptions.AggressiveInlining)]
232232
public static Span2D<T> AsSpan2D<T>(this T[,]? array, int row, int column, int height, int width)
233233
{
234-
return new Span2D<T>(array, row, column, height, width);
234+
return new(array, row, column, height, width);
235235
}
236236

237237
/// <summary>
@@ -244,7 +244,7 @@ public static Span2D<T> AsSpan2D<T>(this T[,]? array, int row, int column, int h
244244
[MethodImpl(MethodImplOptions.AggressiveInlining)]
245245
public static Memory2D<T> AsMemory2D<T>(this T[,]? array)
246246
{
247-
return new Memory2D<T>(array);
247+
return new(array);
248248
}
249249

250250
/// <summary>
@@ -268,7 +268,7 @@ public static Memory2D<T> AsMemory2D<T>(this T[,]? array)
268268
[MethodImpl(MethodImplOptions.AggressiveInlining)]
269269
public static Memory2D<T> AsMemory2D<T>(this T[,]? array, int row, int column, int height, int width)
270270
{
271-
return new Memory2D<T>(array, row, column, height, width);
271+
return new(array, row, column, height, width);
272272
}
273273

274274
#if SPAN_RUNTIME_SUPPORT

Microsoft.Toolkit.HighPerformance/Extensions/ArrayExtensions.3D.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public static Memory<T> AsMemory<T>(this T[,,] array, int depth)
234234
[MethodImpl(MethodImplOptions.AggressiveInlining)]
235235
public static Span2D<T> AsSpan2D<T>(this T[,,] array, int depth)
236236
{
237-
return new Span2D<T>(array, depth);
237+
return new(array, depth);
238238
}
239239

240240
/// <summary>
@@ -252,7 +252,7 @@ public static Span2D<T> AsSpan2D<T>(this T[,,] array, int depth)
252252
[MethodImpl(MethodImplOptions.AggressiveInlining)]
253253
public static Memory2D<T> AsMemory2D<T>(this T[,,] array, int depth)
254254
{
255-
return new Memory2D<T>(array, depth);
255+
return new(array, depth);
256256
}
257257

258258
/// <summary>

Microsoft.Toolkit.HighPerformance/Extensions/MemoryExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static class MemoryExtensions
3535
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3636
public static Memory2D<T> AsMemory2D<T>(this Memory<T> memory, int height, int width)
3737
{
38-
return new Memory2D<T>(memory, height, width);
38+
return new(memory, height, width);
3939
}
4040

4141
/// <summary>
@@ -58,7 +58,7 @@ public static Memory2D<T> AsMemory2D<T>(this Memory<T> memory, int height, int w
5858
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5959
public static Memory2D<T> AsMemory2D<T>(this Memory<T> memory, int offset, int height, int width, int pitch)
6060
{
61-
return new Memory2D<T>(memory, offset, height, width, pitch);
61+
return new(memory, offset, height, width, pitch);
6262
}
6363
#endif
6464

0 commit comments

Comments
 (0)