Burtsev-Alexey / net-object-deep-copy

C# extension method for fast object cloning.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Blows up on multi-dimensional arrays

PaulDolphin1 opened this issue · comments

There is probably a better way of doing this but I changed the code to the following to cope with 2 and 3 dimensional arrays:

Array clonedArray = (Array)clonedFieldValue;
if (clonedArray.Rank == 1)
{
for (long i = 0; i < clonedArray.LongLength; i++)
clonedArray.SetValue(InternalCopy(clonedArray.GetValue(i), visited), i);
}
else if (clonedArray.Rank == 2)
{
for (long i = 0; i < clonedArray.GetLongLength(0); i++)
{
for (long j = 0; j < clonedArray.GetLongLength(1); j++)
{
clonedArray.SetValue(InternalCopy(clonedArray.GetValue(i, j), visited), i, j);
}
}
}
else if (clonedArray.Rank == 3)
{
for (long i = 0; i < clonedArray.GetLongLength(0); i++)
{
for (long j = 0; j < clonedArray.GetLongLength(1); j++)
{
for (long k = 0; k < clonedArray.GetLongLength(2); k++)
{
clonedArray.SetValue(InternalCopy(clonedArray.GetValue(i, j, k), visited), i, j, k);
}
}
}
}
else
{
throw new Exception(string.Format("Array has too many dimensions ({0})", clonedArray.Rank));
}

Yeah, indeed I forgot about multidimensional arrays. Thanks I will fix this.

Excellent.

Great bit of code by the way – very useful.

From: Burtsev Alexey [mailto:notifications@github.com]
Sent: 29 March 2013 10:03
To: Burtsev-Alexey/net-object-deep-copy
Cc: PaulDolphin1
Subject: Re: [net-object-deep-copy] Blows up on multi-dimensional arrays (#2)

Yeah, indeed I forgot about multidimensional arrays. Thanks I will fix this.


Reply to this email directly or view it on GitHub #2 (comment) . https://github.com/notifications/beacon/Ubda7bzUiPEPNKjkMK70HS0ObX_SM3i0Z6EFhPP8Vh6hSFgYVGt3BkDJLxbjpJ18.gif

Great, thanks

Fails on empty arrays (e.g. List x=new List();)
Changed as follows:

if(clonedArray.Length>0)
clonedArray.ForEach((array, indices) => array.SetValue(InternalCopy(clonedArray.GetValue(indices), visited), indices));

Will check it out