Skip to content

Instantly share code, notes, and snippets.

@FaustVX
Created March 8, 2023 20:36
Show Gist options
  • Save FaustVX/45b720a8e1ed3c963da54f09309d31fa to your computer and use it in GitHub Desktop.
Save FaustVX/45b720a8e1ed3c963da54f09309d31fa to your computer and use it in GitHub Desktop.
Movable Window on Span
#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;
}
}
#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