deep-copy

Copy constructor of Array List

这一生的挚爱 提交于 2020-01-30 13:17:08
问题 I just read about deep copy of ArrayList, that people think new ArrayList<>(originalList); will create a shallow copy. And I wrote a small demo ArrayList<String> originalNameList = new ArrayList<>(Arrays.asList("Anna", "Betty", "Chris")); List<String> copyNameList1=originalNameList; List<String> copyNameList2= new ArrayList<>(originalNameList); originalNameList.add("Duke"); copyNameList1.add("Ellen"); copyNameList1.set(1,"Bill"); copyNameList2.set(0,"Andy"); System.out.println(

Problems with Copy and assignment constructor

拜拜、爱过 提交于 2020-01-25 06:43:12
问题 I have the following code, used as a part of a Linked List: // copy constructor: LinkedList<T>(const LinkedList<T> &list) { // make a deep copy for (LinkedList<T>::Iterator i = list.begin(); i != list.end(); i++) { add(*i); } } // assignment constructor LinkedList<T>& operator= (const LinkedList<T> &list) { // make a deep copy for (LinkedList<T>::Iterator i = list.begin(); i != list.end(); i++) { add(*i); } } But when I compile I get the following errors (this is when I use it as an

What is the easiest way to copy a class instance that contains SimPy processes?

天涯浪子 提交于 2020-01-24 20:14:45
问题 I'm trying to create a copy of a class instance that I can simulate without affecting the original instance of the class. I've tried using copy.copy , but I run into this problem: system.simulate(until=100) print(system.env.now) # prints 100 copy_of_system = copy.copy(system) copy_of_system.simulate(until=200) print(copy_of_system.env.now) # prints 200 print(system.env.now) # prints 200, but should print 100 When I use copy.deepcopy I get TypeError: can't pickle generator objects . Is there

JavaScript: Deep Copy Circular JSON

自作多情 提交于 2020-01-14 20:16:35
问题 intro: I'm trying to write a deep copy method, but need to keep track of my visited nodes, so that I can link to the previously visitedNode instead of deep copying forever until stack overflow. attempts: var visitedNodes = {}; var obj = {}; obj.a = obj; // circular; can't use JSON.stringify) var obj2 = {}; visitedNodes[obj] = "should need key obj (not obj2) to access this string"; console.log(visitedNodes[obj2]); // logs the string unfortunately I don't have a unique way of storing the memory

Alternative of strcpy in c++

江枫思渺然 提交于 2020-01-13 08:11:06
问题 In C i used strcpy to make a deep copy of a string, but is it still 'fine' to use strcpy in C++ or are there better alternatives which i should use instead ? 回答1: In C++ the easiest way is usually to use the std::string class instead of char*. #include <string> ... std::string a = "Hello."; std::string b; b = a; The line "b = a;" does the same thing you would usually do with strcpy. 回答2: I put this in the comment above, but just to make the code readable: std::string a = "Hello."; std::string

Call default copy constructor from within overloaded copy constructor

给你一囗甜甜゛ 提交于 2020-01-13 07:55:45
问题 I need to write a copy constructor that deep copies the contents of a std::shared_ptr . However, there are a bunch of variable int a, b, c, d, e; also defined in the class. Is there a way to generate the default copy constructor code (or call the default copy constructor) inside my new overloaded one. Here is a code snippet with a comment that hopefully clarifies the issue. class Foo { public: Foo() {} Foo(Foo const & other); ... private: int a, b, c, d, e; std::shared_ptr<Bla> p; }; Foo::Foo

Does Spreading create shallow copy?

Deadly 提交于 2020-01-11 14:05:10
问题 As per the example given here, let first:number[] = [1, 2]; let second:number[] = [3, 4]; let both_plus:number[] = [0, ...first, ...second, 5]; console.log(`both_plus is ${both_plus}`); first[0] = 20; console.log(`first is ${first}`); console.log(`both_plus is ${both_plus}`); both_plus[1]=30; console.log(`first is ${first}`); console.log(`both_plus is ${both_plus}`); Spreading shows a deep copy, because all three array's have it's own duplicates, based on below output: both_plus is 0,1,2,3,4

Deep Copy using Reflection in an Extension Method for Silverlight?

霸气de小男生 提交于 2020-01-10 00:27:28
问题 So I'm trying to find a generic extension method that creates a deep copy of an object using reflection, that would work in Silverlight. Deep copy using serialization is not so great in Silverlight, since it runs in partial trust and the BinaryFormatter does not exist. I also know that reflection would be faster then serialization for cloning. It would be nice to have a method that works to copy public, private and protected fields, and is recursive so that it can copy objects in objects, and

JS: Does Object.assign() create deep copy or shallow copy

强颜欢笑 提交于 2020-01-09 02:13:48
问题 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 confusion is, if it creates a deep copy, then it would be the easiest way to clone an object. 回答1: Forget about deep copy, even shallow copy isn't safe, if the object you're copying has a property with enumerable attribute set to false. MDN : The

Force deep copy of Django FileField object (mainly new file itself)

。_饼干妹妹 提交于 2020-01-05 18:37:09
问题 I have this model: class DocVersion(Commentable): name = models.CharField( 'Version', max_length=100, ) docfile = models.FileField( 'File', upload_to='content/%Y/%m/%d/%H/%M/%S', ) created = models.DateTimeField( 'Created', auto_now_add=True, ) creater = models.ForeignKey( User, ) class Document(DocumentBase): #..... blah ..... versions = models.ManyToManyField( DocVersion, ) In a function in a view, I have this function to do a deep copy of the DocVersion like so: def cp_document(transfer