1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 12:32:20 +02:00

System.Index support

This commit is contained in:
Brian Fiete 2021-10-24 08:24:53 -07:00
parent 27fd5552cc
commit 05a65287d3
3 changed files with 124 additions and 1 deletions

View file

@ -260,6 +260,35 @@ namespace System
}
}
public ref T this[Index index]
{
[Checked, Inline]
get
{
int idx;
switch (index)
{
case .FromFront(let offset): idx = offset;
case .FromEnd(let offset): idx = mLength - 1 - offset;
}
if ((uint)idx >= (uint)mLength)
Internal.ThrowIndexOutOfRange(1);
return ref (&mFirstElement)[idx];
}
[Unchecked, Inline]
get
{
int idx;
switch (index)
{
case .FromFront(let offset): idx = offset;
case .FromEnd(let offset): idx = mLength - 1 - offset;
}
return ref (&mFirstElement)[idx];
}
}
public Span<T> this[IndexRange range]
{
#if !DEBUG

View file

@ -211,6 +211,65 @@ namespace System.Collections
}
}
public ref T this[Index index]
{
[Checked]
get
{
int idx;
switch (index)
{
case .FromFront(let offset): idx = offset;
case .FromEnd(let offset): idx = mSize - 1 - offset;
}
Runtime.Assert((uint)idx < (uint)mSize);
return ref mItems[idx];
}
[Unchecked, Inline]
get
{
int idx;
switch (index)
{
case .FromFront(let offset): idx = offset;
case .FromEnd(let offset): idx = mSize - 1 - offset;
}
return ref mItems[idx];
}
[Checked]
set
{
int idx;
switch (index)
{
case .FromFront(let offset): idx = offset;
case .FromEnd(let offset): idx = mSize - 1 - offset;
}
Runtime.Assert((uint)idx < (uint)mSize);
mItems[idx] = value;
#if VERSION_LIST
mVersion++;
#endif
}
[Unchecked, Inline]
set
{
int idx;
switch (index)
{
case .FromFront(let offset): idx = offset;
case .FromEnd(let offset): idx = mSize - 1 - offset;
}
mItems[idx] = value;
#if VERSION_LIST
mVersion++;
#endif
}
}
public Span<T> this[IndexRange range]
{
#if !DEBUG

View file

@ -127,13 +127,48 @@ namespace System
public ref T this[int index]
{
[Inline]
[Checked]
get
{
Runtime.Assert((uint)index < (uint)mLength);
return ref mPtr[index];
}
[Unchecked, Inline]
get
{
return ref mPtr[index];
}
}
public ref T this[Index index]
{
[Checked]
get
{
int idx;
switch (index)
{
case .FromFront(let offset): idx = offset;
case .FromEnd(let offset): idx = mLength - 1 - offset;
}
Runtime.Assert((uint)idx < (uint)mLength);
return ref mPtr[idx];
}
[Unchecked, Inline]
get
{
int idx;
switch (index)
{
case .FromFront(let offset): idx = offset;
case .FromEnd(let offset): idx = mLength - 1 - offset;
}
return ref mPtr[idx];
}
}
public Span<T> this[IndexRange range]
{
#if !DEBUG