Created
March 8, 2023 20:36
-
-
Save FaustVX/45b720a8e1ed3c963da54f09309d31fa to your computer and use it in GitHub Desktop.
Movable Window on Span
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#nullable enable | |
public ref struct ReadOnlyWindow<T> | |
{ | |
private readonly ReadOnlySpan<T> _data; | |
private int _position; | |
private readonly int Length { get; } | |
public ReadOnlySpan<T> View => _data.Slice(_position, Length); | |
public Window(ReadOnlySpan<T> data, int length) | |
: this() | |
{ | |
_data = data; | |
Length = length; | |
} | |
public bool MoveRight() | |
{ | |
if (_position + Length >= _data.Length) | |
return false; | |
_position++; | |
return true; | |
} | |
public bool MoveLeft() | |
{ | |
if (_position <= 0) | |
return false; | |
_position--; | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#nullable enable | |
public ref struct Window<T> | |
{ | |
private readonly Span<T> _data; | |
private int _position; | |
private readonly int Length { get; } | |
public Span<T> View => _data.Slice(_position, Length); | |
public Window(Span<T> data, int length) | |
: this() | |
{ | |
_data = data; | |
Length = length; | |
} | |
public bool MoveRight() | |
{ | |
if (_position + Length >= _data.Length) | |
return false; | |
_position++; | |
return true; | |
} | |
public bool MoveLeft() | |
{ | |
if (_position <= 0) | |
return false; | |
_position--; | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment