1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-21 09:27:59 +02:00

Enhanced ranges

This commit is contained in:
Brian Fiete 2021-10-24 08:12:18 -07:00
parent eec2cb5e6c
commit 27fd5552cc
12 changed files with 365 additions and 22 deletions

View file

@ -134,6 +134,53 @@ namespace System
}
}
public Span<T> this[IndexRange range]
{
#if !DEBUG
[Inline]
#endif
get
{
T* start;
switch (range.mStart)
{
case .FromFront(let offset):
Debug.Assert((uint)offset <= (uint)mLength);
start = mPtr + offset;
case .FromEnd(let offset):
Debug.Assert((uint)offset <= (uint)mLength);
start = mPtr + mLength - 1 - offset;
}
T* end;
if (range.mIsClosed)
{
switch (range.mEnd)
{
case .FromFront(let offset):
Debug.Assert((uint)offset < (uint)mLength);
end = mPtr + offset + 1;
case .FromEnd(let offset):
Debug.Assert((uint)offset < (uint)mLength);
end = mPtr + mLength - offset;
}
}
else
{
switch (range.mEnd)
{
case .FromFront(let offset):
Debug.Assert((uint)offset <= (uint)mLength);
end = mPtr + offset;
case .FromEnd(let offset):
Debug.Assert((uint)offset <= (uint)mLength);
end = mPtr + mLength - 1 - offset;
}
}
return .(start, end - start);
}
}
public Span<T> Slice(int index)
{
Debug.Assert((uint)index <= (uint)mLength);