From e699169aed4a2def0c1aad10885dda5aefd555de Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 17 Jun 2024 17:18:00 +0200 Subject: [PATCH] Use .NET functions for Array.Copy and Set --- .../Extensions/ArrayExtensions.cs | 40 ++++++++----------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/Aardvark.Base/Extensions/ArrayExtensions.cs b/src/Aardvark.Base/Extensions/ArrayExtensions.cs index 5561a1a7..ea96ba17 100644 --- a/src/Aardvark.Base/Extensions/ArrayExtensions.cs +++ b/src/Aardvark.Base/Extensions/ArrayExtensions.cs @@ -20,8 +20,12 @@ public static class ArrayFun /// this public static T[] Set(this T[] self, T value) { +#if NET6_0_OR_GREATER + Array.Fill(self, value); +#else var len = self.LongLength; for (int i = 0; i < len; i++) self[i] = value; +#endif return self; } @@ -144,18 +148,18 @@ public static T[] Set(this T[] self, T[] array) return self; } - #endregion +#endregion #region Generic Array Copying /// - /// Use this instead of Clone() in order to get a typed array back. + /// Creates a copy of the array. /// public static T[] Copy(this T[] array) { var count = array.LongLength; var result = new T[count]; - for (var i = 0L; i < count; i++) result[i] = array[i]; + Array.Copy(array, result, count); return result; } @@ -167,7 +171,7 @@ public static T[] Copy(this T[] array, long count) { var result = new T[count]; var len = Math.Min(count, array.LongLength); - for (var i = 0L; i < len; i++) result[i] = array[i]; + Array.Copy(array, result, len); return result; } @@ -179,7 +183,7 @@ public static T[] Copy(this T[] array, int count) { var result = new T[count]; var len = Math.Min(count, array.Length); - for (var i = 0; i < len; i++) result[i] = array[i]; + Array.Copy(array, result, len); return result; } @@ -191,7 +195,7 @@ public static T[] Copy(this T[] array, long start, long count) { var result = new T[count]; var len = Math.Min(count, array.LongLength - start); - for (var i = 0L; i < len; i++) result[i] = array[i + start]; + Array.Copy(array, start, result, 0, len); return result; } @@ -203,7 +207,7 @@ public static T[] Copy(this T[] array, int start, int count) { var result = new T[count]; var len = Math.Min(count, array.Length - start); - for (var i = 0; i < len; i++) result[i] = array[i + start]; + Array.Copy(array, start, result, 0, len); return result; } @@ -383,37 +387,25 @@ public static Tr[] Map( /// Copy a range of elements to the target array. /// public static void CopyTo(this T[] array, long count, T[] target, long targetStart) - { - for (var i = 0L; i < count; i++) - target[targetStart + i] = array[i]; - } + => Array.Copy(array, 0, target, targetStart, count); /// /// Copy a range of elements to the target array. /// public static void CopyTo(this T[] array, int count, T[] target, int targetStart) - { - for (var i = 0; i < count; i++) - target[targetStart + i] = array[i]; - } + => Array.Copy(array, 0, target, targetStart, count); /// /// Copy a range of elements to the target array. /// public static void CopyTo(this T[] array, long start, long count, T[] target, long targetStart) - { - for (var i = 0L; i < count; i++) - target[targetStart + i] = array[start + i]; - } + => Array.Copy(array, start, target, targetStart, count); /// /// Copy a range of elements to the target array. /// public static void CopyTo(this T[] array, int start, int count, T[] target, int targetStart) - { - for (var i = 0; i < count; i++) - target[targetStart + i] = array[start + i]; - } + => Array.Copy(array, start, target, targetStart, count); /// /// Copies the array into a list. @@ -2346,7 +2338,7 @@ public static Span AsByteSpan(this Array data) var span = MemoryMarshal.CreateSpan(ref MemoryMarshal.GetArrayDataReference(data), data.Length * elementSize); return span; } -#endif +#endif [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span AsByteSpan(this T[] data) where T : struct