From 8ee09f278f58e8dcd07e08295008333cd63a77b1 Mon Sep 17 00:00:00 2001 From: Damian Day Date: Wed, 27 May 2020 15:36:51 +0100 Subject: [PATCH 1/3] Array CopyTo for spans CopyTo, allows copying memory from an array to a span. --- BeefLibs/corlib/src/Array.bf | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/BeefLibs/corlib/src/Array.bf b/BeefLibs/corlib/src/Array.bf index 53625396..04c16053 100644 --- a/BeefLibs/corlib/src/Array.bf +++ b/BeefLibs/corlib/src/Array.bf @@ -268,6 +268,20 @@ namespace System arrayTo.GetRef(i + dstOffset) = (T2)GetRef(i + srcOffset); } + public void CopyTo(Span destination) + { + Debug.Assert(destination.Length >= mLength); + Internal.MemCpy(destination.Ptr, &GetRef(0), strideof(T) * mLength, alignof(T)); + } + + public void CopyTo(Span destination, int srcOffset, int length) + { + Debug.Assert(length >= 0); + Debug.Assert((uint)srcOffset + (uint)length <= (uint)mLength); + Debug.Assert((uint)length <= (uint)destination.Length); + Internal.MemCpy(destination.Ptr, &GetRef(srcOffset), strideof(T) * length, alignof(T)); + } + public Span.Enumerator GetEnumerator() { return .(this); From dd33da0d7cbe56f9e32b981e4f31d28e78d18261 Mon Sep 17 00:00:00 2001 From: Damian Day Date: Wed, 27 May 2020 17:26:35 +0100 Subject: [PATCH 2/3] Update Array.bf Remove length parameter. --- BeefLibs/corlib/src/Array.bf | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/BeefLibs/corlib/src/Array.bf b/BeefLibs/corlib/src/Array.bf index 04c16053..a6dc0e86 100644 --- a/BeefLibs/corlib/src/Array.bf +++ b/BeefLibs/corlib/src/Array.bf @@ -274,12 +274,12 @@ namespace System Internal.MemCpy(destination.Ptr, &GetRef(0), strideof(T) * mLength, alignof(T)); } - public void CopyTo(Span destination, int srcOffset, int length) + public void CopyTo(Span destination, int srcOffset) { - Debug.Assert(length >= 0); - Debug.Assert((uint)srcOffset + (uint)length <= (uint)mLength); - Debug.Assert((uint)length <= (uint)destination.Length); - Internal.MemCpy(destination.Ptr, &GetRef(srcOffset), strideof(T) * length, alignof(T)); + Debug.Assert(destination.Length >= mLength); + Debug.Assert((uint)srcOffset <= (uint)mLength); + + Internal.MemCpy(destination.Ptr, &GetRef(srcOffset), strideof(T) * (mLength - srcOffset), alignof(T)); } public Span.Enumerator GetEnumerator() From d9c277389e06836515cb6ad5dc681246a7488f11 Mon Sep 17 00:00:00 2001 From: Damian Day Date: Wed, 27 May 2020 23:33:00 +0100 Subject: [PATCH 3/3] Third time lucky This is my test code. var sourceArr = scope int[10] (1, 2, 3, 4, 5, 6, 7, 8, 9, 10); var destArr = scope int[10]; var detSpan = Span(destArr, 0, 5); sourceArr.CopyTo(detSpan, 2); // destArr holds 3, 4, 5 --- BeefLibs/corlib/src/Array.bf | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/BeefLibs/corlib/src/Array.bf b/BeefLibs/corlib/src/Array.bf index a6dc0e86..b4846ec3 100644 --- a/BeefLibs/corlib/src/Array.bf +++ b/BeefLibs/corlib/src/Array.bf @@ -270,16 +270,18 @@ namespace System public void CopyTo(Span destination) { - Debug.Assert(destination.Length >= mLength); + Debug.Assert(destination.[Friend]mPtr != null); + Debug.Assert(destination.[Friend]mLength >= mLength); + Internal.MemCpy(destination.Ptr, &GetRef(0), strideof(T) * mLength, alignof(T)); } public void CopyTo(Span destination, int srcOffset) { - Debug.Assert(destination.Length >= mLength); - Debug.Assert((uint)srcOffset <= (uint)mLength); + Debug.Assert(destination.[Friend]mPtr != null); + Debug.Assert((uint)destination.[Friend]mLength - (uint)srcOffset < (uint)mLength); - Internal.MemCpy(destination.Ptr, &GetRef(srcOffset), strideof(T) * (mLength - srcOffset), alignof(T)); + Internal.MemCpy(destination.Ptr, &GetRef(srcOffset), strideof(T) * (destination.Length - srcOffset), alignof(T)); } public Span.Enumerator GetEnumerator()