Skip to content

Commit

Permalink
Fix untyped Array.Copy() for multidimensional arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
hyazinthh committed Jun 18, 2024
1 parent 4a41a0c commit b532616
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/Aardvark.Base/Extensions/NonGenericArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public static class NonGenericArrayExtensions
{
#region Non-Generic Array

[Obsolete]
private static readonly Dictionary<Type, Func<Array, Array>> CopyFunMap =
new Dictionary<Type, Func<Array, Array>>
{
Expand Down Expand Up @@ -44,12 +45,25 @@ public static class NonGenericArrayExtensions
{ typeof(V4d[]), a => ((V4d[])a).Copy() },
};

public static Array Copy(this Array array)
{
return CopyFunMap[array.GetType()](array);
}
/// <summary>
/// Creates a copy of the array.
/// </summary>
public static Array Copy(this Array array)
{
var counts = new long[array.Rank];

for (int i = 0; i < counts.Length; i++)
{
counts[i] = array.GetLongLength(i);
}

var result = Array.CreateInstance(array.GetType().GetElementType(), counts);
Array.Copy(array, result, array.LongLength);
return result;
}

private static readonly Dictionary<Type, Func<Array, object, Array>> CopyFunFunMap =
[Obsolete]
private static readonly Dictionary<Type, Func<Array, object, Array>> CopyFunFunMap =
new Dictionary<Type, Func<Array, object, Array>>
{
{ typeof(byte[]), (a, f) => ((byte[])a).Map((Func<byte,byte>)f) },
Expand Down Expand Up @@ -86,7 +100,8 @@ public static Array Copy(this Array array)
{ typeof(V4d[]), (a, f) => ((V4d[])a).Map((Func<V4d,V4d>)f) },
};

public static Array Copy<T>(this Array array,
[Obsolete("Anybody using this?")]
public static Array Copy<T>(this Array array,
Func<T, T> funOfElementTypeToElementType,
Func<Array, Array> defaultFun = null)
{
Expand Down

0 comments on commit b532616

Please sign in to comment.