-
-
- (626)
-
premium membership
-
Posts
270
|
the fastest method to duplicate an array?
|
I need to make a copy of a 2D array of a class object, for restore it later, like a backup. The class object contains 4 bools, 4 enums, 2 ints and 1 float. I have to optimize the copy. Right now im doing the copy using a "for".
I have tried "Array.Copy(sourceArray, destinationArray, sourceArray.Length);" but it seems that the bool values are not copied (i dont know why, but all the bool's turn true).
Any sugestion to optimize this?. THNX!
Edit: I just found out that im asking for DeepCopy, but i dont know how to do it yet...
Edit2: ...and now i know way the bool became true (becouse it was a clone, so changing the destination i was changin the source too...)
Kaotik puzzle in playtest If you want to play the game online add me to your friend list. Gamrtag Silvermax1975. Thanks twitter, facebook, youtube
|
|
-
|
|
Re: the fastest method to duplicate an array?
|
Here's an article about cloning. Maybe it can be of some help: Link
Array.Clone(...) only makes a Shallow copy of the array:
From MSDN:
Creates a shallow copy of the Array
Link to the MSDN article
|
|
-
|
|
Re: the fastest method to duplicate an array?
|
Well you could do it with a for loop if your just copying continious blocks out of the array. But you have to be careful not to create reference links to objects your copying if you intend to modify the data without modifying the origin of the data. Doing a simple MyClass bob = sourceBob, would probably result in a few reference links that cause the sourceBob to be modified when bob is modified, especially booleans have trouble with this from my experience. Easiest way I know to prevent a boolean being reference linked is to do a boolVariable ? true : false instead of destBool = sourceBool.
|
|
-
-
- (17254)
-
premium membership
MVP
-
Posts
11,436
|
Re: the fastest method to duplicate an array?
|
|
|
-
-
- (626)
-
premium membership
-
Posts
270
|
Re: the fastest method to duplicate an array?
|
Rizman:Array.Clone(...) only makes a Shallow copy of the array
That is what i dont want to happen. A shallow copy is a copy of an object that reference the same data, so if one is changed, the other changes too. ShallowCopy is the opposite to DeepCopy... but thanks.
Default Ex:Well you could do it with a for loop
That is how im doing it now. For the AI "thinking" in Another Puzzle, the AI creates a back up of the original blocks panel (a 2D array), make changes, score those changes and choose the best one, and then it has to restore the original panel, and start to play the move it choused. So the backing up and the restore process happen very often. So im wondering why c# cant just duplicate the whole block of memory to another part of the memory and create a new array of the same size and type like the other one and just point the reference to the new block of memory. That is what im looking for (if its possible).
I suppose I can survive with the "for (int x = 0; x < columns; x++) { for (int y = 0; y < rows; y++).. .etc, but to copy the whole array in one shot would be great for performance.
Kaotik puzzle in playtest If you want to play the game online add me to your friend list. Gamrtag Silvermax1975. Thanks twitter, facebook, youtube
|
|
-
|
|
Re: the fastest method to duplicate an array?
|
You can probably get away with Array newArr = new Array(sourceArray). I'm not certain how exactly that constructor works, but I've been using it with my input system when I was having trouble generating events due to shallow copies being made with clone.
|
|
-
-
- (17254)
-
premium membership
MVP
-
Posts
11,436
|
Re: the fastest method to duplicate an array?
|
Array newArr = new Array(sourceArray).
That will work great for an array of valeue types. The copy constructor will copy all the items.
However if the array contains reference types then they will be copied as references so you dont get a deep copy.
Play Kissy Poo - a game for 4 year olds on Xbox and windows The ZBuffer News and information for XNA Follow The Zman on twitter, Email me Please read the forum FAQs - Bug/Feature reporting Don't forget to mark good answers and good playtest feedback when you see it!!!
|
|
-
|
|
Re: the fastest method to duplicate an array?
|
Silvermax:
That is what i dont want to happen. A shallow copy is a copy of an object that reference the same data, so if one is changed, the other changes too. ShallowCopy is the opposite to DeepCopy... but thanks.
Sorry, I was in a hurry while writing the reply. I know that you are looking for a deep copy. Just wanted to confirm through MSDN that Array.Copy (which you used in the first place) would indeed not do what you were looking for.
But the link to Codeproject I posted should give you some hints on what to do to make a Deep copy. (at least I would think so, haven't tried it myself yet)
|
|
-
-
- (626)
-
premium membership
-
Posts
270
|
Re: the fastest method to duplicate an array?
|
Rizman:But the link to Codeproject I posted
I think something is wrong with that link. The only link that works is the one to MSDN. Thats why I didn't understood your meaning. (I think you forgot to paste the address). Can you fix the link please? Thank you.
Kaotik puzzle in playtest If you want to play the game online add me to your friend list. Gamrtag Silvermax1975. Thanks twitter, facebook, youtube
|
|
-
|
|
Re: the fastest method to duplicate an array?
|
Silvermax:
I think something is wrong with that link...
Sorry, I didn't check if the link was OK. I actually forgot to copy/paste the link :-) Fixed it in the original post, or you might just click here
|
|
-
-
- (1157)
-
premium membership
-
Posts
600
|
Re: the fastest method to duplicate an array?
|
I would create a copy constructor. Then you can simply do the copy using a for like this:
| MyObject[ newObjects = new MyObject[oldObjects.Length]; |
| |
| for (int i = 0; i < oldObjects.Length; i++) |
| newObjects[i] = new MyObject(oldObjects[i]); |
That link I posted shows a simple shallow object copy. Note that since your class contains only value types (bool, enum, int, float), a shallow copy will be fine (it will be the same thing as a deep copy). The deep copy that you need is a deep copy of the array object, which is handled in the code snippet above.
The advantage of using the copy constructor approach is that some time in the future, you may add a reference type to your class - meaning you will need a deep object copy. When that happens, you should use the reference type's copy constructor inside your copy constructor (or manually deep copy the reference type).
|
|
-
|
|
Re: the fastest method to duplicate an array?
|
The fastest way I would do it in C would be allocating a big enough memory block and copying the array's memory block with memcpy into the new memory block.
I researched a little bit about C# and was suprised you could do this in C#. You would use the Marshall.AllocHGlobal method to allocate a specific size memory block in memory, then use Marshall.StructureToPtr method to copy over the array block into the newly allocated block of memory.
The problem with this is three-fold:
1) You're allocating a block to unmanaged memory. That means the application would require a security permission to mess with unmanaged memory, which I doubt the Xbox360 will allow.
2) This also means that you have to manage the memory yourself. You must destroy the block of memory when you're finished using it, or else you'll get a memory leak.
3) If objects are involved, this will be a shallow copy. The array will only contain references to the objects, not the actual objects themselves. Primitive types will permit a deep copy, I believe.
This is one of the faster ways in C, but I'm not claiming that it is in C#. It might not be for all I know, as I don't have a deep understanding on how .NET/C# works. Sorry if this post wasn't that helpful, but I was actually kind of curious about this and wanted to see how versatile C# can be.
|
|
-
-
- (352)
-
premium membership
-
Posts
342
|
Re: the fastest method to duplicate an array?
|
I don't know that it's the fastest way but using Buffer.BlockCopy, which works with primitive type arrays (Boolean, Char, SByte, Byte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Single, and Double), uses memcpy internally and should be fast.
| bool[ sourceArray = new bool[ {true,false,true,false,true,false}; |
| bool[ destArray = new bool[sourceArray.Length]; |
| |
| Buffer.BlockCopy(sourceArray, 0, destArray, 0, Buffer.ByteLength(sourceArray)); |
| |
Cheers,
Leaf.
|
|
-
-
- (1157)
-
premium membership
-
Posts
600
|
Re: the fastest method to duplicate an array?
|
Hey nice tip Leaf - that looks really handy. Pity it can't handle all value types (in particular non ordinal types like float and user defined structs). Unfortunately I don't think it will help Silvermax out as he has a class which includes "4 bools, 4 enums, 2 ints and 1 float".
|
|
-
-
- (352)
-
premium membership
-
Posts
342
|
Re: the fastest method to duplicate an array?
|
Roonda:Hey nice tip Leaf - that looks really handy. Pity it can't handle all value types (in particular non ordinal types like float and user defined structs). Unfortunately I don't think it will help Silvermax out as he has a class which includes "4 bools, 4 enums, 2 ints and 1 float".
Hi Roonda,
Thanks for pointing that out - I didn't read the original post properly. My bad, as the youthful folk say. Though it does work for arrays of floats, otherwise known as Single - float is a C# alias for System.Single
Cheers,
Leaf.
|
|
-
-
- (626)
-
premium membership
-
Posts
270
|
Re: the fastest method to duplicate an array?
|
I tryed the BufferCopy method and got an ArgumentException: Object must be an array of primitives.Parameter name: src
My solution, by the moment is to have a Class[x,y].copy(anotherClass[x,y]) and inside that method I copy one variable by one (this.intVar = anotherClass.intVar etc etc). All this was for the AI to start thinking, becouse it was freezing the game some frames (when changes from "executing orders" to "finding orders"). So i had two options: to have a "light deep" thinking ai, but calculated every frame, or, what I ended doing, making the AI in mutithreading, so now the time expended in copying the whole array doesnt bother me so much.
Thnx!!!!
Kaotik puzzle in playtest If you want to play the game online add me to your friend list. Gamrtag Silvermax1975. Thanks twitter, facebook, youtube
|
|
|