Questions tagged [deep-copy]

A deep copy of an object is a separate, fully independent duplicate of that object, such that any references / pointers it holds to other objects refer to deep copies of those to which the original's refer. Use this tag for questions regarding implementing or using deep copying methods.

Deep copying is a process by which an object is fully duplicated to create a copy that is wholly independent of the original. This differs from shallow copying with respect to the treatment of references / pointers to other objects: deep copying makes a deep copy of each referenced object, too, whereas shallow copying copies only the references. Deep copying is "deep" in the sense that it traverses the object's reference graph all the way to the bottom.

Related tags

Links

1483 questions
12
votes
3 answers

Deep copy of a record with R1:=R2, or Is there good way to implement NxM matrix with record?

I'm implementing a N x M matrix (class) with a record and an internal dynamic array like below. TMat = record public // contents _Elem: array of array of Double; // procedure SetSize(Row, Col: Integer); procedure Add(const M:…
benok
  • 688
  • 1
  • 6
  • 21
12
votes
1 answer

Is python multiprocessing Queue safe for object put?

When I put an object in Queue, Is it necessary to create deep copy of object and then put in Queue?
Majid
  • 1,673
  • 18
  • 27
12
votes
9 answers

Create a Deep Copy in C#

I want to make a deep copy of an object so I could change the the new copy and still have the option to cancel my changes and get back the original object. My problem here is that the object can be of any type, even from an unknown assembly. I can…
Orad SA
  • 1,885
  • 5
  • 16
  • 16
12
votes
3 answers

Why isn't there a deep copy method in Ruby?

I am working on a solution for technical drawings (svg/ruby). I want to manipulate rectangles, and have an add! method in this class: class Rect def add!(delta) @x1+=delta ... # and so on self end end I also need an add method…
halfbit
  • 3,773
  • 2
  • 34
  • 47
12
votes
8 answers

How to use both default and custom copy constructor in C++?

I have a long class with a lot of data members. I want to write a copy constructor for it. But, if I write my own copy constructor, I lose access to the default copy constructor. I just want to repair a few pointers in my own copy constructor. So I…
pablo
  • 384
  • 2
  • 5
  • 17
11
votes
5 answers

What is the difference between being shallowly and deeply equal? How is this applied to caching?

Found the following in my notes, but I am unable to make sense of it: Primitive type wrapper classes implement caching for a limited number of values. This guarantees that a limited number of deeply equal wrapper objects are also shallowly…
Albatross32
  • 279
  • 2
  • 4
  • 6
11
votes
2 answers

In python, is a function return a shallow or deep copy?

In python, if I have x = y any modification to x will also modify y, and I can do x = deepcopy(y) if I want to avoid modifying y while working on x Say, instead, that I have: myFunc(): return y def main(): x = myFunc() Is it still the…
user
  • 2,015
  • 6
  • 22
  • 39
11
votes
2 answers

Why does deepcopy fail with "KeyError: '__deepcopy__'" when copying custom object?

I have a class that converts a dictionary to an object like this class Dict2obj(dict): __getattr__= dict.__getitem__ def __init__(self, d): self.update(**dict((k, self.parse(v)) for k, v in…
Anurag Sharma
  • 4,839
  • 13
  • 59
  • 101
11
votes
4 answers

Is shallow copy really needed?

I am currently working on a graphs library for Java. As you expect there exists a Vertex class. That class contains an object of typeVertexData and that itself can contain anything. (I know this might be redundant and i could just do Vertex
gkrls
  • 2,618
  • 2
  • 15
  • 29
11
votes
3 answers

How do you perform a deep copy of a ternary tree in Go?

I'm attempting to perform a deep copy of the following struct: // Ternary Tree type Tree struct { Left *Tree Mid *Tree Right *Tree Value interface{} Parent *Tree Orientation string IsTerminal bool Type string } The…
Adam Soffer
  • 1,614
  • 5
  • 20
  • 36
11
votes
3 answers

Python: Inheritance of a class attribute (list)

inheriting a class attribute from a super class and later changing the value for the subclass works fine: class Unit(object): value = 10 class Archer(Unit): pass print Unit.value print Archer.value Archer.value = 5 print Unit.value print…
Sano98
  • 419
  • 2
  • 7
  • 17
11
votes
6 answers

System.arrayCopy() copies object or reference to object?

I am having a final class NameAndValue. I copied an array of NameAndValue objects using System.arrayCopy() and when I changed a NameAndValue object in copied array, it gets reflected in the original array. public final class NameAndValue { …
prasanth
  • 3,502
  • 4
  • 28
  • 42
11
votes
2 answers

Deep clone without some fields

Let's I have next javascript object. Now I want clone it but without some fields. For example I want cloned object without field "lastName" and "cars.age" Input { "firstName":"Fred", "lastName":"McDonald", "cars":[ { …
Ilya
  • 29,135
  • 19
  • 110
  • 158
11
votes
3 answers

copy.deepcopy raises TypeError on objects with self-defined __new__() method

I want to implement a symbol type, which keeps track of the symbols we already have(saved in _sym_table), and return them if they exist, or create new ones otherwise. The code: # -*- coding: utf-8 -*- _sym_table = {} class Symbol(object): def…
pjhades
  • 1,948
  • 2
  • 19
  • 34
10
votes
3 answers

Deep copying a PSObject

I have a powershell script in which I do the following $somePSObjectHashtables = New-Object Hashtable[] $somePSObject.Length; $somePSObjects = Import-CSV $csvPath 0..($somePSObject.Length - 1) | ForEach-Object { $i = $_; …
Justin Dearing
  • 14,270
  • 22
  • 88
  • 161