How to clone multidimensional array without reference correctly? - c#

I have 3-dimensional array in my programm. I want to debug values in it and I put this array into another array, in which I want to find difference.
I tried this:
Weights weights = new Weights(); // init inside
List<Weights> weightsDebug = new List<Weigts>();
weightsDebug.Add(weights); // I'd put first weigths into the list
for(int i = 0; i < 100; i++) {
// do some bad things with weights;
weightsDebug.Add(weights);
}
After that, I'd got same values in weights in all 99 elements of weigthsDebug. I'd tried to debug and I'd realised, that weigts array is being changed. I understood that problem is in the reference (copy by link, not by value) – all pushed to weightsDebug array elements are linked with weights in main loop.
I'd googled a bit and found some ways how to copy 1-dimensional array. I'tried next:
I added Clone method to my Weights class:
double[][][] values;
public Weights Clone() {
Weights clone = new Weights();
clone.values = (double[][][]) this.values.Clone();
return clone;
}
public Weights()
{
Console.WriteLine("Constructor WITHOUT arguments fired");
}
Now I am doing so when copy weights:
Weights weights = new Weights(); // init inside
List<Weights> weightsDebug = new List<Weigts>();
weightsDebug.Add(weights); // I'd put first weigths into the list
for(int i = 0; i < 100; i++) {
// do some bad things with weights;
Weights weightsClone = weights.Clone();
weightsDebug.Add(weightsClone);
}
And I am still getting last changed value in whole debug array. How I can correct it?

What you really are looking for here is deep cloning rather than the current shallow cloning that you are implementing.
A long time ago, we realized that by making the classes Serializable, we could use the .Net serialization classes to quickly, easily, and correctly make deep clones of any object.
Here is a method that we use for this deep cloning (there is also a SilverLight variation that uses DataContractSerializer):
/// <summary>
/// This method clones all of the items and serializable properties of the current collection by
/// serializing the current object to memory, then deserializing it as a new object. This will
/// ensure that all references are cleaned up.
/// </summary>
/// <returns></returns>
/// <remarks></remarks>
public T CreateSerializedCopy<T>(T oRecordToCopy)
{
// Exceptions are handled by the caller
if (oRecordToCopy == null)
{
return default(T);
}
if (!oRecordToCopy.GetType().IsSerializable)
{
throw new ArgumentException(oRecordToCopy.GetType().ToString() + " is not serializable");
}
var oFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
using (var oStream = new MemoryStream())
{
oFormatter.Serialize(oStream, oRecordToCopy);
oStream.Position = 0;
return (T)oFormatter.Deserialize(oStream);
}
}
To use this, first ensure the Weights class is tagged with the Serializable attribute:
[Serializable]
public class Weights
Then you can clone as needed:
var weights = new List<Weights>();
weights.Add(new Weights());
var debugWeights = CreateSerializedCopy<List<Weights>>(weights);
if (debugWeights[0] == weights[0])
{
System.Diagnostics.Debug.WriteLine("Did not serialize correctly");
}

The problem here is that you don't have a true multi-dimensional array, where there's just a single object. You have a "jagged" multi-dimensional array, where the object is actually an array of arrays of arrays. This means that to clone the object, you need to do more work:
clone.values = new double[values.Length][][];
for (int i = 0; i < values.Length; i++)
{
clone.values[i] = new double[values[i].Length][];
for (int j = 0; j < values[i].Length; j++)
{
clone.values[i][j] = (double[])values[i][j].Clone();
}
}

Related

Issue with dynamically creating jagged array in C#

I am trying to store each iteration of an array as it is being sorted through. For some reason the jagged array I return just becomes an array of the final sorted array. My code is below I cannot figure out why any help would be really appreciated. Thanks.
namespace SortingUI.Utils
{
public class Sorting
{
public static int[][] SortInt(int[] originalArray)
{
int length = originalArray.Length;
int tempVal, smallest;
int[][] iterations = new int[length-1][];
for (int i = 0; i < length - 1; i++)
{
smallest = i;
for (int j = i + 1; j < length; j++)
{
if (originalArray[j] < originalArray[smallest])
{
smallest = j;
}
}
tempVal = originalArray[smallest];
originalArray[smallest] = originalArray[i];
originalArray[i] = tempVal;
iterations[i] = originalArray;
}
return iterations;
}
}
}
If i understand your problem, the reason you are getting the same values (and not a snapshot) is because you are storing a reference to the array, and not a copy of the values themselves.
Arrays are reference types, so when you assign them to another variable you are only copying reference and not the contents. If you change the contents, all the references will reflect that
You can make use of Array.Copy to copy the values at the point in time from the originalArray
...
var tempVal = originalArray[smallest];
originalArray[smallest] = originalArray[i];
originalArray[i] = tempVal;
// Create some new memory
iterations[i] = new int[originalArray.Length];
// Copy the contents
Array.Copy(originalArray, iterations[i], originalArray.Length);
Note this is totally untested and i have no idea if the rest of your code is working as intended
Additional Resources
Array.Copy Method
Copies a range of elements in one Array to another Array and performs
type casting and boxing as required.
Arrays (C# Programming Guide)
Array types are reference types derived from the abstract base type Array. Since this type implements IEnumerable and IEnumerable,
you can use foreach iteration on all arrays in C#.

creating a custom list windows phone 8.1

here is the code adding data to the list, after which i return the list. Problem is that the last set of data seems to be overwriting everything else in the list
for (int k=0; k < jsonObject["mydetails"].GetArray().Count; k++)
{
//add the corresponding data from the sample array
obj.accountNumber = jsonObject["mydetails"].GetArray()[k].GetObject()["id"].GetString();
obj.accountName = jsonObject["mydetails"].GetArray()[k].GetObject()["name"].GetString();
obj.accountBall = jsonObject["mydetails"].GetArray()[k].GetObject()["age"].GetString();
mylist.Add(obj);
}
Your code isn't adding any new objects to the list, it modifies and adds the same object. In the end, the list contains X references to the same object instance.
In general it is consider bad practice to declare a variable outside the scope in which it is used. You could have avoided this problem by writing:
var myArray=jsonObject["mydetails"].GetArray();
for (int k=0; k < myArray.Count; k++)
{
var myJsonObject=myArray[k].GetObject();
var obj=new MyAccountObject();
obj.accountNumber = myJsonObject["id"].GetString();
...
}
Notice that instead of calling GetArray() and GetObject() in each line (essentially doing the job of extracting the array multiple times) I stored them in separate variables. This will result in far less CPU and memory usage.
You always add the same object obj to the list. You need to create a new in the top of the loop.
for (int k=0; k < jsonObject["mydetails"].GetArray().Count; k++)
{
obj = new ObjectType(); // you know better the type of the object you want to create.
//add the corresponding data from the sample array
obj.accountNumber = jsonObject["mydetails"].GetArray()[k].GetObject()["id"].GetString();
obj.accountName = jsonObject["mydetails"].GetArray()[k].GetObject()["name"].GetString();
obj.accountBall = jsonObject["mydetails"].GetArray()[k].GetObject()["age"].GetString();
mylist.Add(obj);
}
Without the the line obj = new ... you change the properties accountNumber, accountName and accountBall of the same object instance. At the end you add always that object reference to the list. So it seems that the last run of the loop changes all objects.

How is it possible that a handMade dictionary is way faster than .Net dictionary?

Reviewing one opensource project, I've come across one interesting Data structure:
// Represents a layer of "something" that covers the map
public class CellLayer<T> : IEnumerable<T>
{
public readonly Size Size;
public readonly TileShape Shape;
public event Action<CPos> CellEntryChanged = null;
readonly T[] entries;
public CellLayer(Map map)
: this(map.TileShape, new Size(map.MapSize.X, map.MapSize.Y)) { }
public CellLayer(TileShape shape, Size size)
{
Size = size;
Shape = shape;
entries = new T[size.Width * size.Height];
}
public void CopyValuesFrom(CellLayer<T> anotherLayer)
{
if (Size != anotherLayer.Size || Shape != anotherLayer.Shape)
throw new ArgumentException(
"layers must have a matching size and shape.", "anotherLayer");
if (CellEntryChanged != null)
throw new InvalidOperationException(
"Cannot copy values when there are listeners attached to the CellEntryChanged event.");
Array.Copy(anotherLayer.entries, entries, entries.Length);
}
// Resolve an array index from cell coordinates
int Index(CPos cell)
{
return Index(cell.ToMPos(Shape));
}
// Resolve an array index from map coordinates
int Index(MPos uv)
{
return uv.V * Size.Width + uv.U;
}
/// <summary>Gets or sets the <see cref="OpenRA.CellLayer"/> using cell coordinates</summary>
public T this[CPos cell]
{
get
{
return entries[Index(cell)];
}
set
{
entries[Index(cell)] = value;
if (CellEntryChanged != null)
CellEntryChanged(cell);
}
}
/// <summary>Gets or sets the layer contents using raw map coordinates (not CPos!)</summary>
public T this[MPos uv]
{
get
{
return entries[Index(uv)];
}
set
{
entries[Index(uv)] = value;
if (CellEntryChanged != null)
CellEntryChanged(uv.ToCPos(Shape));
}
}
/// <summary>Clears the layer contents with a known value</summary>
public void Clear(T clearValue)
{
for (var i = 0; i < entries.Length; i++)
entries[i] = clearValue;
}
public IEnumerator<T> GetEnumerator()
{
return (IEnumerator<T>)entries.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
This structure represents a matrix of type T in that, given a CPos (an X,Y) structure, it returns the T element at that position. Here's one sample usage:
var dic = new CellLayer<CellInfo>(TileShape.Rectangle, new Size(1280,1280));
cellLayer[new CPos(0, 1)] = new CellInfo(0, new CPos(0, 1), false);
Internally, the CellLayer class transforms the given CPos into a int which operates as the index for the internal array.
By the looks of how the class operates from a client-side perspective, it felt to me like a Dictionary, so I replaced the implementation. After several runtimes tests and microbenchmarking, it turned out that using the dictionary was dozens of times slower than using the handmade CellLayer class. That surprised me. Here are the tests I did:
[Test]
public void DictionaryTest()
{
var dic = new Dictionary<CPos, CellInfo>(1280 * 1280);
var watch = Stopwatch.StartNew();
for (int i = 0; i < 1280; i++)
for (int u = 0; u < 1280; u++)
dic[new CPos(i, u)] = new CellInfo(0, new CPos(i, u), false);
Console.WriteLine(watch.ElapsedTicks);
}
[Test]
public void CellLayerTest()
{
var dic = new CellLayer<CellInfo>(TileShape.Rectangle, new Size(1280,1280));
var watch = Stopwatch.StartNew();
for (int i = 0; i < 1280; i++)
for (int u = 0; u < 1280; u++)
dic[new CPos(i, u)] = new CellInfo(0, new CPos(i, u), false);
Console.WriteLine(watch.ElapsedTicks);
}
I thought that .NET Collections were as optimized as possible. Can anyone explain to me how is it that using Dictionary is slower that using a "custom Dictionary"?
Thanks
For the original version, you find the location of an entry by using this formula
uv.V * Size.Width + uv.U
To find the location in a dictionary
Calculate the hash code for CPos.
Find the bucket in the dictionary using a modulus operation hashcode % dictionarySize
If the bucket isn't empty, compare the CPos you have with the CPos in that bucket. If they don't match you have a secondary hash code collision. Move to the next bucket and retry step 3.
If you have a primary has code collision, which is to say lots of different CPos values have the same hash code, your dictionary is going to be ridiculously slow.
If you have unique hash codes, then it is probably the modulus operation that is killing performance. But you would need to attach a profiler (e.g. Redgate ANTS) to find out for sure.
A dictionary maintains an search/retrievable set of itmes (either through a hash-table, a binary tree or something similar). So each Add() and each [key] implies some search (which tends to be relatively "slow").
In your case, if there is a simple mapping from a CPos to an integer (aka array index) there is no search but a direct (and fast) access to a cell of an array.
Or, to put it simpler: Essentially you compare a hash table/binary tree against a flat array.
Edit
Of course both collections are rather fast and show O(1) compexity. A lookup in a hash table is more complex than an array index operation however.

C#: Immutable class

I have a class which should be immutable in this class i have only get indexer a private set property so why this is not immutable and i can set some field in array as you could see in main class...
class ImmutableMatice
{
public decimal[,] Array { get; private set; } // immutable Property
public ImmutableMatice(decimal[,] array)
{
Array = array;
}
public decimal this[int index1, int index2]
{
get { return Array[index1, index2]; }
}
........
and in main method if i fill this class with data and change the data
static void Main(string[] args)
{
decimal[,] testData = new[,] {{1m, 2m}, {3m, 4m}};
ImmutableMatice matrix = new ImmutableMatice(testData);
Console.WriteLine(matrix[0,0]); // writes 1
testData[0, 0] = 999;
Console.WriteLine(matrix[0,0]); // writes 999 but i thought it should
// write 1 because class should be immutable?
}
}
Is there any way how to make this class immutable?
Ah yes the solution was copy array to new array in constructor like this:
public ImmutableMatice(decimal[,] array)
{
decimal[,] _array = new decimal[array.GetLength(0),array.GetLength(1)];
//var _array = new decimal[,] { };
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
_array[i, j] = array[i, j];
}
}
Array = _array;
}
That is because you are actually changing the data in the ARRAY, rather than the indexer.
static void Main(string[] args)
{
decimal[,] testData = new[,] {{1m, 2m}, {3m, 4m}};
ImmutableMatice matrix = new ImmutableMatice(testData);
Console.WriteLine(matrix[0,0]); // writes 1
testData[0, 0] = 999; // <--- THATS YOUR PROBLEM
Console.WriteLine(matrix[0,0]); // writes 999 but i thought it should
// write 1 because class should be immutable?
}
You can copy the array into your private property in the constructor to avoid this situation.
Note that you indeed cannot write matrix[0,0] = 999; because the indexer has no setter.
Edit
As Chris pointed out (how could I have missed it myself?) - you shouldn't expose the array as a property at all (which means in most cases it doesn't even have to be a property).
Consider the following code instead:
private decimal[,] _myArray; // That's private stuff - can't go wrong there.
public decimal this[int index1, int index2]
{
// If you only want to allow get data from the array, thats all you ever need
get { return Array[index1, index2]; }
}
Your class is immutable, but the objects inside it aren't.
Having public decimal[,] Array { get; private set; } will only guarantee that you cannot set the property Array to a new instance of Array, but it does not prevent you from accessing the existing object and changing its values (which aren't immutable).
You might want to look into the appropriately named ReadOnlyCollection<T> class.
As #Mike pointed out and I looked past the first time: there's a twist to this because you are accessing the value through the testData object and not through matrix. While the original point still stands, it is more exact to say that the problem you have is that you are changing values in the underlying object which has its reference passed around. You're bypassing the ImmutableMatice object alltogether.
The beforementioned solution of using a ReadOnlyCollection<T> still stands: by creating this read-only wrapper around it, you won't be able to change it anymore afterwards. Howver this is only the case when you actually use it the way its intended: through ImmutableMatice and not through the underlying collection which you still have a reference to.
Another solution that solves this problem is to copy the contents of the original array to another one to "disconnect" it from the array your still have a reference to.
In order to illustrate this, consider the following samples. The first one demonstrates how the underlying reference can still be influenced while the second one shows how it can be solved by copying your values to a new array.
void Main()
{
var arr = new[] { 5 };
var coll = new ReadOnlyCollection<int>(arr);
Console.WriteLine (coll[0]); // 5
arr[0] = 1;
Console.WriteLine (coll[0]); // 1
}
void Main()
{
var arr = new[] { 5 };
var arr2 = new int[] { 0 };
Array.Copy(arr, arr2, arr.Length);
var coll = new ReadOnlyCollection<int>(arr2);
Console.WriteLine (coll[0]); // 5
arr[0] = 1;
Console.WriteLine (coll[0]); // 5
}

Instantiate an array of objects, in simpliest way?

Given a class:
class clsPerson { public int x, y; }
Is there some way to create an array of these classes with each element initialized to a (default) constructed instance, without doing it manually in a for loop like:
clsPerson[] objArr = new clsPerson[1000];
for (int i = 0; i < 1000; ++i)
objArr[i] = new clsPerson();
Can I shorten the declaration and instantiation of an array of N objects?
The constructor must be run for every item in the array in this scenario. Whether or not you use a loop, collection initializers or a helper method every element in the array must be visited.
If you're just looking for a handy syntax though you could use the following
public static T[] CreateArray<T>(int count) where T : new() {
var array = new T[count];
for (var i = 0; i < count; i++) {
array[i] = new T();
}
return array;
}
clsPerson[] objArary = CreateArray<clsPerson>(1000);
You must invoke the constructor for each item. There is no way to allocate an array and invoke your class constructors on the items without constructing each item.
You could shorten it (a tiny bit) from a loop using:
clsPerson[] objArr = Enumerable.Range(0, 1000).Select(i => new clsPerson()).ToArray();
Personally, I'd still allocate the array and loop through it (and/or move it into a helper routine), though, as it's very clear and still fairly simple:
clsPerson[] objArr = new clsPerson[1000];
for (int i=0;i<1000;++i)
clsPerson[i] = new clsPerson();
If it would make sense to do so, you could change class clsPerson to struct Person. structs always have a default value.

Categories

Resources