Skip to content

Commit

Permalink
Use .NET functions for Array.Copy and Set
Browse files Browse the repository at this point in the history
  • Loading branch information
hyazinthh committed Jun 17, 2024
1 parent 0537771 commit e699169
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions src/Aardvark.Base/Extensions/ArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ public static class ArrayFun
/// <returns>this</returns>
public static T[] Set<T>(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;
}

Expand Down Expand Up @@ -144,18 +148,18 @@ public static T[] Set<T>(this T[] self, T[] array)
return self;
}

#endregion
#endregion

#region Generic Array Copying

/// <summary>
/// Use this instead of Clone() in order to get a typed array back.
/// Creates a copy of the array.
/// </summary>
public static T[] Copy<T>(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;
}

Expand All @@ -167,7 +171,7 @@ public static T[] Copy<T>(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;
}

Expand All @@ -179,7 +183,7 @@ public static T[] Copy<T>(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;
}

Expand All @@ -191,7 +195,7 @@ public static T[] Copy<T>(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;
}

Expand All @@ -203,7 +207,7 @@ public static T[] Copy<T>(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;
}

Expand Down Expand Up @@ -383,37 +387,25 @@ public static Tr[] Map<T, Tr>(
/// Copy a range of elements to the target array.
/// </summary>
public static void CopyTo<T>(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);

/// <summary>
/// Copy a range of elements to the target array.
/// </summary>
public static void CopyTo<T>(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);

/// <summary>
/// Copy a range of elements to the target array.
/// </summary>
public static void CopyTo<T>(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);

/// <summary>
/// Copy a range of elements to the target array.
/// </summary>
public static void CopyTo<T>(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);

/// <summary>
/// Copies the array into a list.
Expand Down Expand Up @@ -2346,7 +2338,7 @@ public static Span<byte> 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<byte> AsByteSpan<T>(this T[] data) where T : struct
Expand Down

0 comments on commit e699169

Please sign in to comment.