deep-copy

How to deep copy (clone) an object with array members in Javascript?

早过忘川 提交于 2019-12-31 03:03:28
问题 Introduction I have a Class Persons that contains an array of Person and functions : function Persons() { this.mItems = []; // Array of Objects Person } Persons.prototype = { calculateScores : function() { // Do some stuff } } The Class Person has members and functions : function Person(name) { this.name = name; // Name of the Person this.score = 0; } Person.prototype = { calculateScore : function() { // Do some stuff } } I want that the program does the following things : var persons = new

replacing items in nested lists python

蹲街弑〆低调 提交于 2019-12-30 11:35:09
问题 I am trying to make a duplicate list of lists and change one element to another within the nested lists of the duplicate list but am having some trouble. How I made the duplicate list: order = [['yhjK', 'F'], 'gap', ['bcsA', 'F'], ['bcsB', 'F'], ['bcsZ', 'F'], 'gap', ['yhjK', 'R']] #order_1 = list(order) #this makes the duplicate list as well order_1 = [] for x in order: order_1.append(x) How I changed the elements: for item in order_1: for n,i in enumerate(item): if i=='R': item[n]='F' if i=

AngularJS : copy vs extend

99封情书 提交于 2019-12-30 10:10:09
问题 Explanation : we come across some situation in which we need to copy one object to another object. In that case, we probably have two solutions: angular.copy() or angular.extend(). Challenge i am facing : As we know angular.copy(source, destination) creates a deep copy of source object and assign it to destination. By writing deep copy, we mean that a new copy of the referred object is made and its working fine. deep copy code : var mySource = {'name' : 'Rohit', 'age' : '24'} var myDest = {}

Copy object properties: reflection or serialization - which is faster?

主宰稳场 提交于 2019-12-30 04:34:25
问题 I have two objects of the same type and need to copy property values from one object to another. There are two options: Use reflection, navigate through the properties of the first object and copy the values. Serialize the first object and deserialize a copy. Both work for my requirement, the question is which do I better use in the terms of speed (cost)? Example class Person { public int ID { get; set; } public string Firsthand { get; set; } public string LastName { get; set; } public int

Is clone() in java shallow copy?

会有一股神秘感。 提交于 2019-12-30 03:15:29
问题 Is clone() in java a shallow copy? Eventually this gets to the clone() method of Object (the uppermost class), which creates a new instance of the same class as the object and copies all the fields to the new instance (a "shallow copy"). I read this from wikipedia. I don't understand why it is a shallow copy. clone() will create a new instance with all fields. Is this just a deep copy? confused. Need some explanation for me. 回答1: The default Object.clone() is indeed a shallow copy. However,

std vector C++ — deep or shallow copy

主宰稳场 提交于 2019-12-29 19:07:12
问题 I wonder whether copying a vector I am copying the vector with its values (whereas this is not working with array, and deep copy need a loop or memcpy). Could you hint to an explanation? Regards 回答1: You are making a deep copy any time you copy a vector. But if your vector is a vector of pointers you are getting the copy of pointers, not the values are pointed to For example: std::vector<Foo> f; std::vector<Foo> cp = f; //deep copy. All Foo copied std::vector<Foo*> f; std::vector<Foo*> cp = f

std vector C++ — deep or shallow copy

梦想的初衷 提交于 2019-12-29 19:05:46
问题 I wonder whether copying a vector I am copying the vector with its values (whereas this is not working with array, and deep copy need a loop or memcpy). Could you hint to an explanation? Regards 回答1: You are making a deep copy any time you copy a vector. But if your vector is a vector of pointers you are getting the copy of pointers, not the values are pointed to For example: std::vector<Foo> f; std::vector<Foo> cp = f; //deep copy. All Foo copied std::vector<Foo*> f; std::vector<Foo*> cp = f

Deep Copy [] and ArrayList Java

送分小仙女□ 提交于 2019-12-29 09:02:41
问题 I want to make a Deep Copy of some Object[] and ArrayList How can i do that (without looping , and calling clone) There aren't standard utils for doing this? Thanks João 回答1: None trivial objects need to provide some to support copying: even if that is in the form of implementing Clone or Serialize. This is more or less why there is no built in method and the loop is unavoidable. If it is all your own code then I would recommend a copy constructor over either clone or serialize/deserialize as

Copy constructor for class that has member without default constructor in C++

淺唱寂寞╮ 提交于 2019-12-24 12:27:15
问题 I have a class: class Geometry{ std::vector<Subset *> subsets; int verticesCount; ... }; I want to add a copy constructor, so I can make a deep copy of that object (with own subsets , NOT only own pointers to same subsets ). I have tried this: Geometry & Geometry::operator=(const Geometry &other){ verticesCount = other.verticesCount; Subset * subset = 0; for(unsigned int i=0; i<other.subsets.size(); i++){ subset = new Subset(); subsets.push_back(subset); } ... return *this; } The problem is

How can I set the StreamingContext in Silverlight DataContractSerizer?

和自甴很熟 提交于 2019-12-24 00:38:02
问题 I need to do a deep copy in Silverlight, which I can do with the tried and tested serialize/deserialize approach. The copied objects aren't exact clones - they need to have some of their properties modified on the copy. I should be able to do something like this: [OnDeserialized()] public void OnDeserializedMethod(StreamingContext context) { if (context.State == StreamingContextStates.Clone) { //stuff } } where the StreamingContext is set up using a NetDataContractSerializer :