diff --git a/BeefLibs/corlib/src/Array.bf b/BeefLibs/corlib/src/Array.bf index 89c44b7e..d6e8b8cf 100644 --- a/BeefLibs/corlib/src/Array.bf +++ b/BeefLibs/corlib/src/Array.bf @@ -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 this[IndexRange range] { #if !DEBUG diff --git a/BeefLibs/corlib/src/Collections/List.bf b/BeefLibs/corlib/src/Collections/List.bf index 9bbb7e7f..a53a3a20 100644 --- a/BeefLibs/corlib/src/Collections/List.bf +++ b/BeefLibs/corlib/src/Collections/List.bf @@ -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 this[IndexRange range] { #if !DEBUG diff --git a/BeefLibs/corlib/src/Span.bf b/BeefLibs/corlib/src/Span.bf index f4c971df..88e70617 100644 --- a/BeefLibs/corlib/src/Span.bf +++ b/BeefLibs/corlib/src/Span.bf @@ -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 this[IndexRange range] { #if !DEBUG