Deep and Shallow DataTree constructor

Hello everyone, I have a beginner question regarding C#.

I’m writing a 3D Game of Life script with an DataTree input sorted by 3D coordinates {z,y}(x).

However it’s not working as intended and I want to rule out it might be caused by using a shallow DataTree copy:

DataTree < int> storage0 = new DataTree(init);
DataTree < int> storage1 = new DataTree(init);

What is the difference between a deep and shallow copy? Could you please help me how to use the deep constructor?

DataTree < T > (DataTree < T > , DataTree < T > .DuplicateT - Create a true duplicate of another data tree.

Thank you,

Cheers
Jonas

private void RunScript(ref object Shallow, ref object Deep)
{
  var meshes = new DataTree<Mesh>();
  meshes.Add(Mesh.CreateFromSphere(new Sphere(Point3d.Origin, 5.0), 5, 5));
  Shallow = new DataTree<Mesh>(meshes);
  Deep = new DataTree<Mesh>(meshes, original => original.DuplicateMesh());
  meshes[new GH_Path(0), 0].VertexColors.CreateMonotoneMesh(System.Drawing.Color.Blue);
}

ShallowVsDeep.gh (13.9 KB)

DataTree(DataTree other) will make a shallow copy of another DataTree. If your T is a value type (Int32, Double, Boolean, Point3d, Plane) then you’ll get a completely fresh DataTree. If your T is a reference type (Curve, Brep, Mesh) then the data will be shared amongst both trees.

If you want to duplicate a tree and duplicate the reference-type data inside, you need to use DataTree(DataTree other, DataTree .DuplicateT copyDelegate) constructor.

2 Likes

A shallow copy means that all the fields of an objeect which are value types will get copied over in to a independent memory location. So if you change the value of one of the copies it wont change the value of the original object. The opposite is true if the class has a field which is a reference type (I.e another class with fields and properties as well). What happens is that the fields of that reference type will get copied but it will reference the same memory location as the parent object. So if you change a value of that reference type in the copy, the same change will be reflected in the original object as well.

A deep copy guarantees that all fields, reference type or value types will get copied bit by bit in to a new memory location on the computer. Any changes in the fields of either the cloned object or the original object won’t have any effect on each other.

I suggest you to google reference types and value types in C# a very important concept to understand and will help you visualise more clearly what I explained

1

The diagram has a little mistake that is worth pointing out. a should be the first element on the stack, then b and then obj at the top

4 Likes

Thank you, @Mahdiyar and @rawitscher-torres!

You both helped me a lot! I just got so stressed out that I thought that Shallow and Deep is regarding the Paths to the items inside the tree. Now I undestand that because it’s a list of integers, they both work the same. However if they referenced objects, a shallow copy would be a new tree of old objects.

Thank you once again, you’re the best!

Jonas