deep-copy

deep copy for array of objects in swift

只谈情不闲聊 提交于 2019-11-27 01:59:52
问题 I have this class named Meal class Meal { var name : String = "" var cnt : Int = 0 var price : String = "" var img : String = "" var id : String = "" init(name:String , cnt : Int, price : String, img : String, id : String) { self.name = name self.cnt = cnt self.price = price self.img = img self.id = id } } and I have an array of Meal : var ordered = [Meal]() I want to duplicate that array and then do some changes to the Meal instances in one of them without changing the Meal instances in the

How to do “Deep Copy” in Swift?

冷暖自知 提交于 2019-11-27 01:49:46
问题 In Objective-C, one can deep-copy by following: Foo *foo = [[Foo alloc] init]; Foo *foo2 = foo.copy; How to do this deep-copy in Swift? 回答1: Deep Copy Your example is not a deep copy as discussed on StackOverflow. Getting a true deep copy of an object would often require NSKeyedArchiver Swift and copying The NSCopying protocol is the Objective-C way of providing object copies because everything was a pointer and you needed a way of managing the generation of copies of arbitrary objects. For

Does a slicing operation give me a deep or shallow copy?

◇◆丶佛笑我妖孽 提交于 2019-11-27 01:46:19
The official Python docs say that using the slicing operator and assigning in Python makes a shallow copy of the sliced list. But when I write code for example: o = [1, 2, 4, 5] p = o[:] And when I write: id(o) id(p) I get different id's and also appending one one list does not reflect in the other list. Isn't it creating a deep copy or is there somewhere I am going wrong? You are creating a shallow copy, because nested values are not copied, merely referenced. A deep copy would create copies of the values referenced by the list too. Demo: >>> lst = [{}] >>> lst_copy = lst[:] >>> lst_copy[0][

Shallow copy and deep copy in C

馋奶兔 提交于 2019-11-27 01:46:05
问题 I tried googling this but only objected oriented languages pop up as results. From my understanding a shallow copy is copying certain members of a struct. so lets say a struct is typedef struct node { char **ok; int hi; int yep; struct node *next; }node_t copying the char** would be a shallow copy but copying the whole linked list would be a deep copy? Do I have the right idea or am I way off? Thanks. 回答1: No. A shallow copy in this particular context means that you copy "references"

Copying an array of objects into another array in javascript (Deep Copy)

删除回忆录丶 提交于 2019-11-27 01:40:14
问题 Copying an array of objects into another array in javascript using slice(0) and concat() doesnt work. I have tried the following to test if i get the expected behaviour of deep copy using this. But the original array is also getting modified after i make changes in the copied array. var tags = []; for(var i=0; i<3; i++) { tags.push({ sortOrder: i, type: 'miss' }) } for(var tag in tags) { if(tags[tag].sortOrder == 1) { tags[tag].type = 'done' } } console.dir(tags) var copy = tags.slice(0)

deepcopy() is extremely slow

女生的网名这么多〃 提交于 2019-11-27 01:30:49
问题 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. How can I make it go fast? Note that copying less is not an option, since the transforms touch just about everything. EDIT : got it down to 8% with judicious implementation of __deepcopy__ on things. Still, not good enough. (Good enough is 1% or

javascript deep copy using JSON

杀马特。学长 韩版系。学妹 提交于 2019-11-27 00:30:51
问题 I have problem with javascript object(array) deep copy. I read many good way to deal with it. And I also know that jQuery has $.extend API to this problem. But my question is: Can I just using JSON stringify and parse method to solve this problem? Here's my code: function deepCopy(oldValue) { var newValue strValue = JSON.stringify(oldValue) return newValue = JSON.parse(strValue) } var a = { b: 'b', c: [1,2,4], d: null } copy = deepCopy(a) console.log(a === copy) // false console.log(a.c ===

Copying an array of objects into another array in javascript

久未见 提交于 2019-11-27 00:26:09
问题 How can I copy every element of an array (where the elements are objects), into another array, so that they are totally independent? I don't want changing an element in one array to affect the other. 回答1: If the destination array doesn't exist yet... ...you can use slice() or concat() . slice() is probably more idiomatic (you'll also see slice(0) , but the default is 0, so...): var destinationArray = sourceArray.slice(); // Probably more idiomatic // or var destinationArray = sourceArray

Deep copy of PHP array of references

空扰寡人 提交于 2019-11-26 23:28:14
问题 So $array is an array of which all elements are references. I want to append this array to another array called $results (in a loop), but since they are references, PHP copies the references and $results is full of identical elements. So far, the best working solution is: $results[] = unserialize(serialize($array)); which I fear to be incredibly inefficient. Is there a better way to do this? 回答1: You can use the fact that functions dereferences results when returning, for exemple here $array

In Javascript, when performing a deep copy, how do I avoid a cycle, due to a property being “this”?

纵饮孤独 提交于 2019-11-26 23:20:25
问题 I have some library code that is cycling endlessly on me. I'm not clear on how to best perform cycle detection and avoidance in javascript. i.e. there's no programmatic way of inspecting whether an object came from a "this" reference, is there? Here's the code. Thanks! setAttrs: function(config) { var go = Kinetic.GlobalObject; var that = this; // set properties from config if(config !== undefined) { function setAttrs(obj, c) { for(var key in c) { var val = c[key]; /* * if property is an