Alternative managed wrapper around flecs-cs bindings for using the ECS framework Flecs in modern .NET.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.2 KiB

using System;
namespace gaemstone.Utility;
public static class SpanExtensions
{
public static T? GetOrNull<T>(this Span<T> span, int index)
where T : struct => GetOrNull((ReadOnlySpan<T>)span, index);
public static T? GetOrNull<T>(this ReadOnlySpan<T> span, int index)
where T : struct => (index >= 0 && index < span.Length) ? span[index] : null;
public static ReadOnlySpanSplitEnumerator<T> Split<T>(this ReadOnlySpan<T> span, T splitOn)
where T : IEquatable<T> => new(span, splitOn);
public ref struct ReadOnlySpanSplitEnumerator<T>
where T : IEquatable<T>
{
private readonly ReadOnlySpan<T> _span;
private readonly T _splitOn;
private int _index = -1;
private int _end = -1;
public ReadOnlySpanSplitEnumerator(ReadOnlySpan<T> span, T splitOn)
{ _span = span; _splitOn = splitOn; }
public ReadOnlySpanSplitEnumerator<T> GetEnumerator() => this;
public ReadOnlySpan<T> Current
=> (_end >= 0) && (_end <= _span.Length)
? _span[_index.._end] : throw new InvalidOperationException();
public bool MoveNext()
{
if (_end == _span.Length) return false;
_end++; _index = _end;
while (_end < _span.Length && !_span[_end].Equals(_splitOn)) _end++;
return true;
}
}
}