浏览代码

Added additional Span length asserts. Added Reversed to List

Brian Fiete 1 年之前
父节点
当前提交
f0a31a0f55
共有 3 个文件被更改,包括 8 次插入1 次删除
  1. 3 1
      BeefLibs/corlib/src/Collections/List.bf
  2. 1 0
      BeefLibs/corlib/src/Range.bf
  3. 4 0
      BeefLibs/corlib/src/Span.bf

+ 3 - 1
BeefLibs/corlib/src/Collections/List.bf

@@ -318,6 +318,8 @@ namespace System.Collections
 			}
 		}
 
+		public Span<T>.ReverseEnumerator Reversed => Span<T>(mItems, mSize).Reversed;
+
 		protected virtual T* Alloc(int size)
 		{
 			return Internal.AllocRawArrayUnmarked<T>(size);
@@ -418,7 +420,7 @@ namespace System.Collections
 				Add(item);
 		}
 
-		public Span<T> GetRange(int offset)
+		public Span<T> GetRange(int offset = 0)
 		{
 			Debug.Assert((uint)offset <= (uint)mSize);
 			return .(mItems + offset, mSize - offset);

+ 1 - 0
BeefLibs/corlib/src/Range.bf

@@ -444,6 +444,7 @@ namespace System
 		[Inline]
 		public this(int start, int end, bool isClosed=true)
 		{
+			Debug.Assert(end >= start);
 			mStart = .FromFront(start);
 			mEnd = .FromFront(end);
 			mIsClosed = isClosed;

+ 4 - 0
BeefLibs/corlib/src/Span.bf

@@ -23,6 +23,7 @@ namespace System
 			}
 			mPtr = &array.[Friend]GetRef(0);
 			mLength = array.[Friend]mLength;
+			Debug.Assert(mLength >= 0);
 		}
 
 		public this(T[] array, int index)
@@ -35,10 +36,12 @@ namespace System
 			}
 			mPtr = &array[index];
 			mLength = array.[Friend]mLength - index;
+			Debug.Assert(mLength >= 0);
 		}
 
 		public this(T[] array, int index, int length)
 		{
+			Debug.Assert(length >= 0);
 			if (array == null)
 			{
 				Debug.Assert(index == 0 && length == 0);
@@ -54,6 +57,7 @@ namespace System
 
 		public this(T* memory, int length)
 		{
+			Debug.Assert(length >= 0);
 			mPtr = memory;
 			mLength = length;
 		}