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
68
votes
7 answers

How do I do a deep copy of a 2d array in Java?

I just got bit by using .clone() on my 2d boolean array, thinking that this was a deep copy. How can I perform a deep copy of my boolean[][] array? Should I loop through it and do a series of System.arraycopy's?
user189661
64
votes
2 answers

How to make a deep copy of Java ArrayList

Possible Duplicate: How to clone ArrayList and also clone its contents? trying to make a copy of an ArrayList. The underlying object is simple containing on Strings, ints, BigDecimals, Dates and DateTime object. How can I ensure that the…
ajayian
  • 933
  • 3
  • 12
  • 19
64
votes
7 answers

Does Object.assign() create a deep copy or a shallow copy?

I just came across this concept of var copy = Object.assign({}, originalObject); which creates a copy of original object into the "copy" object. However, my question is, does this way of cloning object create a deep copy or a shallow copy? PS: The…
ShivangiBilora
  • 2,912
  • 4
  • 20
  • 27
59
votes
9 answers

How to create a deep copy of an object in Ruby?

I did some searching found some different methods and posts about creating a deep copy operator. Is there a quick and easy (built-in) way to deep copy objects in Ruby? The fields are not arrays or hashes. Working in Ruby 1.9.2.
B Seven
  • 44,484
  • 66
  • 240
  • 385
55
votes
5 answers

deepcopy() is extremely slow

I have a game state in Python with about 1000 objects (planetary systems + stars + planets), and I need to copy it and apply a bunch of transformations to it when requested. However, at about 1 request/second, this is taking up 24.63% of my runtime.…
Electro
  • 2,994
  • 5
  • 26
  • 32
51
votes
3 answers

python Pandas DataFrame copy(deep=False) vs copy(deep=True) vs '='

Could somebody explain to me a difference between df2 = df1 df2 = df1.copy() df3 = df1.copy(deep=False) I have tried all options and did as follows: df1 = pd.DataFrame([1,2,3,4,5]) df2 = df1 df3 = df1.copy() df4 = df1.copy(deep=False) df1 =…
Dariusz Krynicki
  • 2,544
  • 1
  • 22
  • 47
43
votes
11 answers

deep extend (like jQuery's) for nodeJS

I am struggling with deep copies of objects in nodeJS. my own extend is crap. underscore's extend is flat. there are rather simple extend variants here on stackexchange, but none are even close to jQuery.extend(true, {}, obj, obj, obj) .. (most are…
itsatony
  • 987
  • 1
  • 9
  • 13
40
votes
3 answers

How to copy a "Dictionary" in Swift?

How to copy a "Dictionary" in Swift? That is, get another object with same keys/values but different memory address. Furthermore, how to copy an object in Swift? Thanks,
allenlinli
  • 2,066
  • 3
  • 27
  • 49
38
votes
9 answers

How to deep copy a Hibernate entity while using a newly generated entity identifier

I'm using a relational DB using a single column pk with a few nested tables. I need to add a simple archiving to my project. The archiving only happens when the application reaches a particular state, so what I was hoping to do was copy my existing…
Code Junkie
  • 7,602
  • 26
  • 79
  • 141
38
votes
6 answers

Shallow copy or Deep copy?

I am a bit new to these two methods of copying one object into the other. I am confused and unable to spot out the major difference between deep copy and shallow copy.. I had gone through a lots of theory regarding this, but I need explanation with…
Srinivas Cheruku
  • 1,314
  • 3
  • 11
  • 19
37
votes
9 answers

How can I make a deepcopy of a function in Python?

I would like to make a deepcopy of a function in Python. The copy module is not helpful, according to the documentation, which says: This module does not copy types like module, method, stack trace, stack frame, file, socket, window, array, or…
Tom
  • 403
  • 1
  • 4
  • 4
36
votes
3 answers

How to copy a Python class instance if deepcopy() does not work?

I would like to make a copy of a class instance in python. I tried copy.deepcopy but I get the error message: RuntimeError: Only Variables created explicitly by the user (graph leaves) support the deepcopy protocol at the moment So suppose I have…
patapouf_ai
  • 17,605
  • 13
  • 92
  • 132
35
votes
3 answers

Cloning a record in rails, is it possible to clone associations and deep copy?

I'm .clone -ing a record in rails... new_blerg = Blerg.find(1).clone This record has loads and loads of associations, and those associations even have associations. Is there a way to deep-copy a record and clone it so it is cloned with all of…
CafeHey
  • 5,699
  • 19
  • 82
  • 145
35
votes
1 answer

How to create a copy of a python function

Is there a possibility to create real copies of python functions? The most obvious choice was http://docs.python.org/2/library/copy.html but there I read: It does “copy” functions and classes (shallow and deeply), by returning the original object…
Achim
  • 15,415
  • 15
  • 80
  • 144
33
votes
1 answer

Deep copying array of nested objects in javascript

I am trying to deep copy array of nested objects in javascript. My array look like this var arr = [{name:"adam",age:"21"}, {name:"freddie",age:"35",children:[{name:"mercury",age:"25"}]}, …
user2912611
  • 534
  • 2
  • 6
  • 16
1
2
3
98 99