|
|
|
@ -15,17 +15,17 @@ public readonly struct Buffer2DRegion<T> |
|
|
|
/// Initializes a new instance of the <see cref="Buffer2DRegion{T}"/> struct.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="buffer">The <see cref="Buffer2D{T}"/>.</param>
|
|
|
|
/// <param name="rectangle">The <see cref="Rectangle"/> defining a rectangular area within the buffer.</param>
|
|
|
|
/// <param name="bounds">The <see cref="Bounds"/> defining a rectangular area within the buffer.</param>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
public Buffer2DRegion(Buffer2D<T> buffer, Rectangle rectangle) |
|
|
|
public Buffer2DRegion(Buffer2D<T> buffer, Rectangle bounds) |
|
|
|
{ |
|
|
|
DebugGuard.MustBeGreaterThanOrEqualTo(rectangle.X, 0, nameof(rectangle)); |
|
|
|
DebugGuard.MustBeGreaterThanOrEqualTo(rectangle.Y, 0, nameof(rectangle)); |
|
|
|
DebugGuard.MustBeLessThanOrEqualTo(rectangle.Width, buffer.Width, nameof(rectangle)); |
|
|
|
DebugGuard.MustBeLessThanOrEqualTo(rectangle.Height, buffer.Height, nameof(rectangle)); |
|
|
|
DebugGuard.MustBeGreaterThanOrEqualTo(bounds.X, 0, nameof(bounds)); |
|
|
|
DebugGuard.MustBeGreaterThanOrEqualTo(bounds.Y, 0, nameof(bounds)); |
|
|
|
DebugGuard.MustBeLessThanOrEqualTo(bounds.Width, buffer.Width, nameof(bounds)); |
|
|
|
DebugGuard.MustBeLessThanOrEqualTo(bounds.Height, buffer.Height, nameof(bounds)); |
|
|
|
|
|
|
|
this.Buffer = buffer; |
|
|
|
this.Rectangle = rectangle; |
|
|
|
this.Bounds = bounds; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -34,15 +34,10 @@ public readonly struct Buffer2DRegion<T> |
|
|
|
/// <param name="buffer">The <see cref="Buffer2D{T}"/>.</param>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
public Buffer2DRegion(Buffer2D<T> buffer) |
|
|
|
: this(buffer, buffer.FullRectangle()) |
|
|
|
: this(buffer, buffer.Bounds) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the rectangle specifying the boundaries of the area in <see cref="Buffer"/>.
|
|
|
|
/// </summary>
|
|
|
|
public Rectangle Rectangle { get; } |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the <see cref="Buffer2D{T}"/> being pointed by this instance.
|
|
|
|
/// </summary>
|
|
|
|
@ -51,12 +46,12 @@ public readonly struct Buffer2DRegion<T> |
|
|
|
/// <summary>
|
|
|
|
/// Gets the width
|
|
|
|
/// </summary>
|
|
|
|
public int Width => this.Rectangle.Width; |
|
|
|
public int Width => this.Bounds.Width; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the height
|
|
|
|
/// </summary>
|
|
|
|
public int Height => this.Rectangle.Height; |
|
|
|
public int Height => this.Bounds.Height; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the number of elements between row starts in <see cref="Buffer"/>.
|
|
|
|
@ -66,12 +61,17 @@ public readonly struct Buffer2DRegion<T> |
|
|
|
/// <summary>
|
|
|
|
/// Gets the size of the area.
|
|
|
|
/// </summary>
|
|
|
|
internal Size Size => this.Rectangle.Size; |
|
|
|
public Size Size => this.Bounds.Size; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the rectangle specifying the boundaries of the area in <see cref="Buffer"/>.
|
|
|
|
/// </summary>
|
|
|
|
public Rectangle Bounds { get; } |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a value indicating whether the area refers to the entire <see cref="Buffer"/>
|
|
|
|
/// </summary>
|
|
|
|
internal bool IsFullBufferArea => this.Size == this.Buffer.Size(); |
|
|
|
internal bool IsFullBufferArea => this.Size == this.Buffer.Size; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a value at the given index.
|
|
|
|
@ -79,7 +79,7 @@ public readonly struct Buffer2DRegion<T> |
|
|
|
/// <param name="x">The position inside a row</param>
|
|
|
|
/// <param name="y">The row index</param>
|
|
|
|
/// <returns>The reference to the value</returns>
|
|
|
|
internal ref T this[int x, int y] => ref this.Buffer[x + this.Rectangle.X, y + this.Rectangle.Y]; |
|
|
|
internal ref T this[int x, int y] => ref this.Buffer[x + this.Bounds.X, y + this.Bounds.Y]; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a span to row 'y' inside this area.
|
|
|
|
@ -89,9 +89,9 @@ public readonly struct Buffer2DRegion<T> |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
public Span<T> DangerousGetRowSpan(int y) |
|
|
|
{ |
|
|
|
int yy = this.Rectangle.Y + y; |
|
|
|
int xx = this.Rectangle.X; |
|
|
|
int width = this.Rectangle.Width; |
|
|
|
int yy = this.Bounds.Y + y; |
|
|
|
int xx = this.Bounds.X; |
|
|
|
int width = this.Bounds.Width; |
|
|
|
|
|
|
|
return this.Buffer.DangerousGetRowSpan(yy).Slice(xx, width); |
|
|
|
} |
|
|
|
@ -114,16 +114,16 @@ public readonly struct Buffer2DRegion<T> |
|
|
|
/// <summary>
|
|
|
|
/// Returns a subregion as <see cref="Buffer2DRegion{T}"/>. (Similar to <see cref="Span{T}.Slice(int, int)"/>.)
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="rectangle">The <see cref="Rectangle"/> specifying the boundaries of the subregion</param>
|
|
|
|
/// <param name="rectangle">The <see cref="Bounds"/> specifying the boundaries of the subregion</param>
|
|
|
|
/// <returns>The subregion</returns>
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
public Buffer2DRegion<T> GetSubRegion(Rectangle rectangle) |
|
|
|
{ |
|
|
|
DebugGuard.MustBeLessThanOrEqualTo(rectangle.Width, this.Rectangle.Width, nameof(rectangle)); |
|
|
|
DebugGuard.MustBeLessThanOrEqualTo(rectangle.Height, this.Rectangle.Height, nameof(rectangle)); |
|
|
|
DebugGuard.MustBeLessThanOrEqualTo(rectangle.Width, this.Bounds.Width, nameof(rectangle)); |
|
|
|
DebugGuard.MustBeLessThanOrEqualTo(rectangle.Height, this.Bounds.Height, nameof(rectangle)); |
|
|
|
|
|
|
|
int x = this.Rectangle.X + rectangle.X; |
|
|
|
int y = this.Rectangle.Y + rectangle.Y; |
|
|
|
int x = this.Bounds.X + rectangle.X; |
|
|
|
int y = this.Bounds.Y + rectangle.Y; |
|
|
|
rectangle = new Rectangle(x, y, rectangle.Width, rectangle.Height); |
|
|
|
return new Buffer2DRegion<T>(this.Buffer, rectangle); |
|
|
|
} |
|
|
|
@ -135,8 +135,8 @@ public readonly struct Buffer2DRegion<T> |
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
|
|
internal ref T GetReferenceToOrigin() |
|
|
|
{ |
|
|
|
int y = this.Rectangle.Y; |
|
|
|
int x = this.Rectangle.X; |
|
|
|
int y = this.Bounds.Y; |
|
|
|
int x = this.Bounds.X; |
|
|
|
return ref this.Buffer.DangerousGetRowSpan(y)[x]; |
|
|
|
} |
|
|
|
|
|
|
|
@ -152,7 +152,7 @@ public readonly struct Buffer2DRegion<T> |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
for (int y = 0; y < this.Rectangle.Height; y++) |
|
|
|
for (int y = 0; y < this.Bounds.Height; y++) |
|
|
|
{ |
|
|
|
Span<T> row = this.DangerousGetRowSpan(y); |
|
|
|
row.Clear(); |
|
|
|
@ -172,7 +172,7 @@ public readonly struct Buffer2DRegion<T> |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
for (int y = 0; y < this.Rectangle.Height; y++) |
|
|
|
for (int y = 0; y < this.Bounds.Height; y++) |
|
|
|
{ |
|
|
|
Span<T> row = this.DangerousGetRowSpan(y); |
|
|
|
row.Fill(value); |
|
|
|
|